int MiscReadCharsAndDoubleFromFile( std::ifstream& ifs, std::string& termString, double* termValue, bool& endOfLineAchieved) { int iRC = UQ_OK_RC; endOfLineAchieved = false; char c = ' '; while (c == ' ') { ifs.get(c); if ((ifs.rdstate() & std::ifstream::failbit)) { iRC = UQ_FAILED_READING_FILE_RC; break; } }; char term[512]; unsigned int pos = 0; if (!iRC) { while ((pos < 512) && (c != '\n') && (c != '\0') && (c != ' ')) { term[pos++] = c; if ((ifs.rdstate() & std::ifstream::failbit)) { iRC = UQ_FAILED_READING_FILE_RC; break; } ifs.get(c); }; } if (!iRC) { if (c == '\n') endOfLineAchieved = true; term[pos] = '\0'; termString = term; //std::cout << "Read chars = " << termString << std::endl; if (termValue) { if (termString == std::string("inf")) { *termValue = INFINITY; } else if (termString == std::string("-inf")) { *termValue = -INFINITY; } else if (termString == std::string("nan")) { *termValue = nan(""); } else { *termValue = strtod(termString.c_str(),NULL); } } } return iRC; }
// read a line in headerLine and discards the any remaining characters static void readLine(char *headerLine, std::ifstream& infile){ infile.getline( headerLine, kMaxNameLength ); // still some characters left on the current line ? if ( !infile.eof() && !infile.bad() && infile.rdstate() & std::ios::failbit){ // clear failbit infile.clear(infile.rdstate() & ~std::ios::failbit); // discard characters. char c; while( infile.get(c) && c != '\n'); } }
/**\brief Used by testers. * * Not operators because that would conflict * with the std:: operators for unsigned ints, alas. */ void in(std::ifstream& i, unsigned48_t& res) { /* * print "0x........,0x........,0x........" */ union { unsigned48_t seed; unsigned short dummy[sizeof(unsigned48_t)/sizeof(unsigned short)]; } PUN ; char comma = ','; unsigned j=0; while( (comma == ',') && (j < sizeof(PUN.dummy)/sizeof(unsigned short)) && (i >> PUN.dummy[j]) ) { if(i.peek() == ',') i >> comma; j++; } if(j < sizeof(PUN.dummy)/sizeof(unsigned short) ) { // This actually sets the badbit: i.clear(std::ios::badbit|i.rdstate()); } res = PUN.seed; }
int MiscReadStringAndDoubleFromFile( std::ifstream& ifs, std::string& termString, double* termValue) { int iRC = UQ_OK_RC; ifs >> termString; if ((ifs.rdstate() & std::ifstream::failbit)) { iRC = UQ_FAILED_READING_FILE_RC; } else if (termValue) { if (termString == std::string("inf")) { *termValue = INFINITY; } else if (termString == std::string("-inf")) { *termValue = -INFINITY; } else if (termString == std::string("nan")) { *termValue = nan(""); } else { *termValue = strtod(termString.c_str(),NULL); } } //if (!iRC) std::cout << "Read termString = " << termString << std::endl; return iRC; }
std::vector<Point> loadPoints(std::ifstream &in) { std::vector<Point> result; for(;;) { double x, y; in >> x >> y; if (in.rdstate()) { // break if any error break; } result.push_back(Point(x, y)); } return result; }
Bool_t RegionFileLoader::FileIsNotOkay(std::ifstream& f) { Bool_t notokay = false; std::ios::iostate state = f.rdstate(); if (f.eof()) { notokay = true; } else if (f.fail()) { std::cerr << std::hex << std::showbase << "fstream is corrupted (badbit=" << (state&std::ios_base::badbit) << ",failbit=" << (state&std::ios_base::failbit) << ")" << std::dec << std::noshowbase << std::endl; notokay = true; } return notokay; }
bool CParser::ReadNextLine (std::ifstream& FileInput, int& nLineNum, std::string& szInputString, const int MAXCHARS, const std::string& szComment, bool bLowerCase) // --------------------------------------------------------------------------- // Function: reads the next line skipping over the comment lines // and converts all alphabets to lower case if requested // Input: file istream, line #, string to hold the input line, // max. # of characters expected in each input line, // comment character(s) at the beginning of a comment line, // lowercase conversion option // Output: updated values of line # and the string // return value is true if successful // false if an error state is encountered // Restriction: Cannot read a line over 256 characters // --------------------------------------------------------------------------- { int flag = 0; int flag1 =0; bool bWhSpc = false; int tokenfound = 1; const int MAXCH = 1000; char szInp[MAXCH]; char szTemp [MAXCH]; std::vector<std::string> tokens; // enough capacity to read and store? if (MAXCHARS > MAXCH) return false; // comment character(s) int nCLen = static_cast<int>(szComment.length()); // read the line (skip over comment lines) for(;;) { ++nLineNum; FileInput.getline (szInp, MAXCHARS); // // end-of-file? // if (FileInput.eof()) // return false; if (FileInput.fail()) FileInput.clear (FileInput.rdstate() & ~std::ios::failbit); // unrecoverable error? if (FileInput.bad()) return false; // successful read szInputString = szInp; GetTokens(szInputString, " ", tokens); bWhSpc = EatWhiteSpace(szInputString); if ((szInputString.substr(0,nCLen) != szComment)&& (bWhSpc ==false)){ szInputString = szInp; GetTokens(szInputString, " ", tokens); for(int i=0; i< abs(tokens.size()); i++){ std::string temptoken = tokens[i]; if (temptoken == "&") flag1 = 1; } //Filter the comment tokens // FilterComment(szInputString, szComment); //if "&" is found continue to read the next line std::string szTempString = szInputString; // check if line is continued & while(flag1 ==1 && tokenfound == 1){ GetTokens(szTempString, " ", tokens); for(int i=1; i<=abs(tokens.size()); i++){ std::string temptoken = tokens[i-1]; if (temptoken == "&"){ tokenfound = 1; flag = 1; } else{ if(flag==1) flag = 1;//do nothing token already found else tokenfound = 0; } } if(tokenfound ==1){ ++nLineNum; RemoveToken(szInputString); //- getting more tokens and add to the existing FileInput.getline (szTemp, MAXCHARS); // end-of-file? if (FileInput.eof()) return false; if (FileInput.fail()) FileInput.clear (FileInput.rdstate() & ~std::ios::failbit); // unrecoverable error? if (FileInput.bad()) return false; // successful read szTempString = szTemp; FilterComment(szTempString, szComment); szInputString+=" "; szInputString+=szTemp; } else{ break;//while loop ents } flag = 0; } // while loop ends // convert to lower case? if (bLowerCase){ for (int i=0; i < static_cast<int>(szInputString.length()); i++) szInputString[i] = tolower(szInputString[i]); } break; } } return true; }