NDDEM
Xml.h
Go to the documentation of this file.
1 #ifndef XML_H
2 #define XML_H
3 
4 #include <cstdio>
5 #include <cstdlib>
6 #include <fstream>
7 #include <chrono>
8 #include <iostream>
9 #include <ctime>
10 #include <vector>
11 #include <tuple>
12 #include <stack>
13 #include <cstring>
14 #include <iostream>
15 #include <iomanip>
16 #include <sstream>
17 #include <map>
18 #include <algorithm>
19 #include <stack>
20 #include <optional>
21 
22 #include "Typedefs.h"
23 
24 using namespace std ;
25 
26 enum class ArrayType {particles, contacts} ;
27 enum class EncodingType {ascii, base64} ;
28 class XMLWriter
29 {
30 public:
31  XMLWriter (string path, std::optional<size_t> restart_location = {}) {
32  if (restart_location.has_value())
33  {
34  fic.open(path, std::ios::in | std::ios::out) ;
35  fic.seekp(restart_location.value(), std::ios_base::beg) ;
36  }
37  else
38  {
39  fic.open(path.c_str()) ;
40  }
41  if (!fic) printf("ERR: cannot open %s in writing \n", path.c_str()) ;
42  }
43  void header (int dimension, string input) ;
44  void writeTs (double ts, tuple<string,vector<vector<double>>*, ArrayType> a);
45  void startTS (double ts) ;
46  void writeArray(string name, vector<vector<double>>*x, int beg, int length, ArrayType t, EncodingType te=EncodingType::ascii) ;
47  void writeArray(string name, std::vector< std::vector< double > >* x, ArrayType t, EncodingType te=EncodingType::ascii) ;
48  void writeArray(string name, std::vector<double>* x, ArrayType t, EncodingType te=EncodingType::ascii) ;
49  void stopTS () ;
50  void close() ;
51  void emergencyclose() ;
52  streamoff get_file_location() {return static_cast<std::streamoff>(fic.tellp()) ; }
53 
54  void openbranch (string name, vector < pair<string,string>> attributes) ;
55  void openbranch (string name) {return openbranch(name, {}) ; }
56  template <class T> void smallbranch (string name, T value) ;
57  template <class T> void smallbranch (string name, vector < pair<string,string>> attributes, T value) ;
58  bool closebranch() ;
59 
60  ofstream fic ;
61 
62  template <class Archive>
63  void save(Archive &ar) const {
64  ar(index_ts, index_loc) ;
65  }
66  template <class Archive>
67  void load(Archive &ar) {
68  ar(index_ts, index_loc) ;
69  hierarchy.push("demnd") ;
70  }
71 
72 private:
73  //vector <pair<double,streampos>> index ;
74  vector <double> index_ts ;
75  vector <std::streamoff> index_loc ;
76  int encodebase64f (ostream &out, vector<double>& val) ;
77  void encodebase64f_end (ostream &out) {static char lst[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" ; if (state!=0) out <<lst[remainer] ; remainer=state=0 ; }
78  unsigned char state=0, remainer=0 ;
79 
80  stack<string> hierarchy;
81 
82  string quote (string a) {return "'"+a+"'" ; }
83 } ;
84 //===============================================================
86 {
87 public:
88  XMLReader_base (string path) ;
89  stack<string> tree ;
90  ifstream fic ;
91  std::pair<string, map<string,string>> gettag() ;
92  tuple <string, map<string,string> , std::vector<double> > gettagdata() ;
93  string getcontent() ;
94  void close () {fic.close() ; }
95 } ;
96 
97 class XMLReader : public XMLReader_base
98 {
99 public:
100  XMLReader(string path): XMLReader_base(path) {tags=gettag() ; if (tags.first!="demnd") printf("ERR:unexpected first entry (should be demnd)\n") ;
101  cout << "File date: "<< tags.second["date"]<< "\nInput file: " << tags.second["input"] <<"\nDimensions: "<< tags.second["dimensions"] <<"\n" ;
102  };
103  int read_boundaries (vector <vector <double>> &boundaries) ;
104  int read_radius (vector <double> &radius) ;
105  double read_nextts(vector<string> &names, vector<vector<vector<double>>> & data) ;
106  std::vector<std::pair<double,std::streampos>> read_index () ;
107  int decodebase64f (istream &in, vector<float>& val) ;
108  int decodebase64f_2dd (istream &in, vector<vector<double>>& val) ;
109 
110  std::pair<string, map<string,string>> tags ;
111 } ;
112 
113 #endif
@ particles
Definition: Parameters.h:8
@ contacts
Definition: Parameters.h:8
EncodingType
Definition: Xml.h:27
ArrayType
Definition: Xml.h:26
Definition: Xml.h:86
ifstream fic
Definition: Xml.h:90
stack< string > tree
Definition: Xml.h:89
void close()
Definition: Xml.h:94
Definition: Xml.h:98
std::pair< string, map< string, string > > tags
Definition: Xml.h:110
XMLReader(string path)
Definition: Xml.h:100
Definition: Xml.h:29
vector< double > index_ts
Definition: Xml.h:74
void encodebase64f_end(ostream &out)
Definition: Xml.h:77
string quote(string a)
Definition: Xml.h:82
streamoff get_file_location()
Definition: Xml.h:52
void writeArray(string name, std::vector< std::vector< double > > *x, ArrayType t, EncodingType te=EncodingType::ascii)
void openbranch(string name)
Definition: Xml.h:55
vector< std::streamoff > index_loc
Definition: Xml.h:75
XMLWriter(string path, std::optional< size_t > restart_location={})
Definition: Xml.h:31
stack< string > hierarchy
Definition: Xml.h:80
void save(Archive &ar) const
Definition: Xml.h:63
void load(Archive &ar)
Definition: Xml.h:67
void writeArray(string name, std::vector< double > *x, ArrayType t, EncodingType te=EncodingType::ascii)
ofstream fic
Definition: Xml.h:60
@ radius
Definition: Typedefs.h:19
Definition: json.hpp:5678
void close(FILE *out)
Definition: Vtk.h:123
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181