// File consumption bool fileConsume( istream & argstream ) { char c = '\0'; size_t pos = 0; ofstream ofile; bool haveFile = filename.length() > 0; writelog2( "Consuming and saving file: ", filename ); if( haveFile ) { ofile.open( filename.c_str(), ios::out | ios::trunc | ios::binary ); } writelog2( "fileBoundaryLen: " , fileBoundaryLen ); // fill the buffer while( argstream.rdstate() == ios::goodbit && pos < fileBoundaryLen && argstream.get(c) ) { testdata[pos++] = c; } writelog2( "final file testdata pos: ", pos-1 ) // scan the buffer while( argstream.rdstate() == ios::goodbit && fileBoundary != testdata && argstream.get(c) ) { if( haveFile ) ofile.put(testdata[0]); memmove( testdata, testdata + 1, fileBoundaryLen ); testdata[fileBoundaryLen-1]=c; } writelog2( "Closing file: ", boundary ); if( haveFile ) ofile.close(); return true; }
void SkipWS( istream& is) { if( is.eof()) throw GDLIOException( "End of file encountered. "+ StreamInfo( &is)); char c; do { c = is.get(); if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::eofbit ) != 0 ) throw GDLIOException( "End of file encountered. "+ StreamInfo( &is)); if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading stream. "+ StreamInfo( &is)); is.clear(); return ; } } while( c == ' ' || c == '\t' || c == '\n'); is.unget(); }
// Data consumption bool dataConsume( istream & argstream ) { bool bRet = false; if( argstream.rdstate() == ios::goodbit ) { value = testline; while( argstream.rdstate() == ios::goodbit && getline( argstream ) && testline != boundary && testline != endboundary ) { value +="\n"; value += testline; } // set the proper state if(testline == boundary) state = newData; else if(testline == endboundary) state = eof; // add the whole item addDataItem(); } else { state = eof; } return bRet; }
// no skip of WS const string ReadComplexElement(istream& is) { SkipWS( is); string buf; char c = is.get(); if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::eofbit ) != 0 ) throw GDLIOException( "End of file encountered. "+ StreamInfo( &is)); if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading stream. "+ StreamInfo( &is)); is.clear(); return buf; } bool brace = (c == '('); if( !brace) { is.unget(); return ReadElement( is); } buf.push_back( c); for(;;) { c = is.get(); if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading line. "+ StreamInfo( &is)); is.clear(); return buf; } if( c == '\n') return buf; buf.push_back( c); if( c == ')') return buf; } }
void seekToFirstTree(istream &inputFile) { if (DEBUG_OUTPUT >= 3){ cout << "seekToFirstTree called." << endl; } string fileInputUpper; getline(inputFile,fileInputUpper); if (inputFile.bad()) { cout << "Can not read file given to seekToFirstTree." << endl; exit (0); } transform(fileInputUpper.begin(), fileInputUpper.end(),fileInputUpper.begin(), ::toupper); while (!( fileInputUpper.find("TREE") != string::npos && fileInputUpper.find("=") != string::npos)) { if (DEBUG_OUTPUT >= 3){ cout << fileInputUpper.substr(0,40) << endl; } getline(inputFile,fileInputUpper); if (inputFile.bad()) { cout << "Can not read file given to seekToFirstTree." << endl; exit (0); } transform(fileInputUpper.begin(), fileInputUpper.end(),fileInputUpper.begin(), ::toupper); } if (DEBUG_OUTPUT >= 3){ cout << fileInputUpper.substr(0,40) << endl; cout << "inputFile.tellg(): " << inputFile.tellg() << endl; cout << "inputFile.bad(): " << inputFile.bad() << endl; cout << "inputFile.good(): " << inputFile.good() << endl; cout << "inputFile.eof(): " << inputFile.eof() << endl; cout << "inputFile.rdstate(): " << inputFile.rdstate() << endl; } // Something is happening to Peter's computer here! int curPosition = inputFile.tellg(); // This might be causing problems to seek negative int from cur position // inputFile.seekg(-(fileInputUpper.size()+1),ios_base::cur); inputFile.seekg( curPosition - ((fileInputUpper.size()+1)), ios_base::beg); if (DEBUG_OUTPUT >= 3){ cout << "inputFile.tellg(): " << inputFile.tellg() << endl; cout << "inputFile.bad(): " << inputFile.bad() << endl; cout << "inputFile.good(): " << inputFile.good() << endl; cout << "inputFile.eof(): " << inputFile.eof() << endl; cout << "inputFile.rdstate(): " << inputFile.rdstate() << endl; cout << "seekToFirstTree done." << endl; } }
// The read specialization takes in each number and stuffs it into the array. int GA1DArrayAlleleGenome<float>::read(istream & is) { unsigned int i=0; float val; do{ is >> val; if(!is.fail()) gene(i++, val); } while(!is.fail() && !is.eof() && i < nx); if(is.eof() && i < nx){ GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF); is.clear(ios::badbit | is.rdstate()); return 1; } return 0; }
// file dump of cgi input bool fileDump( istream & argstream ) { char c = '\0'; size_t pos = 0; ofstream ofile; filename = "cgiDump.log"; writelog2( "Dumping file: ", filename ); ofile.open( filename.c_str(), ios::out | ios::trunc | ios::binary ); while( argstream.rdstate() == ios::goodbit && argstream.get(c) ) { ofile.put(c); } ofile.close(); return true; }
bool getline( istream & argstream ) { char term; if( argstream.rdstate() == ios::goodbit && state != readFile ) // don't want to steal first line from inline file { // try this: std::getline(argstream, testline); // remove the CR of the CRLF pair if( testline.length() ) testline.resize( testline.length() - 1 ); // prepare testline for parsing testline.parseInit(); // to the log if loggin enabled writelog2( "multipart::getline got: ", testline ); } return true; }
// Print state of istream. void Foam::state(istream& from, const string& s) { state_value isState = state_value(from.rdstate()); switch (isState) { case _good: // Do not anything 'unusual'. break; case _eof: Info << "Input stream: premature end of stream", s << endl; Info<< "If all else well, possibly a quote mark missing" << endl; break; case _fail: SeriousErrorIn("state(istream& from, const string& s)") << "Input stream failure (bad format?)", s << endl; Info<< "If all else well, possibly a quote mark missing" << endl; break; case (_fail + _eof) : SeriousErrorIn("state(istream& from, const string& s)") << "Input stream failure and end of stream", s << endl; Info<< "If all else well, possibly a quote mark missing" << endl; break; case _bad: SeriousErrorIn("state(istream& from, const string& s)") << "Serious input stream failure", s << endl; break; default: SeriousErrorIn("state(istream& from, const string& s)") << "Input stream failure of unknown type", s << endl; SeriousErrorIn("state(istream& from, const string& s)") << "Stream state value = ", isState << endl; break; } return; }
// helper function - reads one line, does error checking const string ReadElement(istream& is) { SkipWS( is); string buf; char c; for(;;) { c = is.get(); // int cc = c; // cout << "ReadEl: " << cc << " " << c << ":" << endl; if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading line. "+ StreamInfo( &is)); is.clear(); return buf; } if( c == '\n') return buf; if( c == ' ' || c == '\t') { is.unget(); return buf; } buf.push_back( c); } if( !is.good()) throw GDLIOException( "Error reading stream. "+StreamInfo( &is)); return buf; // // old version (read full line which is then split - does not work with // // different types on the same line) // if( is.eof()) // throw GDLIOException( "End of file encountered. "+ // StreamInfo( &is)); // string retStr; // getline( is, retStr); // if ( (is.rdstate() & ifstream::failbit ) != 0 ) // { // if ( (is.rdstate() & ifstream::eofbit ) != 0 ) // throw GDLIOException( "End of file encountered. "+ // StreamInfo( &is)); // if ( (is.rdstate() & ifstream::badbit ) != 0 ) // throw GDLIOException( "Error reading line. "+ // StreamInfo( &is)); // is.clear(); // return ""; // } // if( !is.good()) // throw GDLIOException( "Error reading line. "+StreamInfo( &is)); // cout << "Read line: " << retStr << endl; // return retStr; }
// data consumption is line oriented until we get to a file attachment bool consume( istream & argstream ) { bool bret = true; ocString test; while( argstream.rdstate() == ios::goodbit && state != eof && getline( argstream ) ) { writelog2("Consumed",testline); switch( state ) { case init: // check to see if we just consumed the boundary if( boundary != testline ) { state = eof; } else { state = newData; } break; case newData: // should be reading the content disposition line test = testline.parse(": "); transform(test.begin(),test.end(),test.begin(),::tolower); writelog2("Testing",test); if( test == "content-disposition" ) { // good - see what the data is test = testline.parse("; "); // Added :: prefix to tolower so SGI recognizes global scope C function transform(test.begin(),test.end(),test.begin(),::tolower); // expect it to be form-data writelog2("Content Testing",test); if( test == "form-data" ) { // parse any remaining parameters while( testline.length() && !testline.endOfParse() ) { ocString test = testline.parse("; "); writelog2("Param Testing",test); if( test.length() ) { string paramname=test.parse("=\""); // Added :: prefix to tolower so SGI recognizes global scope C function transform(paramname.begin(),paramname.end(),paramname.begin(),::tolower); // could be name or filename if( paramname == "name" ) { // set the name name = test.parse("\""); state = dataSep; } else if( paramname == "filename" ) { writelog2("Fix Filename",test); fixupFilename(test.parse("\"")); writelog("Filename Fixed"); state = fileType; } } } } } else { // unexpected place so abort method call state = eof; } break; case fileType: // expect Content-Type: image/jpeg test = testline.parse(": "); // Added :: prefix to tolower so SGI recognizes global scope C function transform(test.begin(),test.end(),test.begin(),::tolower); if( test == "content-type" ) { type = testline.remainder(); state = fileSep; } else { state = eof; } break; case fileSep: if(testline.length() == 0) { state = readFile; } else { state = eof; } break; case dataSep: // expect an empty line if(testline.length() == 0) { state = readData; } else { state = eof; } break; case readData: // expect the data dataConsume(argstream); break; case readFile: fileConsume(argstream); addFile(); addDataItem(); // fileConsume also consumes the boundary (up to the (potential) -- closing) state = fileRead; break; case fileRead: if( testline == "--" ) { state = eof; } state = newData; break; case finished: // see if there is another boundary: if( boundary == testline ) { state = newData; // note that the terminating boundary will have -- at the end } else { state = eof; } break; default: state = eof; break; } // end switch } // end while return bret; }