File: IO.cpp

package info (click to toggle)
objcryst-fox 2022.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,040 kB
  • sloc: cpp: 70,656; xml: 43,909; ansic: 467; python: 170; makefile: 21; sh: 12
file content (500 lines) | stat: -rw-r--r-- 14,150 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*  ObjCryst++ Object-Oriented Crystallographic Library
    (c) 2000-2002 Vincent Favre-Nicolin vincefn@users.sourceforge.net
        2000-2001 University of Geneva (Switzerland)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*
*  source file for generic XMLInput/XMLOutput in
*
*/


#include <sstream>
#include "ObjCryst/RefinableObj/RefinableObj.h"
#include "ObjCryst/RefinableObj/IO.h"

namespace ObjCryst
{
////////////////////////////////////////////////////////////////////////
//
//    XMLCrystTag
//
////////////////////////////////////////////////////////////////////////
XMLCrystTag::XMLCrystTag():
mIsEndTag(false),mIsEmptyTag(false)
{}

XMLCrystTag::XMLCrystTag(istream &is)
{
   is >> *this;
}

XMLCrystTag::XMLCrystTag(const string &tagName,
                         const bool isEndTag,
                         const bool isEmptyTag):
mName(tagName),mIsEndTag(isEndTag),mIsEmptyTag(isEmptyTag)
{}

XMLCrystTag::~XMLCrystTag(){}

const string& XMLCrystTag::GetName()const{return mName;}
const string& XMLCrystTag::GetClassName()const{static string str="XMLCrystTag";return str;}

unsigned int XMLCrystTag::GetNbAttribute()const{return mvAttribute.size();}

void XMLCrystTag::AddAttribute(const string &attName,const string &attValue)
{
   VFN_DEBUG_ENTRY("XMLCrystTag::AddAttribute():"<<this->GetName(),4)
   mvAttribute.push_back(make_pair(attName,attValue));
   VFN_DEBUG_EXIT("XMLCrystTag::AddAttribute():"<<this->GetName(),4)
}
void XMLCrystTag::GetAttribute(const int attNum,string &attName,string &attValue)
{
   attName=mvAttribute[attNum].first;
   attValue=mvAttribute[attNum].second;
}

const string& XMLCrystTag::GetAttributeName(const int attNum)const
{return mvAttribute[attNum].first;}

const string& XMLCrystTag::GetAttributeValue(const int attNum)const
{return mvAttribute[attNum].second;}

void XMLCrystTag::SetIsEndTag(const bool isEndTag){mIsEndTag=isEndTag;}
bool XMLCrystTag::IsEndTag()const{return mIsEndTag;}
void XMLCrystTag::SetIsEmptyTag(const bool isEmptyTag){mIsEmptyTag=isEmptyTag;}
bool XMLCrystTag::IsEmptyTag()const{return mIsEmptyTag;}
void XMLCrystTag::Print()const{cout<<*this;}
#ifdef __WX__CRYST__
WXCrystObj* XMLCrystTag::WXCreate(wxWindow *parent)
{
   //:TODO:
   //mpWXXMLCrystTag=new WXXMLCrystTag (parent,this);
   return mpWXXMLCrystTag;
}
WXCrystObj* XMLCrystTag::WXGet()
{
   return mpWXXMLCrystTag;
}
void XMLCrystTag::WXDelete()
{
   if(0!=mpWXXMLCrystTag) delete mpWXXMLCrystTag;
   mpWXXMLCrystTag=0;
}
void XMLCrystTag::WXNotifyDelete()
{
   mpWXXMLCrystTag=0;
}
#endif

ostream& operator<< (ostream& os, const XMLCrystTag&tag)
{
   if(true==tag.mIsEndTag)
   {
      os <<"</"<<tag.mName<<">";
      return os;
   }
   os <<"<"<<tag.mName;
   for(unsigned int i=0;i<tag.GetNbAttribute();i++)
   {
      os<<" "<<tag.mvAttribute[i].first<<"=\""<<tag.mvAttribute[i].second<<"\"";
   }
   if(true==tag.mIsEmptyTag) os <<"/>";
   else os <<">";
   return os;
}
istream& operator>> (istream& is, XMLCrystTag &tag)
{
   ios::fmtflags f=is.flags();
   is.unsetf(ios::skipws);//skip whitespaces
   tag.mIsEmptyTag=false;
   tag.mIsEndTag=false;
   char tmp;
   is>>tmp;
   while ((tmp!='<') && !(is.eof()) && !(is.fail()) )is>>tmp;
   if(is.eof()) return is;
   if(is.fail()) {cout<<"throw:"<<__FILE__<<":"<<__LINE__<<":"<<tag<<endl;throw ObjCrystException("XMLCrystTag::>>   failed input");}
   while ((tmp==' ')||(tmp=='<'))is>>tmp;

   if('/'==tmp)
   {
      tag.mIsEndTag=true;
      while ((tmp==' ')||(tmp=='/'))is>>tmp;
   }

   string str="";
   do
   {
     str+=tmp;
     is>>tmp;
     VFN_DEBUG_MESSAGE(str,1);
      if(is.fail()) {cout<<"throw:"<<__FILE__<<":"<<__LINE__<<":"<<tag<<endl;throw ObjCrystException("XMLCrystTag::>>   failed input");}
   } while ((tmp!=' ')&&(tmp!='>')&&(tmp!='/'));
   tag.mName=str;

   string str2;
   while(true)
   {
      while(tmp==' ')is>>tmp;
      if(tmp=='>')
      {
         is.setf(f);
         return is;
      }
      if(tmp=='/')
      {
         is>>tmp;
         //if(tmp!='>') ; :TODO:
         tag.mIsEmptyTag=true;
         is.setf(f);
         return is;
      }
      str="";
      do {str+=tmp;is>>tmp;VFN_DEBUG_MESSAGE(str,1)} while ((tmp!=' ')&&(tmp!='='));
      while(tmp!='"')is>>tmp;
      str2="";
      is>>tmp;
      if(tmp!='"') do {str2+=tmp;is>>tmp;VFN_DEBUG_MESSAGE(str2,1)} while (tmp!='"');
      is>>tmp;

      tag.AddAttribute(str,str2);
   }

   is.setf(f);
   return is;
}
////////////////////////////////////////////////////////////////////////
//
//    I/O RefinablePar
//
////////////////////////////////////////////////////////////////////////
void RefinablePar::XMLOutput(ostream &os,const string &name,int indent)const
{
   VFN_DEBUG_ENTRY("RefinablePar::XMLOutput():"<<this->GetName(),5)
   XMLCrystTag tag("Par");
   {
      stringstream ss;
      ss <<!(this->IsFixed());
      tag.AddAttribute("Refined",ss.str());
   }
   {
      stringstream ss;
      ss <<this->IsLimited();
      tag.AddAttribute("Limited",ss.str());
   }
   {
      stringstream ss;
      ss <<this->GetHumanMin();
      tag.AddAttribute("Min",ss.str());
   }
   {
      stringstream ss;
      ss <<this->GetHumanMax();
      tag.AddAttribute("Max",ss.str());
   }
   #if 0
   {//Useless. Periodicity cannot be changed for a given parameter
      stringstream ss;
      ss <<this->IsPeriodic();
      tag.AddAttribute("Periodic",ss.str());
   }
   #endif
   //the name of the parameter is saved last to enhance readability of saved files
   tag.AddAttribute("Name",name);

   for(int i=0;i<indent;i++) os << "  " ;
   os <<tag;
   tag.SetIsEndTag(true);
   os <<this->GetHumanValue()<<tag;
   VFN_DEBUG_EXIT("RefinablePar::XMLOutput():"<<this->GetName(),5)
}
void RefinablePar::XMLOutput(ostream &os,int indent)const
{
   this->XMLOutput(os,mName,indent);
}

void RefinablePar::XMLInput(istream &is,const XMLCrystTag &tag)
{
   VFN_DEBUG_ENTRY("RefinablePar::XMLInput():"<<this->GetName(),5)
   for(unsigned int i=0;i<tag.GetNbAttribute();i++)
   {
      if("Name"==tag.GetAttributeName(i)) continue;//names must be set by the object
      if("Refined"==tag.GetAttributeName(i))
      {
         bool b;
         stringstream ss(tag.GetAttributeValue(i));
         ss.imbue(std::locale::classic());
         ss >>b;
         this->SetIsFixed(!b);
         continue;
      }
      if("Limited"==tag.GetAttributeName(i))
      {
         bool b;
         stringstream ss(tag.GetAttributeValue(i));
         ss.imbue(std::locale::classic());
         ss >>b;
         this->SetIsLimited(b);
         continue;
      }
      if("Min"==tag.GetAttributeName(i))
      {
         REAL f;
         stringstream ss(tag.GetAttributeValue(i));
         ss.imbue(std::locale::classic());
         ss >>f;
         this->SetHumanMin(f);
         continue;
      }
      if("Max"==tag.GetAttributeName(i))
      {
         REAL f;
         stringstream ss(tag.GetAttributeValue(i));
         ss.imbue(std::locale::classic());
         ss >>f;
         this->SetHumanMax(f);
         continue;
      }
      if("Periodic"==tag.GetAttributeName(i))
      {
         bool b;
         stringstream ss(tag.GetAttributeValue(i));
         ss.imbue(std::locale::classic());
         ss >>b;
         this->SetIsPeriodic(b);
         continue;
      }
   }
   VFN_DEBUG_MESSAGE(tag, 10)
   REAL f=InputFloat(is,'<');
   if(ISNAN_OR_INF(f)) f=1.0;
   this->SetHumanValue(f);
   XMLCrystTag junk(is);//read end tag
   VFN_DEBUG_MESSAGE(tag, 10)
   VFN_DEBUG_EXIT("RefinablePar::XMLInput():"<<this->GetName(),5)
}

////////////////////////////////////////////////////////////////////////
//
//    I/O RefObjOpt
//
////////////////////////////////////////////////////////////////////////
void RefObjOpt::XMLOutput(ostream &os,int indent)const
{
   VFN_DEBUG_ENTRY("RefObjOpt::XMLOutput():"<<this->GetName(),5)
   XMLCrystTag tag("Option",false,true);
   tag.AddAttribute("Name",this->GetName());
   {
      stringstream ss;
      ss <<this->GetChoice();
      tag.AddAttribute("Choice",ss.str());
   }
   tag.AddAttribute("ChoiceName",this->GetChoiceName(this->GetChoice()));

   for(int i=0;i<indent;i++) os << "  " ;
   os <<tag;

   VFN_DEBUG_EXIT("RefObjOpt::XMLOutput():"<<this->GetName(),5)
}

void RefObjOpt::XMLInput(istream &is,const XMLCrystTag &tag)
{
   VFN_DEBUG_ENTRY("RefObjOpt::XMLInput():"<<this->GetName(),5)
   for(unsigned int i=0;i<tag.GetNbAttribute();i++)
   {
      if("Name"==tag.GetAttributeName(i)) continue;//names must be set by the object
      if("ChoiceName"==tag.GetAttributeName(i)) continue;//just for info
      if("Choice"==tag.GetAttributeName(i))
      {
         int b;
         stringstream ss(tag.GetAttributeValue(i));
         ss >>b;
         this->SetChoice(b);
         continue;
      }
   }
   VFN_DEBUG_EXIT("RefObjOpt::XMLInput():"<<this->GetName(),5)
}
////////////////////////////////////////////////////////////////////////
//
//    I/O RefinableObj // Does nothing ! Should be purely virtual...
//
////////////////////////////////////////////////////////////////////////
void RefinableObj::XMLOutput(ostream &os,int indent)const
{
   VFN_DEBUG_MESSAGE("RefinableObj::XMLOutput():"<<this->GetName(),5)
}

void RefinableObj::XMLInput(istream &is,const XMLCrystTag &tag)
{
   VFN_DEBUG_MESSAGE("RefinableObj::XMLInput():"<<this->GetName(),5)
}
#if 0
void RefinableObj::XMLInputOld(istream &is,const IOCrystTag &tag)
{
   VFN_DEBUG_MESSAGE("RefinableObj::XMLInput():"<<this->GetName(),5)
}
#endif
////////////////////////////////////////////////////////////////////////
//
//    OLD Functions & objects
//
////////////////////////////////////////////////////////////////////////
#if 0
void IOCrystExtractNameSpace(istream &is,string &str)
{
   ios::fmtflags f=is.flags();
   is.unsetf(ios::skipws);//skip whitespaces
   char tmp;
   do {is>>tmp;} while (tmp==' ');
   str="";
   do {str+=tmp;is>>tmp;VFN_DEBUG_MESSAGE(str,1)} while ((tmp!=' ')&&(tmp!='>'));
   if(tmp=='>') is.putback(tmp);
   is.setf(f);
}

void IOCrystExtractNameLine(istream &is,string &str)
{
   ios::fmtflags f=is.flags();
   is.setf(ios::skipws);//skip leading whitespaces
   getline(is,str);
   is.setf(f);
}

void IOCrystExtractNameQuoted(istream &is,string &str)
{
   char tmp;
   do {is>>tmp;} while ((tmp!='\'')&&(tmp!='\"'));
   str="";
   is>>tmp;
   if((tmp=='\'')||(tmp=='\"')) return;
   ios::fmtflags f=is.flags();
   is.unsetf(ios::skipws);//do not skip whitespaces
   do {str+=tmp;is>>tmp;VFN_DEBUG_MESSAGE(str,1)} while ((tmp!='\'')&&(tmp!='\"'));
   is.setf(f);
}

void IOCrystXMLOutputNameQuoted(ostream &os,const string &str)
{
   os << '\"' << str << '\"';
}

////////////////////////////////////////////////////////////////////////
//
//    IOCrystTag
//
////////////////////////////////////////////////////////////////////////

IOCrystTag::IOCrystTag(const string& type,const string& name, const unsigned long version):
mTagType(type),mTagName(name),mTagVersion(version),mIsClosingTag(false)
{}
IOCrystTag::IOCrystTag(istream &is)
{
   VFN_DEBUG_MESSAGE("IOCrystTag::IOCrystTag(istream &is)",2)
   char tmp;
   do
   {
      is>>tmp;
      if(true==is.eof())
      {
         mTagVersion=0;//closing tag
         mTagName="";
         mTagType="";
         mIsClosingTag=true;
         return;
      }
   } while (tmp!='<');
   IOCrystExtractNameSpace(is,mTagType);
   VFN_DEBUG_MESSAGE("IOCrystTag::IOCrystTag(istream &is):TagType:"<<mTagType,1)
   if(*(mTagType.c_str())=='\\')
   {
      mTagVersion=0;//closing tag
      mTagName="";
      mIsClosingTag=true;
   }
   else
   {
      IOCrystExtractNameQuoted(is,mTagName);
      is >> mTagVersion;
      mIsClosingTag=false;
   }
   do {is>>tmp;} while (tmp!='>');
   VFN_DEBUG_MESSAGE("IOCrystTag::IOCrystTag(istream &is):End",2)
}
IOCrystTag::~IOCrystTag(){}

bool IOCrystTag::operator==(const IOCrystTag& rhs)const
{
   if( (rhs.GetType()==this->GetType()) && (rhs.GetName()==this->GetName())) return true;
   return false;
}
void IOCrystTag::XMLInput(istream &is)
{
   VFN_DEBUG_MESSAGE("IOCrystTag::XMLInput(istream &is)",2)
   char tmp;
   do {is>>tmp;} while ((tmp!='<') && (!is.eof()) );
   if(is.eof()) return;
   IOCrystExtractNameSpace(is,mTagType);
   VFN_DEBUG_MESSAGE("IOCrystTag::XMLInput(istream &is):TagType:"<<mTagType,1)
   if(*(mTagType.c_str())=='\\')
   {
      mTagVersion=0;//closing tag
      mTagName="";
      mIsClosingTag=true;
   }
   else
   {
      IOCrystExtractNameQuoted(is,mTagName);
      is >> mTagVersion;
      mIsClosingTag=false;
   }
   do {is>>tmp;} while (tmp!='>');
   VFN_DEBUG_MESSAGE("IOCrystTag::XMLInput(istream &is):End",2)
}
const string &IOCrystTag::GetType() const{return mTagType;}
const string &IOCrystTag::GetName() const{return mTagName;}
unsigned long IOCrystTag::GetVersion()const{return mTagVersion;}
bool IOCrystTag::IsClosingTag()const{return mIsClosingTag;}
void IOCrystTag::Print() const
{
   if(mIsClosingTag==true) cout <<"<"<<mTagType<<">"<<endl;
   else
      cout <<"<"<<mTagType<<" \""<<mTagName<<"\" "<<mTagVersion<<">"<<endl;
}
const string &IOCrystTag::GetClassName() const{return mTagType;}
#ifdef __WX__CRYST__
WXCrystObj* IOCrystTag::WXCreate(wxWindow *parent)
{
   //mpWXIOCrystTag=new WXIOCrystTag (parent,this);
   return mpWXIOCrystTag;
}
WXCrystObj* IOCrystTag::WXGet()
{
   return mpWXIOCrystTag;
}
void IOCrystTag::WXDelete()
{
   if(0!=mpWXIOCrystTag) delete mpWXIOCrystTag;
   mpWXIOCrystTag=0;
}
void IOCrystTag::WXNotifyDelete()
{
   mpWXIOCrystTag=0;
}
#endif
#endif

} //namespace