pass iterator to FileReader::addFromFile() in order to keep track of the erroneous column, added verbose mode to FileReader
This commit is contained in:
@@ -110,7 +110,7 @@ void printErrorPos(vector<string>::iterator begin, const vector<string>::iterato
|
|||||||
{
|
{
|
||||||
if (pos > begin)
|
if (pos > begin)
|
||||||
pos--;
|
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;
|
cout << "Erroneous item is here:" << endl;
|
||||||
bool first = true;
|
bool first = true;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
@@ -1218,11 +1218,10 @@ result_t DataFieldTemplates::add(DataField* field, bool replace)
|
|||||||
return RESULT_OK;
|
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;
|
DataField* field = NULL;
|
||||||
vector<string>::iterator it = row.begin();
|
result_t result = DataField::create(begin, end, this, field);
|
||||||
result_t result = DataField::create(it, row.end(), this, field);
|
|
||||||
if (result != RESULT_OK)
|
if (result != RESULT_OK)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
@@ -1241,4 +1240,3 @@ DataField* DataFieldTemplates::get(const string name)
|
|||||||
|
|
||||||
return ref->second;
|
return ref->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -678,7 +678,7 @@ public:
|
|||||||
result_t add(DataField* field, bool replace=false);
|
result_t add(DataField* field, bool replace=false);
|
||||||
|
|
||||||
// @copydoc
|
// @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.
|
* @brief Gets the template @a DataField instance with the specified name.
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ using namespace std;
|
|||||||
/** @brief the separator character used to quote text having the @a FIELD_SEPARATOR in it. */
|
/** @brief the separator character used to quote text having the @a FIELD_SEPARATOR in it. */
|
||||||
#define TEXT_SEPARATOR '"'
|
#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.
|
* @brief An abstract class that support reading definitions from a file.
|
||||||
@@ -63,9 +65,10 @@ public:
|
|||||||
* @brief Read the definitions from a file.
|
* @brief Read the definitions from a file.
|
||||||
* @param filename the name of the file being read.
|
* @param filename the name of the file being read.
|
||||||
* @param arg an argument to pass to @a addFromFile().
|
* @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.
|
* @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;
|
ifstream ifs;
|
||||||
ifs.open(filename.c_str(), ifstream::in);
|
ifs.open(filename.c_str(), ifstream::in);
|
||||||
@@ -125,21 +128,25 @@ public:
|
|||||||
row.push_back(field.str());
|
row.push_back(field.str());
|
||||||
|
|
||||||
result_t result;
|
result_t result;
|
||||||
|
vector<string>::iterator it = row.begin();
|
||||||
|
const vector<string>::iterator end = row.end();
|
||||||
if (m_supportsDefaults == true) {
|
if (m_supportsDefaults == true) {
|
||||||
if (line[0] == '*') {
|
if (line[0] == '*') {
|
||||||
row[0] = row[0].substr(1);
|
row[0] = row[0].substr(1);
|
||||||
defaults.push_back(row);
|
defaults.push_back(row);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
result = addFromFile(row, arg, &defaults, filename, lineNo);
|
result = addFromFile(it, end, arg, &defaults, filename, lineNo);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
result = addFromFile(row, arg, NULL, filename, lineNo);
|
result = addFromFile(it, end, arg, NULL, filename, lineNo);
|
||||||
|
|
||||||
if (result != RESULT_OK) {
|
if (result != RESULT_OK) {
|
||||||
cerr << "error reading \"" << filename << "\" line " << static_cast<unsigned>(lineNo) << ": " << getResultCode(result) << endl;
|
if (verbose == false) {
|
||||||
ifs.close();
|
ifs.close();
|
||||||
return result;
|
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.
|
* @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 arg the argument passed to @a readFromFile().
|
||||||
* @param defaults all previously read default rows (initial star char removed), or NULL if not supported.
|
* @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 filename the name of the file being read.
|
||||||
* @param lineNo the current line number in 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.
|
* @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:
|
private:
|
||||||
|
|
||||||
|
|||||||
@@ -376,10 +376,11 @@ result_t MessageMap::add(Message* message)
|
|||||||
return RESULT_OK;
|
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;
|
Message* message = NULL;
|
||||||
string types = row[0];
|
vector<string>::iterator restart = begin;
|
||||||
|
string types = *restart;
|
||||||
if (types.length() == 0)
|
if (types.length() == 0)
|
||||||
types.append("r");
|
types.append("r");
|
||||||
result_t result = RESULT_ERR_EOF;
|
result_t result = RESULT_ERR_EOF;
|
||||||
@@ -387,17 +388,17 @@ result_t MessageMap::addFromFile(vector<string>& row, DataFieldTemplates* arg, v
|
|||||||
istringstream stream(types);
|
istringstream stream(types);
|
||||||
string type;
|
string type;
|
||||||
while (getline(stream, type, VALUE_SEPARATOR) != 0) {
|
while (getline(stream, type, VALUE_SEPARATOR) != 0) {
|
||||||
row[0] = type;
|
*restart = type;
|
||||||
vector<string>::iterator it = row.begin();
|
begin = restart;
|
||||||
result = Message::create(it, row.end(), defaults, arg, message);
|
result = Message::create(begin, end, defaults, arg, message);
|
||||||
if (result != RESULT_OK) {
|
if (result != RESULT_OK)
|
||||||
printErrorPos(row.begin(), row.end(), it, filename, lineNo, result);
|
return result;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
result = add(message);
|
result = add(message);
|
||||||
if (result != RESULT_OK) {
|
if (result != RESULT_OK) {
|
||||||
delete message;
|
delete message;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
begin = restart;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -308,7 +308,7 @@ public:
|
|||||||
result_t add(Message* message);
|
result_t add(Message* message);
|
||||||
|
|
||||||
// @copydoc
|
// @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.
|
* @brief Find the @a Message instance for the specified class and name.
|
||||||
|
|||||||
Reference in New Issue
Block a user