Rev 17770 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 17770 | Rev 17895 | ||
---|---|---|---|
Line 9... | Line 9... | ||
9 | #include "chemeq.h" |
9 | #include "chemeq.h" |
10 | //#include <FlexLexer.h> |
10 | //#include <FlexLexer.h> |
11 | #include <sstream> |
11 | #include <sstream> |
12 | 12 | ||
13 | #include "html_table.cc" |
13 | #include "html_table.cc" |
14 | 14 | ||
15 | int yylex(); |
15 | int yylex(); |
16 | int chemerror(char * msg); |
16 | int chemerror(char * msg); |
17 | int yyerror(const char * msg); |
17 | int yyerror(const char * msg); |
18 | 18 | ||
19 | typedef struct{ |
19 | typedef struct{ |
Line 26... | Line 26... | ||
26 | char symb[4]; |
26 | char symb[4]; |
27 | moltype t; |
27 | moltype t; |
28 | std::string s; |
28 | std::string s; |
29 | } yystype; |
29 | } yystype; |
30 | 30 | ||
31 | #define YYSTYPE yystype |
31 | #define YYSTYPE yystype |
32 | 32 | ||
33 | /* variables globales */ |
33 | /* variables globales */ |
34 | 34 | ||
35 | yystype result; |
35 | yystype result; |
36 | extern char * yytext; |
36 | extern char * yytext; |
Line 66... | Line 66... | ||
66 | %token AntiCompose |
66 | %token AntiCompose |
67 | %token Func |
67 | %token Func |
68 | %% |
68 | %% |
69 | 69 | ||
70 | /* les règles */ |
70 | /* les règles */ |
71 | but : chcompose { |
71 | but : chcompose { |
72 | result=$1; |
72 | result=$1; |
73 | isequation=1; |
73 | isequation=1; |
74 | } |
74 | } |
75 | | molec { |
75 | | molec { |
76 | result=$1; |
76 | result=$1; |
Line 83... | Line 83... | ||
83 | ; |
83 | ; |
84 | 84 | ||
85 | chcompose : chc {$$=$1;} |
85 | chcompose : chc {$$=$1;} |
86 | | chcompose spc01 Compose spc01 chc { |
86 | | chcompose spc01 Compose spc01 chc { |
87 | $1.cq->addChemeq($5.cq); |
87 | $1.cq->addChemeq($5.cq); |
88 | $$=$1; |
88 | $$=$1; |
89 | } |
89 | } |
90 | | chcompose spc01 AntiCompose spc01 chc { |
90 | | chcompose spc01 AntiCompose spc01 chc { |
91 | $1.cq->subChemeq($5.cq); |
91 | $1.cq->subChemeq($5.cq); |
92 | $$=$1; |
92 | $$=$1; |
93 | } |
93 | } |
94 | ; |
94 | ; |
95 | 95 | ||
Line 153... | Line 153... | ||
153 | | Frac spc01 molec { |
153 | | Frac spc01 molec { |
154 | $$ = $3; |
154 | $$ = $3; |
155 | $$.m->nombre($1.i,$1.d); |
155 | $$.m->nombre($1.i,$1.d); |
156 | } |
156 | } |
157 | | molec{ |
157 | | molec{ |
158 | $$ = $1; |
158 | $$ = $1; |
159 | } |
159 | } |
160 | ; |
160 | ; |
161 | 161 | ||
162 | spc01 : /*rien*/ |
162 | spc01 : /*rien*/ |
163 | | Spc |
163 | | Spc |
Line 189... | Line 189... | ||
189 | | Moins{$$.i=-1;} |
189 | | Moins{$$.i=-1;} |
190 | | Haut Moins{$$.i=-1;} |
190 | | Haut Moins{$$.i=-1;} |
191 | ; |
191 | ; |
192 | 192 | ||
193 | groupe : Lpar composition Rpar { |
193 | groupe : Lpar composition Rpar { |
194 | $$=$2; |
194 | $$=$2; |
195 | } |
195 | } |
196 | ; |
196 | ; |
197 | 197 | ||
198 | atome_general : groupe { |
198 | atome_general : groupe { |
199 | $$.al = new AtomeListe("",0,0,$1.al); |
199 | $$.al = new AtomeListe("",0,0,$1.al); |
200 | } |
200 | } |
201 | | Atome{ |
201 | | Atome{ |
202 | char buffer[25]; |
202 | char buffer[25]; |
203 | if ($1.i==-2) { /* ce n'est pas un atome recensé */ |
203 | if ($1.i==-2) { /* ce n'est pas un atome recensé */ |
204 |
|
204 | snprintf(buffer,sizeof(buffer),"nonexistent atom : %s", $1.symb); |
205 | chemerror(buffer); |
205 | chemerror(buffer); |
206 | } |
206 | } |
207 | $$.al = new AtomeListe($1.symb,$1.i); |
207 | $$.al = new AtomeListe($1.symb,$1.i); |
208 | } |
208 | } |
209 | ; |
209 | ; |
Line 221... | Line 221... | ||
221 | 221 | ||
222 | molecularite : /*rien*/{ |
222 | molecularite : /*rien*/{ |
223 | $$.i=1; |
223 | $$.i=1; |
224 | } |
224 | } |
225 | | Int { |
225 | | Int { |
226 | $$=$1; |
226 | $$=$1; |
227 | } |
227 | } |
228 | ; |
228 | ; |
229 | 229 | ||
230 | special_func : Func { |
230 | special_func : Func { |
231 | $$.s=$1.s; |
231 | $$.s=$1.s; |
232 | } |
232 | } |
233 | ; |
233 | ; |
234 | 234 | ||
235 | %% |
235 | %% |
236 | #include "chemlex.cc" |
236 | #include "chemlex.cc" |
237 | 237 | ||
238 | yyFlexLexer lexer; |
238 | yyFlexLexer lexer; |
239 | yyFlexLexer * thelexer = &lexer; |
239 | yyFlexLexer * thelexer = &lexer; |
240 | 240 | ||
241 | int yylex(){ |
241 | int yylex(){ |
242 | return thelexer->yylex(); |
242 | return thelexer->yylex(); |
243 | } |
243 | } |
244 | 244 | ||
245 | /* le programme lui-même */ |
245 | /* le programme lui-même */ |
246 | 246 | ||
247 | inline int chemerror(char * msg){ |
247 | inline int chemerror(char * msg){ |
248 | fprintf(stderr, "ERROR %s at %d\n ", msg, position); |
248 | fprintf(stderr, "ERROR %s at %d\n ", msg, position); |
249 | exit(1); |
249 | exit(1); |
250 | } |
250 | } |
251 | 251 | ||
252 | inline int yyerror(const char * msg){ |
252 | inline int yyerror(const char * msg){ |
253 | printf("ERROR: %s at %d\n", msg, position); |
253 | printf("ERROR: %s at %d\n", msg, position); |
254 | exit(1); |
254 | exit(1); |
255 | } |
255 | } |
256 | 256 | ||
257 | void printVersion(){ |
257 | void printVersion(){ |
258 | std::cout << "Chemeq version " << VERSION << "\n(c) Georges Khaznadar <georgesk@debian.org>."; |
258 | std::cout << "Chemeq version " << VERSION << "\n(c) Georges Khaznadar <georgesk@debian.org>."; |
259 | std::cout << "\nThis program is free, licensed under G.P.L.\n"; |
259 | std::cout << "\nThis program is free, licensed under G.P.L.\n"; |
260 | } |
260 | } |
261 | 261 | ||
262 | void printHelp(){ |
262 | void printHelp(){ |
263 | std::cout << "Usage : chemeq [-m] [-l] [-c] [-w] [-C] [-n] [-e] [-s] [-v] [-h]" << std::endl; |
263 | std::cout << "Usage : chemeq [-m] [-l] [-c] [-w] [-C] [-n] [-e] [-s] [-v] [-h]" << std::endl; |
264 | std::cout << " default is equivalent to 'chemeq -mlcwCn'" << std::endl; |
264 | std::cout << " default is equivalent to 'chemeq -mlcwCn'" << std::endl; |
265 | std::cout << " chemeq reads its standard input which must be a standard" << std::endl; |
265 | std::cout << " chemeq reads its standard input which must be a standard" << std::endl; |
266 | std::cout << " chemical equation. It outputs many strings useful for chemical" << std::endl; |
266 | std::cout << " chemical equation. It outputs many strings useful for chemical" << std::endl; |
267 | std::cout << " calculations." << std::endl; |
267 | std::cout << " calculations." << std::endl; |
268 | std::cout << " As an example you can try the following command :" << std::endl; |
268 | std::cout << " As an example you can try the following command :" << std::endl; |
269 | std::cout << " echo \"Fes -> Fe^2+ + 2 e- (0.44 V)\" | chemeq" << std::endl; |
269 | std::cout << " echo \"Fes -> Fe^2+ + 2 e- (0.44 V)\" | chemeq" << std::endl; |
270 | } |
270 | } |
271 | 271 | ||
272 | #define maxoption 16 |
272 | #define maxoption 16 |
273 | 273 | ||
274 | void optionadd(char* b, char* allowed, int c); |
274 | void optionadd(char* b, char* allowed, int c); |
275 | void optionadd(char* b, char* allowed, int c){ |
275 | void optionadd(char* b, char* allowed, int c){ |
276 | if (strchr(allowed,c) && !strchr(b,c) && strlen(b) < maxoption+1){ |
276 | if (strchr(allowed,c) && !strchr(b,c) && strlen(b) < maxoption+1){ |
277 | int l = strlen(b); |
277 | int l = strlen(b); |
278 | b[l]=c; b[l+1]=0; |
278 | b[l]=c; b[l+1]=0; |
279 | } |
279 | } |
280 | return; |
280 | return; |
281 | } |
281 | } |
282 | 282 | ||
283 | int main(int argc, char * argv[]){ |
283 | int main(int argc, char * argv[]){ |
284 | char * optstr = strdup("mMlcCwnesSvh"); |
284 | char * optstr = strdup("mMlcCwnesSvh"); |
285 | char * envoption=getenv("chemeq_option"); |
285 | char * envoption=getenv("chemeq_option"); |
286 | if (envoption==NULL) envoption=getenv("w_chemeq_option"); |
286 | if (envoption==NULL) envoption=getenv("w_chemeq_option"); |
287 | char * envinput =getenv("chemeq_input"); |
287 | char * envinput =getenv("chemeq_input"); |
Line 295... | Line 295... | ||
295 | while (-1 != (ch=getopt(argc,argv,optstr))){ |
295 | while (-1 != (ch=getopt(argc,argv,optstr))){ |
296 | optionadd(asked,optstr,ch); |
296 | optionadd(asked,optstr,ch); |
297 | } |
297 | } |
298 | if (envoption!=NULL) for(i=0; i<strlen(envoption); i++){ |
298 | if (envoption!=NULL) for(i=0; i<strlen(envoption); i++){ |
299 | optionadd(asked, optstr, envoption[i]); |
299 | optionadd(asked, optstr, envoption[i]); |
300 | } |
300 | } |
301 | if (strchr(asked,'h')!=NULL){ |
301 | if (strchr(asked,'h')!=NULL){ |
302 | printHelp(); return 0; |
302 | printHelp(); return 0; |
303 | } |
303 | } |
304 | if (strchr(asked,'v')!=NULL){ |
304 | if (strchr(asked,'v')!=NULL){ |
305 | printVersion(); return 0; |
305 | printVersion(); return 0; |
306 | } |
306 | } |
307 | if (envinput !=NULL){ |
307 | if (envinput !=NULL){ |
308 | std::istringstream iss(envinput); |
308 | std::istringstream iss(envinput); |
309 | thelexer= new yyFlexLexer (&iss); |
309 | thelexer= new yyFlexLexer (&iss); |
310 | } |
310 | } |
311 | yyparse(); |
311 | yyparse(); |
312 | 312 | ||
313 | bool wantedlatex=(strchr(asked,'l')!=NULL); |
313 | bool wantedlatex=(strchr(asked,'l')!=NULL); |
314 | if (isequation == 1) { |
314 | if (isequation == 1) { |
315 | for(i=0; i<strlen(asked); i++){ |
315 | for(i=0; i<strlen(asked); i++){ |