1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include "parser.h"
#include "journal.h"
#include "config.h"
#include <fstream>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
namespace ledger {
typedef std::list<parser_t *> parsers_list;
static parsers_list * parsers = NULL;
void initialize_parser_support()
{
parsers = new parsers_list;
}
void shutdown_parser_support()
{
if (parsers) {
delete parsers;
parsers = NULL;
}
}
bool register_parser(parser_t * parser)
{
parsers_list::iterator i;
for (i = parsers->begin(); i != parsers->end(); i++)
if (*i == parser)
break;
if (i != parsers->end())
return false;
parsers->push_back(parser);
return true;
}
bool unregister_parser(parser_t * parser)
{
parsers_list::iterator i;
for (i = parsers->begin(); i != parsers->end(); i++)
if (*i == parser)
break;
if (i == parsers->end())
return false;
parsers->erase(i);
return true;
}
unsigned int parse_journal(std::istream& in,
config_t& config,
journal_t * journal,
account_t * master,
const std::string * original_file)
{
if (! master)
master = journal->master;
for (parsers_list::iterator i = parsers->begin();
i != parsers->end();
i++)
if ((*i)->test(in))
return (*i)->parse(in, config, journal, master, original_file);
return 0;
}
unsigned int parse_journal_file(const std::string& path,
config_t& config,
journal_t * journal,
account_t * master,
const std::string * original_file)
{
journal->sources.push_back(path);
if (access(path.c_str(), R_OK) == -1)
throw new error(std::string("Cannot read file '") + path + "'");
if (! original_file)
original_file = &path;
std::ifstream stream(path.c_str());
return parse_journal(stream, config, journal, master, original_file);
}
extern parser_t * binary_parser_ptr;
extern parser_t * xml_parser_ptr;
extern parser_t * textual_parser_ptr;
unsigned int parse_ledger_data(config_t& config,
journal_t * journal,
parser_t * cache_parser,
parser_t * xml_parser,
parser_t * stdin_parser)
{
unsigned int entry_count = 0;
if (! cache_parser)
cache_parser = binary_parser_ptr;
if (! xml_parser)
xml_parser = xml_parser_ptr;
if (! stdin_parser)
stdin_parser = textual_parser_ptr;
DEBUG_PRINT("ledger.config.cache",
"3. use_cache = " << config.use_cache);
if (config.use_cache && ! config.cache_file.empty() &&
config.cache_file != "<none>" && ! config.data_file.empty()) {
DEBUG_PRINT("ledger.config.cache",
"using_cache " << config.cache_file);
config.cache_dirty = true;
if (access(config.cache_file.c_str(), R_OK) != -1) {
std::ifstream stream(config.cache_file.c_str());
if (cache_parser && cache_parser->test(stream)) {
std::string price_db_orig = journal->price_db;
journal->price_db = config.price_db;
entry_count += cache_parser->parse(stream, config, journal,
NULL, &config.data_file);
if (entry_count > 0)
config.cache_dirty = false;
else
journal->price_db = price_db_orig;
}
}
}
if (entry_count == 0 && ! config.data_file.empty()) {
account_t * acct = NULL;
if (! config.account.empty())
acct = journal->find_account(config.account);
journal->price_db = config.price_db;
if (! journal->price_db.empty() &&
access(journal->price_db.c_str(), R_OK) != -1) {
if (parse_journal_file(journal->price_db, config, journal)) {
throw new error("Entries not allowed in price history file");
} else {
DEBUG_PRINT("ledger.config.cache",
"read price database " << journal->price_db);
journal->sources.pop_back();
}
}
DEBUG_PRINT("ledger.config.cache",
"rejected cache, parsing " << config.data_file);
if (config.data_file == "-") {
config.use_cache = false;
journal->sources.push_back("/dev/stdin");
std::ostringstream buffer;
while (std::cin.good() && ! std::cin.eof()) {
static char line[8192];
std::cin.read(line, 8192);
std::streamsize count = std::cin.gcount();
buffer.write(line, count);
}
buffer.flush();
std::istringstream buf_in(buffer.str());
entry_count += parse_journal(buf_in, config, journal, acct);
}
else if (access(config.data_file.c_str(), R_OK) != -1) {
entry_count += parse_journal_file(config.data_file, config,
journal, acct);
if (! journal->price_db.empty())
journal->sources.push_back(journal->price_db);
}
// Clear out what was set during the textual parsing phase
clear_account_xdata acct_cleaner;
walk_accounts(*journal->master, acct_cleaner);
}
VALIDATE(journal->valid());
return entry_count;
}
} // namespace ledger
|