garbage-input.dat 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Copyright (c) 2003-2012, John Wiegley. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of New Artisans LLC nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * @defgroup math Mathematical objects
  33. */
  34. /**
  35. * @file amount.h
  36. * @author John Wiegley
  37. *
  38. * @ingroup math
  39. *
  40. * @brief Basic type for handling commoditized math: amount_t
  41. *
  42. * An amount is the most basic numerical type in Ledger, and relies on
  43. * commodity.h to represent commoditized amounts, which allows Ledger to
  44. * handle mathematical expressions involving disparate commodities.
  45. *
  46. * Amounts can be of virtually infinite size and precision. When
  47. * division or multiplication is performed, the precision is
  48. * automatically expanded to include as many extra digits as necessary
  49. * to avoid losing information.
  50. */
  51. #ifndef _AMOUNT_H
  52. #define _AMOUNT_H
  53. #include "utils.h"
  54. #include "times.h"
  55. #include "flags.h"
  56. namespace ledger {
  57. class commodity_t;
  58. struct annotation_t;
  59. struct keep_details_t;
  60. DECLARE_EXCEPTION(amount_error, std::runtime_error);
  61. enum parse_flags_enum_t {
  62. PARSE_DEFAULT = 0x00,
  63. PARSE_PARTIAL = 0x01,
  64. PARSE_SINGLE = 0x02,
  65. PARSE_NO_MIGRATE = 0x04,
  66. PARSE_NO_REDUCE = 0x08,
  67. PARSE_NO_ASSIGN = 0x10,
  68. PARSE_NO_ANNOT = 0x20,
  69. PARSE_OP_CONTEXT = 0x40,
  70. PARSE_SOFT_FAIL = 0x80
  71. };
  72. typedef basic_flags_t<parse_flags_enum_t, uint_least8_t> parse_flags_t;
  73. /**
  74. * @brief Encapsulate infinite-precision commoditized amounts
  75. *
  76. * Used to represent commoditized infinite-precision numbers, and
  77. * uncommoditized, plain numbers. In the commoditized case, commodities
  78. * keep track of how they are used, and are always displayed back to the
  79. * user after the same fashion. For uncommoditized numbers, no display
  80. * truncation is ever done. In both cases, internal precision is always
  81. * kept to an excessive degree.
  82. */
  83. class amount_t
  84. : public ordered_field_operators<amount_t,
  85. ordered_field_operators<amount_t, double,
  86. ordered_field_operators<amount_t, unsigned long,
  87. ordered_field_operators<amount_t, long> > > >
  88. {
  89. public:
  90. /** Ready the amount subsystem for use.
  91. @note Normally called by session_t::initialize(). */
  92. static void initialize();
  93. /** Shutdown the amount subsystem and free all resources.
  94. @note Normally called by session_t::shutdown(). */
  95. static void shutdown();
  96. static bool is_initialized;
  97. /** The amount's decimal precision. */
  98. typedef uint_least16_t precision_t;
  99. /** Number of places of precision by which values are extended to
  100. avoid losing precision during division and multiplication. */
  101. static const std::size_t extend_by_digits = 6U;
  102. /** If amounts should be streamed using to_fullstring() rather than
  103. to_string(), so that complete precision is always displayed no matter
  104. what the precision of an individual commodity may be. */
  105. static bool stream_fullstrings;
  106. protected:
  107. void _copy(const amount_t& amt);
  108. void _dup();
  109. void _clear();
  110. void _release();
  111. struct bigint_t;
  112. bigint_t * quantity;
  113. commodity_t * commodity_;
  114. public:
  115. /** @name Constructors
  116. @{ */
  117. /** Creates a value for which is_null() is true, and which has no
  118. value or commodity. If used in a value expression it evaluates to
  119. zero, and its commodity equals \c commodity_t::null_commodity. */
  120. amount_t() : quantity(NULL), commodity_(NULL) {
  121. TRACE_CTOR(amount_t, "");
  122. }
  123. /** Convert a double to an amount. As much precision as possible is
  124. decoded from the binary floating point number. */
  125. amount_t(const double val);
  126. /** Convert an unsigned long to an amount. It's precision is zero. */
  127. amount_t(const unsigned long val);
  128. /** Convert a long to an amount. It's precision is zero, and the sign
  129. is preserved. */
  130. amount_t(const long val);
  131. /** Parse a string as an (optionally commoditized) amount. If no
  132. commodity is present, the resulting commodity is \c
  133. commodity_t::null_commodity. The number may be of infinite
  134. precision. */
  135. explicit amount_t(const string& val) : quantity(NULL) {
  136. parse(val);
  137. TRACE_CTOR(amount_t, "const string&");
  138. }
  139. /** Parse a pointer to a C string as an (optionally commoditized)
  140. amount. If no commodity is present, the resulting commodity is \c
  141. commodity_t::null_commodity. The number may be of infinite
  142. precision. */
  143. explicit amount_t(const char * val) : quantity(NULL) {
  144. assert(val);
  145. parse(val);
  146. TRACE_CTOR(amount_t, "const char *");
  147. }
  148. /*@}*/
  149. /** Create an amount whose display precision is never truncated, even
  150. if the amount uses a commodity (which normally causes "round on
  151. streaming" to occur). This function is mostly used by debugging
  152. code and unit tests. This is the proper way to specify \c
  153. $100.005, where display of the extra digit precision is required.
  154. If a regular constructor were used, the amount would stream as \c
  155. $100.01, even though its internal value equals \c $100.005. */
  156. static amount_t exact(const string& value);
  157. /** Release the reference count held for the underlying \c
  158. amount_t::bigint_t object. */
  159. ~amount_t() {
  160. TRACE_DTOR(amount_t);
  161. if (quantity)
  162. _release();
  163. }
  164. /** @name Assignment and copy
  165. @{*/
  166. /** Copy an amount object. Copies are very efficient, using a
  167. copy-on-write model. Until the copy is changed, it refers to the
  168. same memory used by the original via reference counting. The \c
  169. amount_t::bigint_t class in amount.cc maintains the reference. */
  170. amount_t(const amount_t& amt) : quantity(NULL) {
  171. if (amt.quantity)
  172. _copy(amt);
  173. else
  174. commodity_ = NULL;
  175. TRACE_CTOR(amount_t, "copy");
  176. }
  177. /** Copy an amount object, applying the given commodity annotation
  178. details afterward. This is equivalent to doing a normal copy
  179. (@see amount_t(const amount_t&)) and then calling
  180. amount_t::annotate(). */
  181. amount_t(const amount_t& amt, const annotation_t& details) : quantity(NULL) {
  182. assert(amt.quantity);
  183. _copy(amt);
  184. annotate(details);
  185. TRACE_CTOR(amount_t, "const amount_t&, const annotation_t&");
  186. }
  187. /** Assign an amount object. This is like copying if the amount was
  188. null beforehand, otherwise the previous value's reference is must
  189. be freed. */
  190. amount_t& operator=(const amount_t& amt);
  191. amount_t& operator=(const double val) {
  192. return *this = amount_t(val);
  193. }
  194. amount_t& operator=(const unsigned long val) {
  195. return *this = amount_t(val);
  196. }
  197. amount_t& operator=(const long val) {
  198. return *this = amount_t(val);
  199. }
  200. /* Assign a string to an amount. This causes the contents of the
  201. string to be parsed, look for a commoditized or uncommoditized
  202. amount specifier. */
  203. amount_t& operator=(const string& str) {
  204. return *this = amount_t(str);
  205. }
  206. amount_t& operator=(const char * str) {
  207. assert(str);
  208. return *this = amount_t(str);
  209. }
  210. /*@}*/
  211. /** @name Comparison
  212. @{ */
  213. /** Compare two amounts, returning a number less than zero if \p amt
  214. is greater, exactly zero if they are equal, and greater than zero
  215. if \p amt is less. This method is used to implement all of the
  216. other comparison methods.*/
  217. int compare(const amount_t& amt) const;
  218. /** Test two amounts for equality. First the commodity pointers are
  219. quickly tested, then the multi-precision values themselves must be
  220. compared. */
  221. bool operator==(const amount_t& amt) const;
  222. template <typename T>
  223. bool operator==(const T& val) const {
  224. return compare(val) == 0;
  225. }
  226. template <typename T>
  227. bool operator<(const T& amt) const {
  228. return compare(amt) < 0;
  229. }
  230. template <typename T>
  231. bool operator>(const T& amt) const {
  232. return compare(amt) > 0;
  233. }
  234. /*@}*/
  235. /** @name Binary arithmetic
  236. */
  237. /*@{*/
  238. amount_t& operator+=(const amount_t& amt);
  239. amount_t& operator-=(const amount_t& amt);
  240. amount_t& operator*=(const amount_t& amt) {
  241. return multiply(amt);
  242. }
  243. amount_t& multiply(const amount_t& amt, bool ignore_commodity = false);
  244. /** Divide two amounts while extending the precision to preserve the
  245. accuracy of the result. For example, if \c 10 is divided by \c 3,
  246. the result ends up having a precision of \link
  247. amount_t::extend_by_digits \endlink place to avoid losing internal
  248. resolution. */
  249. amount_t& operator/=(const amount_t& amt);
  250. /*@}*/
  251. /** @name Unary arithmetic
  252. @{ */
  253. /** Return an amount's internal precision. To find the precision it
  254. should be displayed at -- assuming it was not created using
  255. amount_t::exact() -- use the following expression instead:
  256. @code
  257. amount.commodity().precision()
  258. @endcode */
  259. precision_t precision() const;
  260. bool keep_precision() const;
  261. void set_keep_precision(const bool keep = true) const;
  262. precision_t display_precision() const;
  263. /** Returns the negated value of an amount.
  264. @see operator-()
  265. */
  266. amount_t negated() const {
  267. amount_t temp(*this);
  268. temp.in_place_negate();
  269. return temp;
  270. }
  271. void in_place_negate();
  272. amount_t operator-() const {
  273. return negated();
  274. }
  275. /** Returns the absolute value of an amount. Equivalent to:
  276. @code
  277. (x < * 0) ? - x : x
  278. @endcode
  279. */
  280. amount_t abs() const {
  281. if (sign() < 0)
  282. return negated();
  283. return *this;
  284. }
  285. amount_t inverted() const {
  286. amount_t temp(*this);
  287. temp.in_place_invert();
  288. return temp;
  289. }
  290. void in_place_invert();
  291. /** Yields an amount whose display precision when output is truncated
  292. to the display precision of its commodity. This is normally the
  293. default state of an amount, but if one has become unrounded, this
  294. sets the "keep precision" state back to false.
  295. @see set_keep_precision */
  296. amount_t rounded() const {
  297. amount_t temp(*this);
  298. temp.in_place_round();
  299. return temp;
  300. }
  301. void in_place_round();
  302. /** Yields an amount which has lost all of its extra precision, beyond what
  303. the display precision of the commodity would have printed. */
  304. amount_t truncated() const {
  305. amount_t temp(*this);
  306. temp.in_place_truncate();
  307. return temp;
  308. }
  309. void in_place_truncate();
  310. /** Yields an amount which has lost all of its extra precision, beyond what
  311. the display precision of the commodity would have printed. */
  312. amount_t floored() const {
  313. amount_t temp(*this);
  314. temp.in_place_floor();
  315. return temp;
  316. }
  317. void in_place_floor();
  318. /** Yields an amount which has lost all of its extra precision, beyond what
  319. the display precision of the commodity would have printed. */
  320. amount_t ceilinged() const {
  321. amount_t temp(*this);
  322. temp.in_place_ceiling();
  323. return temp;
  324. }
  325. void in_place_ceiling();
  326. /** Yields an amount whose display precision is never truncated, even
  327. though its commodity normally displays only rounded values. */
  328. amount_t unrounded() const {
  329. amount_t temp(*this);
  330. temp.in_place_unround();
  331. return temp;
  332. }
  333. void in_place_unround();
  334. /** reduces a value to its most basic commodity form, for amounts that
  335. utilize "scaling commodities". For example, an amount of \c 1h
  336. after reduction will be \c 3600s.
  337. */
  338. amount_t reduced() const {
  339. amount_t temp(*this);
  340. temp.in_place_reduce();
  341. return temp;
  342. }
  343. void in_place_reduce();
  344. /** unreduce(), if used with a "scaling commodity", yields the most
  345. compact form greater than one. That is, \c 3599s will unreduce to
  346. \c 59.98m, while \c 3601 unreduces to \c 1h.
  347. */
  348. amount_t unreduced() const {
  349. amount_t temp(*this);
  350. temp.in_place_unreduce();
  351. return temp;
  352. }
  353. void in_place_unreduce();
  354. /** Returns the historical value for an amount -- the default moment
  355. returns the most recently known price -- based on the price history
  356. for the given commodity (or determined automatically, if none is
  357. provided). For example, if the amount were <tt>10 AAPL</tt>, and
  358. on Apr 10, 2000 each share of \c AAPL was worth \c $10, then
  359. calling value() for that moment in time would yield the amount \c
  360. $100.00.
  361. */
  362. optional<amount_t>
  363. value(const datetime_t& moment = datetime_t(),
  364. const commodity_t * in_terms_of = NULL) const;
  365. optional<amount_t> price() const;
  366. /*@}*/
  367. /** @name Truth tests
  368. */
  369. /*@{*/
  370. /** Truth tests. An amount may be truth test in several ways:
  371. sign() returns an integer less than, greater than, or equal to
  372. zero depending on whether the amount is negative, zero, or
  373. greater than zero. Note that this function tests the actual
  374. value of the amount -- using its internal precision -- and not
  375. the display value. To test its display value, use:
  376. `round().sign()'.
  377. is_nonzero(), or operator bool, returns true if an amount's
  378. display value is not zero.
  379. is_zero() returns true if an amount's display value is zero.
  380. Thus, $0.0001 is considered zero if the current display precision
  381. for dollars is two decimal places.
  382. is_realzero() returns true if an amount's actual value is zero.
  383. Thus, $0.0001 is never considered realzero.
  384. is_null() returns true if an amount has no value and no
  385. commodity. This only occurs if an uninitialized amount has never
  386. been assigned a value.
  387. */
  388. int sign() const;
  389. operator bool() const {
  390. return is_nonzero();
  391. }
  392. bool is_nonzero() const {
  393. return ! is_zero();
  394. }
  395. bool is_zero() const;
  396. bool is_realzero() const {
  397. return sign() == 0;
  398. }
  399. bool is_null() const {
  400. if (! quantity) {
  401. assert(! commodity_);
  402. return true;
  403. }
  404. return false;
  405. }
  406. /*@}*/
  407. /** @name Conversion
  408. */
  409. /*@{*/
  410. /** Conversion methods. An amount may be converted to the same types
  411. it can be constructed from -- with the exception of unsigned
  412. long. Implicit conversions are not allowed in C++ (though they
  413. are in Python), rather the following conversion methods must be
  414. called explicitly:
  415. to_double([bool]) returns an amount as a double. If the optional
  416. boolean argument is true (the default), an exception is thrown if
  417. the conversion would lose information.
  418. to_long([bool]) returns an amount as a long integer. If the
  419. optional boolean argument is true (the default), an exception is
  420. thrown if the conversion would lose information.
  421. fits_in_long() returns true if to_long() would not lose
  422. precision.
  423. to_string() returns an amount'ss "display value" as a string --
  424. after rounding the value according to the commodity's default
  425. precision. It is equivalent to: `round().to_fullstring()'.
  426. to_fullstring() returns an amount's "internal value" as a string,
  427. without any rounding.
  428. quantity_string() returns an amount's "display value", but
  429. without any commodity. Note that this is different from
  430. `number().to_string()', because in that case the commodity has
  431. been stripped and the full, internal precision of the amount
  432. would be displayed.
  433. */
  434. double to_double() const;
  435. long to_long() const;
  436. bool fits_in_long() const;
  437. operator string() const {
  438. return to_string();
  439. }
  440. string to_string() const;
  441. string to_fullstring() const;
  442. string quantity_string() const;
  443. /*@}*/
  444. /** @name Commodity methods
  445. */
  446. /*@{*/
  447. /** The following methods relate to an
  448. amount's commodity:
  449. commodity() returns an amount's commodity. If the amount has no
  450. commodity, the value returned is the `null_commodity'.
  451. has_commodity() returns true if the amount has a commodity.
  452. set_commodity(commodity_t) sets an amount's commodity to the
  453. given value. Note that this merely sets the current amount to
  454. that commodity, it does not "observe" the amount for possible
  455. changes in the maximum display precision of the commodity, the
  456. way that `parse' does.
  457. clear_commodity() sets an amount's commodity to null, such that
  458. has_commodity() afterwards returns false.
  459. number() returns a commodity-less version of an amount. This is
  460. useful for accessing just the numeric portion of an amount.
  461. */
  462. commodity_t * commodity_ptr() const;
  463. commodity_t& commodity() const {
  464. return *commodity_ptr();
  465. }
  466. bool has_commodity() const;
  467. void set_commodity(commodity_t& comm) {
  468. if (! quantity)
  469. *this = 0L;
  470. commodity_ = &comm;
  471. }
  472. amount_t with_commodity(const commodity_t& comm) const {
  473. if (commodity_ == &comm) {
  474. return *this;
  475. } else {
  476. amount_t tmp(*this);
  477. tmp.set_commodity(const_cast<commodity_t&>(comm));
  478. return tmp;
  479. }
  480. }
  481. void clear_commodity() {
  482. commodity_ = NULL;
  483. }
  484. amount_t number() const {
  485. if (! has_commodity())
  486. return *this;
  487. amount_t temp(*this);
  488. temp.clear_commodity();
  489. return temp;
  490. }
  491. /*@}*/
  492. /** @name Commodity annotations
  493. */
  494. /*@{*/
  495. /** An amount's commodity may be annotated with special details, such as the
  496. price it was purchased for, when it was acquired, or an arbitrary note,
  497. identifying perhaps the lot number of an item.
  498. annotate_commodity(amount_t price, [datetime_t date, string tag])
  499. sets the annotations for the current amount's commodity. Only
  500. the price argument is required, although it can be passed as
  501. `none' if no price is desired.
  502. commodity_annotated() returns true if an amount's commodity has
  503. any annotation details associated with it.
  504. annotation_details() returns all of the details of an annotated
  505. commodity's annotations. The structure returns will evaluate as
  506. boolean false if there are no details.
  507. strip_annotations() returns an amount whose commodity's annotations have
  508. been stripped.
  509. */
  510. void annotate(const annotation_t& details);
  511. bool has_annotation() const;
  512. annotation_t& annotation();
  513. const annotation_t& annotation() const {
  514. return const_cast<amount_t&>(*this).annotation();
  515. }
  516. /** If the lot price is considered whenever working with commoditized
  517. values.
  518. Let's say a user adds two values of the following form:
  519. @code
  520. 10 AAPL + 10 AAPL {$20}
  521. @endcode
  522. This expression adds ten shares of Apple stock with another ten
  523. shares that were purchased for \c $20 a share. If \c keep_price
  524. is false, the result of this expression is an amount equal to
  525. <tt>20 AAPL</tt>. If \c keep_price is \c true the expression
  526. yields an exception for adding amounts with different commodities.
  527. In that case, a \link balance_t \endlink object must be used to
  528. store the combined sum. */
  529. amount_t strip_annotations(const keep_details_t& what_to_keep) const;
  530. /*@}*/
  531. /** @name Parsing
  532. */
  533. /*@{*/
  534. /** The `flags' argument of both parsing may be one or more of the
  535. following:
  536. PARSE_NO_MIGRATE means to not pay attention to the way an
  537. amount is used. Ordinarily, if an amount were $100.001, for
  538. example, it would cause the default display precision for $ to be
  539. "widened" to three decimal places. If PARSE_NO_MIGRATE is
  540. used, the commodity's default display precision is not changed.
  541. PARSE_NO_REDUCE means not to call in_place_reduce() on the
  542. resulting amount after it is parsed.
  543. These parsing methods observe the amounts they parse (unless
  544. PARSE_NO_MIGRATE is true), and set the display details of
  545. the corresponding commodity accordingly. This way, amounts do
  546. not require commodities to be pre-defined in any way, but merely
  547. displays them back to the user in the same fashion as it saw them
  548. used.
  549. There is also a static convenience method called
  550. `parse_conversion' which can be used to define a relationship
  551. between scaling commodity values. For example, Ledger uses it to
  552. define the relationships among various time values:
  553. @code
  554. amount_t::parse_conversion("1.0m", "60s"); // a minute is 60 seconds
  555. amount_t::parse_conversion("1.0h", "60m"); // an hour is 60 minutes
  556. @endcode
  557. The method parse() is used to parse an amount from an input stream
  558. or a string. A global operator>>() is also defined which simply
  559. calls parse on the input stream. The parse() method has two forms:
  560. parse(istream, flags_t) parses an amount from the given input
  561. stream.
  562. parse(string, flags_t) parses an amount from the given string.
  563. parse(string, flags_t) also parses an amount from a string.
  564. */
  565. bool parse(std::istream& in,
  566. const parse_flags_t& flags = PARSE_DEFAULT);
  567. bool parse(const string& str,
  568. const parse_flags_t& flags = PARSE_DEFAULT) {
  569. std::istringstream stream(str);
  570. bool result = parse(stream, flags);
  571. return result;
  572. }
  573. static void parse_conversion(const string& larger_str,
  574. const string& smaller_str);
  575. /*@}*/
  576. /** @name Printing
  577. */
  578. /*@{*/
  579. /** An amount may be output to a stream using the `print' method. There is
  580. also a global operator<< defined which simply calls print for an amount
  581. on the given stream. There is one form of the print method, which takes
  582. one required argument and two arguments with default values:
  583. print(ostream, bool omit_commodity = false, bool full_precision = false)
  584. prints an amounts to the given output stream, using its commodity's
  585. default display characteristics. If `omit_commodity' is true, the
  586. commodity will not be displayed, only the amount (although the
  587. commodity's display precision is still used). If `full_precision' is
  588. true, the full internal precision of the amount is displayed, regardless
  589. of its commodity's display precision.
  590. */
  591. #define AMOUNT_PRINT_NO_FLAGS 0x00
  592. #define AMOUNT_PRINT_RIGHT_JUSTIFY 0x01
  593. #define AMOUNT_PRINT_COLORIZE 0x02
  594. #define AMOUNT_PRINT_NO_COMPUTED_ANNOTATIONS 0x04
  595. #define AMOUNT_PRINT_ELIDE_COMMODITY_QUOTES 0x08
  596. void print(std::ostream& out,
  597. const uint_least8_t flags = AMOUNT_PRINT_NO_FLAGS) const;
  598. /*@}*/
  599. /** @name Debugging
  600. */
  601. /*@{*/
  602. /** There are two methods defined to help with debugging:
  603. dump(ostream) dumps an amount to an output stream. There is
  604. little different from print(), it simply surrounds the display
  605. value with a marker, for example "AMOUNT($1.00)". This code is
  606. used by other dumping code elsewhere in Ledger.
  607. valid() returns true if an amount is valid. This ensures that if
  608. an amount has a commodity, it has a valid value pointer, for
  609. example, even if that pointer simply points to a zero value.
  610. */
  611. void dump(std::ostream& out) const {
  612. out << "AMOUNT(";
  613. print(out);
  614. out << ")";
  615. }
  616. bool valid() const;
  617. #if HAVE_BOOST_SERIALIZATION
  618. private:
  619. /** Serialization. */
  620. friend class boost::serialization::access;
  621. template<class Archive>
  622. void serialize(Archive& ar, const unsigned int /* version */);
  623. #endif // HAVE_BOOST_SERIALIZATION
  624. /*@}*/
  625. };
  626. inline amount_t amount_t::exact(const string& value) {
  627. amount_t temp;
  628. temp.parse(value, PARSE_NO_MIGRATE);
  629. return temp;
  630. }
  631. inline string amount_t::to_string() const {
  632. std::ostringstream bufstream;
  633. print(bufstream);
  634. return bufstream.str();
  635. }
  636. inline string amount_t::to_fullstring() const {
  637. std::ostringstream bufstream;
  638. unrounded().print(bufstream);
  639. return bufstream.str();
  640. }
  641. inline string amount_t::quantity_string() const {
  642. std::ostringstream bufstream;
  643. number().print(bufstream);
  644. return bufstream.str();
  645. }
  646. inline std::ostream& operator<<(std::ostream& out, const amount_t& amt) {
  647. if (amount_t::stream_fullstrings)
  648. amt.unrounded().print(out);
  649. else
  650. amt.print(out);
  651. return out;
  652. }
  653. inline std::istream& operator>>(std::istream& in, amount_t& amt) {
  654. amt.parse(in);
  655. return in;
  656. }
  657. void put_amount(property_tree::ptree& pt, const amount_t& amt,
  658. bool wrap = true, bool commodity_details = false);
  659. } // namespace ledger
  660. #endif // _AMOUNT_H