diff options
-rw-r--r-- | binary.h | 25 | ||||
-rw-r--r-- | main.cc | 12 | ||||
-rw-r--r-- | parser.cc | 35 | ||||
-rw-r--r-- | parser.h | 31 | ||||
-rw-r--r-- | qif.h | 21 | ||||
-rw-r--r-- | textual.cc | 4 | ||||
-rw-r--r-- | textual.h | 23 |
7 files changed, 143 insertions, 8 deletions
diff --git a/binary.h b/binary.h new file mode 100644 index 00000000..4b502df5 --- /dev/null +++ b/binary.h @@ -0,0 +1,25 @@ +#ifndef _BINARY_H +#define _BINARY_H + +#include "parser.h" + +namespace ledger { + +class binary_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +void write_binary_journal(std::ostream& out, + journal_t * journal, + strings_list * files = NULL); + +} // namespace ledger + +#endif // _BINARY_H @@ -195,13 +195,13 @@ int main(int argc, char * argv[], char * envp[]) try { if (! config->init_file.empty()) - if (parser_t::parse(config->init_file, journal.get())) + if (parser_t::parse_file(config->init_file, journal.get())) throw error("Entries not allowed in initialization file"); if (use_cache && ! config->cache_file.empty() && ! config->data_file.empty()) { - entry_count += parser_t::parse(config->cache_file, journal.get(), - NULL, &config->data_file); + entry_count += parser_t::parse_file(config->cache_file, journal.get(), + NULL, &config->data_file); journal->sources.pop_front(); // remove cache_file if (entry_count == 0) { @@ -221,12 +221,12 @@ int main(int argc, char * argv[], char * envp[]) use_cache = false; entry_count += text_parser->parse(std::cin, journal.get(), account); } else { - entry_count += parser_t::parse(config->data_file, journal.get(), - account); + entry_count += parser_t::parse_file(config->data_file, journal.get(), + account); } if (! config->price_db.empty()) - if (parser_t::parse(config->price_db, journal.get())) + if (parser_t::parse_file(config->price_db, journal.get())) throw error("Entries not allowed in price history file"); } diff --git a/parser.cc b/parser.cc new file mode 100644 index 00000000..6a97caa2 --- /dev/null +++ b/parser.cc @@ -0,0 +1,35 @@ +#include "parser.h" + +#include <fstream> + +namespace ledger { + +parsers_list parser_t::parsers; + +unsigned int parser_t::parse_file(const std::string& path, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + journal->sources.push_back(path); + + if (access(path.c_str(), R_OK) == -1) + return 0; + + std::ifstream stream(path.c_str()); + + if (! master) + master = journal->master; + if (! original_file) + original_file = &path; + + for (parsers_list::iterator i = parser_t::parsers.begin(); + i != parser_t::parsers.end(); + i++) + if ((*i)->test(stream)) + return (*i)->parse(stream, journal, master, original_file); + + return 0; +} + +} // namespace ledger diff --git a/parser.h b/parser.h new file mode 100644 index 00000000..3ced3432 --- /dev/null +++ b/parser.h @@ -0,0 +1,31 @@ +#ifndef _PARSER_H +#define _PARSER_H + +#include "ledger.h" + +namespace ledger { + +class parser_t; +typedef std::list<parser_t *> parsers_list; + +class parser_t +{ + public: + virtual bool test(std::istream& in) const = 0; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL) = 0; + + static parsers_list parsers; + + static unsigned int parse_file(const std::string& path, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +} // namespace ledger + +#endif // _PARSER_H @@ -0,0 +1,21 @@ +#ifndef _QIF_H +#define _QIF_H + +#include "parser.h" + +namespace ledger { + +class qif_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +} // namespace ledger + +#endif // _QIF_H @@ -533,8 +533,8 @@ unsigned int textual_parser_t::parse(std::istream& in, push_var<unsigned int> save_linenum(linenum); push_var<std::string> save_path(path); - count += parser_t::parse(skip_ws(line), journal, - account_stack.front()); + count += parser_t::parse_file(skip_ws(line), journal, + account_stack.front()); } break; diff --git a/textual.h b/textual.h new file mode 100644 index 00000000..5fe17dd8 --- /dev/null +++ b/textual.h @@ -0,0 +1,23 @@ +#ifndef _TEXTUAL_H +#define _TEXTUAL_H + +#include "parser.h" + +namespace ledger { + +class textual_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const { + return true; + } + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +} // namespace ledger + +#endif // _TEXTUAL_H |