////////////////////////////////////////////////////////////////////////////// // Constructor / Destructor ////////////////////////////////////////////////////////////////////////////// SIMPLE_PARSER::SIMPLE_PARSER(std::string file) { if(file.length()==0) { std::cout << "Skipping config file\n"; return; } std::cout << "Using config file "<< file <<"\n"; int lineCnt=1; //string line; //line.resize(512); char buffer[512]; std::ifstream myfile (file.c_str()); if (!myfile.good()) { cout << " Failed to open file " << file.c_str() << "!!!" << endl; exit(1); } if (myfile.is_open()) { while (! myfile.eof() ) { //std::getline (myfile,line); myfile.getline (buffer, 512); //line = string(buffer); string line(buffer); if(line.length()<1) continue; if(line[0] == '#') continue; size_t pos = line.find_first_of("="); if(pos != string::npos && pos<line.length() ) { string lhs = removeWhitespace( line.substr(0,pos) ); string rhs = removeWhitespace( line.substr(pos+1,line.length()-pos) ); forceLower(lhs); //forceLower(rhs); // store... mVals[lhs] = rhs; } else { // simple check for errors... string check = removeWhitespace(line); if(check.length()>0) { std::cerr<<"Unable to parse, error in line "<<lineCnt<<": '"<< line <<"' !\n"; exit(1); } } lineCnt++; } myfile.close(); } else { std::cerr<<"Unable to parse!\n"; exit(1); } }
const Instruction & Lexer::tokenize(const std::string & instruction, Machine & machine) { static Instruction tokens; tokens.clear(); if (instruction.size() == 0) return tokens; std::string cleanString(removeWhitespace(instruction)); { size_t colonPos = cleanString.find_first_of(':'); if (colonPos != std::string::npos) { char label[Label::length + 1]; strncpy(label, cleanString.substr(0, colonPos).c_str(), Label::length); if (!isalpha(label[0])) throw(std::runtime_error("Lexer::tokenize: Labels must start with a letter")); tokens.label.setLabelData(label); if (colonPos == cleanString.size() - 1) return tokens; cleanString = removeWhitespace(cleanString.substr(colonPos + 1, cleanString.size() - colonPos)); if (cleanString.find_first_of(':') != std::string::npos) throw(std::runtime_error("Lexer::tokenize: Only one label per line allowed")); } } if (cleanString.size() == 0) return tokens; size_t spacePos = cleanString.find_first_of(' '); if (spacePos == std::string::npos) { tokens.opcode = getOpcodeToken(cleanString); return tokens; } tokens.opcode = getOpcodeToken(cleanString.substr(0, spacePos)); cleanString = removeWhitespace(cleanString.substr(spacePos, cleanString.size() - spacePos)); if (cleanString.size() == 0) return tokens; spacePos = cleanString.find_first_of(' '); if (spacePos == std::string::npos) { tokens.operand1 = getOperandToken(cleanString, machine); return tokens; } tokens.operand1 = getOperandToken(cleanString.substr(0, spacePos), machine); cleanString = removeWhitespace(cleanString.substr(spacePos, cleanString.size() - spacePos)); if (cleanString.size() == 0) return tokens; spacePos = cleanString.find_first_of(' '); if (spacePos == std::string::npos) { tokens.operand2 = getOperandToken(cleanString, machine); return tokens; } tokens.operand2 = getOperandToken(cleanString.substr(0, spacePos), machine); return tokens; }
bool str2bool(const std::string& input) { string str = removeWhitespace(input); if (str.compare("true") == 0) { return true; } else if (str.compare("false") == 0) { return false; } else { throw invalid_argument("Error! Input string is not true or false."); } }
/** * @brief xppParser::xppParser Default constructor of the parser object * * @param fn string representing the file name of the ode file * * This constructs the parser object of a given ode file. First unneeded * content is discarded, and a basic correctness check is don. Later arrays are * expanded and special constructs like markov processes and tables are handled. * Finally, the different keywords are parsed and put into the opts arrays. */ xppParser::xppParser(const std::string &fn) : fileName(fn) { try { /* Initially read in the ode file */ readFile(); /* Initialize the keyword tries for command parsing */ initializeTries(); /* Check for incorrect brackets */ checkBrackets(); /* Remove all comments */ removeComments(); /* Remove unnecessary whitespaces */ removeWhitespace(); /* Expand array descriptions */ expandArrays(); /* Extract exports to dlls */ extractExport(); /* Extract markov processes */ extractMarkov(); /* Extract table definitions */ extractTable(); /* Extract wiener processes */ extractWiener(); /* Extract globals */ extractGlobal(); /* Extract all other definitions */ extractDefinition(); /* Catch errors */ } catch (xppParserException& e) { std::cerr << e.what(); } catch (std::runtime_error& e) { std::cerr << e.what(); } catch (std::exception& e) { std::cerr << e.what(); } catch (...) { std::cerr << "Unexpected error\n"; throw; } }
Policy PolicyFactory::parse(std::string policy) { convertToLowerCase(policy); removeWhitespace(policy); Policy p; if (policy == "accept") { p = ACCEPT; } else if (policy == "reject") { p = REJECT; } else if (policy == "drop") { p = DROP; } else { std::cout << "Unsupported policy " << policy << " specified." << std::endl; exit(1); } return p; }
void VolumetricMeshParser::beautifyLine(char * s, int numRetainedSpaces, int removeWhitespace_) { if (removeWhitespace_) removeWhitespace(s, numRetainedSpaces); //upperCase(s); // remove trailing '\n' char * pos = s; while( *pos != '\0' ) pos++; if ( pos != s ) // string is not zero length { if( *(pos-1) == '\n' ) *(pos-1) = '\0'; } }
bool ConfigReader::loadConfig(std::string filename) { Logger::getInstance()->logMessage(0, "ConfigReader loading config file: '%s'", filename.c_str()); std::ifstream file(filename.c_str()); if (!file.is_open()) { Logger::getInstance()->logError(0, "ConfigReader failed to load config file: '%s'", filename.c_str()); return false; } this->filename = filename; bool inSection = false; std::string curSection; while (!file.eof()) { std::string line; std::getline(file, line, '\n'); if (line.empty()) { continue; } if (line[0] == COMMENT_CHAR) { continue; } if (line[0] == SECTION_BEGIN_CHAR) { inSection = true; line = removeWhitespace(line); line.erase(line.begin()); line.erase(line.end()-1); curSection = line; innerMap newMap; config[curSection] = newMap; continue; } std::vector<std::string> current = split(line, '='); if (ConfigReader::hasKey(curSection, current.front())) { Logger::getInstance()->logError(0, "ConfigReader found existing key '" + current.front() + "' that maps to '" + current.back() + "'"); continue; } if (inSection) { std::pair<std::string, std::string> keyPair = std::make_pair(current.front(), current.back()); config[curSection].insert(keyPair); } } Logger::getInstance()->logMessage(0, "ConfigReader done parsing config file"); return true; }