summaryrefslogtreecommitdiff
path: root/amount.cc
blob: 4caac14d0b7e5b693afeff54c9a1748d26384d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include <sstream>

#include <gmp.h>                // GNU multi-precision library

#include "ledger.h"

namespace ledger {

#define MAX_PRECISION 10        // must be 2 or higher

//////////////////////////////////////////////////////////////////////
//
// The `amount' structure.  Every transaction has an associated amount,
// which is represented by this structure.  `amount' uses the GNU
// multi-precision library, allowing for arbitrarily large amounts.
// Each amount is a quantity of commodity at a certain price; the
// default commodity is the US dollar, with a price of 1.00.
//

class gmp_amount : public amount
{
  bool priced;

  mpz_t price;
  commodity * price_comm;

  mpz_t quantity;
  commodity * quantity_comm;

  gmp_amount(const gmp_amount& other) {}
  gmp_amount& operator=(const gmp_amount& other) { return *this; }

 public:
  gmp_amount() : priced(false), price_comm(NULL), quantity_comm(NULL) {
    mpz_init(price);
    mpz_init(quantity);
  }

  virtual ~gmp_amount() {
    mpz_clear(price);
    mpz_clear(quantity);
  }

  virtual commodity * comm() const {
    return quantity_comm;
  }
  virtual const std::string& comm_symbol() const {
    assert(quantity_comm);
    return quantity_comm->symbol;
  }

  virtual amount * copy() const;
  virtual amount * value(amount *) const;
  virtual amount * street() const;

  virtual operator bool() const;

  virtual void credit(const amount * other) {
    *this += *other;
  }
  virtual void negate() {
    mpz_ui_sub(quantity, 0, quantity);
  }
  virtual void operator+=(const amount& other);

  virtual void parse(const char * num) {
    *this = num;
  }
  virtual amount& operator=(const char * num);
  virtual std::string as_str(bool full_prec) const;
  virtual operator std::string() const {
    return as_str(false);
  }

  friend amount * create_amount(const char * value, const amount * price);
};

amount * create_amount(const char * value, const amount * price)
{
  gmp_amount * a = new gmp_amount();
  a->parse(value);

  // If a price was specified, it refers to a total price for the
  // whole `value', meaning we must divide to determine the
  // per-commodity price.

  if (price) {
    assert(! a->priced);        // don't specify price twice!

    const gmp_amount * p = dynamic_cast<const gmp_amount *>(price);
    assert(p);

    // There is no need for per-commodity pricing when the total
    // price is in the same commodity as the quantity!  In that case,
    // the two will always be identical.
    if (a->quantity_comm == p->quantity_comm) {
      assert(mpz_cmp(a->quantity, p->quantity) == 0);
      return a;
    }

    mpz_t quotient;
    mpz_t remainder;
    mpz_t addend;

    mpz_init(quotient);
    mpz_init(remainder);
    mpz_init(addend);

    mpz_ui_pow_ui(addend, 10, MAX_PRECISION);

    mpz_tdiv_qr(quotient, remainder, p->quantity, a->quantity);
    mpz_mul(remainder, remainder, addend);
    mpz_tdiv_q(remainder, remainder, a->quantity);
    mpz_mul(quotient, quotient, addend);
    mpz_add(quotient, quotient, remainder);

    a->priced = true;
    mpz_set(a->price, quotient);
    a->price_comm = p->quantity_comm;

    mpz_clear(quotient);
    mpz_clear(remainder);
    mpz_clear(addend);
  }
  return a;
}

static void round(mpz_t out, const mpz_t val, int prec)
{
  mpz_t divisor;
  mpz_t quotient;
  mpz_t remainder;

  mpz_init(divisor);
  mpz_init(quotient);
  mpz_init(remainder);

  mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec);
  mpz_tdiv_qr(quotient, remainder, val, divisor);

  mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec - 1);
  mpz_mul_ui(divisor, divisor, 5);
  if (mpz_cmp(remainder, divisor) >= 0) {
    mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec);
    mpz_sub(remainder, divisor, remainder);
    mpz_add(out, val, remainder);
  } else {
    mpz_sub(out, val, remainder);
  }

  mpz_clear(divisor);
  mpz_clear(quotient);
  mpz_clear(remainder);
}

static void multiply(mpz_t out, const mpz_t l, const mpz_t r)
{
  mpz_t divisor;

  mpz_init(divisor);

  mpz_mul(out, l, r);

  // The number is at double-precision right now, so rounding at
  // precision 0 effectively means rounding to the ordinary
  // precision.
  round(out, out, 0);

  // after multiplying, truncate to the correct precision
  mpz_ui_pow_ui(divisor, 10, MAX_PRECISION);
  mpz_tdiv_q(out, out, divisor);

  mpz_clear(divisor);
}

amount * gmp_amount::copy() const
{
  gmp_amount * new_amt = new gmp_amount();
#if 0
  // Don't copy the price
  new_amt->priced = priced;
  mpz_set(new_amt->price, price);
  new_amt->price_comm = price_comm;
#endif
  mpz_set(new_amt->quantity, quantity);
  new_amt->quantity_comm = quantity_comm;
  return new_amt;
}

amount * gmp_amount::value(amount * pr) const
{
  if (pr) {
    gmp_amount * p = dynamic_cast<gmp_amount *>(pr);
    assert(p);

    gmp_amount * new_amt = new gmp_amount();

    multiply(new_amt->quantity, quantity, p->quantity);

    // If the price we are multiplying by has no commodity, use the
    // commodity of the current amount.
    if (p->quantity_comm)
      new_amt->quantity_comm = p->quantity_comm;
    else
      new_amt->quantity_comm = quantity_comm;

    return new_amt;
  }
  else if (! priced) {
    return copy();
  }
  else {
    gmp_amount * new_amt = new gmp_amount();
    multiply(new_amt->quantity, quantity, price);
    new_amt->quantity_comm = price_comm;
    return new_amt;
  }
}

amount * gmp_amount::street() const
{
  amount * cost = NULL;
  const amount * amt = this;

  for (int cycles = 0; cycles < 10; cycles++) {
    totals::iterator pi = main_ledger.prices.amounts.find(amt->comm_symbol());
    if (pi == main_ledger.prices.amounts.end()) {
      break;
    } else {
      amount * temp = cost;
      amt = cost = amt->value((*pi).second);
      if (temp)
	delete temp;
    }
  }
  return cost ? cost : copy();
}

gmp_amount::operator bool() const
{
  mpz_t copy;
  mpz_init_set(copy, quantity);
  assert(quantity_comm);
  if (quantity_comm->precision < MAX_PRECISION)
    round(copy, copy, quantity_comm->precision);
  bool zero = mpz_sgn(copy) == 0;
  mpz_clear(copy);
  return ! zero;
}

