Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
    }
}
Ejemplo n.º 3
0
void StringLookups::Open(STRING FileName, boost::iostreams::mapped_file_source &file_map)
{
    try
    {
        file_map.open(FileName);
    }
    catch(std::ios::failure const &ex)
    {
        printer("StringLookups: Error - Unable to open \"%s\" as read only via memory mapping. The file is probably open in another application.\n", FileName);
        throw ex;
    }
    catch(std::exception &ex)
    {
        printer("StringLookups: Error - Unable to open \"%s\" as read only via memory mapping.\n", FileName);
        throw ex;
    }
    catch(...)
    {
        printer("StringLookups: Error - Unable to open \"%s\" as read only via memory mapping.  An unhandled exception occurred.\n", FileName);
        throw;
    }
}
Ejemplo n.º 4
0
 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);
 }