#include "ListParser.h" #include #include using namespace std; // Aid function that chomps the trailing '\n' or "\n\r" if there are any from a string and // returns the string without them. string chomp(const string &strToChomp) { const char CARRIAGE_RETURN = '\r'; const char NEW_LINE = '\n'; int lastIndex = strToChomp.length()-1; if ((lastIndex >= 0) && (strToChomp[lastIndex] == CARRIAGE_RETURN)) { --lastIndex; } if ((lastIndex >= 0) && (strToChomp[lastIndex] == NEW_LINE)) { --lastIndex; } return strToChomp.substr(0, lastIndex + 1); } /* * Parses a text file and puts its lines inside a vector. * filename - the name of the file to parse * vec - the vector to put the lines in. */ int List_Parser::parseItemList(const ACE_TCHAR * fileName, std::vector &vec) { int status = PARSER_STATUS_FAILURE; if (fileName == NULL) { return status; } const char * WHITESPACE_CHARS = " \n\t\r"; ifstream fileStream; string str; bool errorOccured = false; fileStream.open(fileName, ifstream::in); if(!fileStream.fail()) { while ((!errorOccured) && (!fileStream.eof())) { getline(fileStream, str); // if there are empty lines in the file, than skip them. // Do this before reading the fail bits since it IS considered a failure by getline. // but first chomp the line from '\n' or "\n\r" (in Linux) str = chomp(str); if (str.empty()) { continue; } if(!fileStream.fail()) { // remove whitespaces at the beginning int firstNotWhite = str.find_first_not_of(WHITESPACE_CHARS); str.erase(0, firstNotWhite); if (str.at(0) != COMMENT_START_CHARACTER) { vec.push_back(ACE_TString(str.c_str())); } } else { errorOccured = true; } } if (!errorOccured) { status = PARSER_STATUS_SUCCESS; } } fileStream.close(); return status; }