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 
21 #include "Typedefs.h"
22 
23 using namespace std ;
24 
25 enum class ArrayType {particles, contacts} ;
26 enum class EncodingType {ascii, base64} ;
27 class XMLWriter
28 {
29 public:
30  XMLWriter (string path) {fic.open(path.c_str()) ; if (!fic) printf("ERR: cannot open %s in writing \n", path.c_str()) ; }
31  void header (int dimension, string input) ;
32  void writeTs (double ts, tuple<string,vector<vector<double>>*, ArrayType> a);
33  void startTS (double ts) ;
34  void writeArray(string name, vector<vector<double>>*x, int beg, int length, ArrayType t, EncodingType te=EncodingType::ascii) ;
35  void writeArray(string name, std::vector< std::vector< double > >* x, ArrayType t, EncodingType te=EncodingType::ascii) ;
36  void writeArray(string name, std::vector<double>* x, ArrayType t, EncodingType te=EncodingType::ascii) ;
37  void stopTS () ;
38  void close() ;
39  void emergencyclose() ;
40 
41  void openbranch (string name, vector < pair<string,string>> attributes) ;
42  void openbranch (string name) {return openbranch(name, {}) ; }
43  template <class T> void smallbranch (string name, T value) ;
44  template <class T> void smallbranch (string name, vector < pair<string,string>> attributes, T value) ;
45  bool closebranch() ;
46 
47  ofstream fic ;
48 
49 
50 private:
51  vector <pair<double,streampos>> index ;
52  int encodebase64f (ostream &out, vector<double>& val) ;
53  void encodebase64f_end (ostream &out) {static char lst[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" ; if (state!=0) out <<lst[remainer] ; remainer=state=0 ; }
54  unsigned char state=0, remainer=0 ;
55 
56  stack<string> hierarchy;
57 
58  string quote (string a) {return "'"+a+"'" ; }
59 } ;
60 //===============================================================
62 {
63 public:
64  XMLReader_base (string path) ;
65  stack<string> tree ;
66  ifstream fic ;
67  std::pair<string, map<string,string>> gettag() ;
68  tuple <string, map<string,string> , std::vector<double> > gettagdata() ;
69  string getcontent() ;
70  void close () {fic.close() ; }
71 } ;
72 
73 class XMLReader : public XMLReader_base
74 {
75 public:
76  XMLReader(string path): XMLReader_base(path) {tags=gettag() ; if (tags.first!="demnd") printf("ERR:unexpected first entry (should be demnd)\n") ;
77  cout << "File date: "<< tags.second["date"]<< "\nInput file: " << tags.second["input"] <<"\nDimensions: "<< tags.second["dimensions"] <<"\n" ;
78  };
79  int read_boundaries (vector <vector <double>> &boundaries) ;
80  int read_radius (vector <double> &radius) ;
81  double read_nextts(vector<string> &names, vector<vector<vector<double>>> & data) ;
82  std::vector<std::pair<double,std::streampos>> read_index () ;
83  int decodebase64f (istream &in, vector<float>& val) ;
84  int decodebase64f_2dd (istream &in, vector<vector<double>>& val) ;
85 
86  std::pair<string, map<string,string>> tags ;
87 } ;
88 
89 #endif
@ particles
Definition: Parameters.h:8
@ contacts
Definition: Parameters.h:8
EncodingType
Definition: Xml.h:26
ArrayType
Definition: Xml.h:25
Definition: Xml.h:62
ifstream fic
Definition: Xml.h:66
stack< string > tree
Definition: Xml.h:65
void close()
Definition: Xml.h:70
Definition: Xml.h:74
std::pair< string, map< string, string > > tags
Definition: Xml.h:86
XMLReader(string path)
Definition: Xml.h:76
Definition: Xml.h:28
void encodebase64f_end(ostream &out)
Definition: Xml.h:53
string quote(string a)
Definition: Xml.h:58
void writeArray(string name, std::vector< std::vector< double > > *x, ArrayType t, EncodingType te=EncodingType::ascii)
XMLWriter(string path)
Definition: Xml.h:30
vector< pair< double, streampos > > index
Definition: Xml.h:51
void openbranch(string name)
Definition: Xml.h:42
stack< string > hierarchy
Definition: Xml.h:56
void writeArray(string name, std::vector< double > *x, ArrayType t, EncodingType te=EncodingType::ascii)
ofstream fic
Definition: Xml.h:47
@ 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