Record the beginning/ending line and position of entries parsed within
Gnucash files.
This commit is contained in:
parent
b5726ac831
commit
13524610c9
1 changed files with 25 additions and 8 deletions
33
gnucash.cc
33
gnucash.cc
|
|
@ -34,6 +34,14 @@ static account_comm_map account_comms;
|
||||||
static unsigned int count;
|
static unsigned int count;
|
||||||
static std::string have_error;
|
static std::string have_error;
|
||||||
|
|
||||||
|
static std::istream * instreamp;
|
||||||
|
static unsigned int offset;
|
||||||
|
static XML_Parser parser;
|
||||||
|
static std::string path;
|
||||||
|
static unsigned int src_idx;
|
||||||
|
static istream_pos_type beg_pos;
|
||||||
|
static unsigned long beg_line;
|
||||||
|
|
||||||
static enum {
|
static enum {
|
||||||
NO_ACTION,
|
NO_ACTION,
|
||||||
ACCOUNT_NAME,
|
ACCOUNT_NAME,
|
||||||
|
|
@ -126,6 +134,11 @@ static void endElement(void *userData, const char *name)
|
||||||
have_error = "The above entry does not balance";
|
have_error = "The above entry does not balance";
|
||||||
delete curr_entry;
|
delete curr_entry;
|
||||||
} else {
|
} else {
|
||||||
|
curr_entry->src_idx = src_idx;
|
||||||
|
curr_entry->beg_pos = beg_pos;
|
||||||
|
curr_entry->beg_line = beg_line;
|
||||||
|
curr_entry->end_pos = instreamp->tellg();
|
||||||
|
curr_entry->end_line = XML_GetCurrentLineNumber(parser) - offset;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
curr_entry = NULL;
|
curr_entry = NULL;
|
||||||
|
|
@ -309,6 +322,10 @@ unsigned int gnucash_parser_t::parse(std::istream& in,
|
||||||
curr_comm = NULL;
|
curr_comm = NULL;
|
||||||
entry_comm = NULL;
|
entry_comm = NULL;
|
||||||
|
|
||||||
|
instreamp = ∈
|
||||||
|
path = original_file ? *original_file : "<gnucash>";
|
||||||
|
src_idx = journal->sources.size() - 1;
|
||||||
|
|
||||||
// GnuCash uses the USD commodity without defining it, which really
|
// GnuCash uses the USD commodity without defining it, which really
|
||||||
// means $.
|
// means $.
|
||||||
commodity_t * usd;
|
commodity_t * usd;
|
||||||
|
|
@ -317,28 +334,28 @@ unsigned int gnucash_parser_t::parse(std::istream& in,
|
||||||
usd = new commodity_t("$", 2, COMMODITY_STYLE_THOUSANDS);
|
usd = new commodity_t("$", 2, COMMODITY_STYLE_THOUSANDS);
|
||||||
commodity_t::add_commodity(usd, "USD");
|
commodity_t::add_commodity(usd, "USD");
|
||||||
|
|
||||||
unsigned int offset = 2;
|
offset = 2;
|
||||||
XML_Parser parser = XML_ParserCreate(NULL);
|
parser = current_parser = XML_ParserCreate(NULL);
|
||||||
current_parser = parser;
|
|
||||||
|
|
||||||
XML_SetElementHandler(parser, startElement, endElement);
|
XML_SetElementHandler(parser, startElement, endElement);
|
||||||
XML_SetCharacterDataHandler(parser, dataHandler);
|
XML_SetCharacterDataHandler(parser, dataHandler);
|
||||||
|
|
||||||
while (! in.eof()) {
|
while (in.good() && ! in.eof()) {
|
||||||
|
beg_pos = in.tellg();
|
||||||
|
beg_line = (XML_GetCurrentLineNumber(parser) - offset) + 1;
|
||||||
|
|
||||||
in.getline(buf, BUFSIZ - 1);
|
in.getline(buf, BUFSIZ - 1);
|
||||||
std::strcat(buf, "\n");
|
std::strcat(buf, "\n");
|
||||||
if (! XML_Parse(parser, buf, std::strlen(buf), in.eof())) {
|
if (! XML_Parse(parser, buf, std::strlen(buf), in.eof())) {
|
||||||
unsigned long line = XML_GetCurrentLineNumber(parser) - offset++;
|
unsigned long line = XML_GetCurrentLineNumber(parser) - offset++;
|
||||||
const char * msg = XML_ErrorString(XML_GetErrorCode(parser));
|
const char * msg = XML_ErrorString(XML_GetErrorCode(parser));
|
||||||
XML_ParserFree(parser);
|
XML_ParserFree(parser);
|
||||||
throw parse_error(original_file ? *original_file : "<gnucash>", line,
|
throw parse_error(path, line, msg);
|
||||||
msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! have_error.empty()) {
|
if (! have_error.empty()) {
|
||||||
unsigned long line = XML_GetCurrentLineNumber(parser) - offset++;
|
unsigned long line = XML_GetCurrentLineNumber(parser) - offset++;
|
||||||
parse_error err(original_file ? *original_file : "<gnucash>", line,
|
parse_error err(path, line, have_error);
|
||||||
have_error);
|
|
||||||
std::cerr << "Error: " << err.what() << std::endl;
|
std::cerr << "Error: " << err.what() << std::endl;
|
||||||
have_error = "";
|
have_error = "";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue