diff --git a/src/grammar/expenses.y b/src/grammar/expenses.y index a4f8386..499cf15 100644 --- a/src/grammar/expenses.y +++ b/src/grammar/expenses.y @@ -21,6 +21,9 @@ %code requires { #include "../../src/grammar/types.h" + +/* Parse a string with Flex+Bison instead of a file / stdout */ +int grammar_parse_string(const GString *buf); } %define parse.lac full @@ -78,3 +81,11 @@ ; %% +/* Parse a string with Flex+Bison instead of a file / stdout */ +int grammar_parse_string(const GString *buf) { + int res; + yy_scan_string(buf->str); + res = yyparse(); + yylex_destroy(); + return res; +} diff --git a/src/grammar/types.c b/src/grammar/types.c index f595012..f33afb1 100644 --- a/src/grammar/types.c +++ b/src/grammar/types.c @@ -30,15 +30,6 @@ g_filename = fn; } -/* Parse a string with Flex+Bison instead of a file / stdout */ -int grammar_parse_string(const GString *buf) { - int res; - yy_scan_string(buf->str); - res = yyparse(); - yylex_destroy(); - return res; -} - int bot_get_current_entry_id ( const GString* filename @@ -67,13 +58,13 @@ BotExpenseEntry *create_bot_expense_entry ( const BotExpenseEntry *previous, + const unsigned int id, const BotCommand cmd, const double value, GString* user, GString* notes ) { - unsigned int id; double signedValue = value; BotExpenseEntry *entry = malloc(sizeof *entry); // TODO this should be freed once it's written to registry struct tm *lt = malloc(sizeof *lt); @@ -90,12 +81,6 @@ signedValue = -value; } - if(previous == NULL) { - id = 1; - } else { - id = previous->id + 1; - } - entry->id = id; entry->time = *lt; entry->value = signedValue; @@ -196,21 +181,16 @@ unsigned int id; BotExpenseEntry *entry; BotExpenseEntry *previous = bot_get_last_expense(); - bool needsHeaders = false; - entry = create_bot_expense_entry(previous, cmd, value, user, notes); + id = (previous == NULL) ? bot_get_current_entry_id(filename) : previous->id + 1; + + entry = create_bot_expense_entry(previous, id, cmd, value, user, notes); if(entry == NULL) { return -1; } - if(previous == NULL) { /* first message received by the bot; needs to get latest ID */ - id = bot_get_current_entry_id(filename); - if(id == 0) { /* file is empty; initialize with headers */ - needsHeaders = true; - } - } - - result = write_bot_expense_entry(filename, entry, needsHeaders); + /* if no ID, empty file, initialize with headers before writing entry */ + result = write_bot_expense_entry(filename, entry, (id == 0)); if(result > 0) { /* write successful */ bot_set_last_expense(entry); diff --git a/src/grammar/types.h b/src/grammar/types.h index 58cddb1..63562d8 100644 --- a/src/grammar/types.h +++ b/src/grammar/types.h @@ -59,6 +59,7 @@ BotExpenseEntry *create_bot_expense_entry ( const BotExpenseEntry *previous, + const unsigned int id, const BotCommand cmd, const double value, GString* user,