static std::string amount_to_str(const commodity * comm, const mpz_t val,
				 bool full_precision)
{
  mpz_t temp;
  mpz_t quotient;
  mpz_t rquotient;
  mpz_t remainder;
  mpz_t divisor;

  bool negative = false;

  mpz_init_set(temp, val);

  mpz_init(quotient);
  mpz_init(rquotient);
  mpz_init(remainder);
  mpz_init(divisor);

  if (! full_precision && comm->precision < MAX_PRECISION)
    round(temp, temp, comm->precision);

  mpz_ui_pow_ui(divisor, 10, MAX_PRECISION);
  mpz_tdiv_qr(quotient, remainder, temp, divisor);

  if (mpz_sgn(quotient) < 0 || mpz_sgn(remainder) < 0)
    negative = true;
  mpz_abs(quotient, quotient);
  mpz_abs(remainder, remainder);

  if (full_precision || comm->precision == MAX_PRECISION) {
    mpz_set(rquotient, remainder);
  } else {
    assert(MAX_PRECISION - comm->precision > 0);
    mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - comm->precision);
    mpz_tdiv_qr(rquotient, remainder, remainder, divisor);
  }

  std::ostringstream s;

  if (comm->prefix) {
    s << comm->symbol;
    if (comm->separate)
      s << " ";
  }

  if (negative)
    s << "-";

  if (mpz_sgn(quotient) == 0)
    s << '0';
  else if (! comm->thousands)
    s << quotient;
  else {
    // jww (2003-09-29): use a smarter starting value

    bool printed = false;

    for (int powers = 27; powers >= 0; powers -= 3) {
      mpz_ui_pow_ui(divisor, 10, powers);
      mpz_tdiv_q(temp, quotient, divisor);

      if (mpz_sgn(temp) == 0)
	continue;

      mpz_ui_pow_ui(divisor, 10, 3);
      mpz_tdiv_r(temp, temp, divisor);

      if (printed) {
	s.width(3);
	s.fill('0');
      }
      s << temp;

      if (powers > 0) {
	if (comm->european)
	  s << ".";
	else
	  s << ",";

	printed = true;
      }
    }
  }

  if (comm->european)
    s << ',';
  else
    s << '.';

  if (! full_precision || mpz_sgn(rquotient) == 0) {
    s.width(comm->precision);
    s.fill('0');
    s << rquotient;
  } else {
    char buf[MAX_PRECISION + 1];
    gmp_sprintf(buf, "%Zd", rquotient);

    int width = std::strlen(buf);
    char * p = buf + (width - 1);

    width = MAX_PRECISION - width;

    while (p >= buf && *p == '0' &&
	   (p - buf) >= (comm->precision - width))
      p--;
    *(p + 1) = '\0';

    s.width(width + std::strlen(buf));
    s.fill('0');
    s << buf;
  }

  if (! comm->prefix) {
    if (comm->separate)
      s << " ";
    s << comm->symbol;
  }

  mpz_clear(temp);
  mpz_clear(quotient);
  mpz_clear(rquotient);
  mpz_clear(remainder);
  mpz_clear(divisor);

  return s.str();
}

std::string gmp_amount::as_str(bool full_prec) const
{
  std::ostringstream s;

  assert(quantity_comm);
  s << amount_to_str(quantity_comm, quantity, full_prec);

  if (priced) {
    assert(price_comm);
    s << " @ " << amount_to_str(price_comm, price, full_prec);
  }
  return s.str();
}

static void parse_number(mpz_t out, const char * num, commodity * comm)
{
  if (char * p = std::strchr(num, '/')) {
    mpz_t numer;
    mpz_t val;

    // The number was specified as a numerator over denominator, such
    // as 5250/100.  This gives us the precision, and avoids any
    // nastiness having to do with international numbering formats.

    std::string numer_str(num, p - num);
    mpz_init_set_str(numer, numer_str.c_str(), 10);
    mpz_init(val);

    int missing = MAX_PRECISION - (std::strlen(++p) - 1);
    assert(missing > 0);
    mpz_ui_pow_ui(val, 10, missing);

    mpz_mul(out, numer, val);

    mpz_clear(numer);
    mpz_clear(val);
  }
  else {
    static char buf[256];

    // The number is specified as the user desires, with the
    // commodity telling us how to parse it.

    std::memset(buf, '0', 255);
    std::strncpy(buf, num, std::strlen(num));

    if (comm && comm->thousands)
      while (char * t = std::strchr(buf, comm->european ? '.' : ','))
	do { *t = *(t + 1); } while (*(t++ + 1));

    char * t = std::strchr(buf, (comm && comm->european) ? ',' : '.');
    if (! t)
      t = buf + std::strlen(num);

    for (int prec = 0; prec < MAX_PRECISION; prec++) {
      *t = *(t + 1);
      t++;
    }
    *t = '\0';

    mpz_set_str(out, buf, 10);
  }
}

