diff options
-rw-r--r-- | account.cc | 23 | ||||
-rw-r--r-- | ledger.cc | 73 | ||||
-rw-r--r-- | ledger.h | 8 | ||||
-rw-r--r-- | main.cc | 2 |
4 files changed, 106 insertions, 0 deletions
@@ -89,4 +89,27 @@ std::string account_t::fullname() const } } +bool account_t::valid() const +{ + if (name.find('-') != std::string::npos) + return false; + + if (depth > 16) + return false; + + for (transactions_list::const_iterator i = transactions.begin(); + i != transactions.end(); + i++) + if ((*i)->account != this) + return false; + + for (accounts_map::const_iterator i = accounts.begin(); + i != accounts.end(); + i++) + if (! (*i).second->valid()) + return false; + + return true; +} + } // namespace ledger @@ -8,6 +8,65 @@ namespace ledger { const std::string version = "2.0b"; +bool transaction_t::valid() const +{ + if (! entry) + return false; + + bool found = false; + for (transactions_list::const_iterator i = entry->transactions.begin(); + i != entry->transactions.end(); + i++) + if (*i == this) { + found = true; + break; + } + if (! found) + return false; + + if (! account) + return false; + + found = false; + for (transactions_list::const_iterator i = account->transactions.begin(); + i != account->transactions.end(); + i++) + if (*i == this) { + found = true; + break; + } + if (! found) + return false; + + if (! amount.valid()) + return false; + + if (cost && ! cost->valid()) + return false; + + if (flags & ~0x000f) + return false; + + return true; +} + +bool entry_t::valid() const +{ + if (! date || date == -1) + return false; + + if (state != UNCLEARED && state != CLEARED && state != PENDING) + return false; + + for (transactions_list::const_iterator i = transactions.begin(); + i != transactions.end(); + i++) + if ((*i)->entry != this || ! (*i)->valid()) + return false; + + return true; +} + journal_t::~journal_t() { DEBUG_PRINT("ledger.memory.dtors", "dtor journal_t"); @@ -180,6 +239,20 @@ entry_t * journal_t::derive_entry(strings_list::iterator i, return added.release(); } +bool journal_t::valid() const +{ + if (! master->valid()) + return false; + + for (entries_list::const_iterator i = entries.begin(); + i != entries.end(); + i++) + if (! (*i)->valid()) + return false; + + return true; +} + void initialize_amounts(); void shutdown_amounts(); @@ -59,6 +59,8 @@ class transaction_t if (cost) delete cost; } + + bool valid() const; }; @@ -93,6 +95,8 @@ class entry_t transactions.remove(xact); return true; } + + bool valid() const; }; @@ -146,6 +150,8 @@ class account_t bool remove_transaction(transaction_t * xact); friend class journal_t; + + bool valid() const; }; inline std::ostream& operator<<(std::ostream& out, const account_t& acct) { @@ -201,6 +207,8 @@ class journal_t entry_t * derive_entry(strings_list::iterator begin, strings_list::iterator end) const; + + bool valid() const; }; extern const std::string version; @@ -633,6 +633,8 @@ int main(int argc, char * argv[], char * envp[]) TIMER_STOP(write_cache); #ifdef DO_CLEANUP + VALIDATE(journal->valid()); + shutdown(); #endif |