summaryrefslogtreecommitdiff
path: root/main.cc
blob: 2d4f672506184bf940f4567738b0142f1b842a2b (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
#include <fstream>

#include "ledger.h"

#include <pcre.h>               // Perl regular expression library

namespace ledger {
  extern bool parse_ledger(std::istream& in);
  extern bool parse_gnucash(std::istream& in);

  extern void report_balances(int argc, char **argv, std::ostream& out);
  extern void print_ledger(int argc, char *argv[], std::ostream& out);
}

using namespace ledger;

void show_help(std::ostream& out)
{
  out << "usage: ledger [options] DATA_FILE COMMAND [ARGS]"
      << std::endl
      << "options:" << std::endl
      << "  -s  show sub-accounts in balance totals" << std::endl
      << "  -S  show empty accounts in balance totals" << std::endl
      << "commands:" << std::endl
      << "  balance  show balance totals" << std::endl
      << "  print    print all ledger entries" << std::endl;
}

//////////////////////////////////////////////////////////////////////
//
// Command-line parser and top-level logic.
//

int main(int argc, char *argv[])
{
  // Global defaults

  commodity * usd = new commodity("$", true, false, true, false, 2);
  commodities.insert(commodities_entry("USD", usd));

  // Parse the command-line options

  std::istream * file = NULL;

  int c;
  while (-1 != (c = getopt(argc, argv, "+hwf:"))) {
    switch (char(c)) {
    case 'h': show_help(std::cout); break;
    case 'w': use_warnings = true; break;
    case 'f': file = new std::ifstream(optarg); break;
    }
  }

  if (optind == argc) {
    std::cerr << "usage: ledger [options] COMMAND [options] [ARGS]" << std::endl
	      << std::endl
	      << "ledger options:" << std::endl
	      << "  -f FILE  specify pathname of ledger data file" << std::endl
	      << std::endl
	      << "commands:" << std::endl
	      << "  balance  show balance totals" << std::endl
	      << "  print    print all ledger entries" << std::endl
	      << std::endl
	      << "`balance' command options:" << std::endl
	      << "  -s       show sub-accounts in balance totals" << std::endl
	      << "  -S       show empty accounts in balance totals" << std::endl;
    return 1;
  }

  // The -f option is required

  if (! file) {
    std::cerr << "Please specify the ledger file using the -f option."
	      << std::endl;
    return 1;
  }

  // Parse the ledger

  char buf[32];
  file->get(buf, 31);
  file->seekg(0);

  if (std::strncmp(buf, "<?xml version=\"1.0\"?>", 21) == 0)
    parse_gnucash(*file);
  else
    parse_ledger(*file);

  delete file;

  // Process the command

  const std::string command = argv[optind];

  if (command == "balance")
    report_balances(argc - optind, &argv[optind], std::cout);
  else if (command == "print")
    print_ledger(argc - optind, &argv[optind], std::cout);
}

// main.cc ends here.