summaryrefslogtreecommitdiff
path: root/textual.cc
blob: 95506e99bb9f6cc583209445a719b35ef8bd4cd6 (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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
#include "textual.h"
#include "constraint.h"
#include "error.h"

#include <vector>
#include <fstream>
#include <sstream>
#include <cstring>
#include <ctime>
#include <cctype>

#define TIMELOG_SUPPORT 1

namespace ledger {

#if 0
static const std::string entry1_fmt = "%10d %p";
static const std::string entryn_fmt = "    %-30a  %15t";
#endif

#define MAX_LINE 1024

std::string             path;
unsigned int		linenum;

#ifdef TIMELOG_SUPPORT
static std::time_t	time_in;
static account_t *	last_account;
static std::string	last_desc;
#endif

static std::time_t	now	  = std::time(NULL);
static struct std::tm * now_tm	  = std::localtime(&now);

static std::time_t	base      = -1;
static int		base_year = -1;

static const int	month_days[12] = {
  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

static const char *	formats[] = {
  "%Y/%m/%d",
  "%m/%d",
  "%Y.%m.%d",
  "%m.%d",
  "%Y-%m-%d",
  "%m-%d",
  "%a",
  "%A",
  "%b",
  "%B",
  "%Y",
  NULL
};

inline char * skip_ws(char * ptr)
{
  while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
    ptr++;
  return ptr;
}

inline char * next_element(char * buf, bool variable = false)
{
  for (char * p = buf; *p; p++) {
    if (! (*p == ' ' || *p == '\t'))
      continue;

    if (! variable) {
      *p = '\0';
      return skip_ws(p + 1);
    }
    else if (*p == '\t') {
      *p = '\0';
      return skip_ws(p + 1);
    }
    else if (*(p + 1) == ' ') {
      *p = '\0';
      return skip_ws(p + 2);
    }
  }
  return NULL;
}

bool parse_date_mask(const char * date_str, struct std::tm * result)
{
  for (const char ** f = formats; *f; f++) {
    memset(result, INT_MAX, sizeof(struct std::tm));
    if (strptime(date_str, *f, result))
      return true;
  }
  return false;
}

bool parse_date(const char * date_str, std::time_t * result, const int year)
{
  struct std::tm when;

  if (! parse_date_mask(date_str, &when))
    return false;

  when.tm_hour = 0;
  when.tm_min  = 0;
  when.tm_sec  = 0;

  if (when.tm_year == -1)
    when.tm_year = ((year == -1) ? now_tm->tm_year : (year - 1900));

  if (when.tm_mon == -1)
    when.tm_mon = 0;

  if (when.tm_mday == -1)
    when.tm_mday = 1;

  *result = std::mktime(&when);

  return true;
}

static bool quick_parse_date(char * date_str, std::time_t * result)
{
  int year = -1, month = -1, day, num = 0;

  for (char * p = date_str; *p; p++) {
    if (*p == '/' || *p == '-' || *p == '.') {
      if (year == -1)
	year = num;
      else
	month = num;
      num = 0;
    }
    else if (*p < '0' || *p > '9') {
      return false;
    }
    else {
      num *= 10;
      num += *p - '0';
    }
  }

  day = num;

  if (month == -1) {
    month = year;
    year  = -1;
  }

  if (base == -1 || year != base_year) {
    struct std::tm when;

    when.tm_hour = 0;
    when.tm_min  = 0;
    when.tm_sec  = 0;

    base_year    = year == -1 ? now_tm->tm_year + 1900 : year;
    when.tm_year = year == -1 ? now_tm->tm_year : year - 1900;
    when.tm_mon  = 0;
    when.tm_mday = 1;

    base = std::mktime(&when);
  }

  *result = base;

  --month;
  while (--month >= 0) {
    *result += month_days[month] * 24 * 60 * 60;
    if (month == 1 && year % 4 == 0 && year != 2000) // february in leap years
      *result += 24 * 60 * 60;
  }

  if (--day)
    *result += day * 24 * 60 * 60;

  return true;
}

inline char peek_next_nonws(std::istream& in)
{
  char c = in.peek();
  while (! in.eof() && std::isspace(c) && c != '\n') {
    in.get(c);
    c = in.peek();
  }
  return c;
}

transaction_t * parse_transaction_text(char * line, ledger_t * ledger,
				       account_t * account, entry_t * entry)
{
  // The account will be determined later...

  transaction_t * xact = new transaction_t(entry, NULL);

  // The call to `next_element' will skip past the account name,
  // and return a pointer to the beginning of the amount.  Once
  // we know where the amount is, we can strip off any
  // transaction note, and parse it.

  char * p = skip_ws(line);
  if (char * cost_str = next_element(p, true)) {
    if (char * note_str = std::strchr(cost_str, ';')) {
      *note_str++ = '\0';
      xact->note = skip_ws(note_str);
    }

    char * price_str = std::strchr(cost_str, '@');
    if (price_str) {
      *price_str++ = '\0';
      xact->cost.parse(price_str, ledger);
    }

    xact->amount.parse(cost_str, ledger);

    if (price_str)
      xact->cost *= xact->amount;
    else
      xact->cost = xact->amount;
  }

  if (*p == '[' || *p == '(') {
    xact->flags |= TRANSACTION_VIRTUAL;
    if (*p == '[')
      xact->flags |= TRANSACTION_BALANCE;
    p++;

    char * e = p + (std::strlen(p) - 1);
    assert(*e == ')' || *e == ']');
    *e = '\0';
  }

  xact->account = account->find_account(p);

  if (! xact->amount.commodity)
    xact->amount.commodity = ledger->find_commodity("", true);
  if (! xact->cost.commodity)
    xact->cost.commodity = ledger->find_commodity("", true);

  return xact;
}

transaction_t * parse_transaction(std::istream& in, ledger_t * ledger,
				  account_t * account, entry_t * entry)
{
  static char line[MAX_LINE + 1];
  in.getline(line, MAX_LINE);
  linenum++;

  return parse_transaction_text(line, ledger, account, entry);
}

class automated_transaction_t
{
public:
  masks_list        masks;
  transactions_list transactions;

  automated_transaction_t(masks_list& _masks,
			  transactions_list& _transactions) {
    masks.insert(masks.begin(), _masks.begin(), _masks.end());
    transactions.insert(transactions.begin(),
			_transactions.begin(), _transactions.end());
    // Take over ownership of the pointers
    _transactions.clear();
  }

  ~automated_transaction_t() {
    for (transactions_list::iterator i = transactions.begin();
	 i != transactions.end();
	 i++)
      delete *i;
  }

  void extend_entry(entry_t * entry);
};

typedef std::vector<automated_transaction_t *>
  automated_transactions_vector;

void automated_transaction_t::extend_entry(entry_t * entry)
{
  for (transactions_list::iterator i = entry->transactions.begin();
       i != entry->transactions.end();
       i++)
    if (matches(masks, *((*i)->account))) {
      for (transactions_list::iterator t = transactions.begin();
	   t != transactions.end();
	   t++) {
	amount_t amt;
	if ((*t)->amount.commodity->symbol.empty())
	  amt = (*i)->amount * (*t)->amount;
	else
	  amt = (*t)->amount;

	transaction_t * xact
	  = new transaction_t(entry, (*t)->account, amt, amt, (*t)->flags);
	entry->add_transaction(xact);
      }
    }
}

class automated_transactions_t
{
public:
  automated_transactions_vector automated_transactions;

  ~automated_transactions_t() {
    for (automated_transactions_vector::iterator i
	   = automated_transactions.begin();
	 i != automated_transactions.end();
	 i++)
      delete *i;
  }

  void extend_entry(entry_t * entry) {
    for (automated_transactions_vector::iterator i
	   = automated_transactions.begin();
	 i != automated_transactions.end();
	 i++)
      (*i)->extend_entry(entry);
  }

  void add_automated_transaction(automated_transaction_t * auto_xact) {
    automated_transactions.push_back(auto_xact);
  }
  bool remove_automated_transaction(automated_transaction_t * auto_xact) {
    for (automated_transactions_vector::iterator i
	   = automated_transactions.begin();
	 i != automated_transactions.end();
	 i++) {
      if (*i == auto_xact) {
	automated_transactions.erase(i);
	return true;
      }
    }
    return false;
  }
};

void parse_automated_transactions(std::istream& in, ledger_t * ledger,
				  account_t * account,
				  automated_transactions_t& auto_xacts)
{
  static char line[MAX_LINE + 1];

  masks_list masks;

  while (! in.eof() && in.peek() == '=') {
    in.getline(line, MAX_LINE);
    linenum++;

    char * p = line + 1;
    p = skip_ws(p);

    masks.push_back(mask_t(p));
  }

  transactions_list xacts;

  while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t')) {
    if (transaction_t * xact = parse_transaction(in, ledger, account, NULL)) {
      if (! xact->amount)
	throw parse_error(path, linenum,
			  "All automated transactions must have a value");
      else
	xacts.push_back(xact);
    }
  }

  if (! masks.empty() && ! xacts.empty()) {
    automated_transaction_t * auto_xact
      = new automated_transaction_t(masks, xacts);
    auto_xacts.add_automated_transaction(auto_xact);
  }
}

bool finalize_entry(entry_t * entry)
{
  // Scan through and compute the total balance for the entry.  This
  // is used for auto-calculating the value of entries with no cost,
  // and the per-unit price of unpriced commodities.

  balance_t balance;

  for (transactions_list::const_iterator x = entry->transactions.begin();
       x != entry->transactions.end();
       x++)
    if (! ((*x)->flags & TRANSACTION_VIRTUAL) ||
	((*x)->flags & TRANSACTION_BALANCE))
      balance += (*x)->cost;

  // If one transaction of a two-line transaction is of a different
  // commodity than the others, and it has no per-unit price,
  // determine its price by dividing the unit count into the value of
  // the balance.  This is done for the last eligible commodity.

  if (! balance.amounts.empty() && balance.amounts.size() == 2)
    for (transactions_list::const_iterator x = entry->transactions.begin();
	 x != entry->transactions.end();
	 x++) {
      if ((*x)->cost != (*x)->amount || ((*x)->flags & TRANSACTION_VIRTUAL))
	continue;

      for (amounts_map::const_iterator i = balance.amounts.begin();
	   i != balance.amounts.end();
	   i++)
	if ((*i).second.commodity != (*x)->amount.commodity) {
	  assert((*x)->amount);
	  balance -= (*x)->cost;
	  (*x)->cost = - (*i).second;
	  balance += (*x)->cost;
	  break;
	}

      break;
    }

  // Walk through each of the transactions, fixing up any that we
  // can, and performing any on-the-fly calculations.

  bool empty_allowed = true;

  for (transactions_list::const_iterator x = entry->transactions.begin();
       x != entry->transactions.end();
       x++) {
    if ((*x)->amount || ((*x)->flags & TRANSACTION_VIRTUAL))
      continue;

    if (! empty_allowed || balance.amounts.empty() ||
	balance.amounts.size() != 1)
      return false;

    empty_allowed = false;

    // If one transaction gives no value at all -- and all the
    // rest are of the same commodity -- then its value is the
    // inverse of the computed value of the others.

    amounts_map::const_iterator i = balance.amounts.begin();
    (*x)->amount = (*x)->cost = - balance.amount((*i).first);

    balance = 0;
  }

  return ! balance;
}

entry_t * parse_entry(std::istream& in, ledger_t * ledger,
		      account_t * master)
{
  entry_t * curr = new entry_t;

  static char line[MAX_LINE + 1];
  in.getline(line, MAX_LINE);
  linenum++;

  // Parse the date

  char * next = next_element(line);

  if (! quick_parse_date(line, &curr->date))
    throw parse_error(path, linenum, "Failed to parse date");

  // Parse the optional cleared flag: *

  if (*next == '*') {
    curr->state = entry_t::CLEARED;
    next = skip_ws(++next);
  }

  // Parse the optional code: (TEXT)

  if (*next == '(') {
    if (char * p = std::strchr(next++, ')')) {
      *p++ = '\0';
      curr->code = next;
      next = skip_ws(p);
    }
  }

  // Parse the description text

  curr->payee = next;

  // Parse all of the transactions associated with this entry

  while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t'))
    if (transaction_t * xact = parse_transaction(in, ledger, master, curr))
      curr->add_transaction(xact);

  // If there were no transactions, throw away the entry

  if (curr->transactions.empty() || ! finalize_entry(curr)) {
    delete curr;
    return NULL;
  }

  return curr;
}

//////////////////////////////////////////////////////////////////////
//
// Textual ledger parser
//

unsigned int parse_textual_ledger(std::istream& in, ledger_t * ledger,
				  account_t * master)
{
  static char   line[MAX_LINE + 1];
  char		c;
  unsigned int  count = 0;
  unsigned int  errors = 0;
  commodity_t *	time_commodity = NULL;

  std::list<account_t *>   account_stack;
  automated_transactions_t auto_xacts;

  if (! master)
    master = ledger->master;

  account_stack.push_front(master);

  path	  = ledger->sources.back();
  linenum = 1;

  while (! in.eof()) {
    try {
      switch (in.peek()) {
      case -1:                    // end of file
	goto done;

      case ' ':
      case '\t':
	if (peek_next_nonws(in) != '\n') {
	  in.getline(line, MAX_LINE);
	  linenum++;
	  throw parse_error(path, linenum,
			    "Ignoring entry beginning with whitespace");
	}
	// fall through...

      case '\n':
	linenum++;
      case '\r':                  // skip blank lines
	in.get(c);
	break;

#ifdef TIMELOG_SUPPORT
      case 'i':
      case 'I': {
	std::string date, time;

	in >> c;
	in >> date;
	in >> time;
	date += " ";
	date += time;

	in.getline(line, MAX_LINE);
	linenum++;

	char * p = skip_ws(line);
	char * n = next_element(p, true);
	last_desc = n ? n : "";

	static struct std::tm when;
	if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) {
	  time_in      = std::mktime(&when);
	  last_account = account_stack.front()->find_account(p);
	} else {
	  last_account = NULL;
	  throw parse_error(path, linenum, "Cannot parse timelog entry date");
	}
	break;
      }

      case 'o':
      case 'O':
	if (last_account) {
	  std::string date, time;

	  in >> c;
	  in >> date;
	  in >> time;

	  in.getline(line, MAX_LINE);
	  linenum++;

	  date += " ";
	  date += time;

	  static struct std::tm when;
	  if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) {
	    entry_t * curr = new entry_t;
	    curr->date  = std::mktime(&when);
	    curr->state = entry_t::CLEARED;
	    curr->code  = "";
	    curr->payee = last_desc;

	    double diff = std::difftime(curr->date, time_in) / 60.0 / 60.0;
	    char   buf[32];
	    std::sprintf(buf, "%fh", diff);
	    amount_t amt;
	    amt.parse(buf, ledger);
	    time_commodity = amt.commodity;

	    transaction_t * xact = new transaction_t(curr, last_account, amt, amt,
						     TRANSACTION_VIRTUAL);
	    curr->add_transaction(xact);

	    if (! finalize_entry(curr) || ! ledger->add_entry(curr))
	      assert(0);

	    count++;
	  } else {
	    throw parse_error(path, linenum, "Cannot parse timelog entry date");
	  }

	  last_account = NULL;
	} else {
	  in.getline(line, MAX_LINE);
	  linenum++;
	}
	break;
#endif // TIMELOG_SUPPORT

      case 'P': {			// a pricing entry
	in >> c;

	std::time_t date;
	std::string symbol;

	in >> line;		// the date
	if (! quick_parse_date(line, &date))
	  throw parse_error(path, linenum, "Failed to parse date");

	int hour, min, sec;

	in >> hour;		// the time
	in >> c;
	in >> min;
	in >> c;
	in >> sec;

	date = std::time_t(((unsigned long) date) +
			   hour * 3600 + min * 60 + sec);

	amount_t price;

	parse_commodity(in, symbol);
	in >> line;		// the price
	price.parse(line, ledger);

	commodity_t * commodity = ledger->find_commodity(symbol, true);
	commodity->add_price(date, price);
	break;
      }

      case 'N': {			// don't download prices
	std::string symbol;

	in >> c;
	parse_commodity(in, symbol);

	commodity_t * commodity = ledger->find_commodity(line, true);
	commodity->flags |= (COMMODITY_STYLE_CONSULTED |
			     COMMODITY_STYLE_NOMARKET);
	break;
      }

      case 'C': {			// a flat conversion
	in >> c;

	std::string symbol;
	amount_t    price;

	parse_commodity(in, symbol);
	in >> line;		// the price
	price.parse(line, ledger);

	commodity_t * commodity = ledger->find_commodity(symbol, true);
	commodity->set_conversion(price);
	break;
      }

      case 'Y':                   // set the current year
	in >> c;
	in >> now_tm->tm_year;
	now_tm->tm_year -= 1900;
	break;

#ifdef TIMELOG_SUPPORT
      case 'h':
      case 'b':
#endif
      case ';':                   // a comment line
	in.getline(line, MAX_LINE);
	linenum++;
	break;

      case '=':                   // automated transactions
	parse_automated_transactions(in, ledger, account_stack.front(),
				     auto_xacts);
	break;

      case '@': {                 // account specific
	in >> c;
	if (in.peek() == '@') {
	  in.get(c);
	  account_stack.pop_front();
	  break;
	}

	in.getline(line, MAX_LINE);
	linenum++;

	account_t * acct = account_stack.front()->find_account(skip_ws(line));
	account_stack.push_front(acct);
	break;
      }

      case '!':                   // directive
	in >> line;
	if (std::string(line) == "!include") {
	  in.getline(line, MAX_LINE);
	  linenum++;

	  char * p = skip_ws(line);
	  std::ifstream stream(p);

	  ledger->sources.push_back(p);

	  unsigned int curr_linenum = linenum;
	  std::string  curr_path    = path;

	  count += parse_textual_ledger(stream, ledger, account_stack.front());

	  linenum = curr_linenum;
	  path    = curr_path;
	}
	break;

      default: {
	unsigned int first_line = linenum;
	if (entry_t * entry = parse_entry(in, ledger, account_stack.front())) {
	  if (! auto_xacts.automated_transactions.empty())
	    auto_xacts.extend_entry(entry);

	  if (ledger->add_entry(entry))
	    count++;
	  else
	    throw parse_error(path, first_line, "Entry does not balance");
	} else {
	  throw parse_error(path, first_line, "Failed to parse entry");
	}
	break;
      }
      }
    }
    catch (const parse_error& err) {
      std::cerr << "Error: " << err.what() << std::endl;
      errors++;
    }
  }

 done:
  if (time_commodity) {
    time_commodity->precision = 2;
    time_commodity->flags |= (COMMODITY_STYLE_CONSULTED |
			      COMMODITY_STYLE_NOMARKET);
  }

  if (errors > 0) {
    std::ostringstream msg;
    msg << "Errors parsing file '" << path << "'";
    throw error(msg.str());
  }

  return count;
}

