diff --git a/src/model/Account.h b/src/model/Account.h index 42d2fcc..b8ea99a 100644 --- a/src/model/Account.h +++ b/src/model/Account.h @@ -45,6 +45,11 @@ struct Account { return (ac1.name.Cmp(ac2.name) < 0); } + + bool operator == (const wxString& accountId) + { + return id == accountId; + } }; #endif diff --git a/src/model/Category.h b/src/model/Category.h index f1c714c..f099d7e 100644 --- a/src/model/Category.h +++ b/src/model/Category.h @@ -20,6 +20,8 @@ #ifndef CATEGORY_H #define CATEGORY_H +#include + struct Category { wxString id; @@ -29,6 +31,11 @@ struct Category wxColour forecolor; wxString font; bool fix_cost; + + bool operator == (const wxString& catId) + { + return id == catId; + } }; #endif diff --git a/src/model/Operation.cpp b/src/model/Operation.cpp index 6617318..faf41d2 100644 --- a/src/model/Operation.cpp +++ b/src/model/Operation.cpp @@ -19,7 +19,7 @@ #include "Operation.h" -bool sortOperations(Operation op1, Operation op2) +bool sortOperations(const Operation& op1, const Operation& op2) { if (!op1.fix_cost && op2.fix_cost) return false; if (op1.fix_cost && !op2.fix_cost) return true; @@ -42,7 +42,7 @@ bool sortOperations(Operation op1, Operation op2) return false; } -bool reverseSortOperations(Operation op1, Operation op2) +bool reverseSortOperations(const Operation& op1, const Operation& op2) { return !sortOperations(op1, op2); } diff --git a/src/model/Operation.h b/src/model/Operation.h index f9504e2..3ed8fc8 100644 --- a/src/model/Operation.h +++ b/src/model/Operation.h @@ -42,7 +42,7 @@ struct Operation { std::vector childs; } ; -bool sortOperations(Operation op1, Operation op2); -bool reverseSortOperations(Operation op1, Operation op2); +bool sortOperations(const Operation& op1, const Operation& op2); +bool reverseSortOperations(const Operation& op1, const Operation& op2); #endif diff --git a/src/model/User.cpp b/src/model/User.cpp index 1b6110f..559e23d 100644 --- a/src/model/User.cpp +++ b/src/model/User.cpp @@ -45,11 +45,9 @@ void User::InvalidateOperations() Category User::GetCategory(const wxString& catId) { Category cat; - std::vector::iterator it; + std::vector::iterator it = std::find(_categories.begin(), _categories.end(), catId); - for (it=_categories.begin(); it !=_categories.end(); it++) - if (it->id == catId) - return *it; + if (it !=_categories.end()) return *it; if (_db->LoadCategory(catId, wxT(""), cat)) return cat; @@ -67,11 +65,9 @@ Category User::GetCategory(const wxString& catId) wxString User::GetCategoryName(const wxString& catId) { Category cat; - std::vector::iterator it; + std::vector::iterator it = std::find(_categories.begin(), _categories.end(), catId); - for (it=_categories.begin(); it !=_categories.end(); it++) - if (it->id == catId) - return it->name; + if (it !=_categories.end()) return it->name; if (_db->LoadCategory(catId, wxT(""), cat)) return cat.name; @@ -109,20 +105,21 @@ const wxFont User::GetCategoryFont(const wxString& catId) return f; } -Account User::GetAccount(const wxString& accountId) +Account User::GetAccount(const wxString& accountId) throw (AccountNotFound) { - std::vector::iterator it; - for (it=_accounts.begin(); it !=_accounts.end(); it++) - if (it->id == accountId) - return *it; + std::vector::iterator it = std::find(_accounts.begin(), _accounts.end(), accountId); + + if (it != _accounts.end()) + return *it; + + throw AccountNotFound(); } wxString User::GetAccountName(const wxString& accountId) { - std::vector::iterator it; - for (it=_accounts.begin(); it !=_accounts.end(); it++) - if (it->id == accountId) - return it->name; + std::vector::iterator it = std::find(_accounts.begin(), _accounts.end(), accountId); + + if (it != _accounts.end()) return it->name; return _("Unknown") ; } @@ -218,9 +215,7 @@ bool User::Group(std::vector* ops, const Operation& op) { if (it->id == op.parent) { - for (it2 = it->childs.begin(); it2 != it->childs.end(); it2++) - if (*it2 == op.id) - break; + it2 = std::find(it->childs.begin(), it->childs.end(), op.id); // Already into childs if (it2 != it->childs.end()) return true; it->childs.push_back(op.id); diff --git a/src/model/User.h b/src/model/User.h index dbc89cc..65e7d86 100644 --- a/src/model/User.h +++ b/src/model/User.h @@ -51,12 +51,14 @@ public: std::map _preferences; std::map _importPatterns; + class AccountNotFound {}; + Category GetCategory(const wxString& catId); wxString GetCategoryName(const wxString& catId); wxString GetCategoryId(const wxString& catName); const wxFont GetCategoryFont(const wxString& catId); - Account GetAccount(const wxString& accountId); + Account GetAccount(const wxString& accountId) throw (AccountNotFound); wxString GetAccountName(const wxString& accountId); wxString GetAccountId(const wxString& accountName); diff --git a/src/model/import/XMLImportEngine.cpp b/src/model/import/XMLImportEngine.cpp index 692b587..ed10c69 100644 --- a/src/model/import/XMLImportEngine.cpp +++ b/src/model/import/XMLImportEngine.cpp @@ -54,13 +54,13 @@ void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs) if (account_number.Length()) { - for (i=0; i<(int)_this->_user->_accounts.size(); i++) + try { + Account ac = _this->_user->GetAccount(account_number); + _this->_accounts[id] = ac.id; + return; + } + catch (User::AccountNotFound) { - if (_this->_user->_accounts[i].number == account_number) - { - _this->_accounts[id] = _this->_user->_accounts[i].id; - return; - } } } @@ -135,13 +135,12 @@ void XMLImportEngine::LoadCategory(XMLImportEngine* _this, const char** attrs) UNESCAPE_CHARS(cat.name); - for (i=0; i<(int)_this->_user->_categories.size(); i++) + wxString catId = _this->_user->GetCategoryId(name); + + if (catId != wxT("0")) { - if (_this->_user->_categories[i].name == name) - { - _this->_categories[id] = _this->_user->_categories[i].id; - return; - } + _this->_categories[id] = catId; + return; } _this->_categories[id] = wxT("unknown-") + name;