Parameters.cc 17.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
14
15
16
17
#include <fstream>
#include <sstream>
#include <algorithm>
#include <stdarg.h>
#include <time.h>
Thomas Witkowski's avatar
Thomas Witkowski committed
18
#include <cstring>
19
20
#include <sys/types.h>
#include <sys/stat.h>
21
22

#ifndef WIN32
23
#include <unistd.h>
24
25
#endif

Thomas Witkowski's avatar
Thomas Witkowski committed
26
27
#include "Parameters.h"
#include "Serializer.h"
28
29
30

namespace AMDiS {

31
32
  using namespace std;

33
  const char Parameters::comment='%';
34
  const string Parameters::separators=",; ";
35
36
37
  Parameters* Parameters::singlett = NULL;


38
  int Parameters::getGlobalParameter(int info, const string& key, 
39
40
				     const char *format, ...)
  {
41
42
    FUNCNAME("Parameters::getGlobalParameter()");

43
    vector<struct Parameters::param>::iterator i;
44
45
    param tParam(key);
    va_list arg;
46
47
48

    Parameters::initIntern();

49
50
    if (Parameters::singlett->paramInfo) {
      if (Parameters::singlett->paramInfo > 1)
51
	info = max(info, Parameters::singlett->paramInfo - 1);
52
    } else {
53
      info = 0;
54
    }
55

56
    if (0 == Parameters::singlett->allParam.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
57
      if (0 == Parameters::singlett->filename.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
58
	INFO(info, 1)("no parameters defined\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
59
	return 0;
60
61
      } else {
	Parameters::singlett->read(Parameters::singlett->filename,key);
62
      }
63
    }
64

65
    i = find(Parameters::singlett->allParam.begin(),
66
		  Parameters::singlett->allParam.end(), tParam);
67

68
    if (i == Parameters::singlett->allParam.end()) {
69
      INFO(info, 1)("initialization of parameter `%s' fails\n", key.data());     
70
   
Thomas Witkowski's avatar
Thomas Witkowski committed
71
      return 0;
72
    }
73
74

    va_start(arg, format);
Thomas Witkowski's avatar
Thomas Witkowski committed
75
    int count = 0;
76
  
Thomas Witkowski's avatar
Thomas Witkowski committed
77
    INFO(info, 2)("parameter `%s' initialized with: ", key.data());
78

79
    string s = (*i).parameters;
80
 
Thomas Witkowski's avatar
Thomas Witkowski committed
81
    for (const char *p = format; *p; p++) {
82
83
      if (*p != '%')  
	continue;
84

Thomas Witkowski's avatar
Thomas Witkowski committed
85
      const char *word = Parameters::singlett->getNextWord(&s);
86
      if (!*word) {
Thomas Witkowski's avatar
Thomas Witkowski committed
87
	PRINT_INFO(info, 2)("\n");
88
	va_end(arg);
Thomas Witkowski's avatar
Thomas Witkowski committed
89
	return count;
90
      }
91
92
93
94
      count++;

      switch(*++p) {
      case 'S':
Thomas Witkowski's avatar
Thomas Witkowski committed
95
96
97
98
99
	{
	  char *Sval = va_arg(arg, char *);
	  *Sval = static_cast<char>(atoi(word));
	  INFO(info, 2)("%d ", *Sval);
	}
100
101
	break;
      case 'U':
Thomas Witkowski's avatar
Thomas Witkowski committed
102
103
104
105
106
	{
	  unsigned char *Uval = va_arg(arg, unsigned char *);
	  *Uval = static_cast<unsigned char>(atoi(word));
	  INFO(info, 2)("%d ", *Uval);
	}
107
108
	break;
      case 'c':
Thomas Witkowski's avatar
Thomas Witkowski committed
109
110
111
112
113
	{
	  char *cval = va_arg(arg, char *); 
	  *cval = *word;
	  INFO(info, 2)("%c ", *cval);
	}
114
115
	break;
      case 's':
Thomas Witkowski's avatar
Thomas Witkowski committed
116
117
118
119
120
	{
	  char *sval = va_arg(arg, char *);
	  strcpy(sval, word);
	  INFO(info, 2)("%s ", sval);
	}
121
122
	break;
      case 'd':
Thomas Witkowski's avatar
Thomas Witkowski committed
123
124
125
126
127
	{
	  int *ival = va_arg(arg, int *);
	  *ival = atoi(word);
	  INFO(info, 2)("%d ", *ival);
	}
128
129
130
131
	break;
      case 'e':
      case 'f':
      case 'g':
Thomas Witkowski's avatar
Thomas Witkowski committed
132
133
134
135
136
	{
	  double *rval = va_arg(arg, double *);
	  *rval = atof(word);
	  INFO(info, 2)("%g ", *rval);
	}
137
	break;
138
139
140
      case 'v':	
	{
	  WorldVector<double> *vecVal = va_arg(arg, WorldVector<double> *);
141
	  string sstr = string(word);
142
143
	  int ind = 0;
	  int found = sstr.find_first_of(",; ");
144
	  string seperator;
145
146
	  seperator = sstr[found];
	  
147
	  while (found != static_cast<int>(string::npos) && 
148
149
150
151
152
153
154
155
156
157
158
159
		 ind < static_cast<int>(vecVal->size())) {
	    if (found > 0) {
	      (*vecVal)[ind] = atof(sstr.substr(0, found).c_str());
	      ind++;
	    }
	    sstr = sstr.substr(found + 1);
	    found = sstr.find_first_of(seperator);
	  }
	  if (sstr.length() > 0 && ind < static_cast<int>(vecVal->size()))
	    (*vecVal)[ind] = atof(sstr.c_str());	
	}
	break;
160
161
162
163
      case 'E': // list of double values, example parameter: {a,b,c,d,e}
      case 'F':
      case 'G':
	{
164
165
	  vector<double> *vecVal = va_arg(arg, vector<double> *);
	  string sstr = string(word);
166

167
       string brackets1 = "[{(", brackets2 = "]})";
168
169
170
171
172
173
174
175
	  int bracketIdx1 = sstr.find_first_of(brackets1);
       int bracket = brackets1.find_first_of(sstr[bracketIdx1]);
       int bracketIdx2 = sstr.find_first_of(brackets2[bracket]);
       TEST_EXIT(bracket>=0 && bracketIdx1>=0 && bracketIdx2>=0 && bracketIdx1<bracketIdx2)
		("no enclosing brackets found in '%s' \n",key.data());
       sstr = sstr.substr(bracketIdx1+1, bracketIdx2-bracketIdx1);

	  int found = sstr.find_first_of(separators);
176
	  string seperator;
177
178
	  seperator = sstr[found];
	  
179
	  while (found != static_cast<int>(string::npos)) {
180
181
182
183
184
185
186
187
	    if (found > 0) {
	      vecVal->push_back(atof(sstr.substr(0, found).c_str()));
	    }
	    sstr = sstr.substr(found + 1);
	    found = sstr.find_first_of(seperator);
	  }
	  if (sstr.length() > 0)
	    vecVal->push_back(atof(sstr.c_str()));
188
189
	  if (vecVal->size() == 0)
	    WARNING("no values in parameter vector!\n");
190
191
192
193
	}
	break;
      case 'D': // list of int values, example parameter: {a,b,c,d,e}
	{
194
195
	  vector<int> *vecVal = va_arg(arg, vector<int> *);
	  string sstr = string(word);
196

197
       string brackets1 = "[{(", brackets2 = "]})";
198
199
200
201
202
203
204
205
	  int bracketIdx1 = sstr.find_first_of(brackets1);
       int bracket = brackets1.find_first_of(sstr[bracketIdx1]);
       int bracketIdx2 = sstr.find_first_of(brackets2[bracket]);
       TEST_EXIT(bracket>=0 && bracketIdx1>=0 && bracketIdx2>=0 && bracketIdx1<bracketIdx2)
		("no enclosing brackets found in '%s' \n",key.data());
       sstr = sstr.substr(bracketIdx1+1, bracketIdx2-bracketIdx1);

	  int found = sstr.find_first_of(separators);
206
	  string seperator;
207
208
	  seperator = sstr[found];
	  
209
	  while (found != static_cast<int>(string::npos)) {
210
211
212
213
214
215
216
217
	    if (found > 0) {
	      vecVal->push_back(atoi(sstr.substr(0, found).c_str()));
	    }
	    sstr = sstr.substr(found + 1);
	    found = sstr.find_first_of(seperator);
	  }
	  if (sstr.length() > 0)
	    vecVal->push_back(atoi(sstr.c_str()));
218
       if (vecVal->size() == 0)
219
220
221
         WARNING("no values in parameter vector!\n");
	}
	break;
222
223
224
      case '*':
	break;
      default: 
Thomas Witkowski's avatar
Thomas Witkowski committed
225
226
	INFO(info, 2)("\n");
	INFO(info, 2)
227
228
	  ("unknow format specifier `%%%c', skipping initialization of %s\n",
	   *p, key.data());
229
      }
230
231
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
232
    INFO(info, 2)("\n");
233

234
    va_end(arg);
235

236
    return count;
237
238
  }

239

240
241
  void Parameters::read(const string& aFilename, 
			const string& maxKey)
242
243
244
  {
    FUNCNAME("Parameters::read()");

245
246
    char line[256];
    int nLine = 0;
247
248
    string key, parameter;
    string actfile;
249
250
251
252

    if (aFilename.size() == 0)
      return;

253
    singlett->inputFile.open(aFilename.c_str(), ios::in);
254
255
256
257
258
    if (!inputFile.is_open()) {
      ERROR("init-file can't be opened\n");
      return;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
259
    INFO(paramInfo, 2)("reading from file %s\n", aFilename.data());
260
261
    actfile = getActFile(aFilename);
    
262
263
264
265
266
267
268
269
270
271
272
273
274
    while (!inputFile.eof()) {
      inputFile.getline(line, 255);
      nLine++;

      key = getKey(line, nLine, aFilename);
      
      if (key == "")
	continue;

      parameter = getPar(key, line, &nLine, aFilename);
      if (parameter == "")  
	continue;
      
275
      addParam(key, parameter);
276
277
278
279
280
    }

    singlett->inputFile.close();
  }

281

282
  const string& Parameters::getActFile(const string& aFilename)
283
  {
284
285
    FUNCNAME("Parameters::getActFile()");

286
    list< string >::iterator i;
287
    for (i = filenames.begin(); i != filenames.end(); i++)
288
289
      if (aFilename==*i) 
	break;
290
  
291
    if (i != filenames.end()) {
292
      return(const_cast<const string&>(*i));
293
294
    } else {
      filenames.push_back(aFilename);
295
      return(const_cast<const string&>(aFilename));
296
    }
297
298
  }

299

300
301
302
303
304
305
306
  void Parameters::swap(int i, int j)
  {
    param tmp(allParam[i]);
    allParam[i] = allParam[j];
    allParam[j] = tmp;
  }

307

308
309
  void Parameters::qsort(int left, int right)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
310
311
    if (left >= right) 
      return;
312

313
314
315
    swap(left, (left + right) / 2);
    int last = left;
    for (int i = left + 1; i <= right; i++)
316
317
318
319
      if (allParam[i].key.compare(allParam[left].key) < 0)
	swap(++last, i);

    swap(left, last);
320
321
    qsort(left, last - 1);
    qsort(last + 1, right);
322
323
324
  }


325
326
  const string Parameters::getKey(const string& s, int nLine, 
					 const string& aFilename)
327
  {
328
329
    FUNCNAME("Parameters::getKey()");

330
    string fn, key = "";
331
332
    char c;
    int i, epos;
333
    string h = " \t\r\f";
Thomas Witkowski's avatar
Thomas Witkowski committed
334
    int pos = s.find_first_not_of(" \t\f\r"); //skip Blank
335
  
336
    if (pos < 0)
337
338
      return key;

Thomas Witkowski's avatar
Thomas Witkowski committed
339
    if (s[pos] == comment || s[pos] == '\0' || s[pos] == '\n')
340
341
      return(key);

342
    if (s[pos] == '#') {
Praetorius, Simon's avatar
Praetorius, Simon committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//       if (static_cast<int>(s.find("#include")) == pos) {
// 	/****************************************************************************/
// 	/*  another init file has to be included                                    */
// 	/****************************************************************************/
// 
// 	pos += strlen("#include");
// 	pos = s.find_first_not_of(" \t\f\r");
// 
// 	i = 0;
// 	switch (c = s[pos++]) {
// 	case '<':
// 	  c = '>';
// 	case '\"':
// 	  h += c;
// 	  epos = s.find_first_not_of(h, pos);
// 	  fn = s.substr(pos,epos-1);
//        
// 	  if (s[epos] != c) {
// 	    ERROR("aFilename of include not terminated by %c\n", c);
// 	    ERROR("skipping line %d of file %s\n", nLine, aFilename.c_str());
// 	    return("");
// 	  }
// 	  break;
// 	default:
// 	  ERROR("no aFilename of include file found\n");
// 	  ERROR("skipping line %d of file %s\n", nLine, aFilename.c_str());
// 	  return("");
// 	}
// 
// 	read(fn);
// 	return("");
//       } else {
// 	ERROR("# must not be the first character on a line; except #include\n");
// 	return("");
//       }
378
    }
379
380
381
382
383
384

    /****************************************************************************/
    /*  now get the key                                                         */
    /****************************************************************************/
    i = 0;
    epos=s.find_first_of(":#",pos+1);
385
386
387
388
389
    if (s[epos] == '#') {
      ERROR("key must not contain '%c'.\n", '#');
      ERROR("Skipping line %d of file %s\n", nLine, aFilename.c_str());
      return("");
    }
390
  
391
    key = s.substr(pos,epos);
392

393
394
395
396
397
398
399
400
401
402
403
    if (s[epos] != ':') {
      ERROR("key was not terminated by ':'.\n");
      ERROR("Skipping line %d of file %s\n", nLine, aFilename.c_str());
      return("");
    }
    
    if (key.size() == 0) {
      ERROR("use of ':' without key.\n");
      ERROR("Skipping line %d of file %s\n", nLine, aFilename.c_str());
      return("");
    }
404

Thomas Witkowski's avatar
Thomas Witkowski committed
405
    return key;
406
407
  }

408
409
410
411

  const string Parameters::getPar(const string& key, 
				       const string& si, int *nl, 
				       const string& fn)
412
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
413
414
415
    FUNCNAME("Parameters::getPar()");

    int ol = *nl;
416
417
418
    string inp;
    string s = si;
    string parameter="";
419

Thomas Witkowski's avatar
Thomas Witkowski committed
420
421
    int pos = s.find(':');
    pos = s.find_first_not_of(" \r\f\t",pos+1);
422
  
Thomas Witkowski's avatar
Thomas Witkowski committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    int i = pos;
    while(i < static_cast<int>(s.length()) && 
	  s[i] != '#' && s[i] != '%' && s[i] != '\n') {
      if (s[i] == '\\'  && i+1 < static_cast<int>(s.length()) &&  s[i + 1] == '\n') {
	s = s.substr(0, i);
	(*nl)++;
	inputFile >> inp;
	s+=inp;
	if (inputFile.eof()) {
	  ERROR("EOF reached while reading parameters of key %s\n", key.c_str());
	  if (ol == *nl-1) 
	    ERROR("Skipping line %d of file %s\n", *nl, fn.c_str());
	  else     
	    ERROR("Skipping lines %d-%d of file %s\n", ol, *nl, fn.c_str());
437
	    
Thomas Witkowski's avatar
Thomas Witkowski committed
438
	  return("");
439
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
440
441
442
443
      } else {
	if (isBlankChar(static_cast<char>(s[i]))) {
	  parameter+= ' ';
	  i=pos=s.find_first_not_of(" \r\t\f", i);
444
	  if (i == static_cast<int>(string::npos)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
445
446
447
448
449
	    i = parameter.length()-1;
	    break;
	  }
	} else {
	  parameter += s.substr(i++,1);
450
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
451
      }
452
453
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
454
455
    i = parameter.find_last_not_of(" \r\t\f");
    parameter.resize(i + 1);
456

Thomas Witkowski's avatar
Thomas Witkowski committed
457
458
459
460
461
462
    if (i == 0 && isBlankChar(parameter[0])) {
      ERROR("no parameter of key %s.\n", key.c_str());
      if (ol == *nl) 
	ERROR("Skipping line %d of file %s\n", *nl, fn.c_str());
      else   
	ERROR("Skipping lines %d-%d of file %s\n", ol, *nl, fn.c_str());
463

Thomas Witkowski's avatar
Thomas Witkowski committed
464
465
      return("");
    }
466

467
    return parameter;
468
469
  }

470
471
472

  void Parameters::addParam(const string& key, 
			    const string& parameter)
473
474
  {
    FUNCNAME("Parameters::addParam()");
Thomas Witkowski's avatar
Thomas Witkowski committed
475

476
477
478
479
480
481
482
    unsigned size_k, size_p;
    int scmp = 0;
    param newPar;

    size_k = key.size()+1;
    size_p = parameter.size()+1;

483
    vector<param>::iterator it;
Thomas Witkowski's avatar
Thomas Witkowski committed
484
485
486
    for (it = allParam.begin(); it != allParam.end(); it++)
      if ((scmp = key.compare((*it).key)) >=0)  
	break;  
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505

    if (it != allParam.end() && scmp == 0) {
      /****************************************************************************/
      /*  key does already exist: save new parameters                             */
      /****************************************************************************/
      (*it).parameters = parameter;
      
      return;
    }

    newPar.key = key;
    newPar.parameters = parameter;
    allParam.insert(it, newPar);
  }



  void Parameters::initIntern()
  {
506
507
    if (singlett == NULL)
      singlett = new Parameters;
508
509
510
511
512
513
514
515
  }

  void Parameters::printParameters()
  {
    FUNCNAME("Parameters::printParameters()");

    initIntern();

516
517
    for (vector<param>::iterator it = singlett->allParam.begin(); 
	 it != singlett->allParam.end(); it++)
518
519
520
521
      MSG("%s:  %s\n", (*it).key.data(), (*it).parameters.data());
  }


522
  void Parameters::init(int p, string fn, const char *flags)
523
524
525
  {
    FUNCNAME("Parameters::init()");
  
526
527
528
529
530
531
532
533
534
    ostringstream tmp_file;
    ofstream cpp_flags;
    ofstream call_cpp;
    string file;
    ofstream outFile;
    string lfn = fn;
    string::size_type fpos= lfn.find('/');

    if (fpos == string::npos)
535
      fpos = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
536
    else
537
      ++fpos;
Thomas Witkowski's avatar
Thomas Witkowski committed
538
    
539
540
    lfn.insert(fpos,".#");

541
542
543
#ifdef WIN32
    int val(0);
#else
544
545
    struct stat buf;

Thomas Witkowski's avatar
Thomas Witkowski committed
546
547
548
    int val = lstat(lfn.c_str(), &buf);
    if (val == 0)
      if (buf.st_mode&S_IFLNK && buf.st_size > 0)
549
550
	ERROR_EXIT("Unsaved version of init file exists\n");

551
#endif
552
553
    initIntern();
    if (0 == fn.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
554
      Global::init();
555
556
557
558
559
560
      return;
    }

    if (NULL == flags) {
      singlett->read(fn);
    } else {
561
      singlett->inputFile.open(fn.data(), ios::in);
Thomas Witkowski's avatar
Thomas Witkowski committed
562
      if (!(singlett->inputFile.rdstate())) {
563
564
565
566
	ERROR("can not read from file %s\n", fn.data());
	return;
      }
      singlett->inputFile.close();
Thomas Witkowski's avatar
Thomas Witkowski committed
567

Thomas Witkowski's avatar
Thomas Witkowski committed
568
569
570
571
      ERROR("no cpp available; reading file %s without help of cpp\n", fn.c_str());
      init(p, fn);
      
      return;
572
573
574
575
    } 

    val = 10; // default for level of information
    getGlobalParameter(0, "level of information", "%d", &val);
576
    singlett->msgInfo = val;
577
578
579

    val = 1; // default for WAIT
    getGlobalParameter(0, "WAIT", "%d", &val);
580
    singlett->msgWait = val;
581
582
583
584
585
586
587
588

    val = 1; // default for parameter information
    getGlobalParameter(0, "parameter information", "%d", &val);
    singlett->paramInfo = val;

    if (!singlett->msgInfo)  
      singlett->paramInfo = 0;
  
Thomas Witkowski's avatar
Thomas Witkowski committed
589
    if (p && singlett->msgInfo) 
590
591
      printParameters();
  
Thomas Witkowski's avatar
Thomas Witkowski committed
592
    Global::init();
593
594
  } 

595

596
597
598
599
  void Parameters::readArgv(int argc, char **argv)
  {
    FUNCNAME("Parameters::readArgv()");

600
601
    for (int i = 0; i < argc; i++)
      if (strcmp("-rs", argv[i]) == 0)
602
	addGlobalParameter(0, "argv->rs", argv[i + 1]);
603
604
605
  }


606
  int Parameters::binSearch(const string& key, int n_keys)
607
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
608
609
    int left = 0;
    int right = n_keys - 1;
610
611
  
    while (left <= right) {
Thomas Witkowski's avatar
Thomas Witkowski committed
612
613
614
      int mid = (left + right) / 2;
      int cond = allParam[mid].key.compare(key);
      if (cond  < 0) {
615
616
617
618
	left = mid + 1;
      } else if (cond > 0) {
	right = mid - 1;
      }	else {
Thomas Witkowski's avatar
Thomas Witkowski committed
619
	return mid;
620
621
622
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
623
    return -1;
624
625
626
  }

 
627
628
629
  void Parameters::addGlobalParameter(int p, 
				      const string key, 
				      const string par)
630
631
632
633
634
  {
    FUNCNAME("Parameters::addGlobalParameter()");

    initIntern();

Thomas Witkowski's avatar
Thomas Witkowski committed
635
    if (key.size() == 0 || par.size() == 0)
636
637
      return;

638
    singlett->addParam(key, par);
639

Thomas Witkowski's avatar
Thomas Witkowski committed
640
    int val = 10; // defualt for level of information
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    getGlobalParameter(0, "level of information", "%d", &val);
    singlett->msgInfo = val;

    val = 1; // default for WAIT
    getGlobalParameter(0, "WAIT", "%d", &val);
    singlett->msgWait = val;

    val = 1; // default for parameter information
    getGlobalParameter(0, "parameter information", "%d", &val);
    singlett->paramInfo = val;

    if (!singlett->msgInfo)  
      singlett->paramInfo = 0;
  
    if (p && singlett->msgInfo) 
      printParameters();  
  }  


660
  const char *Parameters::getNextWord(string *s) const
661
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
662
    static char Val[512];
663
  
Thomas Witkowski's avatar
Thomas Witkowski committed
664
    int wb1 = s->find_first_not_of(" "); 
665
    if (wb1 == static_cast<int>(string::npos))
Thomas Witkowski's avatar
Thomas Witkowski committed
666
      return NULL;    
667

Thomas Witkowski's avatar
Thomas Witkowski committed
668
    int wb2 = s->find_first_of(" ");
669
    if (wb2 == static_cast<int>(string::npos))
670
      wb2 = s->length();
Thomas Witkowski's avatar
Thomas Witkowski committed
671
672
    
    s->copy(Val, wb2 - wb1, wb1);
673

Thomas Witkowski's avatar
Thomas Witkowski committed
674
    Val[wb2 - wb1] = '\0';
675

Thomas Witkowski's avatar
Thomas Witkowski committed
676
    return Val;
677
678
679
  }


680
681
  void Parameters::save(const string file, int info)
  {
682
683
    initIntern();

684
685
    ofstream fp;
    fp.open(file.data(), ios::out);
Thomas Witkowski's avatar
Thomas Witkowski committed
686
    if (0 != fp.rdstate())
687
688
      return;

689
690
691
    for (vector<param>::iterator it = singlett->allParam.begin(); 
	 it !=singlett->allParam.end(); it++)
      fp << (*it).key <<  ":  " << (*it).parameters << endl;
692
693
694
    fp.close();
  }

695

696
697
  void Parameters::clear() 
  {
698
699
    if (singlett != NULL)
      delete singlett;
700
701
  }

702
703

  void Parameters::serialize(ostream &out)
Thomas Witkowski's avatar
Thomas Witkowski committed
704
705
706
707
708
709
710
711
712
713
  {
    SerUtil::serialize(out, paramInfo);
    SerUtil::serialize(out, msgInfo);
    SerUtil::serialize(out, msgWait);
    int size = static_cast<int>(allParam.size());
    SerUtil::serialize(out, size);
    for (int i = 0; i < size; i++)
      allParam[i].serialize(out);
  }

714
715

  void Parameters::deserialize(istream &in)
Thomas Witkowski's avatar
Thomas Witkowski committed
716
717
718
719
720
721
722
723
724
725
726
727
728
  {
    SerUtil::deserialize(in, paramInfo);
    SerUtil::deserialize(in, msgInfo);
    SerUtil::deserialize(in, msgWait);
    int size;
    SerUtil::deserialize(in, size);
    allParam.resize(size);
    for (int i = 0; i < size; i++)
      allParam[i].deserialize(in);
  }

  int Parameters::param::operator==(const param& aParam) const
  { 
729
730
731
    return key == aParam.key;
  }

732
733

  void Parameters::param::serialize(ostream &out)
Thomas Witkowski's avatar
Thomas Witkowski committed
734
  {
735
736
    out << key << endl;
    out << parameters << endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
737
  }
738

Thomas Witkowski's avatar
Thomas Witkowski committed
739
  
740
  void Parameters::param::deserialize(istream &in)
Thomas Witkowski's avatar
Thomas Witkowski committed
741
742
743
744
745
  {
    in >> key; in.get();
    in >> parameters; in.get();
  }

746
}