QDP++
qdp_xmlio.cc
Go to the documentation of this file.
1//
5
6#include "qdp.h"
7#include <list>
8
9namespace QDP
10{
11
12 using std::string;
13
14 //--------------------------------------------------------------------------------
15 // XML classes
16 // XML reader class
17 XMLReader::XMLReader() {iop=derived=false;}
18
19 XMLReader::XMLReader(const std::string& filename)
20 {
21 iop = derived = false;
22 open(filename);
23 }
24
25 XMLReader::XMLReader(std::istream& is)
26 {
27 iop = derived = false;
28 open(is);
29 }
30
32 {
33 iop = derived = false;
34 open(mw);
35 }
36
37 XMLReader::XMLReader(XMLReader& old, const std::string& xpath) : BasicXPathReader()
38 {
39 iop = false;
40 derived = true;
41 open(old, xpath);
42 }
43
44
45 void XMLReader::open(const std::string& filename)
46 {
48 {
49#if 0
50 BasicXPathReader::open(filename);
51#else
52
53#if defined(USE_REMOTE_QIO)
54 QDPUtil::RemoteInputFileStream f;
55 f.open(filename.c_str(),std::ifstream::in);
56 BasicXPathReader::open(f);
57#else
58 std::ifstream f;
59 f.open(filename.c_str(), std::ios::binary);
60 if (f.fail())
61 {
62 QDPIO::cerr << "Error opening read file = " << filename << std::endl;
63 QDP_abort(1);
64 }
65 BasicXPathReader::open(f);
66#endif
67
68#endif
69 }
70
71 iop = true;
72 derived = false;
73 }
74
75 void XMLReader::open(std::istream& is)
76 {
78 BasicXPathReader::open(is);
79
80 iop = true;
81 derived = false;
82 }
83
85 {
87 {
88 std::istringstream is(const_cast<XMLBufferWriter&>(mw).str()+"\n");
89 BasicXPathReader::open(is);
90 }
91
92 iop = true;
93 derived = false;
94 }
95
96 void XMLReader::open(XMLReader& old, const std::string& xpath)
97 {
99 {
100 BasicXPathReader::open((BasicXPathReader&)old, xpath);
101 }
102
103 iop = true;
104 derived = true;
105 }
106
108 {
109 if (is_open())
110 {
111 if (Layout::primaryNode())
112 BasicXPathReader::close();
113
114 iop = false;
115 derived = false;
116 }
117 }
118
119
120
121 bool XMLReader::is_open() {return iop;}
122
123 bool XMLReader::is_derived() const {return derived;}
124
126
127
128 // Overloaded Reader Functions
129 void XMLReader::get(const std::string& xpath, std::string& result)
130 {
131 // Only primary node can grab string
132 if (Layout::primaryNode())
133 BasicXPathReader::get(xpath, result);
134
135 // broadcast string
137 }
138
139 void XMLReader::get(const std::string& xpath, int& result)
140 {
141 readPrimitive<int>(xpath, result);
142 }
143 void XMLReader::get(const std::string& xpath, unsigned int& result)
144 {
145 readPrimitive<unsigned int>(xpath, result);
146 }
147 void XMLReader::get(const std::string& xpath, short int& result)
148 {
149 readPrimitive<short int>(xpath, result);
150 }
151 void XMLReader::get(const std::string& xpath, unsigned short int& result)
152 {
154 }
155 void XMLReader::get(const std::string& xpath, long int& result)
156 {
157 readPrimitive<long int>(xpath, result);
158 }
159 void XMLReader::get(const std::string& xpath, unsigned long int& result)
160 {
162 }
163 void XMLReader::get(const std::string& xpath, float& result)
164 {
165 readPrimitive<float>(xpath, result);
166 }
167 void XMLReader::get(const std::string& xpath, double& result)
168 {
169 readPrimitive<double>(xpath, result);
170 }
171 void XMLReader::get(const std::string& xpath, bool& result)
172 {
173 readPrimitive<bool>(xpath, result);
174 }
175
176 void XMLReader::getAttribute(const std::string& xpath,
177 const std::string& attrib_name,
178 int& result){
179 readAttrPrimitive<int>(xpath,attrib_name,result);
180 }
181
182 template<typename T>
183 void XMLReader::readPrimitive(const std::string& xpath, T& result)
184 {
185 if (Layout::primaryNode()) {
186 BasicXPathReader::get(xpath, result);
187 }
188
189 // Now broadcast back out to all nodes
191 }
192
193 template<typename T>
194 void XMLReader::readAttrPrimitive(const std::string& xpath,
195 const std::string& attrib_name,
196 T& result)
197 {
198 if (Layout::primaryNode()) {
199 BasicXPathReader::getAttribute(xpath, attrib_name, result);
200 }
201
202 // Now broadcast back out to all nodes
204 }
205
206 void XMLReader::print(std::ostream& os)
207 {
208 std::ostringstream newos;
209 std::string s;
210
212 {
213 BasicXPathReader::print(newos);
214 s = newos.str();
215 }
216
217 // Now broadcast back out to all nodes
219 os << s;
220 }
221
222 void XMLReader::printCurrentContext(std::ostream& os)
223 {
224 std::ostringstream newos;
225 std::string s;
226
228 {
229 if (is_derived())
230 BasicXPathReader::printChildren(newos);
231 else
232 BasicXPathReader::printRoot(newos);
233
234 s = newos.str();
235 }
236
237 // Now broadcast back out to all nodes
239 os << s;
240 }
241
242 int XMLReader::count(const std::string& xpath)
243 {
244 int n;
246 n = BasicXPathReader::count(xpath);
247
248 // Now broadcast back out to all nodes
250 return n;
251 }
252
253 // Namespace Registration?
254 void XMLReader::registerNamespace(const std::string& prefix, const std::string& uri)
255 {
257 BasicXPathReader::registerNamespace(prefix, uri);
258 }
259
260 // Overloaded Reader Functions
261 void read(XMLReader& xml, const std::string& xpath, std::string& result)
262 {
263 xml.get(xpath, result);
264 }
265 void read(XMLReader& xml, const std::string& xpath, char& result)
266 {
267 // Gruesome hack. For some inexplicable reason we don't have read of a char in xpath_reader.
268 std::string d;
269 xml.get(xpath, d);
270 result = d[0];
271 }
272 void read(XMLReader& xml, const std::string& xpath, int& result)
273 {
274 xml.get(xpath, result);
275 }
276 void read(XMLReader& xml, const std::string& xpath, unsigned int& result)
277 {
278 xml.get(xpath, result);
279 }
280 void read(XMLReader& xml, const std::string& xpath, short int& result)
281 {
282 xml.get(xpath, result);
283 }
284 void read(XMLReader& xml, const std::string& xpath, unsigned short int& result)
285 {
286 xml.get(xpath, result);
287 }
288 void read(XMLReader& xml, const std::string& xpath, long int& result)
289 {
290 xml.get(xpath, result);
291 }
292 void read(XMLReader& xml, const std::string& xpath, unsigned long int& result)
293 {
294 xml.get(xpath, result);
295 }
296 void read(XMLReader& xml, const std::string& xpath, float& result)
297 {
298 xml.get(xpath, result);
299 }
300 void read(XMLReader& xml, const std::string& xpath, double& result)
301 {
302 xml.get(xpath, result);
303 }
304 void read(XMLReader& xml, const std::string& xpath, bool& result)
305 {
306 xml.get(xpath, result);
307 }
308
309
311 template<typename T>
312 void readArrayPrimitive(XMLReader& xml, const std::string& s, multi1d<T>& result)
313 {
314 std::ostringstream error_message;
315
316 // Try reading the list as a string
317 std::string list_string;
318 read(xml, s, list_string);
319
320 // Count the number of elements
321 std::istringstream list_stream(list_string);
322
323 int array_size = 0;
324 T dummy;
325 while(list_stream >> dummy)
326 ++array_size;
327
328 if ((! list_stream.eof()) && list_stream.fail())
329 {
330 error_message << "Error in reading array " << s << std::endl;
331 throw error_message.str();
332 }
333
334 // It is not an error to have a zero-length array
335 // if (array_size == 0)
336 // {
337 // error_message << "Something wrong with reading array " << list_string << std::endl;
338 // throw error_message.str();
339 // }
340
341 // Now resize the array to hold the no of elements.
342 result.resize(array_size);
343
344 // Get the elements one by one
345 // I do not understand why, but use a new stringstream
346 // list_stream.str(list_string);
347 std::istringstream list_stream2(list_string);
348
349 for(int i=0; i < result.size(); i++)
350 {
351 // read the element.
352 list_stream2 >> dummy;
353 result[i]=dummy;
354 }
355 }
356
357 template<>
358 void read(XMLReader& xml, const std::string& xpath, multi1d<int>& result)
359 {
360 readArrayPrimitive<int>(xml, xpath, result);
361 }
362 template<>
363 void read(XMLReader& xml, const std::string& xpath, multi1d<unsigned int>& result)
364 {
365 readArrayPrimitive<unsigned int>(xml, xpath, result);
366 }
367 template<>
368 void read(XMLReader& xml, const std::string& xpath, multi1d<short int>& result)
369 {
370 readArrayPrimitive<short int>(xml, xpath, result);
371 }
372 template<>
373 void read(XMLReader& xml, const std::string& xpath, multi1d<unsigned short int>& result)
374 {
375 readArrayPrimitive<unsigned short int>(xml, xpath, result);
376 }
377 template<>
378 void read(XMLReader& xml, const std::string& xpath, multi1d<long int>& result)
379 {
380 readArrayPrimitive<long int>(xml, xpath, result);
381 }
382 template<>
383 void read(XMLReader& xml, const std::string& xpath, multi1d<unsigned long int>& result)
384 {
385 readArrayPrimitive<unsigned long int>(xml, xpath, result);
386 }
387 template<>
388 void read(XMLReader& xml, const std::string& xpath, multi1d<float>& result)
389 {
390 readArrayPrimitive<float>(xml, xpath, result);
391 }
392 template<>
393 void read(XMLReader& xml, const std::string& xpath, multi1d<double>& result)
394 {
395 readArrayPrimitive<double>(xml, xpath, result);
396 }
397 template<>
398 void read(XMLReader& xml, const std::string& xpath, multi1d<bool>& result)
399 {
400 readArrayPrimitive<bool>(xml, xpath, result);
401 }
402 template<>
403 void read(XMLReader& xml, const std::string& xpath, multi1d<Integer>& result)
404 {
405 readArrayPrimitive<Integer>(xml, xpath, result);
406 }
407 template<>
408 void read(XMLReader& xml, const std::string& xpath, multi1d<Real32>& result)
409 {
410 readArrayPrimitive<Real32>(xml, xpath, result);
411 }
412 template<>
413 void read(XMLReader& xml, const std::string& xpath, multi1d<Real64>& result)
414 {
415 readArrayPrimitive<Real64>(xml, xpath, result);
416 }
417 template<>
418 void read(XMLReader& xml, const std::string& xpath, multi1d<Boolean>& result)
419 {
420 readArrayPrimitive<Boolean>(xml, xpath, result);
421 }
422
423
425 template<typename T>
426 void readVectorPrimitive(XMLReader& xml, const std::string& xpath, std::vector<T>& result)
427 {
428 std::ostringstream error_message;
429
430 // Try reading the list as a string
431 std::string list_string;
432 read(xml, xpath, list_string);
433
434 // Count the number of elements
435 std::istringstream list_stream(list_string);
436
437 int array_size = 0;
438 T dummy;
439 while(list_stream >> dummy)
440 ++array_size;
441
442 if ((! list_stream.eof()) && list_stream.fail())
443 {
444 error_message << "Error in reading array " << xpath << std::endl;
445 throw error_message.str();
446 }
447
448 // Now resize the array to hold the no of elements.
449 result.resize(array_size);
450
451 // Get the elements one by one
452 // I do not understand why, but use a new stringstream
453 // list_stream.str(list_string);
454 std::istringstream list_stream2(list_string);
455
456 for(int i=0; i < result.size(); i++)
457 {
458 // read the element.
459 list_stream2 >> result[i];
460 }
461 }
462
463
465 void read(XMLReader& xml, const std::string& xpath, std::vector<int>& result)
466 {
467 readVectorPrimitive<int>(xml, xpath, result);
468 }
469 void read(XMLReader& xml, const std::string& xpath, std::vector<unsigned int>& result)
470 {
471 readVectorPrimitive<unsigned int>(xml, xpath, result);
472 }
473 void read(XMLReader& xml, const std::string& xpath, std::vector<short int>& result)
474 {
475 readVectorPrimitive<short int>(xml, xpath, result);
476 }
477 void read(XMLReader& xml, const std::string& xpath, std::vector<unsigned short int>& result)
478 {
479 readVectorPrimitive<unsigned short int>(xml, xpath, result);
480 }
481 void read(XMLReader& xml, const std::string& xpath, std::vector<long int>& result)
482 {
483 readVectorPrimitive<long int>(xml, xpath, result);
484 }
485 void read(XMLReader& xml, const std::string& xpath, std::vector<unsigned long int>& result)
486 {
487 readVectorPrimitive<unsigned long int>(xml, xpath, result);
488 }
489 void read(XMLReader& xml, const std::string& xpath, std::vector<float>& result)
490 {
491 readVectorPrimitive<float>(xml, xpath, result);
492 }
493 void read(XMLReader& xml, const std::string& xpath, std::vector<double>& result)
494 {
495 readVectorPrimitive<double>(xml, xpath, result);
496 }
497//void read(XMLReader& xml, const std::string& xpath, std::vector<bool>& result)
498//{
499// readVectorPrimitive<bool>(xml, xpath, result);
500//}
501 template<>
502 void read(XMLReader& xml, const std::string& xpath, std::vector<Integer>& result)
503 {
504 readVectorPrimitive<Integer>(xml, xpath, result);
505 }
506 template<>
507 void read(XMLReader& xml, const std::string& xpath, std::vector<Real32>& result)
508 {
509 readVectorPrimitive<Real32>(xml, xpath, result);
510 }
511 template<>
512 void read(XMLReader& xml, const std::string& xpath, std::vector<Real64>& result)
513 {
514 readVectorPrimitive<Real64>(xml, xpath, result);
515 }
516 template<>
517 void read(XMLReader& xml, const std::string& xpath, std::vector<Boolean>& result)
518 {
519 readVectorPrimitive<Boolean>(xml, xpath, result);
520 }
521
522
523
525 template<typename T>
526 void readListPrimitive(XMLReader& xml, const std::string& xpath, std::list<T>& result)
527 {
528 result.clear();
529
530 // Try reading the list as a string
531 std::string list_string;
532 read(xml, xpath, list_string);
533
534 // Parse the string by white-space
535 std::istringstream list_stream(list_string);
536
537 T dummy;
538 while(list_stream >> dummy)
539 {
540 result.push_back(dummy);
541 }
542 }
543
544
546 void read(XMLReader& xml, const std::string& xpath, std::list<int>& result)
547 {
548 readListPrimitive<int>(xml, xpath, result);
549 }
550 void read(XMLReader& xml, const std::string& xpath, std::list<unsigned int>& result)
551 {
552 readListPrimitive<unsigned int>(xml, xpath, result);
553 }
554 void read(XMLReader& xml, const std::string& xpath, std::list<short int>& result)
555 {
556 readListPrimitive<short int>(xml, xpath, result);
557 }
558 void read(XMLReader& xml, const std::string& xpath, std::list<unsigned short int>& result)
559 {
560 readListPrimitive<unsigned short int>(xml, xpath, result);
561 }
562 void read(XMLReader& xml, const std::string& xpath, std::list<long int>& result)
563 {
564 readListPrimitive<long int>(xml, xpath, result);
565 }
566 void read(XMLReader& xml, const std::string& xpath, std::list<unsigned long int>& result)
567 {
568 readListPrimitive<unsigned long int>(xml, xpath, result);
569 }
570 void read(XMLReader& xml, const std::string& xpath, std::list<float>& result)
571 {
572 readListPrimitive<float>(xml, xpath, result);
573 }
574 void read(XMLReader& xml, const std::string& xpath, std::list<double>& result)
575 {
576 readListPrimitive<double>(xml, xpath, result);
577 }
578 void read(XMLReader& xml, const std::string& xpath, std::list<bool>& result)
579 {
580 readListPrimitive<bool>(xml, xpath, result);
581 }
582 template<>
583 void read(XMLReader& xml, const std::string& xpath, std::list<Integer>& result)
584 {
585 readListPrimitive<Integer>(xml, xpath, result);
586 }
587 template<>
588 void read(XMLReader& xml, const std::string& xpath, std::list<Real32>& result)
589 {
590 readListPrimitive<Real32>(xml, xpath, result);
591 }
592 template<>
593 void read(XMLReader& xml, const std::string& xpath, std::list<Real64>& result)
594 {
595 readListPrimitive<Real64>(xml, xpath, result);
596 }
597 template<>
598 void read(XMLReader& xml, const std::string& xpath, std::list<Boolean>& result)
599 {
600 readListPrimitive<Boolean>(xml, xpath, result);
601 }
602
603
604
605
606 //--------------------------------------------------------------------------------
607 // XML writer base class
609 {
610 }
611
615
616 void XMLWriter::openSimple(const std::string& tagname)
617 {
618 openTag(tagname);
619 }
620
622 {
623 closeTag();
624 }
625
626 void XMLWriter::openStruct(const std::string& tagname)
627 {
628 openTag(tagname);
629 }
630
632 {
633 closeTag();
634 }
635
636 void XMLWriter::openTag(const std::string& tagname)
637 {
639 XMLSimpleWriter::openTag(tagname);
640 }
641
642 void XMLWriter::openTag(const std::string& nsprefix, const std::string& tagname)
643 {
645 XMLSimpleWriter::openTag(nsprefix,tagname);
646 }
647
648 void XMLWriter::openTag(const std::string& tagname, XMLWriterAPI::AttributeList& al)
649 {
651 XMLSimpleWriter::openTag(tagname,al);
652 }
653
654 void XMLWriter::openTag(const std::string& nsprefix,
655 const std::string& tagname,
656 XMLWriterAPI::AttributeList& al)
657 {
659 XMLSimpleWriter::openTag(nsprefix,tagname,al);
660 }
661
663 {
665 XMLSimpleWriter::closeTag();
666 }
667
668 void XMLWriter::emptyTag(const std::string& tagname)
669 {
671 XMLSimpleWriter::emptyTag(tagname);
672 }
673 void XMLWriter::emptyTag(const std::string& tagname, XMLWriterAPI::AttributeList& al)
674 {
676 XMLSimpleWriter::emptyTag(tagname,al);
677 }
678
679 void XMLWriter::emptyTag(const std::string& nsprefix,
680 const std::string& tagname,
681 XMLWriterAPI::AttributeList& al)
682 {
684 XMLSimpleWriter::emptyTag(nsprefix,tagname,al);
685 }
686
687
688 // Overloaded Writer Functions
689 void XMLWriter::write(const std::string& output)
690 {
692 XMLSimpleWriter::write(output);
693 }
694 void XMLWriter::write(const int& output)
695 {
697 XMLSimpleWriter::write(output);
698 }
699 void XMLWriter::write(const unsigned int& output)
700 {
702 XMLSimpleWriter::write(output);
703 }
704 void XMLWriter::write(const short int& output)
705 {
707 XMLSimpleWriter::write(output);
708 }
709 void XMLWriter::write(const unsigned short int& output)
710 {
712 XMLSimpleWriter::write(output);
713 }
714 void XMLWriter::write(const long int& output)
715 {
717 XMLSimpleWriter::write(output);
718 }
719 void XMLWriter::write(const unsigned long int& output)
720 {
722 XMLSimpleWriter::write(output);
723 }
724 void XMLWriter::write(const float& output)
725 {
727 XMLSimpleWriter::write(output);
728 }
729 void XMLWriter::write(const double& output)
730 {
732 XMLSimpleWriter::write(output);
733 }
734 void XMLWriter::write(const bool& output)
735 {
737 XMLSimpleWriter::write(output);
738 }
739
740 // Write XML std::string
741 void XMLWriter::writeXML(const std::string& output)
742 {
744 XMLSimpleWriter::writeXML(output);
745 }
746
747
748 // Push a group name
749 void push(XMLWriter& xml, const std::string& s) {xml.openStruct(s);}
750
751 // Pop a group name
752 void pop(XMLWriter& xml) {xml.closeStruct();}
753
754 // Write something from a reader
755 void write(XMLWriter& xml, const std::string& s, const XMLReader& d)
756 {
757 xml.openTag(s);
758 xml << d;
759 xml.closeTag();
760 }
761
763 {
764 std::ostringstream os;
765 const_cast<XMLReader&>(d).printCurrentContext(os);
766 xml.writeXML(os.str());
767 return xml;
768 }
769
770 // Write something from a XMLBufferWriter
771 void write(XMLWriter& xml, const std::string& s, const XMLBufferWriter& d)
772 {
773 xml.openTag(s);
774 xml << d;
775 xml.closeTag();
776 }
777
779 {
780 xml.writeXML(const_cast<XMLBufferWriter&>(d).printCurrentContext());
781 return xml;
782 }
783
784 // Time to build a telephone book of basic primitives
785 template<typename T>
786 void writePrimitive(XMLWriter& xml, const std::string& s, const T& d)
787 {
788 xml.openTag(s);
789 xml.write(d);
790 xml.closeTag();
791 }
792
793 void write(XMLWriter& xml, const std::string& s, const std::string& d)
794 {
796 }
797
798 void write(XMLWriter& xml, const std::string& s, const char* d)
799 {
800 writePrimitive<std::string>(xml, s, std::string(d));
801 }
802
803 void write(XMLWriter& xml, const std::string& s, const char& d)
804 {
805 // Gruesome hack. For some inexplicable reason we don't have a proper write of a char in xpath_reader.
806 writePrimitive<std::string>(xml, s, std::string(1,d));
807 }
808
809 void write(XMLWriter& xml, const std::string& s, const int& d)
810 {
811 writePrimitive<int>(xml, s, d);
812 }
813
814 void write(XMLWriter& xml, const std::string& s, const unsigned int& d)
815 {
817 }
818
819 void write(XMLWriter& xml, const std::string& s, const short int& d)
820 {
821 writePrimitive<short int>(xml, s, d);
822 }
823
824 void write(XMLWriter& xml, const std::string& s, const unsigned short int& d)
825 {
827 }
828
829 void write(XMLWriter& xml, const std::string& s, const long int& d)
830 {
831 writePrimitive<long int>(xml, s, d);
832 }
833
834 void write(XMLWriter& xml, const std::string& s, const unsigned long int& d)
835 {
837 }
838
839 void write(XMLWriter& xml, const std::string& s, const float& d)
840 {
841 writePrimitive<float>(xml, s, d);
842 }
843
844 void write(XMLWriter& xml, const std::string& s, const double& d)
845 {
846 writePrimitive<double>(xml, s, d);
847 }
848
849 void write(XMLWriter& xml, const std::string& s, const bool& d)
850 {
851 writePrimitive<bool>(xml, s, d);
852 }
853
854 // Versions that do not print a name
855 XMLWriter& operator<<(XMLWriter& xml, const std::string& d) {xml.write(d);return xml;}
856 XMLWriter& operator<<(XMLWriter& xml, const char* d) {xml.write(std::string(d));return xml;}
857 XMLWriter& operator<<(XMLWriter& xml, const char& d) {xml.write(d);return xml;}
858 XMLWriter& operator<<(XMLWriter& xml, const int& d) {xml.write(d);return xml;}
859 XMLWriter& operator<<(XMLWriter& xml, const unsigned int& d) {xml.write(d);return xml;}
860 XMLWriter& operator<<(XMLWriter& xml, const short int& d) {xml.write(d);return xml;}
861 XMLWriter& operator<<(XMLWriter& xml, const unsigned short int& d) {xml.write(d);return xml;}
862 XMLWriter& operator<<(XMLWriter& xml, const long int& d) {xml.write(d);return xml;}
863 XMLWriter& operator<<(XMLWriter& xml, const unsigned long int& d) {xml.write(d);return xml;}
864 XMLWriter& operator<<(XMLWriter& xml, const float& d) {xml.write(d);return xml;}
865 XMLWriter& operator<<(XMLWriter& xml, const double& d) {xml.write(d);return xml;}
866 XMLWriter& operator<<(XMLWriter& xml, const bool& d) {xml.write(d);return xml;}
867
868
869 // Write an array of basic types
870 template<typename T>
871 void writeArrayPrimitive(XMLWriter& xml, const std::string& s, const multi1d<T>& s1)
872 {
873 std::ostringstream output;
874
875 if (s1.size() > 0)
876 {
877 output << s1[0];
878 for(int index=1; index < s1.size(); index++)
879 output << " " << s1[index];
880 }
881
882 // Write the array - do not use a normal string write
883 xml.openTag(s);
884 xml << output.str();
885 xml.closeTag();
886 }
887
888
889 template<>
890 void write(XMLWriter& xml, const std::string& xpath, const multi1d<int>& output)
891 {
892 writeArrayPrimitive<int>(xml, xpath, output);
893 }
894 template<>
895 void write(XMLWriter& xml, const std::string& xpath, const multi1d<unsigned int>& output)
896 {
897 writeArrayPrimitive<unsigned int>(xml, xpath, output);
898 }
899 template<>
900 void write(XMLWriter& xml, const std::string& xpath, const multi1d<short int>& output)
901 {
902 writeArrayPrimitive<short int>(xml, xpath, output);
903 }
904 template<>
905 void write(XMLWriter& xml, const std::string& xpath, const multi1d<unsigned short int>& output)
906 {
907 writeArrayPrimitive<unsigned short int>(xml, xpath, output);
908 }
909 template<>
910 void write(XMLWriter& xml, const std::string& xpath, const multi1d<long int>& output)
911 {
912 writeArrayPrimitive<long int>(xml, xpath, output);
913 }
914 template<>
915 void write(XMLWriter& xml, const std::string& xpath, const multi1d<unsigned long int>& output)
916 {
917 writeArrayPrimitive<unsigned long int>(xml, xpath, output);
918 }
919 template<>
920 void write(XMLWriter& xml, const std::string& s, const multi1d<float>& s1)
921 {
922 std::ostringstream output;
923 output.precision(7);
924
925 if (s1.size() > 0)
926 {
927 output << s1[0];
928 for(int index=1; index < s1.size(); index++)
929 output << " " << s1[index];
930 }
931
932 // Write the array - do not use a normal string write
933 xml.openTag(s);
934 xml << output.str();
935 xml.closeTag();
936 }
937 template<>
938 void write(XMLWriter& xml, const std::string& s, const multi1d<double>& s1)
939 {
940 std::ostringstream output;
941 output.precision(15);
942
943 if (s1.size() > 0)
944 {
945 output << s1[0];
946 for(int index=1; index < s1.size(); index++)
947 output << " " << s1[index];
948 }
949
950 // Write the array - do not use a normal string write
951 xml.openTag(s);
952 xml << output.str();
953 xml.closeTag();
954 }
955 template<>
956 void write(XMLWriter& xml, const std::string& xpath, const multi1d<bool>& output)
957 {
958 writeArrayPrimitive<bool>(xml, xpath, output);
959 }
960 template<>
961 void write(XMLWriter& xml, const std::string& xpath, const multi1d<Integer>& output)
962 {
963 writeArrayPrimitive<Integer>(xml, xpath, output);
964 }
965 template<>
966 void write(XMLWriter& xml, const std::string& s, const multi1d<Real32>& s1)
967 {
968 std::ostringstream output;
969 output.precision(7);
970
971 if (s1.size() > 0)
972 {
973 output << s1[0];
974 for(int index=1; index < s1.size(); index++)
975 output << " " << s1[index];
976 }
977
978 // Write the array - do not use a normal string write
979 xml.openTag(s);
980 xml << output.str();
981 xml.closeTag();
982 }
983 template<>
984 void write(XMLWriter& xml, const std::string& s, const multi1d<Real64>& s1)
985 {
986 std::ostringstream output;
987 output.precision(15);
988
989 if (s1.size() > 0)
990 {
991 output << s1[0];
992 for(int index=1; index < s1.size(); index++)
993 output << " " << s1[index];
994 }
995
996 // Write the array - do not use a normal string write
997 xml.openTag(s);
998 xml << output.str();
999 xml.closeTag();
1000 }
1001 template<>
1002 void write(XMLWriter& xml, const std::string& xpath, const multi1d<Boolean>& output)
1003 {
1004 writeArrayPrimitive<Boolean>(xml, xpath, output);
1005 }
1006
1007
1008 // Write a vector of basic types
1009 template<typename T>
1010 void writeVectorPrimitive(XMLWriter& xml, const std::string& s, const std::vector<T>& s1)
1011 {
1012 std::ostringstream output;
1013
1014 if (s1.size() > 0)
1015 {
1016 output << s1[0];
1017 for(unsigned index=1; index < s1.size(); index++)
1018 output << " " << s1[index];
1019 }
1020
1021 // Write the array - do not use a normal string write
1022 xml.openTag(s);
1023 xml << output.str();
1024 xml.closeTag();
1025 }
1026
1027
1028 void write(XMLWriter& xml, const std::string& xpath, const std::vector<int>& output)
1029 {
1030 writeVectorPrimitive<int>(xml, xpath, output);
1031 }
1032 void write(XMLWriter& xml, const std::string& xpath, const std::vector<unsigned int>& output)
1033 {
1034 writeVectorPrimitive<unsigned int>(xml, xpath, output);
1035 }
1036 void write(XMLWriter& xml, const std::string& xpath, const std::vector<short int>& output)
1037 {
1038 writeVectorPrimitive<short int>(xml, xpath, output);
1039 }
1040 void write(XMLWriter& xml, const std::string& xpath, const std::vector<unsigned short int>& output)
1041 {
1042 writeVectorPrimitive<unsigned short int>(xml, xpath, output);
1043 }
1044 void write(XMLWriter& xml, const std::string& xpath, const std::vector<long int>& output)
1045 {
1046 writeVectorPrimitive<long int>(xml, xpath, output);
1047 }
1048 void write(XMLWriter& xml, const std::string& xpath, const std::vector<unsigned long int>& output)
1049 {
1050 writeVectorPrimitive<unsigned long int>(xml, xpath, output);
1051 }
1052 template<>
1053 void write(XMLWriter& xml, const std::string& s, const std::vector<float>& s1)
1054 {
1055 std::ostringstream output;
1056 output.precision(7);
1057
1058 if (s1.size() > 0)
1059 {
1060 output << s1[0];
1061 for(int index=1; index < s1.size(); index++)
1062 output << " " << s1[index];
1063 }
1064
1065 // Write the array - do not use a normal string write
1066 xml.openTag(s);
1067 xml << output.str();
1068 xml.closeTag();
1069 }
1070 template<>
1071 void write(XMLWriter& xml, const std::string& s, const std::vector<double>& s1)
1072 {
1073 std::ostringstream output;
1074 output.precision(15);
1075
1076 if (s1.size() > 0)
1077 {
1078 output << s1[0];
1079 for(int index=1; index < s1.size(); index++)
1080 output << " " << s1[index];
1081 }
1082
1083 // Write the array - do not use a normal string write
1084 xml.openTag(s);
1085 xml << output.str();
1086 xml.closeTag();
1087 }
1088 void write(XMLWriter& xml, const std::string& xpath, const std::vector<bool>& output)
1089 {
1090 writeVectorPrimitive<bool>(xml, xpath, output);
1091 }
1092 template<>
1093 void write(XMLWriter& xml, const std::string& xpath, const std::vector<Integer>& output)
1094 {
1095 writeVectorPrimitive<Integer>(xml, xpath, output);
1096 }
1097 template<>
1098 void write(XMLWriter& xml, const std::string& s, const std::vector<Real32>& s1)
1099 {
1100 std::ostringstream output;
1101 output.precision(7);
1102
1103 if (s1.size() > 0)
1104 {
1105 output << s1[0];
1106 for(int index=1; index < s1.size(); index++)
1107 output << " " << s1[index];
1108 }
1109
1110 // Write the array - do not use a normal string write
1111 xml.openTag(s);
1112 xml << output.str();
1113 xml.closeTag();
1114 }
1115 template<>
1116 void write(XMLWriter& xml, const std::string& s, const std::vector<Real64>& s1)
1117 {
1118 std::ostringstream output;
1119 output.precision(15);
1120
1121 if (s1.size() > 0)
1122 {
1123 output << s1[0];
1124 for(int index=1; index < s1.size(); index++)
1125 output << " " << s1[index];
1126 }
1127
1128 // Write the array - do not use a normal string write
1129 xml.openTag(s);
1130 xml << output.str();
1131 xml.closeTag();
1132 }
1133 template<>
1134 void write(XMLWriter& xml, const std::string& xpath, const std::vector<Boolean>& output)
1135 {
1136 writeVectorPrimitive<Boolean>(xml, xpath, output);
1137 }
1138
1139
1140
1141 // Write a vector of basic types
1142 template<typename T>
1143 void writeListPrimitive(XMLWriter& xml, const std::string& s, const std::list<T>& s1)
1144 {
1145 std::ostringstream output;
1146
1147 if (s1.size() > 0)
1148 {
1149 typename std::list<T>::const_iterator t = s1.begin();
1150
1151 output << *t;
1152 ++t;
1153 for(; t != s1.end(); ++t)
1154 output << " " << *t;
1155 }
1156
1157 // Write the array - do not use a normal string write
1158 xml.openTag(s);
1159 xml << output.str();
1160 xml.closeTag();
1161 }
1162
1163
1164 void write(XMLWriter& xml, const std::string& xpath, const std::list<int>& output)
1165 {
1166 writeListPrimitive<int>(xml, xpath, output);
1167 }
1168 void write(XMLWriter& xml, const std::string& xpath, const std::list<unsigned int>& output)
1169 {
1170 writeListPrimitive<unsigned int>(xml, xpath, output);
1171 }
1172 void write(XMLWriter& xml, const std::string& xpath, const std::list<short int>& output)
1173 {
1174 writeListPrimitive<short int>(xml, xpath, output);
1175 }
1176 void write(XMLWriter& xml, const std::string& xpath, const std::list<unsigned short int>& output)
1177 {
1178 writeListPrimitive<unsigned short int>(xml, xpath, output);
1179 }
1180 void write(XMLWriter& xml, const std::string& xpath, const std::list<long int>& output)
1181 {
1182 writeListPrimitive<long int>(xml, xpath, output);
1183 }
1184 void write(XMLWriter& xml, const std::string& xpath, const std::list<unsigned long int>& output)
1185 {
1186 writeListPrimitive<unsigned long int>(xml, xpath, output);
1187 }
1188 template<>
1189 void write(XMLWriter& xml, const std::string& s, const std::list<float>& s1)
1190 {
1191 std::ostringstream output;
1192 output.precision(7);
1193
1194 if (s1.size() > 0)
1195 {
1196 std::list<float>::const_iterator t = s1.begin();
1197
1198 output << *t;
1199 ++t;
1200 for(; t != s1.end(); ++t)
1201 output << " " << *t;
1202 }
1203
1204 // Write the array - do not use a normal string write
1205 xml.openTag(s);
1206 xml << output.str();
1207 xml.closeTag();
1208 }
1209 template<>
1210 void write(XMLWriter& xml, const std::string& s, const std::list<double>& s1)
1211 {
1212 std::ostringstream output;
1213 output.precision(15);
1214
1215 if (s1.size() > 0)
1216 {
1217 std::list<double>::const_iterator t = s1.begin();
1218
1219 output << *t;
1220 ++t;
1221 for(; t != s1.end(); ++t)
1222 output << " " << *t;
1223 }
1224
1225 // Write the array - do not use a normal string write
1226 xml.openTag(s);
1227 xml << output.str();
1228 xml.closeTag();
1229 }
1230 void write(XMLWriter& xml, const std::string& xpath, const std::list<bool>& output)
1231 {
1232 writeListPrimitive<bool>(xml, xpath, output);
1233 }
1234 template<>
1235 void write(XMLWriter& xml, const std::string& xpath, const std::list<Integer>& output)
1236 {
1237 writeListPrimitive<Integer>(xml, xpath, output);
1238 }
1239 template<>
1240 void write(XMLWriter& xml, const std::string& s, const std::list<Real32>& s1)
1241 {
1242 std::ostringstream output;
1243 output.precision(7);
1244
1245 if (s1.size() > 0)
1246 {
1247 std::list<Real32>::const_iterator t = s1.begin();
1248
1249 output << *t;
1250 ++t;
1251 for(; t != s1.end(); ++t)
1252 output << " " << *t;
1253 }
1254
1255 // Write the array - do not use a normal string write
1256 xml.openTag(s);
1257 xml << output.str();
1258 xml.closeTag();
1259 }
1260 template<>
1261 void write(XMLWriter& xml, const std::string& s, const std::list<Real64>& s1)
1262 {
1263 std::ostringstream output;
1264 output.precision(15);
1265
1266 if (s1.size() > 0)
1267 {
1268 std::list<Real64>::const_iterator t = s1.begin();
1269
1270 output << *t;
1271 ++t;
1272 for(; t != s1.end(); ++t)
1273 output << " " << *t;
1274 }
1275
1276 // Write the array - do not use a normal string write
1277 xml.openTag(s);
1278 xml << output.str();
1279 xml.closeTag();
1280 }
1281 template<>
1282 void write(XMLWriter& xml, const std::string& xpath, const std::list<Boolean>& output)
1283 {
1284 writeListPrimitive<Boolean>(xml, xpath, output);
1285 }
1286
1287 //--------------------------------------------------------------------------------
1288 // XML writer to a buffer
1290
1291 XMLBufferWriter::XMLBufferWriter(const std::string& s) {open(s);}
1292
1293 void XMLBufferWriter::open(const std::string& s)
1294 {
1295 if (Layout::primaryNode())
1296 output_stream.str(s);
1297 }
1298
1300 {
1301 std::ostringstream s;
1302
1303 if (Layout::primaryNode())
1304 {
1305 writePrologue(s);
1306 s << output_stream.str() << "\n";
1307 }
1308
1309 return s.str();
1310 }
1311
1312 string XMLBufferWriter::printCurrentContext() const {return output_stream.str();}
1313
1315
1316
1317 //--------------------------------------------------------------------------------
1318 // XML Writer to a file
1320
1321 void XMLFileWriter::open(const std::string& filename, bool write_prologue)
1322 {
1323 if (Layout::primaryNode())
1324 {
1325 output_stream.open(filename.c_str(), std::ofstream::out);
1326 if (output_stream.fail())
1327 {
1328 QDPIO::cerr << "Error opening write file = " << filename << std::endl;
1329 QDP_abort(1);
1330 }
1331 if (write_prologue)
1332 writePrologue(output_stream);
1333 }
1334
1335 indent_level=0;
1336 }
1337
1338
1340 {
1341 if (is_open())
1342 {
1343 if (Layout::primaryNode())
1344 output_stream.close();
1345 }
1346 }
1347
1348 // Propagate status to all nodes
1350 {
1351 bool s = QDP_isInitialized();
1352
1353 if (s)
1354 {
1355 if (Layout::primaryNode())
1356 s = output_stream.is_open();
1357
1359 }
1360
1361 return s;
1362 }
1363
1364
1365 // Flush the buffer
1367 {
1368 if (is_open())
1369 {
1370 if (Layout::primaryNode())
1371 output_stream.flush();
1372 }
1373 }
1374
1375 // Propagate status to all nodes
1377 {
1378 bool s = QDP_isInitialized();
1379
1380 if (s)
1381 {
1382 if (Layout::primaryNode())
1383 s = output_stream.fail();
1384
1386 }
1387
1388 return s;
1389 }
1390
1392
1393
1394 //--------------------------------------------------------------------------------
1395 // XML handle class for arrays
1397 {
1398 if (initP)
1399 closeArray();
1400 }
1401
1402 void XMLArrayWriter::openArray(const string& tagname)
1403 {
1404 // QDP_info("openArray: stack_empty = %d tagname=%s",
1405 // (contextStack.empty()) ? 1 : 0,
1406 // tagname.c_str());
1407
1408 if (initP)
1409 QDP_error_exit("XMLArrayWriter: calling openArray twice");
1410
1411 if (arrayTag)
1412 QDP_error_exit("XMLArrayWriter: internal error - array tag already written");
1413
1414 if (! contextStack.empty())
1415 QDP_error_exit("XMLArrayWriter: context stack not empty");
1416
1417 qname = tagname;
1418 elem_qname = "elem"; // for now fix the name - maintains internal consistency
1419
1420 openTag(qname); // array tagname
1421
1422 initP = false; // not fully initialized yet
1423 arrayTag = true;
1424 }
1425
1427 {
1428 // QDP_info("closeArray");
1429
1430 if (! initP)
1431 QDP_error_exit("XMLArrayWriter: calling closeArray but not initialized");
1432
1433 if (! contextStack.empty())
1434 QDP_error_exit("XMLArrayWriter: context stack not empty");
1435
1436 closeTag(); // array tagname
1437
1438 if (array_size > 0 && elements_written != array_size)
1439 QDP_error_exit("XMLArrayWriter: failed to write all the %d required elements: instead = %d",
1440 array_size,elements_written);
1441
1442 initP = arrayTag = false;
1443 elements_written = 0;
1444 indent_level = 0;
1445 simpleElements = false; // do not know this yet
1446 }
1447
1448 void XMLArrayWriter::openStruct(const string& tagname)
1449 {
1450 // QDP_info("openStruct: stack_empty = %d tagname=%s",
1451 // (contextStack.empty()) ? 1 : 0,
1452 // tagname.c_str());
1453
1454 if (! arrayTag)
1455 {
1456 openArray(tagname);
1457 return;
1458 }
1459
1460 if (! initP)
1461 {
1462 if (elements_written == 0)
1463 {
1464 // This is the first time this is called
1465 // From now on, all elements must be STRUCT
1466 simpleElements = false;
1467 }
1468 else
1469 QDP_error_exit("XMLArrayWriter: internal error - data written but state not initialized");
1470
1471 initP = true;
1472 }
1473
1474 if (simpleElements)
1475 QDP_error_exit("XMLArrayWriter: suppose to write simple types but asked to write a struct");
1476
1477
1478 if (contextStack.empty())
1479 openTag(elem_qname); // ignore user provided name and use default name
1480 else
1481 openTag(tagname); // use user provided name
1482
1483 ElementType el = STRUCT;
1484 contextStack.push(el);
1485 }
1486
1488 {
1489 // QDP_info("closeStruct: stack_empty = %d",
1490 // (contextStack.empty()) ? 1 : 0);
1491
1492 if (! initP)
1493 QDP_error_exit("XMLArrayWriter: calling closeStruct but not initialized");
1494
1495 if (contextStack.empty())
1496 {
1497 // QDP_error_exit("XMLArrayWriter: context stack empty - probably no openStruct");
1498 closeArray();
1499 return;
1500 }
1501
1502 ElementType topval = contextStack.top();
1503 if (topval != STRUCT)
1504 QDP_error_exit("XMLArrayWriter: found closeStruct without corresponding openStruct");
1505
1506 contextStack.pop();
1507
1508 closeTag(); // struct (or elem_qname) tagname
1509
1510 if (contextStack.empty())
1511 {
1512 elements_written++;
1513 // QDP_info("finished writing element %d",elements_written);
1514 }
1515 }
1516
1517 // Push a group name
1518 void push(XMLArrayWriter& xml) {xml.openStruct("");}
1519
1520 // Pop a group name
1521 void pop(XMLArrayWriter& xml) {xml.closeStruct();}
1522
1523
1524} // namespace QDP;
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)
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
~XMLBufferWriter()
Destroy.
std::string str() const
Return entire buffer as a string.
void open(const std::string &s)
Construct from a string.
void flush()
Flush the buffer.
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
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 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
bool primaryNode()
Returns whether this is the primary node.
StandardOutputStream cerr
Definition qdp_stdio.cc:22
void broadcast(T &dest)
Broadcast from primary node to all other nodes.
void broadcast_str(std::string &result)
Broadcast a string from primary node to all other nodes.
Yet another random number generator.
void writePrimitive(XMLWriter &xml, const std::string &s, const T &d)
Definition qdp_xmlio.cc:786
bool QDP_isInitialized()
Is the machine initialized?
void readListPrimitive(XMLReader &xml, const std::string &xpath, std::list< T > &result)
Read a XML list element.
Definition qdp_xmlio.cc:526
void QDP_error_exit(const char *format,...)
Simple error display and abort routine.
Definition qdp_util.cc:93
void readVectorPrimitive(XMLReader &xml, const std::string &xpath, std::vector< T > &result)
Read a XML vector element.
Definition qdp_xmlio.cc:426
void push(XMLWriter &xml, const std::string &s)
Push a group name.
Definition qdp_xmlio.cc:749
void writeListPrimitive(XMLWriter &xml, const std::string &s, const std::list< T > &s1)
void pop(XMLWriter &xml)
Pop a group name.
Definition qdp_xmlio.cc:752
void readArrayPrimitive(XMLReader &xml, const std::string &s, multi1d< T > &result)
Read a XML multi1d element.
Definition qdp_xmlio.cc:312
void QDP_abort(int status)
Panic button.
void writeVectorPrimitive(XMLWriter &xml, const std::string &s, const std::vector< T > &s1)
void writeArrayPrimitive(XMLWriter &xml, const std::string &s, const multi1d< T > &s1)
Definition qdp_xmlio.cc:871
Primary include file for QDP.