//////////////////////////////////////////////////////////////////////
//
// Textual ledger printing code
//

void print_transaction(std::ostream& out, transaction_t * xact,
		       bool display_amount, bool display_cost)
{
  std::ostringstream s;
  s << *(xact->account);
  std::string acct_name = s.str();

  if (xact->flags & TRANSACTION_VIRTUAL) {
    if (xact->flags & TRANSACTION_BALANCE)
      acct_name = std::string("[") + acct_name + "]";
    else
      acct_name = std::string("(") + acct_name + ")";
  }

  out.width(30);
  out.fill(' ');
  out << std::left << acct_name;

  if (xact->amount && display_amount) {
    out << "  ";
    out.width(12);
    out.fill(' ');
    std::ostringstream s;
    s << xact->amount;
    out << std::right << s.str();
  }

  if (xact->amount && display_cost &&
      xact->amount != xact->cost) {
    out << " @ ";
    out << xact->cost / xact->amount;
  }

  if (! xact->note.empty())
    out << "  ; " << xact->note;

  out << std::endl;
}

void print_textual_entry(std::ostream& out, entry_t * entry, bool shortcut)
{
  char buf[32];
  std::strftime(buf, 31, "%Y/%m/%d ", std::gmtime(&entry->date));
  out << buf;

  if (entry->state == entry_t::CLEARED)
    out << "* ";
  if (! entry->code.empty())
    out << '(' << entry->code << ") ";
  if (! entry->payee.empty())
    out << entry->payee;

  out << std::endl;

  const commodity_t * comm = NULL;
  int size = 0;

  for (transactions_list::const_iterator x
	 = entry->transactions.begin();
       x != entry->transactions.end();
       x++) {
    if ((*x)->flags & TRANSACTION_VIRTUAL &&
	! ((*x)->flags & TRANSACTION_BALANCE))
      continue;

    if (! comm)
      comm = (*x)->amount.commodity;
    else if (comm != (*x)->amount.commodity)
      shortcut = false;

    size++;
  }

  if (shortcut && size != 2)
    shortcut = false;

  for (transactions_list::const_iterator x
	 = entry->transactions.begin();
       x != entry->transactions.end();
       x++) {
    out << "    ";
    print_transaction(out, *x,
		      (! shortcut || x == entry->transactions.begin() ||
		       ((*x)->flags & TRANSACTION_VIRTUAL &&
			! ((*x)->flags & TRANSACTION_BALANCE))),
		      size != 2);
  }

  out << std::endl;
}

void print_textual_ledger(std::ostream& out, ledger_t * ledger,
			  bool shortcut)
{
  for (entries_list::const_iterator i = ledger->entries.begin();
       i != ledger->entries.end();
       i++)
    print_textual_entry(out, *i, shortcut);
}

} // namespace ledger

#ifdef PARSE_TEST

int main(int argc, char *argv[])
{
  book.sources.push_back(argv[1]);
  std::ifstream stream(argv[1]);
  ledger::ledger_t book;
  int count = parse_textual_ledger(stream, &book, book.master);
  std::cout << "Read " << count << " entries." << std::endl;
  print_textual_ledger(std::cout, &book, true);
}

#endif // PARSE_TEST