/* * TremoloParser.cpp * * Created on: Mar 2, 2010 * Author: metzler */ #include "TremoloParser.hpp" #include "World.hpp" #include "atom.hpp" #include "element.hpp" #include "periodentafel.hpp" #include #include using namespace std; /** * Constructor. */ TremoloParser::TremoloParser() { knownKeys[" "] = noKey; // so we can detect invalid keys knownKeys["x"] = x; knownKeys["u"] = u; knownKeys["F"] = F; knownKeys["stress"] = stress; knownKeys["Id"] = Id; knownKeys["neighbors"] = neighbors; knownKeys["imprData"] = imprData; knownKeys["GroupMeasureTypeNo"] = GroupMeasureTypeNo; knownKeys["Type"] = Type; knownKeys["extType"] = extType; knownKeys["name"] = name; knownKeys["resName"] = resName; knownKeys["chainID"] = chainID; knownKeys["resSeq"] = resSeq; knownKeys["occupancy"] = occupancy; knownKeys["tempFactor"] = tempFactor; knownKeys["segID"] = segID; knownKeys["Charge"] = Charge; knownKeys["charge"] = charge; knownKeys["GrpTypeNo"] = GrpTypeNo; } /** * Destructor. */ TremoloParser::~TremoloParser() { } /** * Stores keys from the ATOMDATA line. * * \param line to parse the keys from * \param with which offset the keys begin within the line */ void TremoloParser::parseAtomDataKeysLine(string line, int offset) { string keyword; stringstream lineStream; lineStream << line.substr(offset); while (lineStream.good()) { lineStream >> keyword; if (knownKeys[keyword.substr(0, keyword.find("="))] == noKey) { // throw exception about unknown key cout << "Unknown key: " << keyword << " is not part of the tremolo format specification." << endl; break; } usedFields.push_back(keyword); } } /** * Reads one data line of a tremolo file and interprets it according to the keys * obtained from the ATOMDATA line. * * \param line to parse as an atom */ void TremoloParser::readAtomDataLine(string line) { vector::iterator it; stringstream lineStream; atom* newAtom = World::getInstance().createAtom(); TremoloAtomInfoContainer atomInfo = *(new TremoloAtomInfoContainer()); atomDataKey currentField; string word; lineStream << line; for (it=usedFields.begin(); it < usedFields.end(); it++) { cout << *it << " -- " << it->substr(0, it->find("=")) << " -- " << knownKeys[it->substr(0, it->find("="))] << endl; currentField = knownKeys[it->substr(0, it->find("="))]; switch (currentField) { case x : // for the moment, assume there are always three dimensions lineStream >> newAtom->x.x[0]; lineStream >> newAtom->x.x[1]; lineStream >> newAtom->x.x[2]; break; case u : // for the moment, assume there are always three dimensions lineStream >> newAtom->v.x[0]; lineStream >> newAtom->v.x[1]; lineStream >> newAtom->v.x[2]; break; case F : lineStream >> word; atomInfo.F = word; break; case stress : lineStream >> word; atomInfo.F = word; break; case Id : // this ID is not used break; case neighbors : // TODO neighbor information lineStream >> word; break; case imprData : lineStream >> word; atomInfo.imprData = word; break; case GroupMeasureTypeNo : lineStream >> word; atomInfo.GroupMeasureTypeNo = word; break; case Type : char type[3]; lineStream >> type; newAtom->setType(World::getInstance().getPeriode()->FindElement(type)); break; case extType : lineStream >> word; atomInfo.extType = word; break; case name : lineStream >> word; atomInfo.name = word; break; case resName : lineStream >> word; atomInfo.resName = word; break; case chainID : lineStream >> word; atomInfo.chainID = word; break; case resSeq : lineStream >> word; atomInfo.resSeq = word; break; case occupancy : lineStream >> word; atomInfo.occupancy = word; break; case tempFactor : lineStream >> word; atomInfo.segID = word; break; case segID : lineStream >> word; atomInfo.F = word; break; case Charge : lineStream >> word; atomInfo.Charge = word; break; case charge : lineStream >> word; atomInfo.charge = word; break; case GrpTypeNo : lineStream >> word; atomInfo.GrpTypeNo = word; break; default : lineStream >> word; cout << "Unknown key: " << *it << ", word: " << word << endl; break; } } moreData[newAtom->getId()] = atomInfo; } /** * Loads atoms from a tremolo-formatted file. * * \param tremolo file */ void TremoloParser::load(istream* file) { string line; string::size_type location; usedFields.clear(); while (file->good()) { std::getline(*file, line, '\n'); if (usedFields.empty()) { location = line.find("ATOMDATA", 0); if (location != string::npos) { parseAtomDataKeysLine(line, location + 8); } } if (line.length() > 0 && line.at(0) != '#') { readAtomDataLine(line); } } } /** * Saves the World's current state into as a tremolo file. * * \param file where to save the state */ void TremoloParser::save(ostream* file) { /* write header for (each atom in world) { write coordinates and additional data } */ } TremoloAtomInfoContainer::TremoloAtomInfoContainer() { name = "none"; /* Add suitable default values. std::string F; std::string stress; std::string imprData; std::string GroupMeasureTypeNo; std::string extType; std::string name; std::string resName; std::string chainID; std::string resSeq; std::string occupancy; std::string tempFactor; std::string segID; std::string Charge; std::string charge; std::string GrpTypeNo; */ };