1 /**********************************************************
2 * Version $Id$
3 *********************************************************/
4
5 #include <iostream>
6 #include <algorithm>
7 #include <sstream>
8 #include <sstream>
9 #include <locale>
10 #include <functional>
11
12 #include "basistypen.h"
13
14 #include <assert.h>
15
16 #define WHITE_SP " \t\n"
17 #define WHITE_SP_TRENNER " ,;\t\n"
18
19 using namespace std;
20
21 T_VarList VarList;
22 T_InputText InputText;
23 T_InputText InputGrids;
24 int FehlerZeile;
25 int FehlerPos1;
26 int FehlerPos2;
27 string FehlerString("");
28 bool isSyntaxCheck;
29
30 class compare_BB_greater : public greater<BBTyp *>
31 {
32 public:
33 // o.c. bool operator()(const BBTyp * &x, const BBTyp * &y) const
operator ()(BBTyp * x,BBTyp * y) const34 bool operator()(BBTyp *x, BBTyp *y) const
35 {
36 return x->name < y->name;
37 };
38 };/**/
39
40 class compare_BBType
41 {
42 public:
operator ()(const BBTyp * x,const BBTyp * y)43 bool operator() (const BBTyp *x, const BBTyp *y)
44 {
45 return x->name < y->name;
46 }
47 };
48
49 class compare_BBType2
50 {
51 public:
operator ()(BBTyp * x,BBTyp y)52 bool operator() (BBTyp *x, BBTyp y)
53 {
54 return x->name < y.name;
55 }
56 };
57
58 class compare_BBType3
59 {
60 public:
operator ()(BBTyp x,BBTyp y)61 bool operator() (BBTyp x, BBTyp y)
62 {
63 return x.name < y.name;
64 }
65 };
66
GetInputText(string bsl)67 void GetInputText(string bsl )
68 // wandelt EditControl-Text in Vector von Strings um
69 {
70 /* size = e.GetLineCount();
71 char *lpszBuffer = new char [1000];
72 int buffanz = 1000;
73 lpszBuffer[0] = 0;
74 InputText.clear();
75 for (int i=0; i<size; i++)
76 {
77 l= e.LineLength(i);
78 if (l > buffanz-2)
79 {
80 delete [] lpszBuffer;
81 buffanz = l+2;
82 lpszBuffer = new char [buffanz];
83 }
84 lpszBuffer[0] = buffanz;
85 charcopied = e.GetLine( i, lpszBuffer );
86 lpszBuffer[charcopied] = 0;
87 string s(lpszBuffer);
88 InputText.push_back(s);
89 }
90 delete [] lpszBuffer;*/
91 }
92
WhiteSpace(string & s,int & pos,bool vorn)93 void WhiteSpace(string& s, int& pos, bool vorn/* = true*/)
94 // entfertn entweder vorne die Wildcards oder schneidet ab dem
95 // ersten Whitespace/Trenner den Rest ab
96 //
97 // Funktionweise:
98 // vorn == true (default):
99 // Es werden soviele Stellen zu "pos" addiert, wie Wildcards
100 // am Anfang von "s" stehen
101 // vorn == false:
102 // Es wird der String s ab dem ersten vorkommenden Wildcard abgeschnitten
103 //
104 //
105 {
106 if (vorn)
107 {
108 int pos1 = s.find_first_not_of(WHITE_SP);
109 if (pos1 > 0)
110 {
111 s.erase(s.begin(), s.begin() + pos1); // o.c
112 // o.c s.erase(s.begin(), &s[pos1]);
113 pos += pos1;
114 }
115 }
116 else
117 {
118 int pos1 = s.find_first_of(WHITE_SP_TRENNER);
119 if (pos1 > 0)
120 {
121 s.erase(s.begin() + pos1, s.end()); // o.c
122 // o.c s.erase(&s[pos1], s.end());
123 }
124 }
125 }
126
trim(string & s)127 void trim(string& s)
128 {
129 // vorne Whitespaces entfernen
130 int pos1 = s.find_first_not_of(WHITE_SP);
131 if (pos1 > 0)
132 {
133 s.erase(s.begin(), s.begin() + pos1);
134 }
135
136 // hinter Whitespaces entfernen
137 pos1 = s.find_last_not_of(WHITE_SP);
138 if (pos1 >= 0)
139 {
140 s.erase(s.begin() + pos1+1, s.end()); // o.c.
141 }
142 }
143
isNextToken(int zeile,int pos,string & cmp)144 bool isNextToken(int zeile, int pos, string& cmp)
145 {
146 string s = InputText[zeile].substr(pos);
147 if (!isNotEnd(zeile, pos, s))
148 return false;
149
150 WhiteSpace(s, pos);
151 return s == cmp;
152 }
153
isNextChar(int zeile,int pos,const char c)154 bool isNextChar(int zeile, int pos, const char c)
155 {
156 string s = InputText[zeile].substr(pos);
157 if (!isNotEnd(zeile, pos, s))
158 return false;
159
160 WhiteSpace(s, pos);
161 return s[0] == c;
162 }
163
getNextToken(int & zeile,int & pos,string & erg)164 bool getNextToken(int &zeile, int& pos, string& erg)
165 {
166
167 string s = InputText[zeile];
168 erg = InputText[zeile].substr(pos);
169 if (!isNotEnd(zeile, pos, erg))
170 return false;
171
172 WhiteSpace(erg, pos);
173 WhiteSpace(erg, pos, false);
174 pos += erg.size();
175 return true;
176 }
177
getNextZeile(int & zeile,int & pos,string & erg)178 bool getNextZeile(int &zeile, int& pos, string& erg)
179 {
180 // Erstellt string von (zeile, pos) bis zum n�chsten Komma
181 if (zeile >= (int)InputText.size())
182 return false;
183 string sub = InputText[zeile].substr(pos);
184 erg = "";
185 int p;
186 do
187 {
188 if ((p = sub.find_first_of(';')) >= 0)
189 {
190 sub.erase(p, sub.size());
191 pos = p;
192 erg += sub;
193 return true;
194 }
195 erg += sub;
196 p = sub.size()+pos;
197 if (!isNotEnd(zeile,p, sub))
198 return false;
199 } while (true);
200 return false;
201 }
202
getNextChar(int & zeile,int & pos,char & c)203 bool getNextChar(int& zeile, int& pos, char& c)
204 {
205 string s = InputText[zeile].substr(pos);
206 if (!isNotEnd(zeile, pos, s))
207 return false;
208
209 WhiteSpace(s, pos);
210 pos++;
211 c = s[0];
212 return true;
213 }
214
215
isNotEnd(int & zeile,int & pos,string & s)216 bool isNotEnd(int& zeile, int& pos, string& s)
217 {
218 if (zeile >= (int)InputText.size())
219 return false;
220 int pos1;
221 if (pos >= (int)InputText[zeile].size() || (pos1 = InputText[zeile].substr(pos).find_first_not_of(WHITE_SP)) < 0)
222 {
223 int p;
224 do
225 {
226 zeile++;
227 if (zeile >= (int)InputText.size())
228 return false;
229 p = InputText[zeile].find_first_not_of(WHITE_SP);
230 }
231 while (p < 0);
232
233 pos = 0;
234 s = InputText[zeile];
235 }
236 else
237 {
238 // �berpr�fen, ob s nur aus Whitespaces besteht
239
240 }
241 return true;
242 }
243
DeleteVarList(void)244 void DeleteVarList(void)
245 {
246 if (VarList.empty())
247 return;
248 T_VarList::iterator it;
249 for (it = VarList.begin(); it != VarList.end(); it++)
250 {
251 delete (*it);
252 }
253 VarList.clear();
254 }
255
256 // wandelt Strings in Variablen um (aus InputText wird VarList)
ParseVars(int & zeile,int & pos)257 void ParseVars(int& zeile, int& pos)
258 {
259 // Syntax: Type varname [, varname [...] ] ;
260 string subz;
261 BBTyp::T_type t;
262 char c;
263 DeleteVarList();
264 int zeile_old = zeile;
265 int pos_old = pos;
266 FehlerZeile = zeile;
267 while (getNextToken(zeile, pos, subz))
268 {
269 // Typ angeben
270 if (subz == "Integer")
271 t = BBTyp::IType;
272 else if (subz == "Float")
273 t = BBTyp::FType;
274 else if (subz == "Point")
275 t = BBTyp::PType;
276 else if (subz == "Matrix")
277 t = BBTyp::MType;
278 else
279 {
280 zeile = zeile_old;
281 pos = pos_old;
282 break;
283 }
284
285 // Variablen-Name, durch Komma getrennt
286 while (getNextToken(zeile, pos, subz))
287 {
288 FehlerZeile = zeile;
289 // einf�gen in VarList
290 BBTyp *bt;
291 switch(t)
292 {
293 case BBTyp::IType:
294 bt = new BBInteger;
295 bt->name = subz;
296 bt->type = t;
297 break;
298 case BBTyp::FType:
299 bt = new BBFloat;
300 bt->name = subz;
301 bt->type = t;
302 break;
303 case BBTyp::PType:
304 bt = new BBPoint;
305 bt->name = subz;
306 bt->type = t;
307 break;
308 case BBTyp::MType:
309 // falls Name ()
310 {
311 int l = subz.size();
312 if (subz[l-1] == ')' && subz[l-2] == '(')
313 {
314 // Matrix: M()
315 subz.erase(l-2, 2);
316 bt = new BBMatrix(NULL);
317 }
318 else if (subz[l-1] == ')')
319 {
320 printf("loading files not supported");
321 return;
322 /*
323 int posk;
324 // Matrix: M("filename")
325 if ((posk = subz.find('(')) <= 0)
326 throw BBFehlerException(zeile);
327 // Filename herausfinden
328 string filename;
329 filename = subz.substr(posk+1);
330 if (filename.size() < 1)
331 throw BBFehlerException(zeile);
332 filename.erase(filename.size()-1);
333 if (filename[0] !='"' || filename[filename.size()-1] != '"')
334 throw BBFehlerException(zeile);
335 // Variablen-Name herausfinden
336 filename.erase(0, 1);
337 filename.erase(filename.size()-1);
338 subz.erase(posk);
339 // Datei laden
340 bt = new BBMatrix();
341 //
342 // surferDoc sd;
343 CFile file;
344 if (!file.Open(filename.data(), CFile::modeRead))
345 {
346 string out;
347 ostringstream ostr(out);
348 ostr << "Die Datei >" << filename << "< existiert nicht!" << ends;
349 AfxMessageBox(ostr.str().data());
350 delete bt;
351 throw BBFehlerException(zeile);
352 }
353 if (!isSyntaxCheck)
354 {
355 CArchive archive(&file, CArchive::load);
356 // sd.readGrid(archive, *(((BBMatrix *) bt)->M));
357 }
358 */
359 }
360 else
361 bt = new BBMatrix;
362 bt->name = subz;
363 bt->type = t;
364 }
365 break;
366 }
367 if (isVar(subz) != NULL) // falls Name bereits vorhanden
368 {
369 delete bt;
370 throw BBFehlerException(zeile);
371 }
372 VarList.push_back(bt);
373
374 // �berpr�fen auf Komma
375 if (!isNextChar(zeile, pos, ','))
376 break;
377 // wenn Komma, nochmal
378 if (!getNextChar(zeile, pos, c))
379 throw BBFehlerException(zeile);
380 }
381 // �berpr�fen auf Semikolon;
382 if (!getNextChar(zeile, pos, c) || c != ';')
383 throw BBFehlerException(zeile);
384 zeile_old = zeile;
385 pos_old = pos;
386 }
387 }
388
389 // �berpr�ft, ob string eine g�ltige Variable ist
isVar(const string & s)390 BBTyp *isVar(const string& s)
391 {
392 if (VarList.empty())
393 return NULL;
394 // bool exist;
395 T_VarList::iterator it;
396 for (it = VarList.begin(); it != VarList.end(); it++)
397 {
398 string ss = (*it)->name;
399 if ((*it)->name == s)
400 return (*it);
401 }
402 return NULL;
403 /* BBTyp bt;
404 bt.name = s;
405 exist = binary_search(VarList.begin(), VarList.end(), &bt, compare_BBType() );
406 if (exist)
407 {
408 it = search(VarList.begin(), VarList.end(), &bt, &bt, compare_BBType2() );
409 if (it != VarList.end())
410 return (*it);
411 else
412 return NULL;
413 }
414 else
415 return NULL;
416 */
417 }
418
419
420 // ermittelt den Variablen-Typ einer Variablen
getVarType(BBTyp * s)421 BBTyp::T_type getVarType(BBTyp *s)
422 {
423 return s->type;
424 }
425
426 // liefert die jeweilige Variable zur�ck
getVarI(BBTyp * s)427 BBInteger *getVarI(BBTyp *s)
428 {
429 return ((BBInteger *) s);
430 }
431
getVarF(BBTyp * s)432 BBFloat *getVarF(BBTyp *s)
433 {
434 return ((BBFloat *) s);
435 }
436
getVarM(BBTyp * s)437 BBMatrix *getVarM(BBTyp *s)
438 {
439 return ((BBMatrix *) s);
440 }
441
getVarP(BBTyp * s)442 BBPoint *getVarP(BBTyp *s)
443 {
444 return ((BBPoint *) s);
445 }
446
setMatrixVariables(BBMatrix * M)447 void setMatrixVariables(BBMatrix *M)
448 {
449 BBInteger *i;
450 BBFloat *f;
451 BBTyp *b;
452
453 // xanz
454 b = isVar(M->name + ".xanz");
455 assert(b != NULL);
456 i = getVarI(b);
457 assert(i->i == NULL);
458 i->i = &(M->M->xanz);
459 // yanz
460 b = isVar(M->name + ".yanz");
461 assert(b != NULL);
462 i = getVarI(b);
463 assert(i->i == NULL);
464 i->i = &(M->M->yanz);
465 // xll
466 b = isVar(M->name + ".xll");
467 assert(b != NULL);
468 f = getVarF(b);
469 assert(f->f == NULL);
470 f->f = &(M->M->xll);
471 // yll
472 b = isVar(M->name + ".yll");
473 assert(b != NULL);
474 f = getVarF(b);
475 assert(f->f == NULL);
476 f->f = &(M->M->yll);
477 // dxy
478 b = isVar(M->name + ".dxy");
479 assert(b != NULL);
480 f = getVarF(b);
481 assert(f->f == NULL);
482 f->f = &(M->M->dxy);
483 }
484
GetMemoryGrids(CSG_Parameters * BSLParameters)485 bool GetMemoryGrids(CSG_Parameters *BSLParameters)
486 {
487 T_VarList::iterator it;
488
489 for (it = VarList.begin(); it != VarList.end(); it++)
490 {
491 BBTyp::T_type t = getVarType(*it);
492
493 if (t == BBTyp::MType)
494 {
495 BBMatrix *M = getVarM(*it);
496
497 if (!M->isMem) // falls noch erzeugt werden mu�
498 {
499 CSG_Grid *pInput = (*BSLParameters)(M->name.c_str())->asGrid();
500 GridWerte *pGrid = new GridWerte();
501
502 pGrid->Create(*pInput);
503
504 pGrid->xanz = pGrid->Get_NX();
505 pGrid->yanz = pGrid->Get_NY();
506 pGrid->dxy = pGrid->Get_Cellsize();
507 pGrid->xll = pGrid->Get_XMin();
508 pGrid->yll = pGrid->Get_YMin();
509
510 pGrid->calcMinMax();
511
512 M->M = pGrid;
513 M->isMem = true;
514
515 setMatrixVariables(M);
516 }
517 }
518 }
519
520 bool ret = (it == VarList.end());
521 VarList.sort(compare_BB_greater());
522 return ret;
523 }
524
FindMemoryGrids(void)525 bool FindMemoryGrids(void)
526 {
527 InputGrids.clear();
528
529 T_VarList::iterator it;
530 for (it = VarList.begin(); it != VarList.end(); it++)
531 {
532 BBTyp::T_type t = getVarType(*it);
533 if (t == BBTyp::MType)
534 {
535 BBMatrix *M = getVarM(*it);
536 if (!M->isMem)
537 {
538 InputGrids.push_back(M->name);
539 }
540 }
541
542 }
543 bool ret = (it == VarList.end());
544 return ret;
545 }
546
AddMatrixPointVariables(bool pointer2matrix)547 void AddMatrixPointVariables(bool pointer2matrix)
548 {
549 // if (pointer2matrix)
550 // {
551 // Alle anderen Matirx/Point-Variablen hinzu
552 T_VarList::iterator it;
553 for (it = VarList.begin(); it != VarList.end(); it++)
554 {
555 if ((*it)->type == BBTyp::MType)
556 {
557 BBMatrix *m = getVarM(*it);
558 BBTyp *bt;
559 bt = new BBInteger((m->isMem ? &(((BBMatrix *)(*it))->M->xanz) : NULL));
560 bt->name = (*it)->name + string(".xanz");
561 VarList.push_back(bt);
562 bt = new BBInteger((m->isMem ? &(((BBMatrix *)(*it))->M->yanz) : NULL));
563 bt->name = (*it)->name + string(".yanz");
564 VarList.push_back(bt);
565 bt = new BBFloat((m->isMem ? &(((BBMatrix *)(*it))->M->dxy) : NULL));
566 bt->name = (*it)->name + string(".dxy");
567 VarList.push_back(bt);
568 bt = new BBFloat((m->isMem ? &(((BBMatrix *)(*it))->M->xll) : NULL));
569 bt->name = (*it)->name + string(".xll");
570 VarList.push_back(bt);
571 bt = new BBFloat((m->isMem ? &(((BBMatrix *)(*it))->M->yll) : NULL));
572 bt->name = (*it)->name + string(".yll");
573 VarList.push_back(bt);
574 }
575 else if ((*it)->type == BBTyp::PType)
576 {
577 BBTyp *bt;
578 bt = new BBInteger(&(((BBPoint *)(*it))->v.x));
579 bt->name = (*it)->name + string(".x");
580 VarList.push_back(bt);
581 bt = new BBInteger(&(((BBPoint *)(*it))->v.y));
582 bt->name = (*it)->name + string(".y");
583 VarList.push_back(bt);
584 }
585 }
586 /* }
587 else
588 {
589 // Alle anderen Matirx/Point-Variablen hinzu
590 T_VarList::iterator it;
591 for (it = VarList.begin(); it != VarList.end(); it++)
592 {
593 if ((*it)->type == BBTyp::MType)
594 {
595 BBTyp *bt;
596 bt = new BBInteger();
597 bt->name = (*it)->name + string(".xanz");
598 VarList.push_back(bt);
599 bt = new BBInteger();
600 bt->name = (*it)->name + string(".yanz");
601 VarList.push_back(bt);
602 bt = new BBFloat();
603 bt->name = (*it)->name + string(".dxy");
604 VarList.push_back(bt);
605 bt = new BBFloat();
606 bt->name = (*it)->name + string(".xll");
607 VarList.push_back(bt);
608 bt = new BBFloat();
609 bt->name = (*it)->name + string(".yll");
610 VarList.push_back(bt);
611 }
612 else if ((*it)->type == BBTyp::PType)
613 {
614 BBTyp *bt;
615 bt = new BBInteger();
616 bt->name = (*it)->name + string(".x");
617 VarList.push_back(bt);
618 bt = new BBInteger();
619 bt->name = (*it)->name + string(".y");
620 VarList.push_back(bt);
621 }
622 }
623
624 }
625
626
627 */
628
629 // kommt sp�ter
630 //if (!pointer2matrix)
631 VarList.sort(compare_BB_greater());
632 }
633