diff options
-rw-r--r-- | src/main/Parser.cc | 281 | ||||
-rw-r--r-- | src/main/Parser.h | 132 | ||||
-rw-r--r-- | src/main/exceptions.cc | 14 | ||||
-rw-r--r-- | src/main/exceptions.h | 18 | ||||
-rw-r--r-- | src/main/simulation.cc | 7 | ||||
-rw-r--r-- | src/main/simulation.xml | 87 | ||||
-rw-r--r-- | src/test/AccelTest.cc | 2 | ||||
-rw-r--r-- | src/test/exerciceP11Test.cc | 2 |
8 files changed, 451 insertions, 92 deletions
diff --git a/src/main/Parser.cc b/src/main/Parser.cc index bad70b7..5ac74a5 100644 --- a/src/main/Parser.cc +++ b/src/main/Parser.cc @@ -7,75 +7,254 @@ #include "Parser.h" -balise Parser::system = "<System>"; -balise Parser::accelerator ="<Accelerator>"; -balise Parser::constants ="<constants>"; -balise Parser::curvedElement ="<CurvedElement>"; -balise Parser::dipole ="<Dipole>"; -balise Parser::fodo ="<FODO>"; -balise Parser::particle ="<Particle>"; -balise Parser::quadrupole ="<Quadrupole>"; -balise Parser::straightElement ="<StraightElement>"; -balise Parser::systemCl = "<\System>"; -balise Parser::acceleratorCl ="<<Accelerator>"; -balise Parser::constantsCl ="<\constants>"; -balise Parser::curvedElementCl ="<\CurvedElement>"; -balise Parser::dipoleCl ="<\Dipole>"; -balise Parser::fodoCl ="<\FODO>"; -balise Parser::particleCl ="<\Particle>"; -balise Parser::quadrupoleCl ="<\Quadrupole>"; -balise Parser::straightElementCl ="<\StraightElement>"; - -Parser::Parser(string file) { - // TODO Auto-generated constructor stub +namespace vhc { +/** Cf. header.**/ +Parser::Parser(string file) + :nameOfFile(file){} +//===========================================balises ouvrantes========================================================================== + +//balises ouvrantes d'un système +tagStat Parser::system = "<System>"; +tagStat Parser::accelerator ="<Accelerator>"; +tagStat Parser::comment = "<--"; + +//balises ouvrantes d'une particule +tagStat Parser::particle ="<Particle>"; +tagStat Parser::position = "<Position>"; +tagStat Parser::mass = "<Mass>"; +tagStat Parser::charge = "<Charge>"; +tagStat Parser::energy = "<Energy>"; +tagStat Parser::direction = "<Direction>"; + +//balises ouvrantes d'un Element +tagStat Parser::entryPos = "<EntryPosition>"; +tagStat Parser::exitPos = "<ExitPosition>"; +tagStat Parser::sectionRadius = "<SectionRadius>"; + +//balises ouvrantes d'un Dipole +tagStat Parser::dipole ="<Dipole>"; +tagStat Parser::curvature = "<Curvature>"; +tagStat Parser::magneticField = "<MagneticField>"; + +//balise ouvrante commune aux quadrupoles et aux fodo +tagStat Parser::focCoeff = "<FocalizingCoefficient>"; + +//balises ouvrantes d'une FODO +tagStat Parser::fodo ="<FODO>"; +tagStat Parser::straightLength = "<StraightLenght>"; + +//balises ouvrantes d'un Quadrupole +tagStat Parser::quadrupole ="<Quadrupole>"; + +//balises ouvrantes d'un StraightElement +tagStat Parser::straightElement ="<StraightElement>"; + +//===================================================balises fermantes================================================================== + +//balises fermantes d'un système +tagStat Parser::systemCl = "<\System>"; +tagStat Parser::acceleratorCl ="<\Accelerator>"; +tagStat Parser::commentCl = "-->"; + +//balises fermantes d'une particule +tagStat Parser::particleCl ="<\Particle>"; +tagStat Parser::positionCl = "<\Position>"; +tagStat Parser::massCl = "<\Mass>"; +tagStat Parser::chargeCl = "<\Charge>"; +tagStat Parser::energyCl = "<\Energy>"; +tagStat Parser::directionCl = "<\Direction>"; + +//balises fermantes d'un Element +tagStat Parser::entryPosCl = "<\EntryPosition>"; +tagStat Parser::exitPosCl = "<\ExitPosition>"; +tagStat Parser::sectionRadiusCl = "<\SectionRadius>"; + +//balises fermantes d'un Dipole +tagStat Parser::dipoleCl ="<\Dipole>"; +tagStat Parser::curvatureCl = "<\Curvature>"; +tagStat Parser::magneticFieldCl = "<\MagneticField"; + +//balise fermantes commune aux quadrupoles et aux fodo +tagStat Parser::focCoeffCl = "<\FocalizingCoefficient>"; + +//balises fermantes d'une FODO +tagStat Parser::fodoCl ="<\FODO>"; +tagStat Parser::straightLengthCl = "<\StraightLenght>"; + +//balises fermantes d'un Quadrupole +tagStat Parser::quadrupoleCl ="<\Quadrupole>"; + +//balises fermantes d'un StraightElement +tagStat Parser::straightElementCl ="<\StraightElement>"; +//====================================================================================================================================== +//====================================================================================================================================== + +//====================================================================================================================================== +Parser::readOneChar(ifstream& file){ + char tmp; + file>>tmp; + return tmp; +} +Parser::readOneInt(ifstream& file){ + int tmp; + file>>tmp; + return tmp; +} +Parser::readOneDouble(ifstream& file){ + double tmp; + file>>tmp; + return tmp; } +Parser::readVector3D(ifstream& file){ -Parser::extract(string file) { + double x = readOneDouble(file); + readComma(file); + double y = readOneDouble(file); + readComma(file); + double z = readOneDouble(file); - //met le nom qu'on rajoute après le programme du fichier dans une string - string nameOfFile; - getline(cin, nameOfFile); + return Vector3D(x,y,z); +} +Parser::readOneTag(ifstream& file){ + tag tmp; + file>>tag; + return tmp; +} +Parser::readComma(ifstream& file){ + char trash; + file>>trash; +} +Parser::readClosingTag(ifstream& file){ + tag tmp; + file>>tmp; + if(tmp != *"Cl") + throw ReadExceptionMessage("No closing tag found."); +} +Parser::findNextTag(ifstream& file){} +Parser::jumpComment(ifstream& file){} +//====================================================================================================================================== +Parser::extract() { //on déclare une flot d'entrée, ainsi qu'une sortie ifstream entry; - //TODO - ofstream exit; - //TODO est-ce qu'un xml est un binaire? auquel cas rajouter l'argument ios::in|ios::binary //associe le flot d'entrée au fichier donné par l'utilisateur entry.open(nameOfFile.c_str()); - //test si l'association a bien pu se faire + //teste si l'association a bien pu se faire if(not entry.fail()){ - //met le premier mot du fichier dans une string temporaire - string tmp = readOneWord(entry); - //TODO encore plein de choses à faire + readFile(entry); + //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; //TODO une erreur + throw IOException("Cannot open file. "+nameOfFile+" not found."); } } -//TODO use exceptions! -Parser::readOneChar(ifstream& file){}; -Parser::readOneInt(ifstream& file){}; -Parser::readOneDouble(ifstream& file){}; -Parser::readOnePosition(ifstream& file){}; -Parser::readOneBalise(ifstream& file){}; -Parser::findNextBalise(ifstream& file){}; -Parser::jumpComment(ifstream& file){}; -Parser::readFile(ifstream& file){}; +Parser::readTag(ifstream& file){ + try { -Parser::readOneWord(ifstream& file) { - string tmp; - file>>tmp; - return tmp; -} + tag tmp; -Parser::readOneLine(ifstream& file){ - string tmp; - getline(file, tmp); - return tmp; + //Tant qu'on n'est pas arrivé à la fin du fichier, on continue à le lire + while(not file.eof()){ + + tmp = readOneTag(file); + + //on teste si la ligne correspond à une des balises + switch(tmp){ + + case system : + break; + + case accelerator : + + Accelerator* acc = new Accelerator; + break; + + case dipole : + + try{ + tag tmp3; + tmp=readOneTag(file); + + switch(tmp3){ + + case entryPos : + Vector3D diEntryPos = readVector3D(file); + case exitPos : + Vector3D diExitPos = dsfaga + } + + + Dipole* di = new Dipole(lkdsflksdanlkfdag); + } + catch(ReadException& ex){ + e.addReadExceptionMessage("Error building Dipole."); + throw; + } + + case fodo : + + try{ + FODO* fodo = new FODO(readsmth); + } + catch(ReadException& ex){ + e.addReadExceptionMessage("Error building FODO."); + throw; + } + + case particle : + + try{ + Particle* pa = new Particle(readsmth); + } + catch(ReadException& ex){ + e.addReadExceptionMessage("Error building Particle."); + throw; + } + + case quadrupole : + + try{ + Quadrupole* quad = new Quadrupole(readsmth); + } + catch(ReadException& ex){ + e.addReadExceptionMessage("Error building Quadrupole."); + throw; + } + + case straightElement : + + try{ + StraightElement* se = new StraightElement(readsmth); + } + catch(ReadException& ex){ + e.addReadExceptionMessage("Error building StraightElement."); + throw; + } + + // si la variable tmp est vide, on ne fait rien, si elle est non-vide mais ne contient pas de balise, + // c'est qu'une valeur (celle contenue par la variable donc) n'a pas été récupérée comme il faut. + default: + //TODO si l'opérateur ne lit pas les blancs, sinon il fau mettre ça ailleurs + readClosingTag(file); + + //TODO est-ce que l'opérateur >> lit les blancs? + //if(tmp != " ") throw ReadException("Values found outside of tag"); + break; + + + } + } + + } + catch(Exception const& ex){ + std::cout<<ex.getMessage()<<std::endl; + } } + +}//namespace diff --git a/src/main/Parser.h b/src/main/Parser.h index dd153be..85460c9 100644 --- a/src/main/Parser.h +++ b/src/main/Parser.h @@ -9,50 +9,122 @@ #include <vector> #include <ifstream> #include <ofstream> +#include "Accelerator.h" #include "Vector3D.h" +#include "exceptions.h" #ifndef PARSER_H_ #define PARSER_H_ -typedef balise string; -typedef balStat static balise; +namespace vhc { + + +typedef tag string; +typedef tagStat static tag; class Parser { public: - Parser(string file); - virtual ~Parser(){}; - void extract(string file); + Parser(string); + virtual ~Parser(){} + Accelerator* extract(string); private: - balStat system; - balStat accelerator; - balStat constants; - balStat curvedElement; - balStat dipole; - balStat fodo; - balStat particle; - balStat quadrupole; - balStat straightElement; - - balStat systemCl; - balStat acceleratorCl; - balStat constantsCl; - balStat curvedElementCl; - balStat dipoleCl; - balStat fodoCl; - balStat particleCl; - balStat quadrupoleCl; - balStat straightElementCl; + string nameOfFile; +//===========================================balises ouvrantes========================================================================== + + //balises ouvrantes d'un système + tagStat system; + tagStat accelerator; + tagStat comment; + + //balises ouvrantes d'une particule + tagStat particle; + tagStat position; + tagStat mass; + tagStat charge; + tagStat energy; + tagStat direction; + + //balises ouvrantes d'un Element + tagStat entryPos; + tagStat exitPos; + tagStat sectionRadius; + + //balises ouvrantes d'un dipole + tagStat dipole; + tagStat curvature; + tagStat magneticField; + + //balise ouvrantes commune aux quadrupoles et aux fodo + tagStat focCoeff; + + //balises ouvrantes d'une FODO + tagStat fodo; + tagStat straightLength; + + //balises ouvrante d'un quadrupole + tagStat quadrupole; + + //balises ouvrantes d'une straightElement + tagStat straightElement; + +//===================================================balises fermantes================================================================== + + //balises fermantes d'un système + tagStat systemCl; + tagStat acceleratorCl; + tagStat commentCl; + + //balises fermantes d'une particule + tagStat particleCl; + tagStat positionCl; + tagStat massCl; + tagStat chargeCl; + tagStat energyCl; + tagStat directionCl; + + //balises fermantes d'un Element + tagStat entryPosCl; + tagStat exitPosCl; + tagStat sectionRadiusCl; + //balises fermantes d'un dipole + tagStat dipoleCl; + tagStat curvatureCl; + tagStat magneticFieldCl; + + //balise fermantes commune aux quadrupoles et aux fodo + tagStat focCoeffCl; + + //balises fermantes d'une FODO + tagStat fodoCl; + tagStat straightLengthCl; + + //balises fermantes d'un quadrupole + tagStat quadrupoleCl; + + //balises fermantes d'une straightElement + tagStat straightElementCl; + +//======================================================autres méthodes================================================================= + + //méthodes de lectures génériques + string readFile(ifstream& file); + + //méthodes gestion balises + tag readOpeningBalise(ifstream& file); + tag readClosingBalise(ifstream& file); + tag findNextBalise(ifstream& file); + + //méthode pour commentaire + void jumpComment(ifstream& file); + + //méthodes atomiques de lecture char readOneChar(ifstream& file); int readOneInt(ifstream& file); double readOneDouble(ifstream& file); Vector3D readOnePosition(ifstream& file); - balise readOneBalise(ifstream& file); - balise findNextBalise(ifstream& file); - void jumpComment(ifstream& file); - string readOneWord(ifstream& file); - string readOneLine(ifstream& file); - string readFile(ifstream& file); + }; +} #endif /* PARSER_H_ */ diff --git a/src/main/exceptions.cc b/src/main/exceptions.cc index 577a079..4489519 100644 --- a/src/main/exceptions.cc +++ b/src/main/exceptions.cc @@ -39,5 +39,19 @@ UnsupportedOperationException::UnsupportedOperationException(std::string message std::string UnsupportedOperationException::getExceptionType() const {return "UnsupportedOperationException";} + + +IOException::IOException(): Exception() {}; +IOException::IOException(std::string message): Exception(message) {}; + +std::string IOException::getExceptionType() const {return "IOException";} + +ReadException::ReadException(): Exception() {}; +ReadException::ReadException(std::string message): Exception(message) {}; + +std::string ReadException::getExceptionType() const {return "ReadException";} + +void ReadException::addReadExceptionMessage(std::string mess) {message += (" "+mess);} + } diff --git a/src/main/exceptions.h b/src/main/exceptions.h index 2bf40f0..521620e 100644 --- a/src/main/exceptions.h +++ b/src/main/exceptions.h @@ -17,7 +17,7 @@ namespace vhc { * Pour avoir une representation convenable de cette exception, appeller la methode <code>toString()</code>. */ class Exception: public Printable { -private: +protected: /** Message d'erreur de cette exception. */ std::string message; @@ -67,6 +67,22 @@ public: virtual std::string getExceptionType() const; }; +class IOException: public Exception { +public: + IOException(); + IOException(std::string message); + + virtual std::string getExceptionType() const; +}; + +class ReadException: public Exception { +public: + ReadException(); + ReadException(std::string message); + + virtual std::string getExceptionType() const; + void addReadExceptionMessage(std::string mess); +}; } diff --git a/src/main/simulation.cc b/src/main/simulation.cc index 11804ab..e69de29 100644 --- a/src/main/simulation.cc +++ b/src/main/simulation.cc @@ -1,7 +0,0 @@ -/* - * simulation.cc - * - * Created on: 16 mai 2011 - * Author: christian - */ -//On utilise ce fichier pour écrire dedans avec la classe parseur diff --git a/src/main/simulation.xml b/src/main/simulation.xml new file mode 100644 index 0000000..c70f26e --- /dev/null +++ b/src/main/simulation.xml @@ -0,0 +1,87 @@ +<System> + <Particle> + <Position> + 3.01,0,0 + <\Position> + <Mass> + 1 + <\Mass> + <!-- ICI PAR RAPPORT A LA MASSE DU PROTON --> + <Charge> + 1 + <\Charge> + <!-- ICI PAR RAPPORT A LA CHARGE DE L'ELECTRON --> + <Energy> + 2 + <\Energy> + <!-- ICI PAR RAPPORT A UN GeV --> + <Direction> + 0,-1,0 + <\Direction> + <\Particle> + <Particle> + <Position>2.99,0,0<\Position> + <Mass>1<\Mass><!-- ICI PAR RAPPORT A LA MASSE DU PROTON --> + <Charge>1<\Charge><!-- ICI PAR RAPPORT A LA CHARGE DE L'ELECTRON --> + <Energy>2<\Energy><!-- ICI PAR RAPPORT A UN GeV --> + <Direction>0,-1,0<\Direction> + <\Particle> + <Accelerator> + <FODO> + <EntryPosition>3,2,0<\EntryPosition> + <ExitPosition>3,-2,0<\ExitPosition> + <SectionRadius>0.2<\SectionRadius> + <StraightLenght>1.<\StraightLenght> + <FocalizingCoefficient>1.2<\FocalizingCoefficient> + <\FODO> + <Dipole> + <EntryPosition>3,-2,0<\EntryPosition> + <ExitPosition>2,-3,0<\ExitPosition> + <SectionRadius>0.1<\SectionRadius> + <Curvature>1.<\Curvature> + <MagneticField>0,0,5.89158<\MagneticField> + <\Dipole> + <FODO> + <EntryPosition>2,-3,0<\EntryPosition> + <ExitPosition>-2,-3,0<\ExitPosition> + <SectionRadius>0.2<\SectionRadius> + <StraightLenght>1.<\StraightLenght> + <FocalizingCoefficient>1.2<\FocalizingCoefficient> + <\FODO> + <Dipole> + <EntryPosition>-2,-3,0<\EntryPosition> + <ExitPosition>-3,-2,0<\ExitPosition> + <SectionRadius>0.1<\SectionRadius> + <Curvature>1.<\Curvature> + <MagneticField>0,0,5.89158<\MagneticField> + <\Dipole> + <FODO> + <EntryPosition>-3,-2,0<\EntryPosition> + <ExitPosition>-3,2,0<\ExitPosition> + <SectionRadius>0.2<\SectionRadius> + <StraightLenght>1.<\StraightLenght> + <FocalizingCoefficient>1.2<\FocalizingCoefficient> + <\FODO> + <Dipole> + <EntryPosition>-3,2,0<\EntryPosition> + <ExitPosition>-2,3,0<\ExitPosition> + <SectionRadius>0.1<\SectionRadius> + <Curvature>1.<\Curvature> + <MagneticField>0,0,5.89158<\MagneticField> + <\Dipole> + <FODO> + <EntryPosition>-2,3,0<\EntryPosition> + <ExitPosition>2,3,0<\ExitPosition> + <SectionRadius>0.2<\SectionRadius> + <StraightLenght>1.<\StraightLenght> + <FocalizingCoefficient>1.2<\FocalizingCoefficient> + <\FODO> + <Dipole> + <EntryPosition>2,3,0<\EntryPosition> + <ExitPosition>3,2,0<\ExitPosition> + <SectionRadius>0.1<\SectionRadius> + <Curvature>1.<\Curvature> + <MagneticField>0,0,5.89158<\MagneticField> + <\Dipole> + <\Accelerator> +<\System>
\ No newline at end of file diff --git a/src/test/AccelTest.cc b/src/test/AccelTest.cc index dc5b9c4..4983303 100644 --- a/src/test/AccelTest.cc +++ b/src/test/AccelTest.cc @@ -17,8 +17,6 @@ using namespace std; using namespace vhc; -//TODO erreur sur l'affichage des particules en général ===> "NAN" <========= - /** Test d'affichage.*/ void printTest() { diff --git a/src/test/exerciceP11Test.cc b/src/test/exerciceP11Test.cc index 8bb1cd0..fcb7bb5 100644 --- a/src/test/exerciceP11Test.cc +++ b/src/test/exerciceP11Test.cc @@ -17,7 +17,7 @@ using namespace std; using namespace vhc; -// A bannir car ne garantit pas que les entrées-sorties de séléments soient cohérents. +// Construction par boucle à bannir car ne garantit pas que les entrées-sorties de séléments soient cohérents. // De plus il n'extise aucune moyen d'initaliser ces attributs automatiquement. Accelerator* creatAcc() { |