syngroups: simplify structure, allow unlimited line length

This commit is contained in:
Jean-Francois Dockes 2015-11-21 12:45:18 +01:00
parent 413caa050b
commit 062f7a6991

View file

@ -46,9 +46,9 @@ public:
} }
bool ok; bool ok;
// Term to group num // Term to group num
STD_UNORDERED_MAP<string, int> terms; STD_UNORDERED_MAP<string, unsigned int> terms;
// Group num to group // Group num to group
STD_UNORDERED_MAP<int, vector<string> > groups; vector<vector<string> > groups;
}; };
bool SynGroups::ok() bool SynGroups::ok()
@ -61,8 +61,6 @@ SynGroups::~SynGroups()
delete m; delete m;
} }
const int LL = 1024;
SynGroups::SynGroups() SynGroups::SynGroups()
: m(new Internal) : m(new Internal)
{ {
@ -72,13 +70,18 @@ bool SynGroups::setfile(const string& fn)
{ {
LOGDEB(("SynGroups::setfile(%s)\n", fn.c_str())); LOGDEB(("SynGroups::setfile(%s)\n", fn.c_str()));
if (!m) { if (!m) {
LOGERR(("SynGroups:setfile:: new Internal failed: no mem ?\n")); m = new Internal;
return false; if (!m) {
LOGERR(("SynGroups:setfile:: new Internal failed: no mem ?\n"));
return false;
}
} }
// Don't set ok to true. if (fn.empty()) {
if (fn.empty()) delete m;
m = 0;
return true; return true;
}
ifstream input; ifstream input;
input.open(fn.c_str(), ios::in); input.open(fn.c_str(), ios::in);
@ -88,18 +91,19 @@ bool SynGroups::setfile(const string& fn)
return false; return false;
} }
char cline[LL]; string cline;
bool appending = false; bool appending = false;
string line; string line;
bool eof = false; bool eof = false;
int lnum = 0; int lnum = 0;
for (;;) { for (;;) {
cline[0] = 0; cline.clear();
input.getline(cline, LL-1); getline(input, cline);
if (!input.good()) { if (!input.good()) {
if (input.bad()) { if (input.bad()) {
LOGDEB(("Parse: input.bad()\n")); LOGERR(("Syngroup::setfile(%s):Parse: input.bad()\n",
fn.c_str()));
return false; return false;
} }
// Must be eof ? But maybe we have a partial line which // Must be eof ? But maybe we have a partial line which
@ -110,10 +114,11 @@ bool SynGroups::setfile(const string& fn)
lnum++; lnum++;
{ {
size_t ll = strlen(cline); string::size_type pos = cline.find_last_not_of("\n\r");
while (ll > 0 && (cline[ll-1] == '\n' || cline[ll-1] == '\r')) { if (pos == string::npos) {
cline[ll-1] = 0; cline.clear();
ll--; } else if (pos != cline.length()-1) {
cline.erase(pos+1);
} }
} }
@ -147,18 +152,18 @@ bool SynGroups::setfile(const string& fn)
if (words.empty()) if (words.empty())
continue; continue;
if (words.size() == 1) { if (words.size() == 1) {
LOGDEB(("SynGroups:setfile: single term group at line %d ??\n", LOGERR(("Syngroup::setfile(%s):single term group at line %d ??\n",
lnum)); fn.c_str(), lnum));
continue; continue;
} }
m->groups[lnum] = words; m->groups.push_back(words);
for (vector<string>::const_iterator it = words.begin(); for (vector<string>::const_iterator it = words.begin();
it != words.end(); it++) { it != words.end(); it++) {
m->terms[*it] = lnum; m->terms[*it] = m->groups.size()-1;
} }
LOGDEB(("SynGroups::setfile: group: [%s]\n", LOGDEB1(("SynGroups::setfile: group: [%s]\n",
stringsToString(m->groups[lnum]).c_str())); stringsToString(m->groups.back()).c_str()));
} }
m->ok = true; m->ok = true;
return true; return true;
@ -170,28 +175,26 @@ vector<string> SynGroups::getgroup(const string& term)
if (!ok()) if (!ok())
return ret; return ret;
STD_UNORDERED_MAP<string, int>::const_iterator it1 = m->terms.find(term); STD_UNORDERED_MAP<string, unsigned int>::const_iterator it1 =
m->terms.find(term);
if (it1 == m->terms.end()) { if (it1 == m->terms.end()) {
LOGDEB1(("SynGroups::getgroup: [%s] not found in direct map\n", LOGDEB1(("SynGroups::getgroup: [%s] not found in direct map\n",
term.c_str())); term.c_str()));
return ret; return ret;
} }
// Group num to group unsigned int idx = it1->second;
STD_UNORDERED_MAP<int, vector<string> >::const_iterator it2 = if (idx >= m->groups.size()) {
m->groups.find(it1->second); LOGERR(("SynGroups::getgroup: line index higher than line count !\n"));
return ret;
if (it2 == m->groups.end()) {
LOGERR(("SynGroups::getgroup: %s found in direct map but no group\n",
term.c_str()));
return ret;
} }
return it2->second; return m->groups[idx];
} }
#else #else
#include "syngroups.h" #include "syngroups.h"
#include "debuglog.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
@ -229,7 +232,10 @@ int main(int argc, char **argv)
string fn = *argv++;argc--; string fn = *argv++;argc--;
string word = *argv++;argc--; string word = *argv++;argc--;
SynGroups syns(fn); DebugLog::getdbl()->setloglevel(DEBDEB1);
DebugLog::setfilename("stderr");
SynGroups syns;
syns.setfile(fn);
if (!syns.ok()) { if (!syns.ok()) {
cerr << "Initialization failed\n"; cerr << "Initialization failed\n";
return 1; return 1;