added some error checking

This commit is contained in:
John Wiegley 2004-09-23 02:37:42 -04:00
parent 12c0c08f1e
commit 4e6157b74a
2 changed files with 26 additions and 8 deletions

View file

@ -3,6 +3,7 @@
#include "quotes.h"
#include <fstream>
#include <stdlib.h>
namespace ledger {
@ -206,14 +207,16 @@ void parse_ledger_data(journal_t * journal,
{
int entry_count = 0;
if (! config.init_file.empty()) {
if (! config.init_file.empty() &&
access(config.init_file.c_str(), R_OK) != -1) {
if (parse_journal_file(config.init_file, journal))
throw error("Entries not allowed in initialization file");
journal->sources.pop_front(); // remove init file
}
if (cache_parser && config.use_cache &&
! config.cache_file.empty() && ! config.data_file.empty()) {
! config.cache_file.empty() &&
! config.data_file.empty()) {
config.cache_dirty = true;
if (access(config.cache_file.c_str(), R_OK) != -1) {
std::ifstream stream(config.cache_file.c_str());
@ -355,7 +358,11 @@ OPT_BEGIN(init, "i:") {
} OPT_END(init);
OPT_BEGIN(file, "f:") {
config.data_file = optarg;
if (access(optarg, R_OK) != -1)
config.data_file = optarg;
else
throw error(std::string("The ledger file '") + optarg +
"' does not exist or is not readable");
} OPT_END(file);
OPT_BEGIN(cache, ":") {

21
main.cc
View file

@ -247,8 +247,13 @@ int parse_and_report(int argc, char * argv[], char * envp[])
TIMER_START(report_gen);
std::ostream * out = &std::cout;
if (! config.output_file.empty())
out = new std::ofstream(config.output_file.c_str());
if (! config.output_file.empty()) {
if (access(config.output_file.c_str(), W_OK) == -1)
throw error(std::string("Cannot write output to file '" +
config.output_file + "'"));
else
out = new std::ofstream(config.output_file.c_str());
}
// Compile the format strings
@ -328,9 +333,15 @@ int parse_and_report(int argc, char * argv[], char * envp[])
TIMER_START(write_cache);
if (config.use_cache && config.cache_dirty && ! config.cache_file.empty()) {
std::ofstream stream(config.cache_file.c_str());
write_binary_journal(stream, journal.get(), &journal->sources);
if (config.use_cache && config.cache_dirty &&
! config.cache_file.empty()) {
if (access(config.cache_file.c_str(), W_OK) == -1) {
std::cerr << "Warning: Cannot update cache file '"
<< config.cache_file << "'" << std::endl;
} else {
std::ofstream stream(config.cache_file.c_str());
write_binary_journal(stream, journal.get(), &journal->sources);
}
}
TIMER_STOP(write_cache);