diff options
Diffstat (limited to 'plugins/Boltun/src/Engine/Mind.cpp')
-rw-r--r-- | plugins/Boltun/src/Engine/Mind.cpp | 188 |
1 files changed, 78 insertions, 110 deletions
diff --git a/plugins/Boltun/src/Engine/Mind.cpp b/plugins/Boltun/src/Engine/Mind.cpp index 5158a459e6..11e636cd81 100644 --- a/plugins/Boltun/src/Engine/Mind.cpp +++ b/plugins/Boltun/src/Engine/Mind.cpp @@ -80,8 +80,7 @@ vector<tstring> Mind::Parse(tstring s) return res;
s.resize(len);
int it = 0;
- while (it != len)
- {
+ while (it != len) {
while (it != len && _istspace(s[it]))
it++;
if (it == len)
@@ -110,19 +109,15 @@ void Mind::Load(tstring filename) bool start = true;
- try
- {
- while (file.good())
- {
+ try {
+ while (file.good()) {
getline(file, st);
if (st.empty())
break;
line++;
- if (start)
- {
- if (st[0] == 65279)
- {
+ if (start) {
+ if (st[0] == 65279) {
st.erase(0, 1);
fileTypeMark = true;
}
@@ -136,16 +131,13 @@ void Mind::Load(tstring filename) c = co = new TCHAR[count + 1];
mir_tstrcpy(c, st.c_str());
size_t pos = 0;
- while (pos < count && _istspace(*c))
- {
+ while (pos < count && _istspace(*c)) {
++pos;
++c;
}
count -= pos;
- if (count > 2)
- {
- switch (*c)
- {
+ if (count > 2) {
+ switch (*c) {
case '(':
if (c[count - 1] != ')')
abort();
@@ -160,13 +152,15 @@ void Mind::Load(tstring filename) toLowerStr(c);
{
WordsList l(c);
- if (!l.IsEmpty())
+ if (!l.IsEmpty()) {
if (l.IsQuestion())
data->qkeywords.insert(make_pair(l, s1));
else
data->keywords.insert(make_pair(l, s1));
+ }
}
break;
+
case '{':
if (c[count - 1] != '}')
abort();
@@ -188,6 +182,7 @@ void Mind::Load(tstring filename) data->specialEscapes.insert(make_pair(l, s1));
}
break;
+
case '[':
if (c[count - 1] != ']')
throw error;
@@ -202,6 +197,7 @@ void Mind::Load(tstring filename) toLowerStr(c);
data->widelyUsed.insert(make_pair(c, s1));
break;
+
case '<':
if (c[count - 1] != '>')
throw error;
@@ -213,62 +209,56 @@ void Mind::Load(tstring filename) ++c;
count -= 2;
c[count] = '\0';
- if (mir_tstrcmp(c, _T("QUESTION")) == 0)
- {
+ if (mir_tstrcmp(c, _T("QUESTION")) == 0) {
toLowerStr(c);
data->question.insert(s1);
}
- else
- if (mir_tstrcmp(c, _T("IGNORED")) == 0)
- {
- toLowerStr(c);
- data->special.insert(s1);
- }
- else
- if (mir_tstrcmp(c, _T("ESCAPE")) == 0)
- {
- data->escape.push_back(s1);
- }
- else
- if (mir_tstrcmp(c, _T("FAILURE")) == 0)
- {
- data->failure.push_back(s1);
- }
- else
- if (mir_tstrcmp(c, _T("REPEAT")) == 0)
- {
- data->repeats.push_back(s1);
- }
- else
- {
- if (mir_tstrcmp(c, _T("INITIAL")) != 0)
- throw error;
- data->initial.push_back(s1);
- }
+ else if (mir_tstrcmp(c, _T("IGNORED")) == 0) {
+ toLowerStr(c);
+ data->special.insert(s1);
+ }
+ else if (mir_tstrcmp(c, _T("ESCAPE")) == 0) {
+ data->escape.push_back(s1);
+ }
+ else if (mir_tstrcmp(c, _T("FAILURE")) == 0) {
+ data->failure.push_back(s1);
+ }
+ else if (mir_tstrcmp(c, _T("REPEAT")) == 0) {
+ data->repeats.push_back(s1);
+ }
+ else {
+ if (mir_tstrcmp(c, _T("INITIAL")) != 0)
+ throw error;
+ data->initial.push_back(s1);
+ }
break;
+
case '@':
- {
if (file.eof())
throw error;
+
getline(file, s1);
line++;
format(s1);
++c;
count -= 1;
toLowerStr(c);
- tstring sc(c);
- int count1 = (int)s1.length();
- TCHAR *c = new TCHAR[count1 + 1];
- mir_tstrcpy(c, s1.c_str());
- CharLower(c);
- s1 = c;
- delete c;
- vector<tstring> strs = Parse(s1);
- data->raliases.insert(make_pair(sc, strs));
- for (vector<tstring>::const_iterator it = strs.begin(); it != strs.end(); ++it)
- data->aliases.insert(make_pair(*it, sc));
- }
- break;
+ {
+ std::transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
+ int count1 = (int)s1.length();
+ TCHAR *c = new TCHAR[count1 + 1];
+ mir_tstrcpy(c, s1.c_str());
+ CharLower(c);
+ s1 = c;
+ delete c;
+ vector<tstring> strs = Parse(s1);
+ tstring sc(c);
+ data->raliases.insert(make_pair(sc, strs));
+ for (vector<tstring>::const_iterator it = strs.begin(); it != strs.end(); ++it)
+ data->aliases.insert(make_pair(*it, sc));
+ }
+ break;
+
default:
if (file.eof())
throw error;
@@ -279,25 +269,21 @@ void Mind::Load(tstring filename) data->study.insert(make_pair(c, s1));
}
}
- else
- if (count)
- {
- if (file.eof())
- throw error;
- getline(file, s1);
- line++;
- format(s1);
- data->study.insert(make_pair(c, s1));
- }
+ else if (count) {
+ if (file.eof())
+ throw error;
+ getline(file, s1);
+ line++;
+ format(s1);
+ data->study.insert(make_pair(c, s1));
+ }
}
if (!file.eof())
- {
throw error;
- }
+
delete co;
}
- catch (...)
- {
+ catch (...) {
throw CorruptedMind(line);
delete co;
}
@@ -315,80 +301,64 @@ void Mind::Save(tstring filename) const if (fileTypeMark)
file << TCHAR(65279);
- for (string_mmap::iterator it = data->study.begin(); it != data->study.end(); ++it)
- {
+ for (string_mmap::iterator it = data->study.begin(); it != data->study.end(); ++it) {
file << (*it).first << _T('\r') << endl;
file << (*it).second << _T('\r') << endl;
}
- for (multimap<WordsList, tstring>::iterator it = data->keywords.begin(); it != data->keywords.end(); ++it)
- {
+ for (multimap<WordsList, tstring>::iterator it = data->keywords.begin(); it != data->keywords.end(); ++it) {
file << _T(" (") << (tstring)(*it).first << _T(")") << _T('\r') << endl;
file << (*it).second << _T('\r') << endl;
}
- for (multimap<WordsList, tstring>::iterator it = data->qkeywords.begin(); it != data->qkeywords.end(); ++it)
- {
+ for (multimap<WordsList, tstring>::iterator it = data->qkeywords.begin(); it != data->qkeywords.end(); ++it) {
file << _T(" (") << (tstring)(*it).first << _T(")") << _T('\r') << endl;
file << (*it).second << _T('\r') << endl;
}
- for (multimap<WordsList, tstring>::iterator it = data->specialEscapes.begin(); it != data->specialEscapes.end(); ++it)
- {
+ for (multimap<WordsList, tstring>::iterator it = data->specialEscapes.begin(); it != data->specialEscapes.end(); ++it) {
file << _T(" {") << (tstring)(*it).first << _T("}") << _T('\r') << endl;
file << (*it).second << _T('\r') << endl;
}
- for (multimap<WordsList, tstring>::iterator it = data->qspecialEscapes.begin(); it != data->qspecialEscapes.end(); ++it)
- {
+ for (multimap<WordsList, tstring>::iterator it = data->qspecialEscapes.begin(); it != data->qspecialEscapes.end(); ++it) {
file << _T(" {") << (tstring)(*it).first << _T("}") << _T('\r') << endl;
file << (*it).second << _T('\r') << endl;
}
- for (string_mmap::iterator it = data->widelyUsed.begin(); it != data->widelyUsed.end(); ++it)
- {
+ for (string_mmap::iterator it = data->widelyUsed.begin(); it != data->widelyUsed.end(); ++it) {
file << _T(" [") << (*it).first << _T("]") << _T('\r') << endl;
file << (*it).second << _T('\r') << endl;
}
- for (set<tstring>::iterator it = data->question.begin(); it != data->question.end(); ++it)
- {
+ for (set<tstring>::iterator it = data->question.begin(); it != data->question.end(); ++it) {
file << _T(" <QUESTION>") << _T('\r') << endl;
file << (*it) << _T('\r') << endl;
}
- for (set<tstring>::iterator it = data->special.begin(); it != data->special.end(); ++it)
- {
+ for (set<tstring>::iterator it = data->special.begin(); it != data->special.end(); ++it) {
file << _T(" <IGNORED>") << _T('\r') << endl;
file << (*it) << _T('\r') << endl;
}
- for (string_vec::iterator it = data->escape.begin(); it != data->escape.end(); ++it)
- {
+ for (string_vec::iterator it = data->escape.begin(); it != data->escape.end(); ++it) {
file << _T(" <ESCAPE>") << _T('\r') << endl;
file << (*it) << _T('\r') << endl;
}
- for (string_vec::iterator it = data->initial.begin(); it != data->initial.end(); ++it)
- {
+ for (string_vec::iterator it = data->initial.begin(); it != data->initial.end(); ++it) {
file << _T(" <INITIAL>") << _T('\r') << endl;
file << (*it) << _T('\r') << endl;
}
- for (string_vec::iterator it = data->failure.begin(); it != data->failure.end(); ++it)
- {
+ for (string_vec::iterator it = data->failure.begin(); it != data->failure.end(); ++it) {
file << _T(" <FAILURE>") << _T('\r') << endl;
file << (*it) << _T('\r') << endl;
}
- for (string_vec::iterator it = data->repeats.begin(); it != data->repeats.end(); ++it)
- {
+ for (string_vec::iterator it = data->repeats.begin(); it != data->repeats.end(); ++it) {
file << _T(" <REPEAT>") << _T('\r') << endl;
file << (*it) << _T('\r') << endl;
}
- for (map<tstring, vector<tstring>>::const_iterator it = data->raliases.begin(); it != data->raliases.end(); ++it)
- {
+ for (map<tstring, vector<tstring>>::const_iterator it = data->raliases.begin(); it != data->raliases.end(); ++it) {
tstring s;
const vector<tstring>& v = (*it).second;
bool first = true;
- for (vector<tstring>::const_iterator it1 = v.begin(); it1 != v.end(); ++it1)
- {
- if (first)
- {
+ for (vector<tstring>::const_iterator it1 = v.begin(); it1 != v.end(); ++it1) {
+ if (first) {
first = false;
s = *it1;
}
- else
- {
+ else {
s += _T(" ") + *it1;
}
}
@@ -404,8 +374,7 @@ void Mind::LoadSmiles(tstring filename) data->smiles.clear();
tstring s;
unsigned int l = 0;
- while (!file.eof())
- {
+ while (!file.eof()) {
getline(file, s);
if (s.length() > l)
l = (int)s.length();
@@ -420,8 +389,7 @@ void Mind::LoadSmiles(void *smiles, size_t size) TCHAR* buf = (TCHAR*)smiles;
unsigned l = 0;
TCHAR* end = buf + size;
- while (buf != end)
- {
+ while (buf != end) {
TCHAR *lend = buf;
while (lend != end && *lend != _T('\r'))
lend++;
|