diff options
author | John Wiegley <johnw@newartisans.com> | 2003-09-30 00:09:43 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2003-09-30 00:09:43 +0000 |
commit | 7bf86bc48a564ffffa46461c15ae2ab34b258fe8 (patch) | |
tree | a33b8365b8dc834413997170e2200f818f5fb894 /gnucash.cc | |
parent | 3667f06594a99dd6f0080747732e7eff639343b1 (diff) | |
download | fork-ledger-7bf86bc48a564ffffa46461c15ae2ab34b258fe8.tar.gz fork-ledger-7bf86bc48a564ffffa46461c15ae2ab34b258fe8.tar.bz2 fork-ledger-7bf86bc48a564ffffa46461c15ae2ab34b258fe8.zip |
*** empty log message ***
Diffstat (limited to 'gnucash.cc')
-rw-r--r-- | gnucash.cc | 41 |
1 files changed, 21 insertions, 20 deletions
@@ -1,14 +1,12 @@ #include <sstream> -#include <vector> #include <cstring> -#include <cassert> + +#include "ledger.h" extern "C" { #include <xmlparse.h> // expat XML parser } -#include "ledger.h" - namespace ledger { static account * curr_account; @@ -20,8 +18,6 @@ static amount * curr_value; static std::string curr_quant; static XML_Parser current_parser; -static std::vector<entry *> * current_ledger; - enum { NO_ACTION, ACCOUNT_NAME, @@ -116,7 +112,7 @@ static void endElement(void *userData, const char *name) << XML_GetCurrentLineNumber(current_parser) << std::endl; curr_entry->print(std::cerr); } else { - current_ledger->push_back(curr_entry); + ledger.push_back(curr_entry); } curr_entry = NULL; } @@ -192,11 +188,18 @@ static void dataHandler(void *userData, const char *s, int len) case XACT_ACCOUNT: { accounts_iterator i = accounts.find(std::string(s, len)); - assert(i != accounts.end()); - curr_entry->xacts.back()->acct = (*i).second; + if (i == accounts.end()) { + std::cerr << "Could not find account " << std::string(s, len) + << " at line " << XML_GetCurrentLineNumber(current_parser) + << std::endl; + std::exit(1); + } + + transaction * xact = curr_entry->xacts.back(); + xact->acct = (*i).second; std::string value = curr_quant + " " + (*i).second->comm->symbol; - curr_entry->xacts.back()->cost = create_amount(value.c_str(), curr_value); + xact->cost = create_amount(value.c_str(), curr_value); break; } @@ -215,27 +218,25 @@ static void dataHandler(void *userData, const char *s, int len) } } -bool parse_gnucash(std::istream& in, std::vector<entry *>& ledger) +bool parse_gnucash(std::istream& in) { char buf[BUFSIZ]; - XML_Parser parser = XML_ParserCreate(NULL); - current_parser = parser; - - //XML_SetUserData(parser, &depth); - XML_SetElementHandler(parser, startElement, endElement); - XML_SetCharacterDataHandler(parser, dataHandler); - - current_ledger = &ledger; - curr_account = NULL; curr_entry = NULL; curr_comm = NULL; action = NO_ACTION; + XML_Parser parser = XML_ParserCreate(NULL); + current_parser = parser; + + XML_SetElementHandler(parser, startElement, endElement); + XML_SetCharacterDataHandler(parser, dataHandler); + while (! in.eof()) { in.getline(buf, BUFSIZ - 1); + if (! XML_Parse(parser, buf, std::strlen(buf), in.eof())) { std::cerr << XML_ErrorString(XML_GetErrorCode(parser)) << " at line " << XML_GetCurrentLineNumber(parser) |