summaryrefslogtreecommitdiff
path: root/src/main/Parser.cc
blob: 5ac74a59ad4f366129fa4d6e93baba644b568d40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Parser.cc
 *
 *  Created on: 11 mai 2011
 *      Author: christian
 */

#include "Parser.h"

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){

	double x = readOneDouble(file);
	readComma(file);
	double y = readOneDouble(file);
	readComma(file);
	double z = readOneDouble(file);

	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;

	//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()){
		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 IOException("Cannot open file. "+nameOfFile+" not found.");
	}
}


Parser::readTag(ifstream& file){
	try {

		tag 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