static commodity * parse_amount(mpz_t out, const char * num,
				int matched, int * ovector, int base)
{
  static char buf[256];

  bool saw_commodity = false;

  std::string symbol;
  bool prefix, separate, thousands, european;
  int precision, result;

  if (ovector[base * 2] >= 0) {
    // A prefix symbol was found
    saw_commodity = true;
    prefix = true;
    separate = ovector[(base + 2) * 2] != ovector[(base + 2) * 2 + 1];
    result = pcre_copy_substring(num, ovector, matched, base + 1, buf, 255);
    assert(result >= 0);
    symbol = buf;
  }

  // This is the value, and must be present
  assert(ovector[(base + 3) * 2] >= 0);
  result = pcre_copy_substring(num, ovector, matched, base + 3, buf, 255);
  assert(result >= 0);

  // Determine the precision used
  if (char * p = std::strchr(buf, '.'))
    precision = std::strlen(++p);
  else if (char * p = std::strchr(buf, '/'))
    precision = std::strlen(++p) - 1;
  else
    precision = 0;

  // Where "thousands" markers used?  Is it a european number?
  if (char * p = std::strrchr(buf, ',')) {
    if (std::strchr(p, '.'))
      thousands = true;
    else
      european = true;
  }

  // Parse the actual quantity
  std::string value_str = buf;

  if (ovector[(base + 4) * 2] >= 0) {
    // A suffix symbol was found
    saw_commodity = true;
    prefix = false;
    separate = ovector[(base + 5) * 2] != ovector[(base + 5) * 2 + 1];
    result = pcre_copy_substring(num, ovector, matched, base + 6, buf, 255);
    assert(result >= 0);
    symbol = buf;
  }

  commodity * comm = NULL;

  if (saw_commodity) {
    commodities_iterator item = main_ledger.commodities.find(symbol.c_str());
    if (item == main_ledger.commodities.end()) {
      comm = new commodity(symbol, prefix, separate,
			   thousands, european, precision);
    } else {
      comm = (*item).second;
#if 0
      // If a finer precision was used than the commodity allows,
      // increase the precision.
      if (precision > comm->precision)
	comm->precision = precision;
#else
      if (use_warnings && precision > comm->precision)
	std::cerr << "Warning: Use of higher precision than expected: "
		  << value_str << std::endl;
#endif
    }
  }

  parse_number(out, value_str.c_str(), comm);

  return comm;
}

amount& gmp_amount::operator=(const char * num)
{
  // Compile the regular expression used for parsing amounts
  static pcre * re = NULL;
  if (! re) {
    const char *error;
    int erroffset;
    static const std::string amount_re =
      "(([^-0-9/., ]+)(\\s*))?([-0-9/.,]+)((\\s*)([^-0-9/., @]+))?";
    const std::string regexp =
      "^" + amount_re + "(\\s*@\\s*" + amount_re + ")?$";
    re = pcre_compile(regexp.c_str(), 0, &error, &erroffset, NULL);
  }

  int ovector[60];
  int matched;

  matched = pcre_exec(re, NULL, num, std::strlen(num), 0, 0, ovector, 60);
  if (matched > 0) {
    quantity_comm = parse_amount(quantity, num, matched, ovector, 1);

    // If the following succeeded, then we have a price
    if (ovector[8 * 2] >= 0) {
      priced = true;
      price_comm = parse_amount(price, num, matched, ovector, 9);
    }
  } else {
    std::cerr << "Failed to parse amount: " << num << std::endl;
  }
  return *this;
}

void gmp_amount::operator+=(const amount& _other)
{
  const gmp_amount& other = dynamic_cast<const gmp_amount&>(_other);
  assert(quantity_comm == other.quantity_comm);
  mpz_add(quantity, quantity, other.quantity);
}

} // namespace ledger