/*
Copyright 2010-2011 Grégory Soutadé
This file is part of KissCount.
KissCount is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KissCount is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KissCount. If not, see .
*/
#include "ImportEngine.h"
ImportEngine::ImportEngine()
{
}
ImportEngine::~ImportEngine()
{
}
bool ImportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
{
_path = path;
_user = user;
_db = db;
_kiss = kiss;
_accounts.clear();
_unresolvedAccounts.clear();
_operations.clear();
_descriptions.clear();
return path.EndsWith(_shortExt) || path.EndsWith(_shortExt.Upper());
}
wxString ImportEngine::GetFileExt()
{
return wxGetTranslation(_longExt);
}
std::vector ExplodeString(wxString& s)
{
wxString tmp = s, cur = wxT("");
std::vector res;
while (tmp.Len())
{
if (tmp.StartsWith(wxT(" ")) ||
tmp.StartsWith(wxT("\t")) ||
tmp.StartsWith(wxT("\n")) ||
tmp.StartsWith(wxT("\r")))
{
if (cur.Len())
{
res.push_back(cur);
cur = wxT("");
}
tmp = tmp.Mid(1);
continue;
}
cur += tmp.SubString(0, 0);
tmp = tmp.Mid(1);
}
if (cur.Len())
res.push_back(cur);
return res;
}
/*
Remove :
- head spaces
- tail spaces
- every word starting by a number
*/
wxString ImportEngine::RemoveUnused(wxString& s)
{
wxString tmp = s, tmp2;
wxString res;
tmp = tmp.Trim(true);
tmp = tmp.Trim(false);
while (tmp.Len())
{
tmp2 = tmp.SubString(0, 0);
if (tmp2.IsNumber())
{
do
{
tmp = tmp.Mid(1);
tmp2 = tmp.SubString(0, 0);
} while (tmp.Len() && tmp2 != wxT(" ") &&
tmp2 != wxT("\t") &&
tmp2 != wxT("\r") &&
tmp2 != wxT("\n"));
}
res += tmp2;
tmp = tmp.Mid(1);
}
return res;
}
/*
Find a pattern between the two strings:
%mX : lower string of orig[X]
%MX : upper string of orig[X]
%fX : First case in upper case of orig[X]
%wX : word orig[X]
other : constants
*/
wxString ImportEngine::FindPattern(wxString& orig, wxString& dest)
{
wxString pattern, cur_pat;
int i, a;
std::vector tok1 = ExplodeString(orig);
std::vector tok2 = ExplodeString(dest);
int size1 = tok1.size(), size2 = tok2.size();
for(i=0; i tok1 = ExplodeString(pattern.filter);
std::vector tok2 = ExplodeString(op.description);
int size1 = tok1.size(), i;
long pos;
op.description = wxT("");
for(i=0; i_importPatterns[key1] = pattern;
for(i=pos+1; i<(int)_operations.size(); i++)
{
key2 = RemoveUnused(_descriptions[_operations[i].id]);
if (key1 == key2)
{
ApplyPattern(_user->_importPatterns[key2], _operations[i]);
nbOpUpdated++;
}
}
return nbOpUpdated;
}
void ImportEngine::MatchPattern(wxString& originalKey, Operation& op)
{
wxString key1;
ImportPattern pattern;
if (!_user) return;
key1 = RemoveUnused(originalKey);
if (!_user->_importPatterns.count(key1))
{
pattern.filter = FindPattern(originalKey, op.description);
pattern.account = op.account;
pattern.category = op.category;
_user->_importPatterns[key1] = pattern;
// std::cout << "New pattern " << key1.mb_str() << "\t" << pattern.filter.mb_str() << std::endl;
}
else
{
op.description = _descriptions[op.id];
ApplyPattern(_user->_importPatterns[key1], op);
}
}