QDP++
qdp_xmlio.h
Go to the documentation of this file.
1// -*- C++ -*-
2
6
7#ifndef QDP_XMLIO_H
8#define QDP_XMLIO_H
9
10#include <string>
11#include <sstream>
12#include <stack>
13#include <list>
14#include <vector>
15#include <map>
16
17#include "xml_simplewriter.h"
18#include "basic_xpath_reader.h"
19
20namespace QDP
21{
22
23 // Forward declarations
24 class XMLReader;
25 class XMLWriter;
26 class XMLBufferWriter;
27 class XMLFileWriter;
28 class XMLArrayWriter;
29
30
34
35 //--------------------------------------------------------------------------------
37
43 class XMLReader : protected XMLXPathReader::BasicXPathReader
44 {
45 public:
47 XMLReader();
48
50
54 XMLReader(const std::string& filename);
55
57 XMLReader(std::istream& is);
58
60 XMLReader(const XMLBufferWriter& mw);
61
63 XMLReader(XMLReader& old, const std::string& xpath);
64 ~XMLReader();
65
66 /* The meaning of these should be clear to you */
67
69
73 void open(const std::string& filename);
74
76
80 void open(std::istream& is);
81
83 void open(const XMLBufferWriter& mw);
84
86
89 bool is_open();
90
92
96 bool is_derived() const;
97
99 void close();
100
101 /* So should these, there is just a lot of overloading */
103 void get(const std::string& xpath, std::string& result);
105 void get(const std::string& xpath, int& result);
107 void get(const std::string& xpath, unsigned int& result);
109 void get(const std::string& xpath, short int& result);
111 void get(const std::string& xpath, unsigned short int& result);
113 void get(const std::string& xpath, long int& result);
115 void get(const std::string& xpath, unsigned long int& result);
117 void get(const std::string& xpath, float& result);
119 void get(const std::string& xpath, double& result);
121 void get(const std::string& xpath, bool& result);
122
124 void getAttribute(const std::string& xpath,
125 const std::string& attrib_name,
126 int& result);
127
129 template<typename T>
130 void set(const std::string& xpath, const T& to_set)
131 {
133 {
134 BasicXPathReader::set<T>(xpath, to_set);
135 }
136 }
137
138
140 void print(std::ostream& is);
141
143 void printCurrentContext(std::ostream& is);
144
146 int count(const std::string& xpath);
147
148 void registerNamespace(const std::string& prefix, const std::string& uri);
149
150 private:
152 void operator=(const XMLReader&) {}
153
155 XMLReader(const XMLReader&) {}
156
157 void open(XMLReader& old, const std::string& xpath);
158 protected:
159 // The universal data-reader. All the read functions call this
160 template<typename T>
161 void
162 readPrimitive(const std::string& xpath, T& output);
163
164 // The universal attribute reader
165 template<typename T>
166 void
167 readAttrPrimitive(const std::string& xpath,
168 const std::string& attrib_name,
169 T& result);
170 private:
171 bool iop; //file open or closed?
172 bool derived; // is this reader derived from another reader?
173 };
174
175
176 // Time to build a telephone book of basic primitives
178 void read(XMLReader& xml, const std::string& s, std::string& input);
180 void read(XMLReader& xml, const std::string& s, char& input);
182 void read(XMLReader& xml, const std::string& s, int& input);
184 void read(XMLReader& xml, const std::string& s, unsigned int& input);
186 void read(XMLReader& xml, const std::string& s, short int& input);
188 void read(XMLReader& xml, const std::string& s, unsigned short int& input);
190 void read(XMLReader& xml, const std::string& s, long int& input);
192 void read(XMLReader& xml, const std::string& s, unsigned long int& input);
194 void read(XMLReader& xml, const std::string& s, float& input);
196 void read(XMLReader& xml, const std::string& s, double& input);
198 void read(XMLReader& xml, const std::string& s, bool& input);
199
200
202 template<class T>
203 inline
204 void read(XMLReader& xml, const std::string& s, multi1d<T>& input)
205 {
206 XMLReader arraytop(xml, s);
207
208 std::ostringstream error_message;
209
210 // Count the number of elements
211 std::string elem_base_query = "elem";
212
213 int array_size;
214 try {
215 array_size = arraytop.count(elem_base_query);
216 }
217 catch( const std::string& e) {
218 error_message << "Exception occurred while counting " << elem_base_query
219 << " during array read " << std::endl;
220 }
221
222 // Now resize the array to hold the no of elements.
223 input.resize(array_size);
224
225 // Get the elements one by one
226 for(int i=0; i < input.size(); i++)
227 {
228 std::ostringstream element_xpath;
229
230 // Create the query for the element
231 element_xpath << elem_base_query << "[" << (i+1) << "]";
232
233 // recursively try and read the element.
234 try
235 {
236 read(arraytop, element_xpath.str(), input[i]);
237 }
238 catch (const std::string& e)
239 {
240 error_message << "Failed to match element " << i
241 << " of array with query " << element_xpath.str()
242 << std::endl
243 << "Query returned error: " << e;
244 throw error_message.str();
245 }
246 }
247 }
248
249
250 // Specialized versions for basic types
251 template<>
252 void read(XMLReader& xml, const std::string& s, multi1d<int>& input);
253 template<>
254 void read(XMLReader& xml, const std::string& s, multi1d<unsigned int>& input);
255 template<>
256 void read(XMLReader& xml, const std::string& s, multi1d<short int>& input);
257 template<>
258 void read(XMLReader& xml, const std::string& s, multi1d<unsigned short int>& input);
259 template<>
260 void read(XMLReader& xml, const std::string& s, multi1d<long int>& input);
261 template<>
262 void read(XMLReader& xml, const std::string& s, multi1d<unsigned long int>& input);
263 template<>
264 void read(XMLReader& xml, const std::string& s, multi1d<float>& input);
265 template<>
266 void read(XMLReader& xml, const std::string& s, multi1d<double>& input);
267 template<>
268 void read(XMLReader& xml, const std::string& s, multi1d<bool>& input);
269
270
271 //---------------------------------------------------------------
272 //---------------------------------------------------------------
274 template<class T>
275 inline
276 void read(XMLReader& xml, const std::string& s, std::vector<T>& input)
277 {
278 XMLReader arraytop(xml, s);
279
280 std::ostringstream error_message;
281 std::string elemName = "elem";
282
283 // Count the number of elements
284 std::string elem_base_query = elemName;
285
286 int array_size;
287 try {
288 array_size = arraytop.count(elem_base_query);
289 }
290 catch( const std::string& e) {
291 error_message << "Exception occurred while counting " << elem_base_query
292 << " during array read " << s << std::endl;
293 arraytop.close();
294 throw error_message.str();
295 }
296
297 // Now resize the array to hold the no of elements.
298 input.resize(array_size);
299
300 // Get the elements one by one
301 for(int i=0; i < input.size(); i++)
302 {
303 std::ostringstream element_xpath;
304
305 // Create the query for the element
306 element_xpath << elem_base_query << "[" << (i+1) << "]";
307
308 // recursively try and read the element.
309 try {
310 read(arraytop, element_xpath.str(), input[i]);
311 }
312 catch (const std::string& e)
313 {
314 error_message << "Failed to match element " << i
315 << " of array " << s << " with query " << element_xpath.str()
316 << std::endl
317 << "Query returned error: " << e;
318 arraytop.close();
319 throw error_message.str();
320 }
321 }
322
323 // Arraytop should self destruct but just to be sure.
324 arraytop.close();
325 }
326
327
328 // Specialized versions for basic types
329 void read(XMLReader& xml, const std::string& s, std::vector<int>& input);
330 void read(XMLReader& xml, const std::string& s, std::vector<unsigned int>& input);
331 void read(XMLReader& xml, const std::string& s, std::vector<short int>& input);
332 void read(XMLReader& xml, const std::string& s, std::vector<unsigned short int>& input);
333 void read(XMLReader& xml, const std::string& s, std::vector<long int>& input);
334 void read(XMLReader& xml, const std::string& s, std::vector<unsigned long int>& input);
335 void read(XMLReader& xml, const std::string& s, std::vector<float>& input);
336 void read(XMLReader& xml, const std::string& s, std::vector<double>& input);
337// void read(XMLReader& xml, const std::string& s, std::vector<bool>& input); // does not seem to exist
338
339
340
341 //---------------------------------------------------------------
343 template<class T>
344 inline
345 void read(XMLReader& xml, const std::string& s, std::list<T>& input)
346 {
347 XMLReader arraytop(xml, s);
348
349 std::ostringstream error_message;
350 std::string elemName = "elem";
351
352 // Count the number of elements
353 std::string elem_base_query = elemName;
354
355 int array_size;
356 try {
357 array_size = arraytop.count(elem_base_query);
358 }
359 catch( const std::string& e) {
360 error_message << "Exception occurred while counting " << elem_base_query
361 << " during array read " << s << std::endl;
362 arraytop.close();
363 throw error_message.str();
364 }
365
366 // Clear out the original list
367 input.clear();
368
369 // Get the elements one by one
370 for(int i=0; i < input.size(); i++)
371 {
372 std::ostringstream element_xpath;
373
374 // Create the query for the element
375 element_xpath << elem_base_query << "[" << (i+1) << "]";
376
377 // recursively try and read the element.
378 try {
379 T thingy;
380 read(arraytop, element_xpath.str(), thingy);
381
382 input.push_back(thingy);
383 }
384 catch (const std::string& e)
385 {
386 error_message << "Failed to match element " << i
387 << " of array " << s << " with query " << element_xpath.str()
388 << std::endl
389 << "Query returned error: " << e;
390 arraytop.close();
391 throw error_message.str();
392 }
393 }
394
395 // Arraytop should self destruct but just to be sure.
396 arraytop.close();
397 }
398
399
400 // Specialized versions for basic types
401 void read(XMLReader& xml, const std::string& s, std::list<int>& input);
402 void read(XMLReader& xml, const std::string& s, std::list<unsigned int>& input);
403 void read(XMLReader& xml, const std::string& s, std::list<short int>& input);
404 void read(XMLReader& xml, const std::string& s, std::list<unsigned short int>& input);
405 void read(XMLReader& xml, const std::string& s, std::list<long int>& input);
406 void read(XMLReader& xml, const std::string& s, std::list<unsigned long int>& input);
407 void read(XMLReader& xml, const std::string& s, std::list<float>& input);
408 void read(XMLReader& xml, const std::string& s, std::list<double>& input);
409
410
411
412 //---------------------------------------------------------------
414 template<class T>
415 inline
416 void read(XMLReader& xml, const std::string& s, Array1dO<T>& input)
417 {
418 read(xml, s, input.ref());
419 }
420
421
422 //---------------------------------------------------------------
424 template<typename K, typename V>
425 inline
426 void read(XMLReader& xml, const std::string& s, std::map<K,V>& input)
427 {
428 XMLReader arraytop(xml, s);
429
430 std::ostringstream error_message;
431 std::string elemName = "elem";
432
433 int array_size;
434 try {
435 array_size = arraytop.count(elemName);
436 }
437 catch( const std::string& e) {
438 error_message << "Exception occurred while counting " << elemName
439 << " during array read " << s << std::endl;
440 arraytop.close();
441 throw error_message.str();
442 }
443
444 // Get the elements one by one
445 for(int i=0; i < array_size; i++)
446 {
447 std::ostringstream element_xpath;
448
449 // Create the query for the element
450 element_xpath << elemName << "[" << (i+1) << "]";
451
452 // recursively try and read the element.
453 try {
454 XMLReader xml_elem(arraytop, element_xpath.str());
455
456 K key;
457 V val;
458
459 read(xml_elem, "Key", key);
460 read(xml_elem, "Val", val);
461
462 input.insert(std::make_pair(key, val));
463 }
464 catch (const std::string& e)
465 {
466 error_message << "Failed to match element " << i
467 << " of array " << s << " with query " << element_xpath.str()
468 << std::endl
469 << "Query returned error: " << e;
470 arraytop.close();
471 throw error_message.str();
472 }
473 }
474
475 // Arraytop should self destruct but just to be sure.
476 arraytop.close();
477 }
478
479
480
481 //---------------------------------------------------------------
483 template<typename T1, typename T2>
484 inline
485 void read(XMLReader& xml, const std::string& s, std::pair<T1,T2>& input)
486 {
487 XMLReader paramtop(xml, s);
488
489 read(paramtop, "First", input.first);
490 read(paramtop, "Second", input.second);
491 }
492
493
494
495 //--------------------------------------------------------------------------------
496 //--------------------------------------------------------------------------------
498
505 class XMLWriter : protected XMLWriterAPI::XMLSimpleWriter
506 {
507 public:
508 XMLWriter();
509
510 // Virtual destructor
511 virtual ~XMLWriter();
512
514
517 virtual void openSimple(const std::string& tagname);
518 virtual void closeSimple();
519
521
524 virtual void openStruct(const std::string& tagname);
525 virtual void closeStruct();
526
528
531 void openTag(const std::string& tagname);
532
534
538 void openTag(const std::string& nsprefix, const std::string& tagname);
539
541
545 void openTag(const std::string& tagname, XMLWriterAPI::AttributeList& al);
546
548
553 void openTag(const std::string& nsprefix,
554 const std::string& tagname,
555 XMLWriterAPI::AttributeList& al);
556
558 void closeTag();
559
561
564 void emptyTag(const std::string& tagname);
565
567
571 void emptyTag(const std::string& nsprefix, const std::string& tagname);
572
574
578 void emptyTag(const std::string& tagname, XMLWriterAPI::AttributeList& al);
579
581
586 void emptyTag(const std::string& nsprefix,
587 const std::string& tagname,
588 XMLWriterAPI::AttributeList& al);
589
590
591 // Overloaded Writer Functions
592
594 void write(const std::string& output);
596 void write(const int& output);
598 void write(const unsigned int& output);
600 void write(const short int& output);
602 void write(const unsigned short int& output);
604 void write(const long int& output);
606 void write(const unsigned long int& output);
608 void write(const float& output);
610 void write(const double& output);
612 void write(const bool& output);
613
614 // Write all the XML to std::string
615 void writeXML(const std::string& output);
616
617 friend class XMLArrayWriter;
618 };
619
620
622
626 void push(XMLWriter& xml, const std::string& s);
627
629
630
631 void pop(XMLWriter& xml);
632
634 void write(XMLWriter& xml, const std::string& s, const XMLReader& d);
635 XMLWriter& operator<<(XMLWriter& xml, const XMLReader& d);
636
638 void write(XMLWriter& xml, const std::string& s, const XMLBufferWriter& d);
640
641 // Time to build a telephone book of basic primitives
643
648 void write(XMLWriter& xml, const std::string& s, const std::string& output);
650
655 void write(XMLWriter& xml, const std::string& s, const char* output);
657
662 void write(XMLWriter& xml, const std::string& s, const char& output);
664
669 void write(XMLWriter& xml, const std::string& s, const int& output);
671
676 void write(XMLWriter& xml, const std::string& s, const unsigned int& output);
678
683 void write(XMLWriter& xml, const std::string& s, const short int& output);
685
690 void write(XMLWriter& xml, const std::string& s, const unsigned short int& output);
692
697 void write(XMLWriter& xml, const std::string& s, const long int& output);
699
704 void write(XMLWriter& xml, const std::string& s, const unsigned long int& output);
706
711 void write(XMLWriter& xml, const std::string& s, const float& output);
713
718 void write(XMLWriter& xml, const std::string& s, const double& output);
720
725 void write(XMLWriter& xml, const std::string& s, const bool& output);
726
727 // Versions that do not print a name
728 XMLWriter& operator<<(XMLWriter& xml, const std::string& output);
729 XMLWriter& operator<<(XMLWriter& xml, const char* output);
730 XMLWriter& operator<<(XMLWriter& xml, const char& output);
731 XMLWriter& operator<<(XMLWriter& xml, const int& output);
732 XMLWriter& operator<<(XMLWriter& xml, const unsigned int& output);
733 XMLWriter& operator<<(XMLWriter& xml, const short int& output);
734 XMLWriter& operator<<(XMLWriter& xml, const unsigned short int& output);
735 XMLWriter& operator<<(XMLWriter& xml, const long int& output);
736 XMLWriter& operator<<(XMLWriter& xml, const unsigned long int& output);
737 XMLWriter& operator<<(XMLWriter& xml, const float& output);
738 XMLWriter& operator<<(XMLWriter& xml, const double& output);
739 XMLWriter& operator<<(XMLWriter& xml, const bool& output);
740
742
748 template<class T>
749 inline
750 void write(XMLWriter& xml, const std::string& s, const multi1d<T>& s1)
751 {
752 // Write the array name
753 xml.openTag(s);
754
755#if 0
756 // This stuff is for schemas
757 XMLWriterAPI::AttributeList alist;
758 alist.push_back(XMLWriterAPI::Attribute("minOccurs", s1.size()));
759 alist.push_back(XMLWriterAPI::Attribute("maxOccurs", s1.size()));
760
761 xml.openTag("complexType");
762 xml.openTag("sequence", alist);
763#endif
764
765 for(int index=0; index < s1.size(); index++)
766 {
767 write(xml, "elem", s1[index]); // Possibly grab user defines here
768 }
769
770#if 0
771 // This stuff is for schemas
772 xml.closeTag(); // sequence
773 xml.closeTag(); // complexType
774#endif
775 xml.closeTag(); // Array name
776 }
777
778
779 // Writers for arrays of basic types
781
787 template<>
788 void write(XMLWriter& xml, const std::string& s, const multi1d<int>& output);
790
796 template<>
797 void write(XMLWriter& xml, const std::string& s, const multi1d<unsigned int>& output);
799
805 template<>
806 void write(XMLWriter& xml, const std::string& s, const multi1d<short int>& output);
808
814 template<>
815 void write(XMLWriter& xml, const std::string& s, const multi1d<unsigned short int>& output);
817
823 template<>
824 void write(XMLWriter& xml, const std::string& s, const multi1d<long int>& output);
826
832 template<>
833 void write(XMLWriter& xml, const std::string& s, const multi1d<unsigned long int>& output);
835
841 template<>
842 void write(XMLWriter& xml, const std::string& s, const multi1d<float>& output);
844
850 template<>
851 void write(XMLWriter& xml, const std::string& s, const multi1d<double>& output);
853
859 template<>
860 void write(XMLWriter& xml, const std::string& s, const multi1d<bool>& output);
861
863 template<class RHS, class C>
864 void write(XMLWriter& xml, const std::string& s, const QDPExpr<RHS,C>& d)
865 {
866 return write(xml, s, C(d));
867 }
868
870 template<class T>
871 inline
872 void write(XMLWriter& xml, const std::string& s, const OScalar<T>& d)
873 {
874 xml.openTag(s);
875 xml << d;
876 xml.closeTag();
877 }
878
880 template<class T>
881 inline
882 void write(XMLWriter& xml, const std::string& s, const OLattice<T>& d)
883 {
884 xml.openTag(s);
885 xml << d;
886 xml.closeTag();
887 }
888
889
890#if 0
891 // NEED TO FIX THIS
893 template<class T>
894 inline
895 void write(XMLWriter& xml, const std::string& s, const multi2d<T>& s1)
896 {
897 for(int j=0; j < s1.size1(); ++j)
898 for(int i=0; i < s1.size2(); ++i)
899 {
900 std::ostringstream ost;
901 if (Layout::primaryNode())
902 ost << s << "[ " << i << " ][ " << j << " ]";
903 write(xml, ost.str(), s1[i][j]);
904 }
905 }
906
907#endif
908
909
910
911 //---------------------------------------------------------------
912 //---------------------------------------------------------------
914 template<class T>
915 inline
916 void write(XMLWriter& xml, const std::string& s, const std::vector<T>& s1)
917 {
918 // Write the array name
919 xml.openTag(s);
920
921 for(unsigned index=0; index < s1.size(); index++)
922 {
923 write(xml, "elem", s1[index]); // Possibly grab user defines here
924 }
925
926 xml.closeTag(); // Array name
927 }
928
929
930 // Writers for arrays of basic types
931 void write(XMLWriter& xml, const std::string& s, const std::vector<int>& output);
932 void write(XMLWriter& xml, const std::string& s, const std::vector<unsigned int>& output);
933 void write(XMLWriter& xml, const std::string& s, const std::vector<short int>& output);
934 void write(XMLWriter& xml, const std::string& s, const std::vector<unsigned short int>& output);
935 void write(XMLWriter& xml, const std::string& s, const std::vector<long int>& output);
936 void write(XMLWriter& xml, const std::string& s, const std::vector<unsigned long int>& output);
937 void write(XMLWriter& xml, const std::string& s, const std::vector<float>& output);
938 void write(XMLWriter& xml, const std::string& s, const std::vector<double>& output);
939 void write(XMLWriter& xml, const std::string& s, const std::vector<bool>& output);
940
941
942 //---------------------------------------------------------------
943 //---------------------------------------------------------------
945 template<class T>
946 inline
947 void write(XMLWriter& xml, const std::string& s, const std::list<T>& s1)
948 {
949 // Write the array name
950 xml.openTag(s);
951
952 for(typename std::list<T>::const_iterator t=s1.begin(); t != s1.end(); ++t)
953 {
954 write(xml, "elem", *t); // Possibly grab user defines here
955 }
956
957 xml.closeTag(); // Array name
958 }
959
960
961 // Writers for arrays of basic types
962 void write(XMLWriter& xml, const std::string& s, const std::list<int>& output);
963 void write(XMLWriter& xml, const std::string& s, const std::list<unsigned int>& output);
964 void write(XMLWriter& xml, const std::string& s, const std::list<short int>& output);
965 void write(XMLWriter& xml, const std::string& s, const std::list<unsigned short int>& output);
966 void write(XMLWriter& xml, const std::string& s, const std::list<long int>& output);
967 void write(XMLWriter& xml, const std::string& s, const std::list<unsigned long int>& output);
968 void write(XMLWriter& xml, const std::string& s, const std::list<float>& output);
969 void write(XMLWriter& xml, const std::string& s, const std::list<double>& output);
970 void write(XMLWriter& xml, const std::string& s, const std::list<bool>& output);
971
972
973 //---------------------------------------------------------------
974 //---------------------------------------------------------------
976 template<class T>
977 inline
978 void write(XMLWriter& xml, const std::string& s, const Array1dO<T>& s1)
979 {
980 write(xml, s, s1.ref());
981 }
982
983
984 //---------------------------------------------------------------
985 //---------------------------------------------------------------
987 template<typename K, typename V>
988 inline
989 void write(XMLWriter& xml, const std::string& path, const std::map<K,V>& param)
990 {
991 push(xml, path);
992
993 for(typename std::map<K,V>::const_iterator pp = param.begin();
994 pp != param.end();
995 ++pp)
996 {
997 push(xml, "elem");
998 write(xml, "Key", pp->first);
999 write(xml, "Val", pp->second);
1000 pop(xml);
1001 }
1002
1003 pop(xml);
1004 }
1005
1006
1008 template<typename T1, typename T2>
1009 inline
1010 void write(XMLWriter& xml, const std::string& path, const std::pair<T1,T2>& param)
1011 {
1012 push(xml, path);
1013
1014 write(xml, "First", param.first);
1015 write(xml, "Second", param.second);
1016
1017 pop(xml);
1018 }
1019
1020
1021 //--------------------------------------------------------------------------------
1024 {
1025 public:
1026
1029
1031 explicit XMLBufferWriter(const std::string& s);
1032
1035
1037 void open(const std::string& s);
1038
1040 std::string str() const;
1041
1042 // Return root element as a string
1043 std::string printCurrentContext() const;
1044
1046 void flush() {}
1047
1049 bool fail() const {return false;}
1050
1051 private:
1052 // The output stream...
1053 std::ostringstream output_stream;
1054
1055 // The function that supplies the stream to the parent...
1056 std::ostream& getOstream(void) {return output_stream;}
1057 };
1058
1059
1060 //--------------------------------------------------------------------------------
1062
1065
1067 {
1068 public:
1069
1070 XMLFileWriter();
1071
1073
1078 explicit XMLFileWriter(const std::string& filename, bool write_prologue=true)
1079 {
1080 open(filename, write_prologue);
1081 }
1082
1084
1086
1089 bool is_open();
1090
1092
1097 void open(const std::string& filename, bool write_prologue=true);
1098
1100 void flush();
1101
1103 bool fail() const;
1104
1106 void close();
1107
1108 private:
1109 std::ofstream output_stream;
1110 std::ostream& getOstream(void) {return output_stream;}
1111 };
1112
1113
1114
1115 //--------------------------------------------------------------------------------
1118 {
1119 public:
1120
1125 explicit XMLArrayWriter(XMLWriter& xml_out, int size = -1) :
1126 output_xml(xml_out), array_size(size)
1127 {
1128 initP = arrayTag = false;
1129 elements_written = 0;
1130 indent_level = xml_out.indent_level;
1131 simpleElements = false; // do not know this yet
1132 }
1133
1134
1136
1137 // Flush the buffer
1138 // void flush();
1139
1141
1142 void close();
1143
1145 int size() const {return array_size;}
1146
1147 void openArray(const std::string& tagname);
1148 void closeArray();
1149
1150 // virtual void openSimple(const std::string& tagname);
1151 // virtual void closeSimple();
1152
1153 void openStruct(const std::string& tagname);
1154 void closeStruct();
1155
1156 private:
1157 std::string qname;
1158 std::string elem_qname;
1159
1160 bool arrayTag; // set once array tag is written
1161 bool initP; // set once we know how the array is composed
1162 bool simpleElements; // true if elements will all be simple types
1163
1164 // output stream is actually the original stream
1165 XMLWriter& output_xml;
1166
1167 int array_size; // total array element size
1168 int elements_written; // elements written so far
1169
1170 // A stack to hold context.
1171 enum ElementType {SIMPLE, STRUCT};
1172 std::stack<ElementType> contextStack;
1173
1174 std::ostream& getOstream(void) {return output_xml.getOstream();}
1175 };
1176
1178 void push(XMLArrayWriter& xml);
1179
1181 void pop(XMLArrayWriter& xml);
1182
1183 // end of group io
1185
1186} // namespace QDP
1187
1188#endif
1d array, with 1-based indices
Definition qdp_arrays.h:18
const std::vector< T > & ref() const
Return ref to underlying array.
Definition qdp_arrays.h:52
Outer grid Lattice type.
Definition qdp_outer.h:264
Outer grid Scalar class *‍/.
Definition qdp_outer.h:37
Expression class for QDP.
Definition qdp_qdpexpr.h:16
Writes metadata to an array which serves as a handle for another XML object.
Definition qdp_xmlio.h:1118
void openArray(const std::string &tagname)
XMLArrayWriter(XMLWriter &xml_out, int size=-1)
Definition qdp_xmlio.h:1125
void close()
Closes the array writer.
int size() const
Returns the size of the array.
Definition qdp_xmlio.h:1145
void openStruct(const std::string &tagname)
Writes an opening XML tag.
Writes XML metadata to a buffer.
Definition qdp_xmlio.h:1024
std::string printCurrentContext() const
void flush()
Flush the buffer.
Definition qdp_xmlio.h:1046
~XMLBufferWriter()
Destroy.
bool fail() const
Return true if some failure occurred in previous IO operation.
Definition qdp_xmlio.h:1049
std::string str() const
Return entire buffer as a string.
void open(const std::string &s)
Construct from a string.
Writes XML metadata to a file.
Definition qdp_xmlio.h:1067
void flush()
Flush the buffer.
XMLFileWriter(const std::string &filename, bool write_prologue=true)
Constructor from a filename.
Definition qdp_xmlio.h:1078
void close()
Closes the last file opened.
void open(const std::string &filename, bool write_prologue=true)
Opens a file.
bool is_open()
Queries whether the binary file is open.
bool fail() const
Return true if some failure occurred in previous IO operation.
XML reader class.
Definition qdp_xmlio.h:44
void open(const std::string &filename)
Opens and reads an XML file.
Definition qdp_xmlio.cc:45
void getAttribute(const std::string &xpath, const std::string &attrib_name, int &result)
read integer attributes so that I can read Matrices in XML
Definition qdp_xmlio.cc:176
void printCurrentContext(std::ostream &is)
Print the current context.
Definition qdp_xmlio.cc:222
XMLReader()
Empty constructor.
Definition qdp_xmlio.cc:17
void readPrimitive(const std::string &xpath, T &output)
Definition qdp_xmlio.cc:183
bool is_derived() const
Queries whether the XML data has been obtained from another XMLReader.
Definition qdp_xmlio.cc:123
void registerNamespace(const std::string &prefix, const std::string &uri)
Definition qdp_xmlio.cc:254
void set(const std::string &xpath, const T &to_set)
Set a replacement of a primitive.
Definition qdp_xmlio.h:130
int count(const std::string &xpath)
Count the number of occurances from the Xpath query.
Definition qdp_xmlio.cc:242
void get(const std::string &xpath, std::string &result)
Xpath query.
Definition qdp_xmlio.cc:129
bool is_open()
Queries whether the binary file is open.
Definition qdp_xmlio.cc:121
void close()
Closes the last file opened.
Definition qdp_xmlio.cc:107
void print(std::ostream &is)
Return the entire contents of the Reader as a stream.
Definition qdp_xmlio.cc:206
void readAttrPrimitive(const std::string &xpath, const std::string &attrib_name, T &result)
Definition qdp_xmlio.cc:194
Metadata output class.
Definition qdp_xmlio.h:506
virtual ~XMLWriter()
Definition qdp_xmlio.cc:612
virtual void closeStruct()
Definition qdp_xmlio.cc:631
virtual void closeSimple()
Definition qdp_xmlio.cc:621
void openTag(const std::string &tagname)
Writes an opening XML tag.
Definition qdp_xmlio.cc:636
void emptyTag(const std::string &nsprefix, const std::string &tagname)
Writes an empty tag.
friend class XMLArrayWriter
Definition qdp_xmlio.h:617
void write(const std::string &output)
Write tag contents.
Definition qdp_xmlio.cc:689
void emptyTag(const std::string &tagname)
Writes an empty tag.
Definition qdp_xmlio.cc:668
void writeXML(const std::string &output)
Definition qdp_xmlio.cc:741
void closeTag()
Closes a tag.
Definition qdp_xmlio.cc:662
virtual void openSimple(const std::string &tagname)
Writes an opening XML tag.
Definition qdp_xmlio.cc:616
virtual void openStruct(const std::string &tagname)
Writes an opening XML tag.
Definition qdp_xmlio.cc:626
Container for a multi-dimensional 1D array.
Definition qdp_multi.h:25
int size() const
Size of array.
Definition qdp_multi.h:60
void resize(int ns1)
Resize routine, call a templated resize, using *this to disambiguate.
Definition qdp_multi.h:56
TextWriter & operator<<(TextWriter &txt, const std::string &output)
Definition qdp_io.cc:283
void write(BinaryWriter &bin, const std::string &output)
Definition qdp_io.cc:1204
void read(BinaryReader &bin, std::string &input, size_t maxBytes)
Definition qdp_io.cc:778
OLattice< PScalar< PColorMatrix< RComplexFloat, 3 > > > C
bool primaryNode()
Returns whether this is the primary node.
Yet another random number generator.
void push(XMLWriter &xml, const std::string &s)
Push a group name.
Definition qdp_xmlio.cc:749
void pop(XMLWriter &xml)
Pop a group name.
Definition qdp_xmlio.cc:752