pass iterator to FileReader::addFromFile() in order to keep track of the erroneous column, added verbose mode to FileReader

This commit is contained in:
john30
2014-12-18 23:06:31 +01:00
parent 4b7ff1d97f
commit 985d1f4389
5 changed files with 31 additions and 24 deletions
+3 -5
View File
@@ -110,7 +110,7 @@ void printErrorPos(vector<string>::iterator begin, const vector<string>::iterato
{
if (pos > begin)
pos--;
cout << "Error reading \"" << filename << "\" line " << static_cast<unsigned>(pos.base()-begin.base()) << " value \"" << *pos << "\": " << getResultCode(result) << endl;
cout << "Error reading \"" << filename << "\" line " << static_cast<unsigned>(lineNo) << " field " << static_cast<unsigned>(1+pos.base()-begin.base()) << " value \"" << *pos << "\": " << getResultCode(result) << endl;
cout << "Erroneous item is here:" << endl;
bool first = true;
int cnt = 0;
@@ -1218,11 +1218,10 @@ result_t DataFieldTemplates::add(DataField* field, bool replace)
return RESULT_OK;
}
result_t DataFieldTemplates::addFromFile(vector<string>& row, void* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo)
result_t DataFieldTemplates::addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, void* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo)
{
DataField* field = NULL;
vector<string>::iterator it = row.begin();
result_t result = DataField::create(it, row.end(), this, field);
result_t result = DataField::create(begin, end, this, field);
if (result != RESULT_OK)
return result;
@@ -1241,4 +1240,3 @@ DataField* DataFieldTemplates::get(const string name)
return ref->second;
}
+1 -1
View File
@@ -678,7 +678,7 @@ public:
result_t add(DataField* field, bool replace=false);
// @copydoc
virtual result_t addFromFile(vector<string>& row, void* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo);
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, void* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo);
/**
* @brief Gets the template @a DataField instance with the specified name.
+16 -8
View File
@@ -39,6 +39,8 @@ using namespace std;
/** @brief the separator character used to quote text having the @a FIELD_SEPARATOR in it. */
#define TEXT_SEPARATOR '"'
extern void printErrorPos(vector<string>::iterator begin, const vector<string>::iterator end, vector<string>::iterator pos, string filename, size_t lineNo, result_t result);
/**
* @brief An abstract class that support reading definitions from a file.
@@ -63,9 +65,10 @@ public:
* @brief Read the definitions from a file.
* @param filename the name of the file being read.
* @param arg an argument to pass to @a addFromFile().
* @param verbose whether to verbosely log problems.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t readFromFile(const string filename, T arg=NULL)
virtual result_t readFromFile(const string filename, T arg=NULL, bool verbose=false)
{
ifstream ifs;
ifs.open(filename.c_str(), ifstream::in);
@@ -125,21 +128,25 @@ public:
row.push_back(field.str());
result_t result;
vector<string>::iterator it = row.begin();
const vector<string>::iterator end = row.end();
if (m_supportsDefaults == true) {
if (line[0] == '*') {
row[0] = row[0].substr(1);
defaults.push_back(row);
continue;
}
result = addFromFile(row, arg, &defaults, filename, lineNo);
result = addFromFile(it, end, arg, &defaults, filename, lineNo);
}
else
result = addFromFile(row, arg, NULL, filename, lineNo);
result = addFromFile(it, end, arg, NULL, filename, lineNo);
if (result != RESULT_OK) {
cerr << "error reading \"" << filename << "\" line " << static_cast<unsigned>(lineNo) << ": " << getResultCode(result) << endl;
ifs.close();
return result;
if (verbose == false) {
ifs.close();
return result;
}
printErrorPos(row.begin(), end, it, filename, lineNo, result);
}
}
@@ -149,14 +156,15 @@ public:
/**
* @brief Add a definition that was read from a file.
* @param row the definition row read from the file.
* @param begin an iterator to the first column of the definition row to read.
* @param end the end iterator of the definition row to read.
* @param arg the argument passed to @a readFromFile().
* @param defaults all previously read default rows (initial star char removed), or NULL if not supported.
* @param filename the name of the file being read.
* @param lineNo the current line number in the file being read.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t addFromFile(vector<string>& row, T arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo) = 0;
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, T arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo) = 0;
private:
+10 -9
View File
@@ -376,10 +376,11 @@ result_t MessageMap::add(Message* message)
return RESULT_OK;
}
result_t MessageMap::addFromFile(vector<string>& row, DataFieldTemplates* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo)
result_t MessageMap::addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, DataFieldTemplates* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo)
{
Message* message = NULL;
string types = row[0];
vector<string>::iterator restart = begin;
string types = *restart;
if (types.length() == 0)
types.append("r");
result_t result = RESULT_ERR_EOF;
@@ -387,17 +388,17 @@ result_t MessageMap::addFromFile(vector<string>& row, DataFieldTemplates* arg, v
istringstream stream(types);
string type;
while (getline(stream, type, VALUE_SEPARATOR) != 0) {
row[0] = type;
vector<string>::iterator it = row.begin();
result = Message::create(it, row.end(), defaults, arg, message);
if (result != RESULT_OK) {
printErrorPos(row.begin(), row.end(), it, filename, lineNo, result);
continue;
}
*restart = type;
begin = restart;
result = Message::create(begin, end, defaults, arg, message);
if (result != RESULT_OK)
return result;
result = add(message);
if (result != RESULT_OK) {
delete message;
return result;
}
begin = restart;
}
return result;
}
+1 -1
View File
@@ -308,7 +308,7 @@ public:
result_t add(Message* message);
// @copydoc
virtual result_t addFromFile(vector<string>& row, DataFieldTemplates* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo);
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, DataFieldTemplates* arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo);
/**
* @brief Find the @a Message instance for the specified class and name.