/* * Parser.cc * * Created on: 11 mai 2011 * Author: christian */ #include #include #include "Parser.h" using namespace std; namespace vhc { //===========================================balises ouvrantes========================================================================== //balises ouvrantes d'un système static tag Parser::system = ""; static tag Parser::accelerator =""; static tag Parser::comment = ""; //balises fermantes d'une particule static tag Parser::particleCl ="<\Particle>"; static tag Parser::positionCl = "<\Position>"; static tag Parser::massCl = "<\Mass>"; static tag Parser::chargeCl = "<\Charge>"; static tag Parser::energyCl = "<\Energy>"; static tag Parser::directionCl = "<\Direction>"; //balises fermantes d'un Element static tag Parser::entryPosCl = "<\EntryPosition>"; static tag Parser::exitPosCl = "<\ExitPosition>"; static tag Parser::sectionRadiusCl = "<\SectionRadius>"; //balises fermantes d'un Dipole static tag Parser::dipoleCl ="<\Dipole>"; static tag Parser::curvatureCl = "<\Curvature>"; static tag Parser::magneticFieldCl = "<\MagneticField"; //balise fermantes commune aux quadrupoles et aux fodo static tag Parser::focCoeffCl = "<\FocalizingCoefficient>"; //balises fermantes d'une FODO static tag Parser::fodoCl ="<\FODO>"; static tag Parser::straightLengthCl = "<\StraightLenght>"; //balises fermantes d'un Quadrupole static tag Parser::quadrupoleCl ="<\Quadrupole>"; //balises fermantes d'un StraightElement static tag Parser::straightElementCl ="<\StraightElement>"; //===========================================================création et renvoi d'un accélérateur======================================= Parser::extract() { Accelerator* acc = new Accelerator; //on déclare une flot d'entrée ifstream entry; //associe le flot d'entrée au fichier donné par l'utilisateur entry.open(nameOfFile.c_str()); //teste si l'association a bien pu se faire if(not entry.fail()){ //Tant qu'on n'est pas arrivé à la fin du fichier, on continue à le lire while(not file.eof()){ //tout se fait depuis ici readFile(entry, *acc); } //A la fin de la lecture, on ferme le flot entry.close(); }else{ //si l'association du flot d'entrée avec le fichier n'a pas pu se faire, on lance une exception throw IOException("Cannot open file. "+nameOfFile+" not found."); } acc->close(); return acc; } //===========================================================lecture du fichier .xml==================================================== Parser::readFile(ifstream& file, Accelerator& acc){ try { // on lit la balise qu'on doit traiter tag tmp1 = readOneTag(file); /*on teste si tmp1 correspond à une des balises suivantes : * - soit system * - soit un commentaire * - sinon il y a un problème car on ne s'attend pas à autre chose à ce niveau-là */ switch(tmp1){ case system : readSystem(file, acc); closingTag(file, systemCl); break; case comment : jumpComment(file); break; /* si la variable tmp1 ne contient pas les balises voulues, c'est qu'une valeur * (celle contenue par la variable tmp1 donc) * n'a pas été récupérée comme il faut. */ default: throw ReadException("Values found outside of following tags : "+system+ "\n Value is : "+tmp1); break; } } catch(Exception const& ex){ cout<>ws; double tmp; file>>tmp; return tmp; } /** Cf. header. */ void Parser::readComma(ifstream& file){ char trash; file>>ws; file>>trash; } //===========================================================lecture moléculaire======================================================== /** Cf. header. */ Vector3D Parser::readVector3D(ifstream& file){ double x = readOneDouble(file); readComma(file); double y = readOneDouble(file); readComma(file); double z = readOneDouble(file); file>>ws; return Vector3D(x,y,z); } /** Cf. header. */ tag Parser::readOneTag(ifstream& file){ tag tmp; file>>ws; file>>tag; return tmp; } /** Cf. header. */ void Parser::closingTag(ifstream& file, std::string ta){ tag tmp; file>>ws; file>>tmp; //si on a pas de balise fermante (ici il devrait il y en avoir normalement une) alors on lance une exception found=tmp.find(ta); if(found() == string::npos) throw ReadExceptionMessage("No closing tag "+ta+" found."); } //===========================================================lecture commentaire======================================================== /**Cf. header. */ void Parser::jumpComment(ifstream& file){ tag tmp; do{ file>>ws; file>>tmp; found=tmp.find("-->"); } while(found() == string::npos); if(file.eof()) throw ReadExceptionMessage("No closing tag for comment found."); } }//namespace