source: ThirdParty/mpqc_open/src/lib/util/keyval/keyvalval.cc@ 860145

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 860145 was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 11.3 KB
Line 
1//
2// keyvalval.cc
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUG__
29#pragma implementation
30#endif
31
32#include <string.h>
33#include <ctype.h>
34#include <math.h>
35
36#include <util/keyval/keyvalval.h>
37
38using namespace std;
39using namespace sc;
40
41/////////////////////////////////////////////////////////////////////////
42
43KeyValValue::KeyValValue(const KeyValValue&)
44{
45}
46
47KeyValValue::~KeyValValue()
48{
49}
50
51KeyValValue::KeyValValueError
52KeyValValue::doublevalue(double& val) const
53{
54 KeyValValuedouble def;
55 def.doublevalue(val);
56 return KeyValValue::WrongType;
57}
58
59KeyValValue::KeyValValueError
60KeyValValue::booleanvalue(int& val) const
61{
62 KeyValValueboolean def;
63 def.booleanvalue(val);
64 return KeyValValue::WrongType;
65}
66
67KeyValValue::KeyValValueError
68KeyValValue::floatvalue(float& val) const
69{
70 KeyValValuefloat def;
71 def.floatvalue(val);
72 return KeyValValue::WrongType;
73}
74
75KeyValValue::KeyValValueError
76KeyValValue::charvalue(char& val) const
77{
78 KeyValValuechar def;
79 def.charvalue(val);
80 return KeyValValue::WrongType;
81}
82
83KeyValValue::KeyValValueError
84KeyValValue::intvalue(int& val) const
85{
86 KeyValValueint def;
87 def.intvalue(val);
88 return KeyValValue::WrongType;
89}
90
91KeyValValue::KeyValValueError
92KeyValValue::sizevalue(size_t& val) const
93{
94 KeyValValuesize def;
95 def.sizevalue(val);
96 return KeyValValue::WrongType;
97}
98
99KeyValValue::KeyValValueError
100KeyValValue::pcharvalue(const char*& val) const
101{
102 KeyValValuepchar def;
103 def.pcharvalue(val);
104 return KeyValValue::WrongType;
105}
106
107KeyValValue::KeyValValueError
108KeyValValue::stringvalue(std::string& val) const
109{
110 KeyValValuestring def;
111 def.stringvalue(val);
112 return KeyValValue::WrongType;
113}
114
115KeyValValue::KeyValValueError
116KeyValValue::describedclassvalue(Ref<DescribedClass>& val) const
117{
118 KeyValValueRefDescribedClass def;
119 def.describedclassvalue(val);
120 return KeyValValue::WrongType;
121}
122
123void
124KeyValValue::print(ostream&o) const
125{
126 o << "(empty value)";
127}
128
129ostream&
130sc::operator << (ostream&o, const KeyValValue &val)
131{
132 val.print(o);
133 return o;
134}
135
136/////////////////////////////////////////////////////////////////////////
137
138KeyValValuedouble::KeyValValuedouble(const KeyValValuedouble&val):
139 _val(val._val)
140{
141}
142KeyValValuedouble::~KeyValValuedouble()
143{
144}
145
146KeyValValue::KeyValValueError
147KeyValValuedouble::doublevalue(double&val) const
148{
149 val = _val;
150 return KeyValValue::OK;
151}
152
153void
154KeyValValuedouble::print(ostream&o) const
155{
156 o << _val;
157}
158
159/////////////////////////////////////////////////////////////////////////
160
161KeyValValueboolean::KeyValValueboolean(const KeyValValueboolean&val):
162 _val(val._val)
163{
164}
165KeyValValueboolean::~KeyValValueboolean()
166{
167}
168
169KeyValValue::KeyValValueError
170KeyValValueboolean::booleanvalue(int&val) const
171{
172 val = _val;
173 return KeyValValue::OK;
174}
175
176void
177KeyValValueboolean::print(ostream&o) const
178{
179 o << (_val?"true":"false");
180}
181
182/////////////////////////////////////////////////////////////////////////
183
184KeyValValuefloat::KeyValValuefloat(const KeyValValuefloat&val):
185 _val(val._val)
186{
187}
188KeyValValuefloat::~KeyValValuefloat()
189{
190}
191
192KeyValValue::KeyValValueError
193KeyValValuefloat::floatvalue(float&val) const
194{
195 val = _val;
196 return KeyValValue::OK;
197}
198
199void
200KeyValValuefloat::print(ostream&o) const
201{
202 o << _val;
203}
204
205/////////////////////////////////////////////////////////////////////////
206
207KeyValValuechar::KeyValValuechar(const KeyValValuechar&val):
208 _val(val._val)
209{
210}
211KeyValValuechar::~KeyValValuechar()
212{
213}
214
215KeyValValue::KeyValValueError
216KeyValValuechar::charvalue(char&val) const
217{
218 val = _val;
219 return KeyValValue::OK;
220}
221
222void
223KeyValValuechar::print(ostream&o) const
224{
225 o << _val;
226}
227
228/////////////////////////////////////////////////////////////////////////
229
230KeyValValueint::KeyValValueint(const KeyValValueint&val):
231 _val(val._val)
232{
233}
234KeyValValueint::~KeyValValueint()
235{
236}
237
238KeyValValue::KeyValValueError
239KeyValValueint::intvalue(int&val) const
240{
241 val = _val;
242 return KeyValValue::OK;
243}
244
245void
246KeyValValueint::print(ostream&o) const
247{
248 o << _val;
249}
250
251/////////////////////////////////////////////////////////////////////////
252
253KeyValValuesize::KeyValValuesize(const KeyValValuesize&val):
254 _val(val._val)
255{
256}
257KeyValValuesize::~KeyValValuesize()
258{
259}
260
261KeyValValue::KeyValValueError
262KeyValValuesize::sizevalue(size_t&val) const
263{
264 val = _val;
265 return KeyValValue::OK;
266}
267
268void
269KeyValValuesize::print(ostream&o) const
270{
271 o << _val;
272}
273
274/////////////////////////////////////////////////////////////////////////
275
276KeyValValuepchar::KeyValValuepchar(const char* val):
277 _val(strcpy(new char[strlen(val)+1],val))
278{
279}
280KeyValValuepchar::KeyValValuepchar(const KeyValValuepchar&val):
281 _val(strcpy(new char[strlen(val._val)+1],val._val))
282{
283}
284KeyValValuepchar::~KeyValValuepchar()
285{
286 delete[] _val;
287}
288KeyValValue::KeyValValueError
289KeyValValuepchar::pcharvalue(const char*&val) const
290{
291 val = _val;
292 return KeyValValue::OK;
293}
294KeyValValue::KeyValValueError
295KeyValValuepchar::stringvalue(std::string&val) const
296{
297 val = _val;
298 return KeyValValue::OK;
299}
300
301void
302KeyValValuepchar::print(ostream&o) const
303{
304 if (_val == 0) {
305 o << "(null)";
306 }
307 else {
308 o << _val;
309 }
310}
311
312/////////////////////////////////////////////////////////////////////////
313
314KeyValValuestring::KeyValValuestring(const std::string &val):
315 _val(val)
316{
317}
318KeyValValuestring::KeyValValuestring(const KeyValValuestring&val):
319 _val(val._val)
320{
321}
322KeyValValuestring::~KeyValValuestring()
323{
324}
325KeyValValue::KeyValValueError
326KeyValValuestring::pcharvalue(const char*&val) const
327{
328 val = _val.c_str();
329 return KeyValValue::OK;
330}
331KeyValValue::KeyValValueError
332KeyValValuestring::stringvalue(std::string&val) const
333{
334 val = _val;
335 return KeyValValue::OK;
336}
337
338void
339KeyValValuestring::print(ostream&o) const
340{
341 o << _val;
342}
343
344/////////////////////////////////////////////////////////////////////////
345
346KeyValValueRefDescribedClass::
347 KeyValValueRefDescribedClass(const KeyValValueRefDescribedClass& val):
348 _val(val._val)
349{
350}
351KeyValValueRefDescribedClass::
352 ~KeyValValueRefDescribedClass()
353{
354}
355KeyValValue::KeyValValueError
356KeyValValueRefDescribedClass::describedclassvalue(Ref<DescribedClass>&val) const
357{
358 val = _val;
359 return KeyValValue::OK;
360}
361
362void
363KeyValValueRefDescribedClass::print(ostream&o) const
364{
365 if (_val.nonnull()) {
366 o << "<" << _val->class_name()
367 << " " << _val->identifier()
368 << ">";
369 }
370 else {
371 o << "<empty object>";
372 }
373}
374
375/////////////////////////////////////////////////////////////////////////
376
377KeyValValueString::KeyValValueString(
378 const char* val, KeyValValueString::Storage s)
379{
380 switch (s) {
381 case Copy:
382 _val_to_delete = strcpy(new char[strlen(val)+1], val);
383 _val = _val_to_delete;
384 break;
385 case Steal:
386 ExEnv::errn() << "KeyValValueString: CTOR: cannot steal const string" << endl;
387 abort();
388 break;
389 case Use:
390 _val = val;
391 _val_to_delete = 0;
392 break;
393 }
394}
395KeyValValueString::KeyValValueString(
396 char* val, KeyValValueString::Storage s)
397{
398 switch (s) {
399 case Copy:
400 _val_to_delete = strcpy(new char[strlen(val)+1], val);
401 _val = _val_to_delete;
402 break;
403 case Steal:
404 _val = val;
405 _val_to_delete = val;
406 break;
407 case Use:
408 _val = val;
409 _val_to_delete = 0;
410 break;
411 }
412}
413KeyValValueString::KeyValValueString(const KeyValValueString&val)
414{
415 if (val._val_to_delete == 0) {
416 _val = val._val;
417 _val_to_delete = 0;
418 }
419 else {
420 _val_to_delete = strcpy(new char[strlen(val._val)+1], val._val);
421 _val = _val_to_delete;
422 }
423}
424KeyValValueString::~KeyValValueString()
425{
426 delete[] _val_to_delete;
427}
428KeyValValue::KeyValValueError
429KeyValValueString::doublevalue(double&val) const
430{
431 val = atof(_val);
432 return KeyValValue::OK;
433}
434KeyValValue::KeyValValueError
435KeyValValueString::booleanvalue(int&val) const
436{
437 char lc_kv[20];
438 strncpy(lc_kv,_val,20);
439 for (int i=0; i<20; i++) {
440 if (isupper(lc_kv[i])) lc_kv[i] = tolower(lc_kv[i]);
441 }
442 if (!strcmp(lc_kv,"yes")) val = 1;
443 else if (!strcmp(lc_kv,"true")) val = 1;
444 else if (!strcmp(lc_kv,"1")) val = 1;
445 else if (!strcmp(lc_kv,"no")) val = 0;
446 else if (!strcmp(lc_kv,"false")) val = 0;
447 else if (!strcmp(lc_kv,"0")) val = 0;
448 else {
449 val = 0;
450 return KeyValValue::WrongType;
451 }
452
453 return KeyValValue::OK;
454}
455KeyValValue::KeyValValueError
456KeyValValueString::floatvalue(float&val) const
457{
458 val = (float) atof(_val);
459 return KeyValValue::OK;
460}
461KeyValValue::KeyValValueError
462KeyValValueString::charvalue(char&val) const
463{
464 val = _val[0];
465 return KeyValValue::OK;
466}
467KeyValValue::KeyValValueError
468KeyValValueString::intvalue(int&val) const
469{
470 val = atoi(_val);
471 return KeyValValue::OK;
472}
473KeyValValue::KeyValValueError
474KeyValValueString::sizevalue(size_t&val) const
475{
476 int n = ::strlen(_val);
477 int gotdigitspace = 0;
478 int gotdigit = 0;
479 int gotdecimal = 0;
480 int denom = 1;
481 double dval = 0;
482 for (int i=0; i<n; i++) {
483 if (isdigit(_val[i]) && !gotdigitspace) {
484 char tmp[2]; tmp[0] = _val[i]; tmp[1] = '\0';
485 dval = dval * 10 + atoi(tmp);
486 gotdigit = 1;
487 if (gotdecimal) denom *= 10;
488 }
489 else if (_val[i] == '.' && !gotdigitspace && !gotdecimal) {
490 gotdecimal = 1;
491 }
492 else if (_val[i] == ' ') {
493 if (gotdigit) gotdigitspace = 1;
494 }
495 else if (strcmp(&_val[i],"KIB") == 0
496 || strcmp(&_val[i],"KiB") == 0) {
497 dval *= 1024;
498 i+=2;
499 }
500 else if (strcmp(&_val[i],"MIB") == 0
501 || strcmp(&_val[i],"MiB") == 0) {
502 dval *= 1048576;
503 i+=2;
504 }
505 else if (strcmp(&_val[i],"GIB") == 0
506 ||strcmp(&_val[i],"GiB") == 0) {
507 dval *= 1073741824;
508 i+=2;
509 }
510 else if (strcmp(&_val[i],"KB") == 0) {
511 dval *= 1000;
512 i++;
513 }
514 else if (strcmp(&_val[i],"MB") == 0) {
515 dval *= 1000000;
516 i++;
517 }
518 else if (strcmp(&_val[i],"GB") == 0) {
519 dval *= 1000000000;
520 i++;
521 }
522 else {
523 val = 0;
524 return KeyValValue::WrongType;
525 }
526 }
527 dval /= denom;
528 val = size_t(dval);
529 return KeyValValue::OK;
530}
531KeyValValue::KeyValValueError
532KeyValValueString::pcharvalue(const char*&val) const
533{
534 val = _val;
535 return KeyValValue::OK;
536}
537KeyValValue::KeyValValueError
538KeyValValueString::stringvalue(std::string&val) const
539{
540 val = _val;
541 return KeyValValue::OK;
542}
543
544void
545KeyValValueString::print(ostream&o) const
546{
547 if (_val) o << _val;
548 else o << "(empty value)";
549}
550
551/////////////////////////////////////////////////////////////////////////////
552
553// Local Variables:
554// mode: c++
555// c-file-style: "CLJ"
556// End:
Note: See TracBrowser for help on using the repository browser.