source: ThirdParty/mpqc_open/src/lib/util/state/state_text.cc@ 398fcd

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests 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_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 398fcd 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: 13.7 KB
Line 
1//
2// state_text.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 __GNUC__
29#pragma implementation
30#endif
31
32#include <stdarg.h>
33#include <stdio.h>
34
35#include <util/state/state_text.h>
36
37using namespace std;
38using namespace sc;
39
40static ClassDesc StateOutText_cd(
41 typeid(StateOutText),"StateOutText",1,"public StateOutFile");
42
43StateOutText::StateOutText() :
44 StateOutFile(),
45 no_newline_(0),
46 no_array_(0)
47{
48}
49
50StateOutText::StateOutText(ostream&s) :
51 StateOutFile(s),
52 no_newline_(0),
53 no_array_(0)
54{
55}
56
57StateOutText::StateOutText(const char *path) :
58 StateOutFile(path),
59 no_newline_(0),
60 no_array_(0)
61{
62}
63
64StateOutText::~StateOutText()
65{
66}
67
68static ClassDesc StateInText_cd(typeid(StateInText),
69 "StateInText",1,"public StateInFile",
70 0, create<StateInText>);
71
72StateInText::StateInText() :
73 StateInFile(),
74 newlines_(0),
75 no_newline_(0),
76 no_array_(0)
77{
78}
79
80StateInText::StateInText(istream& s) :
81 StateInFile(s),
82 newlines_(0),
83 no_newline_(0),
84 no_array_(0)
85{
86}
87
88StateInText::StateInText(const char *path) :
89 StateInFile(path),
90 newlines_(0),
91 no_newline_(0),
92 no_array_(0)
93{
94}
95
96StateInText::StateInText(const Ref<KeyVal> &keyval):
97 newlines_(0),
98 no_newline_(0),
99 no_array_(0)
100{
101 char *path = keyval->pcharvalue("file");
102 if (!path) {
103 ExEnv::errn() << "StateInText(const Ref<KeyVal>&): no path given" << endl;
104 }
105 open(path);
106 delete[] path;
107}
108
109StateInText::~StateInText()
110{
111}
112
113///////////////////////////////////////////////////////////////////////
114
115// no_newline() and its associated no_newline_ variable
116// are used to omit the next newline generated by newline() in
117// the input/output stream
118void
119StateOutText::no_newline()
120{
121 no_newline_ = 1;
122}
123void
124StateOutText::no_array()
125{
126 no_array_ = 1;
127}
128void
129StateInText::no_newline()
130{
131 no_newline_ = 1;
132}
133void
134StateInText::no_array()
135{
136 no_array_ = 1;
137}
138
139///////////////////////////////////////////////////////////////////////
140
141int
142StateInText::read(char*s)
143{
144 istream in(buf_);
145 in >> s;
146 if (in.fail()) {
147 ExEnv::errn() << "StateInText::read(char*): failed" << endl;
148 abort();
149 }
150 return strlen(s)+1;
151}
152
153int
154StateInText::read(unsigned int&i)
155{
156 istream in(buf_);
157 in >> i;
158 if (in.fail()) {
159 ExEnv::errn() << "StateInText::read(unsigned int&): failed\n" << endl;
160 abort();
161 }
162 return (sizeof(int));
163}
164
165int
166StateInText::read(int&i)
167{
168 istream in(buf_);
169 in >> i;
170 if (in.fail()) {
171 ExEnv::errn() << "StateInText::read(int&): failed\n" << endl;
172 abort();
173 }
174 return (sizeof(int));
175}
176
177int
178StateInText::read(float&f)
179{
180 istream in(buf_);
181 in >> f;
182 if (in.fail()) {
183 ExEnv::errn() << "StateInText::read(float&): failed" << endl;
184 abort();
185 }
186 return sizeof(float);
187}
188
189int
190StateInText::read(double&d)
191{
192 istream in(buf_);
193 in >> d;
194 if (in.fail()) {
195 ExEnv::errn() << "StateInText::read(double&): failed" << endl;
196 abort();
197 }
198 return sizeof(double);
199}
200
201void
202StateInText::abort()
203{
204 ExEnv::errn() << "StateInText aborting at line " << newlines_+1 << " in the input"
205 << endl;
206 ::abort();
207}
208
209
210///////////////////////////////////////////////////////////////////////
211
212int StateOutText::put(const ClassDesc*cd)
213{
214 ostream out(buf_);
215 //
216 // write out parent info
217 if (classidmap_.find((ClassDesc*)cd) == classidmap_.end()) {
218 putparents(cd);
219 out << " version of class " << cd->name()
220 << " is " << cd->version() << endl;
221 out.flush();
222 classidmap_[(ClassDesc*)cd] = nextclassid_++;
223 }
224 out << "object of class " << cd->name() << " being written" << endl;
225 out.flush();
226 return 0;
227 }
228int
229StateOutText::putparents(const ClassDesc*cd)
230{
231 ostream out(buf_);
232 const ParentClasses& parents = cd->parents();
233
234 for (int i=0; i<parents.n(); i++) {
235 // the cast is needed to de-const-ify the class descriptor
236 ClassDesc*tmp = (ClassDesc*) parents[i].classdesc();
237 if (classidmap_.find(tmp) == classidmap_.end()) {
238 putparents(tmp);
239 out << " version of class " << tmp->name()
240 << " is " << tmp->version() << endl;
241 out.flush();
242 classidmap_[tmp] = nextclassid_++;
243 }
244 }
245 return 0;
246}
247int StateInText::get(const ClassDesc**cd)
248{
249 istream in(buf_);
250 const int line_length = 512;
251 char line[line_length];
252
253 // if a list of class descriptors exists then read it in
254
255 in.getline(line,line_length); newlines_++;
256 while (strncmp(line,"object",6)) {
257 char name[line_length];
258 int version;
259 sscanf(line," version of class %s is %d\n",
260 name,
261 &version);
262 ClassDesc* tmp = ClassDesc::name_to_class_desc(name);
263 // save the class descriptor and the version
264 int classid = nextclassid_++;
265 classidmap_[tmp] = classid;
266 StateClassData classdat(version,tmp);
267 classdatamap_[classid] = classdat;
268 in.getline(line,line_length); newlines_++;
269 }
270
271 // get the class name for the object
272 char classname[line_length];
273 sscanf(line,"object of class %s being written\n", classname);
274
275 // convert the class id into the class descriptor
276 *cd = ClassDesc::name_to_class_desc(classname);
277
278 return 0;
279}
280
281int StateOutText::put(char r)
282{
283 no_array();
284 return StateOut::put(r);
285}
286int StateInText::get(char&r, const char *key)
287{
288 no_array();
289 return StateIn::get(r,key);
290}
291
292int StateOutText::put(unsigned int r)
293{
294 no_array();
295 return StateOut::put(r);
296}
297int StateInText::get(unsigned int&r, const char *key)
298{
299 no_array();
300 return StateIn::get(r,key);
301}
302
303int StateOutText::put(int r)
304{
305 no_array();
306 return StateOut::put(r);
307}
308int StateInText::get(int&r, const char *key)
309{
310 no_array();
311 return StateIn::get(r,key);
312}
313
314int StateOutText::put(float r)
315{
316 no_array();
317 return StateOut::put(r);
318}
319int StateInText::get(float&r, const char *key)
320{
321 no_array();
322 return StateIn::get(r,key);
323}
324
325int StateOutText::put(double r)
326{
327 no_array();
328 return StateOut::put(r);
329}
330int StateInText::get(double&r, const char *key)
331{
332 no_array();
333 return StateIn::get(r,key);
334}
335
336int StateOutText::put(const char*d,int n)
337{
338 return StateOut::put(d,n);
339}
340int StateInText::get(char*&r)
341{
342 return StateIn::get(r);
343}
344int StateOutText::put(const unsigned int*d,int n)
345{
346 return StateOut::put(d,n);
347}
348int StateInText::get(unsigned int*&r)
349{
350 return StateIn::get(r);
351}
352int StateOutText::put(const int*d,int n)
353{
354 return StateOut::put(d,n);
355}
356int StateInText::get(int*&r)
357{
358 return StateIn::get(r);
359}
360int StateOutText::put(const float*d,int n)
361{
362 return StateOut::put(d,n);
363}
364int StateInText::get(float*&r)
365{
366 return StateIn::get(r);
367}
368int StateOutText::put(const double*d,int n)
369{
370 return StateOut::put(d,n);
371}
372int StateInText::get(double*&r)
373{
374 return StateIn::get(r);
375}
376
377int StateOutText::putobject(const Ref<SavableState> &p)
378{
379 ostream out(buf_);
380 int r=0;
381 if (p.null()) {
382 out << "reference to null" << endl;
383 out.flush();
384 }
385 else {
386 std::map<Ref<SavableState>,StateOutData>::iterator ind = ps_.find(p);
387 if (ind == ps_.end() || copy_references_) {
388 // object has not been written yet
389 StateOutData dp;
390 dp.num = next_object_number_++;
391 out << "writing object " << dp.num << endl;
392 out.flush();
393 const ClassDesc *cd = p->class_desc();
394 put(cd);
395 out.flush();
396 dp.type = classidmap_[(ClassDesc*)cd];
397 if (!copy_references_) ps_[p] = dp;
398 have_classdesc();
399 p->save_vbase_state(*this);
400 p->save_data_state(*this);
401 }
402 else {
403 out << "reference to object " << ind->second.num << endl;
404 out.flush();
405 }
406 }
407 return r;
408}
409int StateInText::getobject(Ref<SavableState> &p)
410{
411 istream in(buf_);
412 const int line_length = 512;
413 char line[line_length];
414
415 in.getline(line,line_length);
416 newlines_++;
417
418 if (!strcmp("reference to null",line)) {
419 p = 0;
420 }
421 else if (!strncmp("writing",line,7)) {
422 int refnum;
423 sscanf(line,"writing object %d",&refnum);
424 const ClassDesc *cd;
425 get(&cd);
426 have_classdesc();
427 nextobject(refnum);
428 DescribedClass *dc = cd->create(*this);
429 p = dynamic_cast<SavableState*>(dc);
430 }
431 else if (!strncmp("reference",line,9)) {
432 int refnum;
433 sscanf(line,"reference to object %d",&refnum);
434 p = ps_[refnum].ptr;
435 }
436 else {
437 ExEnv::errn() << "StateInText: couldn't find a reference object" << endl;
438 abort();
439 }
440
441 return 0;
442}
443
444void
445StateOutText::start_array()
446{
447 ostream out(buf_);
448 if (!no_array_) { out.put(' '); out.put('<'); }
449}
450void
451StateInText::start_array()
452{
453 istream in(buf_);
454 if (!no_array_) {
455 if (in.get() != ' ' || in.get() != '<') {
456 ExEnv::errn() << "StateInText: expected a \" <\"" << endl;
457 abort();
458 }
459 }
460}
461
462void
463StateOutText::end_array()
464{
465 ostream out(buf_);
466 if (!no_array_) {
467 out.put(' '); out.put('>');
468 }
469 else {
470 no_array_ = 0;
471 }
472}
473void
474StateInText::end_array()
475{
476 istream in(buf_);
477 if (!no_array_) {
478 if (in.get() != ' ' || in.get() != '>') {
479 ExEnv::errn() << "StateInText: expected a \"> \"" << endl;
480 abort();
481 }
482 }
483 else {
484 no_array_ = 0;
485 }
486}
487
488void
489StateOutText::newline()
490{
491 ostream out(buf_);
492 if (no_newline_) {
493 no_newline_ = 0;
494 return;
495 }
496 out << endl;
497 out.flush();
498}
499void
500StateInText::newline()
501{
502 istream in(buf_);
503 if (no_newline_) {
504 no_newline_ = 0;
505 return;
506 }
507 if (in.get() != '\n') {
508 ExEnv::errn() << "StateInText: expected newline" << endl;
509 abort();
510 }
511 newlines_++;
512}
513
514///////////////////////////////////////////////////////////////////////
515
516int StateOutText::putstring(const char*s)
517{
518 int r = 0;
519 if (s) {
520 int size = strlen(s);
521 no_newline(); r += put(size);
522 if (size) {
523 r += put_array_char(s,size);
524 }
525 }
526 else {
527 r += put((int)0);
528 }
529 return r;
530}
531int StateInText::getstring(char*&s)
532{
533 int r = 0;
534 int size;
535 no_newline(); r += get(size);
536 if (size) {
537 s = new char[size+1];
538 s[size] = '\0';
539 if (size) {
540 r += get_array_char(s,size);
541 }
542 }
543 else {
544 s = 0;
545 }
546 return r;
547}
548
549///////////////////////////////////////////////////////////////////////
550
551int StateOutText::put_array_char(const char*d,int size)
552{
553 ostream out(buf_);
554 start_array();
555 int nwrit=size+1;
556 for (int i=0; i<size; i++) { out.put(d[i]); }
557 end_array();
558 newline();
559 return nwrit;
560}
561int StateInText::get_array_char(char*d,int size)
562{
563 istream in(buf_);
564 start_array();
565 int ch;
566 for (int i=0; i<size; i++) {
567 ch = in.get();
568 if (ch == EOF) {
569 ExEnv::errn() << "StateInText::get_array_char: EOF while reading array"
570 << endl;
571 abort();
572 }
573 d[i] = ch;
574 }
575 end_array();
576 newline();
577 return size+1;
578}
579
580int StateOutText::put_array_uint(const unsigned int*d,int size)
581{
582 ostream out(buf_);
583 start_array();
584 int nwrit=0;
585 for (int i=0; i<size; i++) { out << ' ' << d[i]; nwrit++; }
586 out.flush();
587 end_array();
588 newline();
589 return nwrit;
590}
591int StateInText::get_array_uint(unsigned int*d,int size)
592{
593 start_array();
594 int nread,tnread=0;
595 for (int i=0; i<size; i++) {
596 nread=read(d[i]);
597 tnread += nread;
598 }
599 end_array();
600 newline();
601 return tnread;
602}
603
604int StateOutText::put_array_int(const int*d,int size)
605{
606 ostream out(buf_);
607 start_array();
608 int nwrit=0;
609 for (int i=0; i<size; i++) { out << ' ' << d[i]; nwrit++; }
610 out.flush();
611 end_array();
612 newline();
613 return nwrit;
614}
615int StateInText::get_array_int(int*d,int size)
616{
617 start_array();
618 int nread,tnread=0;
619 for (int i=0; i<size; i++) {
620 nread=read(d[i]);
621 tnread += nread;
622 }
623 end_array();
624 newline();
625 return tnread;
626}
627
628int StateOutText::put_array_float(const float*d,int size)
629{
630 ostream out(buf_);
631 start_array();
632 int nwrit=0;
633 for (int i=0; i<size; i++) {
634 out.setf(ios::scientific);
635 out.width(20);
636 out.precision(15);
637 out << ' ' << d[i];
638 nwrit++;
639 }
640 out.flush();
641 end_array();
642 newline();
643 return nwrit;
644}
645int StateInText::get_array_float(float*d,int size)
646{
647 start_array();
648 int nread,tnread=0;
649 for (int i=0; i<size; i++) {
650 nread=read(d[i]);
651 tnread += nread;
652 }
653 end_array();
654 newline();
655 return tnread;
656}
657
658int StateOutText::put_array_double(const double*d,int size)
659{
660 ostream out(buf_);
661 start_array();
662 int nwrit=0;
663 for (int i=0; i<size; i++) {
664 out.setf(ios::scientific);
665 out.width(20);
666 out.precision(15);
667 out << ' ' << d[i];
668 nwrit++;
669 }
670 out.flush();
671 end_array();
672 newline();
673 return nwrit;
674}
675int StateInText::get_array_double(double*d,int size)
676{
677 start_array();
678 int nread,tnread=0;
679 for (int i=0; i<size; i++) {
680 nread=read(d[i]);
681 tnread += nread;
682 }
683 end_array();
684 newline();
685 return tnread;
686}
687
688/////////////////////////////////////////////////////////////////////////////
689
690// Local Variables:
691// mode: c++
692// c-file-style: "CLJ"
693// End:
Note: See TracBrowser for help on using the repository browser.