///////////////////////////////////////////////////////////// //returns true if the file "svgformat.script" was inserted into the output bool SVGFormat::EmbedScript(ostream& ofs) { ifstream ifs; if(!ifs || OpenDatafile(ifs, "svgformat.script").empty()) return false; ofs << ifs.rdbuf(); //copy whole file return true; }
bool OBGroupContrib::ParseFile(const char *filename) { OBSmartsPattern *sp; // open data file ifstream ifs; if (OpenDatafile(ifs, filename).length() == 0) { obErrorLog.ThrowError(__FUNCTION__, " Could not find contribution data file.", obError); return false; } vector<string> vs; bool heavy = false; char buffer[80]; while (ifs.getline(buffer, 80)) { if (EQn(buffer, "#", 1)) continue; if (EQn(buffer, ";heavy", 6)) heavy = true; else if (EQn(buffer, ";", 1)) continue; tokenize(vs, buffer); if (vs.size() < 2) continue; sp = new OBSmartsPattern; if (sp->Init(vs[0])) { if (heavy) _contribsHeavy.push_back(pair<OBSmartsPattern*, double> (sp, atof(vs[1].c_str()))); else _contribsHydrogen.push_back(pair<OBSmartsPattern*, double> (sp, atof(vs[1].c_str()))); } else { delete sp; sp = NULL; obErrorLog.ThrowError(__FUNCTION__, " Could not parse SMARTS from contribution data file", obInfo); return false; } } return true; }
//! Loads a file containing nth ionization energies and metal ionic charges // TODO Move all relevant data into OBElementTable and eliminate this // function / data file. bool EQEqCharges::ParseParamFile() { int atomicNumber, i; vector<string> vs; char buffer[BUFF_SIZE]; // open data/eqeqIonizations.txt ifstream ifs; if (OpenDatafile(ifs, "eqeqIonizations.txt").length() == 0) { obErrorLog.ThrowError(__FUNCTION__, "Cannot open eqeqIonizations.txt", obError); return false; } // Set the locale for number parsing to avoid locale issues: PR#1785463 obLocale.SetLocale(); while (ifs.getline(buffer, BUFF_SIZE)) { if (buffer[0] == '#') continue; tokenize(vs, buffer); if (vs.size() != 12) { obErrorLog.ThrowError(__FUNCTION__, "Format error in eqeqIonizations.txt. Each data row should have exactly 12 elements.", obError); return false; } // Reads in a line of parameters. The format is: // Atomic Number Element Ion Charge Affinity Ionizations (x8) atomicNumber = atoi(vs[0].c_str()); _chargeCenter[atomicNumber] = atoi(vs[2].c_str()); for (i = 0; i < 9; i ++) _ionizations[atomicNumber][i] = atof(vs[i + 3].c_str()); // The electron affinity of hydrogen is a custom-fit parameter _ionizations[1][0] = hi_0; } return true; }
bool OpTransform::Initialize() { _dataLoaded=true; _transforms.clear(); ifstream ifs; if(ifs.is_open()) ifs.close(); char charBuffer[BUFF_SIZE]; // Set the locale for number parsing to avoid locale issues: PR#1785463 obLocale.SetLocale(); if(strcmp(_filename,"*")) { if(!strncmp(_filename,"TRANSFORM",9))//A single transform can replace the filename { ParseLine(_filename); return true; } OpenDatafile(ifs, _filename); if(!ifs) { obErrorLog.ThrowError(__FUNCTION__," Could not open " + string(_filename), obError); return false; } while(ifs.getline(charBuffer,BUFF_SIZE)) ParseLine(charBuffer); } else //When filename is * use data in lines following for(int i=4;i<_textlines.size();++i) ParseLine(_textlines[i].c_str()); // return the locale to the original one obLocale.RestoreLocale(); return true; }
void EEMCharges::_loadParameters() { std::ifstream ifs; if (!OpenDatafile(ifs, _parameters_file).length()) { obErrorLog.ThrowError(__FUNCTION__, std::string("Cannot open file with parameters: ").append(_parameters_file), obError); return; } std::string line; std::getline(ifs, line); std::stringstream ss(line); std::string dummy; ss >> dummy >>_kappa; while(std::getline(ifs, line)) { ss.str(line); ss.clear(); std::string symbol; std::string bond_order; struct EEMParameter parameter; ss >> symbol >> bond_order >> parameter.A >> parameter.B; parameter.Z = symbol == "*" ? -1 : etab.GetAtomicNum(symbol.c_str()); parameter.bond_order = bond_order == "*" ? -1 : std::atoi(bond_order.c_str()); _parameters.push_back(parameter); } }
bool ReadPatternFile(const string& filename, vector<string>& lines) { //Reads two types of file: SMARTS + comments and vice versa //depending on whether the first line is #Comments after SMARTS //Output strings in vector are SMARTS + comments string file = filename; ifstream ifs; #ifdef HAVE_SSTREAM stringstream errorMsg; #else strstream errorMsg; #endif if (OpenDatafile(ifs, filename).length() == 0) { errorMsg << "Cannot open " << filename << endl; obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obError); return false; } if(!(ifs)) { errorMsg << "Cannot open " << filename << endl; obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obError); return false; } string smarts, formatline; if(!getline(ifs, formatline)) return false; if(Trim(formatline)=="#Comments after SMARTS") { while(ifs.good()) { if( getline(ifs,smarts) && Trim(smarts).size() > 0 && smarts[0] != '#') lines.push_back(smarts); //leave the comments in } } else { // Christian Laggner's format: SMARTS at end of line while(ifs.good()) { if( getline(ifs,smarts) && smarts[0]!='#') { string::size_type pos = smarts.find(':'); if(pos!=string::npos) { pos = smarts.find_first_not_of(" \t", pos+1); if(pos!=string::npos) { string buf = smarts.substr(pos); lines.push_back(Trim(buf) + ' ' + smarts.substr(0,pos)); } } } } } if (ifs) ifs.close(); return true; }
/** Attempts to read the index file datafilename.obindx successively from the following directories: - the current directory - that in the environment variable BABEL_DATADIR or in the macro BABEL_DATADIR if the environment variable is not set - in a subdirectory of the BABEL_DATADIR directory with the version of OpenBabel as its name An index of type NameIndexType is then constructed. NameIndexType is defined in obmolecformat.h and may be a std::tr1::unordered_map (a hash_map) or std::map. In any case it is searched by @code NameIndexType::iterator itr = index.find(molecule_name); if(itr!=index.end()) unsigned pos_in_datafile = itr->second; @endcode pos_in_datafile is used as a parameter in seekg() to read from the datafile If no index is found, it is constructed from the datafile by reading all of it using the format pInFormat, and written to the directory containing the datafile. This means that this function can be used without worrying whether there is an index. It will be slow to execute the first time, but subsequent uses get the speed benefit of indexed access to the datafile. The serialization and de-serialization of the NameIndexType is entirely in this routine and could possibly be improved. Currently re-hashing is done every time the index is read. **/ bool OBMoleculeFormat::ReadNameIndex(NameIndexType& index, const string& datafilename, OBFormat* pInFormat) { struct headertype { char filename[256]; unsigned size; } header; NameIndexType::iterator itr; ifstream indexstream; OpenDatafile(indexstream, datafilename + ".obindx"); if(!indexstream) { //Need to prepare the index ifstream datastream; string datafilepath = OpenDatafile(datastream, datafilename); if(!datastream) { obErrorLog.ThrowError(__FUNCTION__, datafilename + " was not found or could not be opened", obError); return false; } OBConversion Conv(&datastream,NULL); Conv.SetInFormat(pInFormat); OBMol mol; streampos pos; while(Conv.Read(&mol)) { string name = mol.GetTitle(); if(!name.empty()) index.insert(make_pair(name, pos)); mol.Clear(); pos = datastream.tellg(); } obErrorLog.ThrowError(__FUNCTION__, "Prepared an index for " + datafilepath, obAuditMsg); //Save index to file ofstream dofs((datafilepath + ".obindx").c_str(), ios_base::out|ios_base::binary); if(!dofs) return false; strncpy(header.filename,datafilename.c_str(), sizeof(header.filename)); header.filename[sizeof(header.filename) - 1] = '\0'; header.size = index.size(); dofs.write((const char*)&header, sizeof(headertype)); for(itr=index.begin();itr!=index.end();++itr) { //#chars; chars; ofset(4bytes). const char n = itr->first.size(); dofs.put(n); dofs.write(itr->first.c_str(),n); dofs.write((const char*)&itr->second,sizeof(unsigned)); } } else { //Read index data from file and put into hash_map indexstream.read((char*)&header,sizeof(headertype)); itr=index.begin(); // for hint for(unsigned int i=0;i<header.size;++i) { char len; indexstream.get(len); string title(len, 0); unsigned pos; indexstream.read(&title[0],len); indexstream.read((char*)&pos,sizeof(unsigned)); index.insert(itr, make_pair(title,pos)); } } return true; }
void OBGlobalDataBase::Init() { if (_init) return; _init = true; ifstream ifs; char charBuffer[BUFF_SIZE]; // Set the locale for number parsing to avoid locale issues: PR#1785463 obLocale.SetLocale(); // Check return value from OpenDatafile // Suggestion from Zhiguo Liu string fn_open = OpenDatafile(ifs, _filename, _envvar); // Check _subdir directory if (fn_open == "") string fn_open = OpenDatafile(ifs, _filename, _subdir); if (fn_open != "" && (ifs)) { while(ifs.getline(charBuffer,BUFF_SIZE)) ParseLine(charBuffer); } else // If all else fails, use the compiled in values if (_dataptr) { obErrorLog.ThrowError(__FUNCTION__, "Cannot open " + _filename + " defaulting to compiled data.", obWarning); const char *p1,*p2; for (p1 = p2 = _dataptr;*p2 != '\0';++p2) if (*p2 == '\n') { strncpy(charBuffer, p1, (p2 - p1)); charBuffer[(p2 - p1)] = '\0'; ParseLine(charBuffer); p1 = ++p2; } } else { string s = "Unable to open data file '"; s += _filename; s += "'"; obErrorLog.ThrowError(__FUNCTION__, s, obWarning); } // return the locale to the original one obLocale.RestoreLocale(); if (ifs) ifs.close(); if (GetSize() == 0) { string s = "Cannot initialize database '"; s += _filename; s += "' which may cause further errors."; obErrorLog.ThrowError(__FUNCTION__, s, obWarning); } }