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
|
#include "ledger.h"
#include <fstream>
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_register(int argc, char **argv, std::ostream& out);
extern void print_ledger(int argc, char *argv[], std::ostream& out);
bool show_cleared;
std::time_t begin_date;
bool have_beginning;
std::time_t end_date;
bool have_ending;
}
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[])
{
// Parse the command-line options
std::istream * file = NULL;
#ifdef HUQUQULLAH
compute_huquq = true;
#endif
have_beginning = false;
have_ending = false;
show_cleared = false;
int c;
while (-1 != (c = getopt(argc, argv, "+b:e:cChHwf:i:p:"))) {
switch (char(c)) {
case 'b': {
struct tm * when = getdate(optarg);
if (! when) {
std::cerr << "Error: Bad begin date string: " << optarg
<< std::endl;
} else {
begin_date = std::mktime(when);
have_beginning = true;
}
break;
}
case 'e': {
struct tm * when = getdate(optarg);
if (! when) {
std::cerr << "Error: Bad end date string: " << optarg
<< std::endl;
} else {
end_date = std::mktime(when);
have_ending = true;
}
break;
}
case 'c':
end_date = std::time(NULL);
have_ending = true;
break;
case 'C': show_cleared = true; break;
case 'h': show_help(std::cout); break;
#ifdef HUQUQULLAH
case 'H': compute_huquq = false; break;
#endif
case 'w': use_warnings = true; break;
case 'f': file = new std::ifstream(optarg); break;
// -i path-to-file-of-regexps
case 'i':
read_regexps(optarg, regexps);
break;
// -p "COMMODITY=PRICE"
// -p path-to-price-database
case 'p':
if (access(optarg, R_OK) != -1) {
std::ifstream pricedb(optarg);
while (! pricedb.eof()) {
char buf[80];
pricedb.getline(buf, 79);
if (*buf && ! std::isspace(*buf))
main_ledger.record_price(buf);
}
} else {
main_ledger.record_price(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 || ! *file) {
std::cerr << "Please specify the ledger file using the -f option."
<< std::endl;
return 1;
}
// Global defaults
commodity * usd = new commodity("$", true, false, true, false, 2);
main_ledger.commodities.insert(commodities_entry("USD", usd));
#ifdef HUQUQULLAH
if (compute_huquq) {
new commodity("H", true, true, true, false, 2);
new commodity("mithqal", false, true, true, false, 1);
read_regexps(".huquq", huquq_categories);
main_ledger.record_price("H=" DEFAULT_COMMODITY "0.19");
main_ledger.record_price("troy=8.5410148523 mithqal");
}
#endif
// Read the command word
const std::string command = argv[optind];
#ifdef HUQUQ_CATEGORIES
if (command == "register")
compute_huquq = false;
#endif
// 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
if (command == "balance")
report_balances(argc - optind, &argv[optind], std::cout);
else if (command == "register")
print_register(argc - optind, &argv[optind], std::cout);
else if (command == "print")
print_ledger(argc - optind, &argv[optind], std::cout);
}
// main.cc ends here.
|