diff options
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 65 |
1 files changed, 31 insertions, 34 deletions
@@ -1,44 +1,49 @@ #include <fstream> -#include <vector> -#include <cassert> - -#include <pcre.h> // Perl regular expression library #include "ledger.h" +#include <pcre.h> // Perl regular expression library + ////////////////////////////////////////////////////////////////////// // // Command-line parser and top-level logic. // namespace ledger { - extern bool parse_ledger(std::istream& in, std::vector<entry *>& ledger); - extern bool parse_gnucash(std::istream& in, std::vector<entry *>& ledger); - extern void report_balances(std::ostream& out, std::vector<entry *>& ledger, - bool show_children, bool show_empty); - extern void print_ledger(std::ostream& out, std::vector<entry *>& 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; +} + int main(int argc, char *argv[]) { - // Setup global defaults + // Global defaults commodity_usd = new commodity("$", true, false, 2); - commodities.insert(commodities_entry("$", commodity_usd)); commodities.insert(commodities_entry("USD", commodity_usd)); // Parse the command-line options - bool show_children = false; - bool show_empty = false; - int c; - while (-1 != (c = getopt(argc, argv, "sS"))) { + while (-1 != (c = getopt(argc, argv, "+h"))) { switch (char(c)) { - case 's': show_children = true; break; - case 'S': show_empty = true; break; + case 'h': show_help(std::cout); break; } } @@ -51,36 +56,28 @@ int main(int argc, char *argv[]) << "commands:" << std::endl << " balance show balance totals" << std::endl << " print print all ledger entries" << std::endl; - std::exit(1); + return 1; } // Parse the ledger std::ifstream file(argv[optind++]); - std::vector<entry *> ledger; - char buf[256]; - file.get(buf, 255); + char buf[32]; + file.get(buf, 31); file.seekg(0); if (std::strncmp(buf, "<?xml version=\"1.0\"?>", 21) == 0) - parse_gnucash(file, ledger); + parse_gnucash(file); else - parse_ledger(file, ledger); - - // Read the command word - - if (optind == argc) { - std::cerr << "Command word missing" << std::endl; - return 1; - } - - const std::string command = argv[optind++]; + parse_ledger(file); // Process the command + const std::string command = argv[optind]; + if (command == "balance") - report_balances(std::cout, ledger, show_children, show_empty); + report_balances(argc - optind, &argv[optind], std::cout); else if (command == "print") - print_ledger(std::cout, ledger); + print_ledger(argc - optind, &argv[optind], std::cout); } |