HostPathFile::HostPathFile(const Path& path, OpenMode mode) { #if defined(NUCLEUS_PLATFORM_WINDOWS) fopen_s(&handle, path.c_str(), getOpenMode(mode)); #else handle = fopen(path.c_str(), getOpenMode(mode)); #endif }
bool LineBasedFile::search(const String& text, const String& stop, bool return_to_start) { if (!isOpen() || getOpenMode() != MODE_IN) { throw Exception::ParseError(__FILE__, __LINE__, String("File '") + getName() + "' not open for reading" , "LineBasedFile::search"); } Position start_point = line_number_; while (readLine()) { if (startsWith(stop)) { if (return_to_start) { gotoLine(start_point); } return false; } if (startsWith(text)) { return true; } } if (return_to_start) { gotoLine(start_point); } return false; }
void GenericMolFile::close() { LineBasedFile::close(); if (gmf_is_closed_) return; if (getOpenMode() == std::ios::in) { if (input_is_temporary_) { File::remove(name_); } } else if(compress_output_) { std::fstream::close(); std::ifstream unzipped_file(name_.c_str(), ios_base::in); boost::iostreams::filtering_streambuf<boost::iostreams::input> in; in.push(boost::iostreams::gzip_compressor()); in.push(unzipped_file); std::ofstream zipped_file(zipped_filename_.c_str(), std::ios::out | std::ios_base::binary); boost::iostreams::copy(in, zipped_file); File::remove(name_); } gmf_is_closed_ = true; }
bool HMOFile::write(Surface const& surface) { // we can only write something if the file has been opened correctly if (!isOpen() || !(getOpenMode() & MODE_OUT)) { throw File::CannotWrite(__FILE__, __LINE__, name_); } // write a comment as a header replacement if (comments_.size() == 0) { getFileStream() << "# HYPERMESH file written by BALL::HMOFile" << std::endl << std::endl; } else { for (Position i=0; i<comments_.size(); ++i) { getFileStream() << comments_[i] << std::endl; } getFileStream() << std::endl; } writeNodes_(surface); writeElements_(surface); return true; }
bool HMOFile::write(Surface const& surface, AtomContainer const& ac) { if (!isOpen() || !(getOpenMode() & MODE_OUT)) { throw File::CannotWrite(__FILE__, __LINE__, name_); } // write a comment as a header replacement if (comments_.size() == 0) { getFileStream() << "# HYPERMESH file for " << ac.getName() << ", written by BALL::HMOFile" << std::endl << std::endl; } else { for (Position i=0; i<comments_.size(); ++i) { getFileStream() << comments_[i] << std::endl; } getFileStream() << std::endl; } writeNodes_(surface); writeElements_(surface); writeCharges_(ac); return true; }
struct func_result_t Open(int entryStatus) { gcInitBuiltinEnv(); if (BUILTIN_FRAG->length < 2) PRINT_AND_EXIT(TOO_FEW_ARGUMENTS); if (_memMngr.vterms[BUILTIN_FRAG->offset].tag != V_CHAR_TAG) FMT_PRINT_AND_EXIT(BAD_FILE_OPEN_MODE, "Open"); uint8_t mode = getOpenMode(BUILTIN_FRAG); uint8_t descr = getDescr(BUILTIN_FRAG); gcOpenFile(BUILTIN_FRAG, mode, descr); return (struct func_result_t){.status = OK_RESULT, .fieldChain = 0, .callChain = 0}; } void gcOpenDefaultFile(uint8_t descr, uint8_t mode) { checkAndCleanHeaps(0, PATTERN_FILE_NAME_LENGHT); char* fileName = (char*)(_memMngr.data + _memMngr.dataOffset); snprintf(fileName, PATTERN_FILE_NAME_LENGHT, FILE_NAME_PATTERN, descr); openFileWithName(fileName, mode, descr); }
bool GenericMolFile::write(const Molecule& /* molecule */) { if (!isOpen() || getOpenMode() != std::ios::out) { throw (File::CannotWrite(__FILE__, __LINE__, name_)); } return true; }
/** ToDo: Implement! :-) **/ bool GAMESSLogFile::write(const System& system) { if (!isOpen() || getOpenMode() != std::ios::out) { throw(File::CannotWrite(__FILE__, __LINE__, name_)); } /** Here goes the code... **/ return true; }
bool GenericMolFile::isCompressedFile() { if (getOpenMode() == std::ios::in) { return input_is_temporary_; } else { return compress_output_; } }
bool LineBasedFile::readLine() { if (!isOpen() || getOpenMode() != MODE_IN) { throw Exception::ParseError(__FILE__, __LINE__, String("File '") + getName() + "' not open for reading" , "LineBasedFile::readLine"); } line_.getline(getFileStream()); if (trim_whitespaces_) line_.trim(); ++line_number_; return !eof(); }
bool CIFFile::write() { if (!isOpen() || getOpenMode() != std::ios::out) { throw(File::CannotWrite(__FILE__, __LINE__, name_)); } vector<Datablock>::iterator si; for (si = datablocks_.begin(); si != datablocks_.end(); si++) { *si >> getFileStream(); } return true; }
bool GenericMolFile::write(const System& system) { if (!isOpen() || getOpenMode() != std::ios::out) { throw (File::CannotWrite(__FILE__, __LINE__, name_)); } initWrite_(); MoleculeConstIterator molecule = system.beginMolecule(); for (; +molecule; ++molecule) { if (!write(*molecule)) return false; } return true; }
bool HMOFile::read(Surface& surface) { // we can only read something if the file has been opened correctly if (!isOpen() || getOpenMode() != MODE_IN) { return false; } // now make sure that there is no old crap lying around charges_.clear(); comments_.clear(); // find the first block of interest (NODL_DATA) if (!readUntil_("BEG_NODL_DATA")) { throw(Exception::ParseError(__FILE__, __LINE__, "HMOFile::read", "file does not contain node data")); } readNodeData_(surface); // now, find the geometric elements (ELEM_DATA) if (!readUntil_("BEG_ELEM_DATA")) { throw(Exception::ParseError(__FILE__, __LINE__, "HMOFile::read", "file does not contain element data")); } readElementData_(surface); // now, see if there is additional charge data (CHARGE_DATA); if not, we are done if (readUntil_("BEG_CHARGE_DATA")) { readChargeData_(); } return true; }
bool KCFFile::write(const Molecule& molecule) throw(File::CannotWrite) { if (!isOpen() || getOpenMode() != std::ios::out) { throw File::CannotWrite(__FILE__, __LINE__, name_); } // An alias for simplicity's sake... std::ostream& os(getFileStream()); // Write ENTRY block // number of blanks???? properties are not read, written??? Which ones are there? os << ENTRY_TAG << " " << molecule.getName() << std::endl; static char buffer[BALL_MAX_LINE_LENGTH]; // Write NODE block // How to create the KEGG atom types? How many blanks? // This is not specified in the KCF format description, so we use what we can // deduce from example files. // First line gets the NODE tag os << NODE_TAG << " " << molecule.countAtoms() << "\n"; Size count = 1; AtomConstIterator ai(molecule.beginAtom()); std::map<const Atom*, Position> atom_to_index; for (; +ai; ++ai, ++count) { // Write the atom line. // Blanks???? String type = ai->getTypeName(); String comment; // Make sure the type is in the set of KEGG types???? // Blanks? sprintf(buffer, " %d %s %s %6.4f %6.4f %s\n", count, type.c_str(), ai->getElement().getSymbol().c_str(), ai->getPosition().x, ai->getPosition().y, comment.c_str()); os << buffer; // Remember the index of the current atom to map atom // pointers back to indices for the EDGE section. atom_to_index[&*ai] = count; } // Write EDGE block. Walk over all bonds to do so. // Blanks???? os << "EDGE " << molecule.countBonds() << "\n"; count = 1; for (ai = molecule.beginAtom(); +ai; ++ai) { for (Atom::BondConstIterator bi(ai->beginBond()); +bi; ++bi) { Position index1 = atom_to_index[bi->getFirstAtom()]; Position index2 = atom_to_index[bi->getSecondAtom()]; String comment; // Write every bond just once if (bi->getFirstAtom() == &*ai) { sprintf(buffer, " %4d %4d %4d %1d%s\n", count, index1, index2, bi->getOrder(), comment.c_str()); os << buffer; ++count; } } } // Write the DELIMITER block os << DELIMITER_TAG << std::endl; return true; }
bool AntechamberFile::write(const AtomContainer& ac) { if (!isOpen() || getOpenMode() != std::ios::out) { throw (File::CannotWrite(__FILE__, __LINE__, name_)); } // first, we write the charge information float total_charge = 0.f; for (AtomConstIterator at_it = ac.beginAtom(); +at_it; ++at_it) { total_charge += at_it->getCharge(); } int rounded_charge = (int) Maths::round(total_charge); String charge_line(255, "CHARGE %.2f ( %d )", total_charge, rounded_charge); (File&)(*this) << charge_line << std::endl; // next, we need the element counts std::map<String, Size> element_counts; for (AtomConstIterator at_it = ac.beginAtom(); +at_it; ++at_it) { String const& element = at_it->getElement().getSymbol(); if (element_counts.find(element) != element_counts.end()) element_counts[element]++; else element_counts[element] = 1; } // and write it to the file. String formula_line("Formula: "); std::map<String, Size>::iterator element_iterator = element_counts.begin(); for (; element_iterator != element_counts.end(); ++element_iterator) { formula_line += element_iterator->first + String(element_iterator->second) + " "; } (File&)(*this) << formula_line << std::endl; // write all atoms String atom_format("ATOM%7d %-4s%-4s%5d%12.3f%8.3f%8.3f%10.6lf%10s"); Position current_atom = 1; HashMap<Atom const*, Position> atom_indices; HashMap<Atom const*, String> atom_names; HashMap<String, Position> element_count; AtomConstIterator at_it = ac.beginAtom(); for (; +at_it; ++at_it, ++current_atom) { // we will need the indices for the bonds later on atom_indices[&*at_it] = current_atom; String parent_name = ((AtomContainer const*)(at_it->getParent()))->getName(); parent_name.truncate(4); if (parent_name == "") parent_name = "UNK"; Position residue_id = 1; Residue const* residue = at_it->getResidue(); if (residue) { residue_id = residue->getID().toInt(); } String atom_type = at_it->getTypeName(); if (at_it->hasProperty("atomtype")) atom_type = at_it->getProperty("atomtype").getString(); if (atom_type.size()>10) atom_type.truncate(10); String atom_name = at_it->getElement().getSymbol(); if (element_count.has(atom_name)) { ++element_count[atom_name]; } else { element_count[atom_name] = 1; } atom_name += String(element_count[atom_name]); //atom_name.substitute(" ", "_"); //String atom_name = at_it->getName(); atom_name.truncate(4); atom_names[&*at_it] = atom_name; String atom_line(255, atom_format.c_str(), current_atom, atom_name.c_str(), parent_name.c_str(), residue_id, at_it->getPosition().x, at_it->getPosition().y, at_it->getPosition().z, at_it->getCharge(), atom_type.c_str()); (File&)(*this) << atom_line << std::endl; } // and finally, write out all bonds String bond_format("BOND%5d%5d%5d%5d %5s%5s"); Position current_bond = 1; Atom::BondConstIterator bit; BALL_FOREACH_BOND(ac, at_it, bit) { String bond_line(255, bond_format.c_str(), current_bond, atom_indices[bit->getFirstAtom()], atom_indices[bit->getSecondAtom()], bit->getOrder(), atom_names[bit->getFirstAtom()].c_str(), atom_names[bit->getSecondAtom()].c_str()); (File&)(*this) << bond_line << std::endl; ++current_bond; }