std::vector<TautomerTransform> readTautomers(std::istream& inStream, int nToRead) { std::vector<TautomerTransform> tautomers; tautomers.clear(); if (inStream.bad()) { throw BadFileException("Bad stream contents."); } const int MAX_LINE_LEN = 512; char inLine[MAX_LINE_LEN]; std::string tmpstr; int nRead = 0; while (!inStream.eof() && (nToRead < 0 || nRead < nToRead)) { inStream.getline(inLine, MAX_LINE_LEN, '\n'); tmpstr = inLine; // parse the molpair on this line (if there is one) TautomerTransform* transform = getTautomer(tmpstr); if (transform != nullptr) { // std::cout << MolToSmiles(*(transform->Mol) ) << //std::endl; tautomers.push_back(*transform); nRead++; } } return tautomers; }
TDTMolSupplier::TDTMolSupplier(const std::string &fileName, const std::string &nameRecord, int confId2D, int confId3D, bool sanitize){ init(); d_confId2D=confId2D; d_confId3D=confId3D; d_nameProp=nameRecord; // FIX: this binary moe of opening file is here because of a bug in VC++ 6.0 // the function "tellg" does not work correctly if we do not open it this way // Need to check if this has been fixed in VC++ 7.0 std::istream *tmpStream=0; tmpStream = static_cast<std::istream *>(new std::ifstream(fileName.c_str(), std::ios_base::binary)); if (!tmpStream || (!(*tmpStream)) || (tmpStream->bad()) ) { std::ostringstream errout; errout << "Bad input file " << fileName; throw BadFileException(errout.str()); } dp_inStream = tmpStream; df_owner = true; this->advanceToNextRecord(); d_molpos.push_back(dp_inStream->tellg()); df_sanitize = sanitize; this->checkForEnd(); }
SmilesMolSupplier::SmilesMolSupplier(const std::string &fileName, const std::string &delimiter, int smilesColumn, int nameColumn, bool titleLine, bool sanitize) { init(); // FIX: this binary mode of opening file is here because of a bug in VC++ 6.0 // the function "tellg" does not work correctly if we do not open it this way // Need to check if this has been fixed in VC++ 7.0 std::ifstream *tmpStream = new std::ifstream(fileName.c_str(), std::ios_base::binary); if (!tmpStream || (!(*tmpStream)) || (tmpStream->bad())) { std::ostringstream errout; errout << "Bad input file " << fileName; throw BadFileException(errout.str()); } dp_inStream = static_cast<std::istream *>(tmpStream); CHECK_INVARIANT(dp_inStream, "bad instream"); CHECK_INVARIANT(!(dp_inStream->eof()), "early EOF"); d_delim = delimiter; df_sanitize = sanitize; df_title = titleLine; d_smi = smilesColumn; d_name = nameColumn; df_end = false; // if(d_title) processTitleLine(); this->checkForEnd(); POSTCONDITION(dp_inStream, "bad instream"); }
SDMolSupplier::SDMolSupplier(const std::string &fileName, bool sanitize, bool removeHs, bool strictParsing) { init(); // FIX: this binary mode of opening file is here because of a bug in VC++ 6.0 // the function "tellg" does not work correctly if we do not open it this way // Jan 2009: Confirmed that this is still the case in visual studio 2008 std::istream *tmpStream = 0; tmpStream = static_cast<std::istream *>( new std::ifstream(fileName.c_str(), std::ios_base::binary)); if (!tmpStream || (!(*tmpStream)) || (tmpStream->bad())) { std::ostringstream errout; errout << "Bad input file " << fileName; throw BadFileException(errout.str()); } // dp_inStream = static_cast<std::istream *>(tmpStream); dp_inStream = tmpStream; df_owner = true; d_molpos.push_back(dp_inStream->tellg()); df_sanitize = sanitize; df_removeHs = removeHs; df_strictParsing = strictParsing; this->checkForEnd(); if (df_end) { // checkForEnd() sets d_len if we're at EOF. undo that (was GitHub issue // 19): d_len = 0; } POSTCONDITION(dp_inStream, "bad instream"); }
std::vector<TautomerTransform> readTautomers(std::string fileName) { std::ifstream inStream(fileName.c_str()); if ((!inStream) || (inStream.bad())) { std::ostringstream errout; errout << "Bad input file " << fileName; throw BadFileException(errout.str()); } std::vector<TautomerTransform> tautomers = readTautomers(inStream); return tautomers; }
MOL_SPTR_VECT readFuncGroups(std::string fileName) { std::ifstream inStream(fileName.c_str()); if ((!inStream) || (inStream.bad()) ) { std::ostringstream errout; errout << "Bad input file " << fileName; throw BadFileException(errout.str()); } MOL_SPTR_VECT funcGroups; funcGroups=readFuncGroups(inStream); return funcGroups; }
void MolToTPLFile(const ROMol &mol,std::string fName, std::string partialChargeProp, bool writeFirstConfTwice){ std::ofstream *outStream = new std::ofstream(fName.c_str()); if(!outStream||!(*outStream)||outStream->bad()){ std::ostringstream errout; errout << "Bad output file " << fName; throw BadFileException(errout.str()); } std::string outString = MolToTPLText(mol,partialChargeProp, writeFirstConfTwice); *outStream << outString; delete outStream; }
SDWriter::SDWriter(std::string fileName) { if(fileName!= "-"){ std::ofstream *tmpStream = new std::ofstream(fileName.c_str()); df_owner=true; if ( !tmpStream || !(*tmpStream) || (tmpStream->bad()) ) { std::ostringstream errout; errout << "Bad output file " << fileName; throw BadFileException(errout.str()); } dp_ostream = static_cast<std::ostream *>(tmpStream); } else { dp_ostream = static_cast<std::ostream *>(&std::cout); df_owner=false; } d_molid = 0; }
sudokuBoardReader::sudokuBoardReader(std::string fileName){ try{ std::ifstream sudokuFile; std::string line; sudokuFile.open(fileName); if (!sudokuFile) throw (BadFileException()); std::getline(sudokuFile, line); parseHeader(line); int i = 0; while (i < this-> n && std::getline(sudokuFile, line)) { fillRow(i, line); i++; } sudokuFile.close(); } catch(std::ifstream::failure e) { std::cout << "Failed to read board file\n"; } }
TDTWriter::TDTWriter(const std::string &fileName) { if (fileName != "-") { auto *tmpStream = new std::ofstream(fileName.c_str()); if (!tmpStream || !(*tmpStream) || (tmpStream->bad())) { std::ostringstream errout; errout << "Bad output file " << fileName; throw BadFileException(errout.str()); } dp_ostream = static_cast<std::ostream *>(tmpStream); df_owner = true; } else { dp_ostream = static_cast<std::ostream *>(&std::cout); df_owner = false; } d_molid = 0; d_numDigits = 4; df_write2D = false; df_writeNames = true; }
MOL_SPTR_VECT readFuncGroups(std::istream &inStream,int nToRead) { MOL_SPTR_VECT funcGroups; funcGroups.clear(); if (inStream.bad()) { throw BadFileException("Bad stream contents."); } const int MAX_LINE_LEN = 512; char inLine[MAX_LINE_LEN]; std::string tmpstr; int nRead=0; while (!inStream.eof() && (nToRead<0 || nRead<nToRead)) { inStream.getline(inLine, MAX_LINE_LEN,'\n'); tmpstr = inLine; // parse the molecule on this line (if there is one) ROMol *mol = getSmarts(tmpstr); if (mol) { funcGroups.push_back(ROMOL_SPTR(mol)); nRead++; } } return funcGroups; }
void Netlist::handle_line(const std::string& line) { // The first character defines if the line describes an element, a comment or // the simulation parameters try { switch (line[0]) { case '.': { // simulation parameters statements.push_back(Statement::get_statement(line)); break; } case '*': { // comment, do nothing break; } default: { // regular elements elements.push_back(Element::get_element(line)); } } } catch (const BadElementString& e) { throw BadFileException(to_str("Line: " << current_line << " \"" << line << "\"")); } current_line++; }
// ============================================== // // Load a mesh material from a file t_error ResourceManager::LoadMeshMaterial (const char* filename, Mesh* mesh) { // load the mesh material file ifstream fileHandle( filename ); if ( !fileHandle.is_open() ) { Log( str( format( "Error opening material file (%1%)" ) % filename ), LOG_ERROR ); throw BadFileException(filename); } // parse mtl file string buffer; while ( fileHandle.good() ) { getline( fileHandle, buffer ); if ( buffer.empty() ) continue; // setup tokenizer CharSeparator tokenSeparator( " \t" ); CharTokenizer tokens( buffer, tokenSeparator ); CharTokenizer::iterator token = tokens.begin(); CharTokenizer::iterator lastToken = tokens.end(); // Log ( str( format( "buffer: %1%" ) % (buffer) ) ); // Log ( str( format( "token: %1%" ) % (*token) ) ); if ( (*token)[0] == '#' ) { continue; } // comment else if ( (*token) == "Ka" ) { // ambient colour float r = tokenToFloat(++token); float g = tokenToFloat(++token); float b = tokenToFloat(++token); Log( str( format( "Ka: %1%,%2%,%3%" ) % r % g % b ), LOG_DEBUG ); mesh->color_ambient = fTriple( r, g, b ); } else if ( (*token) == "Kd" ) { // diffuse colour float r = tokenToFloat(++token); float g = tokenToFloat(++token); float b = tokenToFloat(++token); Log( str( format( "Kd: %1%,%2%,%3%" ) % r % g % b ), LOG_DEBUG ); mesh->color_diffuse = fTriple( r, g, b ); } else if ( (*token) == "Ks" ) { // specular colour float r = tokenToFloat(++token); float g = tokenToFloat(++token); float b = tokenToFloat(++token); Log( str( format( "Ks: %1%,%2%,%3%" ) % r % g % b ), LOG_DEBUG ); mesh->color_specular = fTriple( r, g, b ); } else if ( (*token) == "map_Kd" ) { // texture // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // TODO: load texture in resource mgr; use a lookup table to find the gl id and point to that string texFilename = *(++token); mesh->loadTexture( texFilename.c_str(), MeshRenderData::MESH_RENDER_TEXTURE ); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! } else if ( (*token) == "bump" ) { // bumpmap string texFilename = *(++token); mesh->loadTexture( texFilename.c_str(), MeshRenderData::MESH_RENDER_TEXBUMP ); } else { Log ( str( format( "Unknown line: %1%" ) % buffer ) ); } } fileHandle.close(); return NO_ERROR; }
// ============================================== // // Load a mesh from a file Entity* ResourceManager::LoadMesh (RenderGroup* renderer, const char* filename) { // load the mesh file ifstream fileHandle( filename ); if ( !fileHandle.is_open() ) { Log( str( format( "Error opening mesh file (%1%)" ) % filename ), LOG_ERROR ); throw BadFileException(filename); } // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // TODO: store mesh elsewhere Mesh* mesh; if (renderer) { mesh = new Mesh(true); mesh->renderData->gl = renderer->program->programid; glGenTextures(2, mesh->renderData->textures); } else { mesh = new Mesh(false); } // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // parse obj file string buffer; while ( fileHandle.good() ) { getline( fileHandle, buffer ); if ( buffer.empty() ) continue; // setup tokenizer CharSeparator tokenSeparator( " \t" ); CharTokenizer tokens( buffer, tokenSeparator ); CharTokenizer::iterator token = tokens.begin(); CharTokenizer::iterator lastToken = tokens.end(); // Log ( str( format( "buffer: %1%" ) % (buffer) ) ); // Log ( str( format( "token: %1%" ) % (*token) ) ); if ( (*token)[0] == '#' ) { continue; } // comment else if ( (*token) == "v" ) { // vertex float x = tokenToFloat(++token); float y = tokenToFloat(++token); float z = tokenToFloat(++token); mesh->addVertex( x, y, z ); } else if ( (*token) == "vn" ) { // vertex normal float x = tokenToFloat(++token); float y = tokenToFloat(++token); float z = tokenToFloat(++token); mesh->addVertexNormal( x, y, z ); } else if ( *token == "vt" ) { // vertex texcoord float s = tokenToFloat(++token); float t = tokenToFloat(++token); mesh->addVertexTex( s, t ); } else if ( *token == "f" ) { // face /* each face contains 3 or more vertices * * Vertex only format is: v1 v2 v3 ... * Vertex/Texture format: v1/vt1 v2/vt2 v3/vt3 ... * Vertex/Tex/Norm format: v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... * Vertex/Normal format: v1//vn1 v2//vn2 v3//vn3 ... * **/ (++token); ushort point_index = 0; // this helps with auto-face texcoord deciding (in case no UV mapping is specified) short v_index, n_index, t_index; do { // setup sub-tokenizer (vert, norm, texcoord) v_index = -1, n_index = -1, t_index = -1; // NOTE: -1 means nonexistent; do NOT use ushort since 0 is a legal index CharTokenizer v_indices( (*token), CharSeparator("/ ","",boost::keep_empty_tokens) ); CharTokenizer::iterator index = v_indices.begin(); for (uchar tokenIndex=0; (index) != v_indices.end() && tokenIndex <= 2 ; ++tokenIndex, ++index ) { if ( (*index).empty() ) continue; switch (tokenIndex) { case 0: // Vertex Index // Log( str( format( "Face Index: %1%" ) % tokenToUShort( index ) ) ); v_index = tokenToUShort( index ) - 1; break; case 1: // Texcoord Index // Log( str( format( "Tex Index: %1%" ) % tokenToUShort( index ) ) ); t_index = tokenToUShort( index ) -1; break; case 2: // Norm Index // Log( str( format( "Norm Index: %1%" ) % tokenToUShort( index ) ) ); n_index = tokenToUShort( index ) -1; break; } } // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // if no texcoord specified then create one // TODO: this assumes we're running quads, provide a NON-HARDCODED solution if ( t_index == -1 ) { if ( mesh->texcoords.empty() ) { float top = 0.0f, bottom = 1.0f; // TODO: test st coords for correct top/bottom mesh->texcoords.push_back( 1.0f ); // bottom right mesh->texcoords.push_back( bottom ); mesh->texcoords.push_back( 1.0f ); // top right mesh->texcoords.push_back( top ); mesh->texcoords.push_back( 0.0f ); // top left mesh->texcoords.push_back( top ); mesh->texcoords.push_back( 0.0f ); // bottom left mesh->texcoords.push_back( bottom ); } t_index = point_index % 4; } // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! mesh->pushVertex( v_index, t_index, n_index ); point_index++; } while( (++token) != tokens.end() ); if ( point_index == 3 ) { // tri-face; since we're expecting quads just overlap the 2 end points mesh->pushVertex( v_index, t_index, n_index ); } } else if ( *token == "mtllib" ) { // material file // TODO: test that this will work on Windows (\\ path end could return only \ which may break the mtl filename) ushort path_ending = string( filename ).find_last_of( "/\\" ) + 1; // works for /home/user/.. or c:\\programfiles\\.. string mtlFilename = string( filename ).substr( 0, path_ending ).append( *(++token) ); // load material file if ( LoadMeshMaterial( mtlFilename.c_str(), mesh ) & ERROR ) { Log( str( format( "Error opening material file (%1%)" ) % mtlFilename ), LOG_ERROR ); throw BadFileException(mtlFilename); } Log( str( format( "Loaded material file (%1%)" ) % mtlFilename ), LOG_INFO ); } else { Log ( str( format( "Unknown line: %1%" ) % buffer ) ); } } fileHandle.close(); // finish the entity construction mesh->setupRenderData(); Entity* entity = new Entity(); entity->mesh = mesh; entity->updateAABB(); world->entities.push_back(entity); if (renderer) renderer->entities.push_back(entity); return entity; }
void FPBReader::init() { PRECONDITION(dp_istrm, "no stream"); dp_impl = new detail::FPBReader_impl; dp_impl->istrm = dp_istrm; dp_impl->df_lazy = df_lazyRead; char magic[detail::magicSize]; dp_istrm->read(magic, detail::magicSize); if (detail::FPB_MAGIC != std::string(magic, detail::magicSize)) { throw BadFileException("Invalid FPB magic"); } while (1) { if (dp_istrm->eof()) throw BadFileException("EOF hit before FEND record"); std::string chunkNm; boost::uint64_t chunkSz; boost::uint8_t *chunk = NULL; detail::readChunkDetails(*dp_istrm, chunkNm, chunkSz); // std::cerr << " Chunk: " << chunkNm << " " << chunkSz << std::endl; if (!df_lazyRead || (chunkNm != "AREN" && chunkNm != "FPID")) { detail::readChunkData(*dp_istrm, chunkSz, chunk); if (chunkNm == "FEND") { break; } else if (chunkNm == "POPC") { detail::extractPopCounts(dp_impl, chunkSz, chunk); } else if (chunkNm == "AREN") { dp_impl->dp_arenaChunk.reset(chunk); detail::extractArena(dp_impl, chunkSz, chunk); chunk = NULL; } else if (chunkNm == "FPID") { dp_impl->dp_idChunk.reset(chunk); detail::extractIds(dp_impl, chunkSz, chunk); chunk = NULL; } else if (chunkNm == "META") { // currently ignored } else if (chunkNm == "HASH") { // currently ignored } else { BOOST_LOG(rdWarningLog) << "Unknown chunk: " << chunkNm << " ignored." << std::endl; } delete[] chunk; } else { // we are reading the AREN or FPID chunk in lazy mode, just get our // position in // the file. if (chunkNm == "AREN") { detail::extractArenaDetails(dp_impl, chunkSz); } else if (chunkNm == "FPID") { detail::extractIdsDetails(dp_impl, chunkSz); } } } if ((!df_lazyRead && !dp_impl->dp_arenaChunk) || (df_lazyRead && !dp_impl->fpDataOffset)) throw BadFileException("No AREN record found"); if ((!df_lazyRead && !dp_impl->dp_idChunk) || (df_lazyRead && !dp_impl->idDataOffset)) throw BadFileException("No FPID record found"); df_init = true; };