bool XyzFileFormat::readMappedFile(const boost::iostreams::mapped_file_source &input, chemkit::MoleculeFile *file) { const char *data = input.data(); size_t position = 0; // atom count line unsigned int atomCount = 0; int count = sscanf(&data[position], "%u", &atomCount); if(count != 1){ setErrorString("Failed to read atom count line"); return false; } while(data[position++] != '\n'); // comment line while(data[position++] != '\n'); // create molecule boost::shared_ptr<chemkit::Molecule> molecule = boost::make_shared<chemkit::Molecule>(); // read atoms and coordinates for(unsigned int i = 0; i < atomCount; i++){ char symbol[4]; double x, y, z; count = sscanf(&data[position], "%3s %lf %lf %lf", symbol, &x, &y, &z); if(count != 4){ setErrorString("Failed to read atom line"); continue; } while(data[position++] != '\n'); chemkit::Atom *atom = 0; if(strlen(symbol) == 0){ continue; } else if(isdigit(symbol[0])){ chemkit::Element::AtomicNumberType atomicNumber = boost::lexical_cast<chemkit::Element::AtomicNumberType>(symbol); atom = molecule->addAtom(atomicNumber); } else{ atom = molecule->addAtom(symbol); } // set atom position if(atom){ atom->setPosition(x, y, z); } } // add molecule to file file->addMolecule(molecule); return true; }
void StringLookups::Load(boost::iostreams::mapped_file_source &file_map, typeTypes Type) { UINT8 *buffer, *dataBegin; buffer = const_cast<UINT8 *>(reinterpret_cast<const UINT8 *>(file_map.data())); UINT32 stringCount = *reinterpret_cast<UINT32 *>(buffer); buffer += 8; // 4 for stringCount, 4 for dataSize (skipped) dataBegin = buffer + (stringCount * 8) + (Type == 0 ? 0 : 4); for(UINT32 i = 0; i < stringCount; ++i) { UINT32 id = *reinterpret_cast<UINT32 *>(buffer); UINT32 offset = *reinterpret_cast<UINT32 *>(buffer); STRING value = reinterpret_cast<STRING>(dataBegin + offset); Strings[id] = value; } }
size_t map(T& val, boost::iostreams::mapped_file_source const& m, uint64_t flags = 0, const char* friendly_name = "<TOP>") { return map(val, m.data(), flags, friendly_name); }