INMOST
A toolkit for distributed mathematical modeling
inmost_xml.h
1 #ifndef INMOST_XML_INCLUDED
2 #define INMOST_XML_INCLUDED
3 
4 #include "inmost.h"
5 
6 namespace INMOST
7 {
8  std::string CharToHex(char c);
9 #if defined(USE_MESH)
10  std::string ReferenceToString(INMOST::HandleType h, int pos);
11 #endif
12 #if defined(USE_AUTODIFF)
13  std::string VariableToString(INMOST::variable v, bool noder=false);
14 #endif
15 
16 
17 
18  class XMLReader
19  {
20  class Interpreter
21  {
22  bool error_state;
23  std::vector<std::string> Expand(const std::string & input) const;
24  std::vector<std::string> MakePolish(const std::vector<std::string> & input);
25  void Print(const std::vector<std::string> & polish) const;
26  double Run(const std::vector<std::string> & polish);
27  public:
28  Interpreter();
29  Interpreter(const Interpreter & b);
30  Interpreter & operator = (Interpreter const & b);
31  double Evaluate(const std::string & str);
32  bool isError();
33  void ClearError();
34  };
35  Interpreter intrp;
36  struct Stream
37  {
38  std::string src;
39  std::istream * s;
40  int linebreak, linechar;
41  int hadlinebreak, hadlinechar;
42  };
43  std::vector<Stream> inp;
44  int verbose;
45  enum State
46  {
47  Intro, //read tag or read tag contents
48  WaitTag, //wait tag name or comment
49  ReadTag, //reading in tag name
50  ReadCommentExclamation, //skipping comments
51  ReadCommentQuestion, //skipping comments
52  WaitAttribute, //reading attribute name
53  ReadAttribute,
54  WaitAttributeValue, //read attribute value
55  ReadAttributeValue,
56  ReadAttributeValueQuote,
57  EndTag, //tag ended read closing
58  WaitCloseTag,
59  ReadCloseTagSlash,
60  ReadCloseTagName,
61 
62  WaitContentsOpen,
63  WaitContents,
64  ReadContents, //parse a word
65  ReadContentsVector, // {0,1,2,3}
66  ReadContentsQuotes, // "hello world"
67  ReadContentsMultiplier, // 123*5
68  ReadContentsMultiplierSkopes, // 123*(SetSize/2)
69  EndContents,
70  ReadVector,
71 
72  EndOfFile, // end of file reached
73  Failure //unexpected error
74  } _state;
75  Stream & get_Stream();
76  const Stream & get_Stream() const;
77  std::istream & get_iStream();
78  const std::istream & get_iStream() const;
79  //should not share the reference to the stream with another reader
80  XMLReader(const XMLReader & other);
81  XMLReader & operator =(XMLReader & other);
82  char GetChar();
83  //return one character back to the stream
84  void RetChar();
85  void SkipComments(State RetState);
86  std::string StateName(State s) const;
87  public:
90  void SetVerbosity(int verbosity) {verbose = verbosity;}
91  void Report(const char * fmt, ...) const;
92  XMLReader(std::string sourcename, std::istream & input);
93  void PushStream(std::string file);
94  void PopStream();
95  //wait for '<' on input,
96  //returns true and changes state to WaitTag if '<' encountered,
97  //otherwise returns false
98  bool ExpectOpenTag();
99  //read in <TagName returns TagName
100  std::string ReadOpenTag();
101  //read > or /> skipping for attributes
102  int ReadCloseTag();
103  bool isTagFinish() const;
104  //read </TagName> or fail
105  bool ReadFinishTag(std::string TagName);
106  //read next attribute name, check isTagEnded
107  std::string AttributeName();
108  //read value of the attribute after reading it's name
109  std::string AttributeValue();
110  // > or /> was reached, should close ReadCloseTag
111  // to finalize
112  bool isTagEnded() const;
113  //read in <![CDATA[
114  //Quick and dirty, rewrite with states!
115  bool ReadOpenContents();
116  //get next full word inside contents
117  std::string GetContentsWord();
118  //read ]]>
119  bool ReadCloseContents();
120  //]]> was reached, should call ReadCloseContents
121  //to finalize
122  bool isContentsEnded() const;
123  bool isFailure() const;
124  bool isEof() const;
125 
126 #if defined(USE_MESH)
127  INMOST::ElementType atoes(const char * _str);
128  INMOST::ElementType atoe(const char * _str);
129  std::pair<INMOST::ElementType,int> atoh(const char * _str);
130  std::pair<std::string,std::pair<INMOST::ElementType,int> > atorh(const char * _str);
131 #endif
132 #if defined(USE_AUTODIFF)
133  INMOST::variable atov(const char * _str);
134 #endif
135  int EvaluateExpression(std::string expression);
136  int ConvertMultiplier(std::string expression, int SetSize);
137  void SplitValueMultiplier(std::string expression, std::string & value, std::string & multiplier);
138  bool ParseBool(std::string word);
139  void ParseCommaSeparated(std::string word, std::vector<std::string> & parsed, char symbol = ',');
140 #if defined(USE_MESH)
141  void ParseReal(std::string word, std::vector<INMOST::Storage::real> & Vector, int & Repeat, int SetSize);
142 #if defined(USE_AUTODIFF)
143  void ParseVariable(std::string word, std::vector<INMOST::Storage::var> & Vector, int & Repeat, int SetSize);
144 #endif
145  void ParseInteger(std::string word, std::vector<INMOST::Storage::integer> & Vector, int & Repeat, int SetSize);
146 #endif
147  void ParseBulk(std::string word, std::string & Vector, int & Repeat, int SetSize);
148 #if defined(USE_MESH)
149  void ParseReference(std::string word, std::vector<std::pair<INMOST::ElementType,int> > & Vector, int & Repeat, int SetSize);
150  void ParseRemoteReference(std::string word, std::vector< std::pair<std::string,std::pair<INMOST::ElementType,int> > > & Vector, int & Repeat, int SetSize);
151 #endif
152 
154  struct XMLAttrib
155  {
156  std::string name; //< Name of the attribute.
157  std::string value; //< Value of the attribute.
158  };
159 
161  struct XMLTag
162  {
163  std::string name; //<Name of the XML tag.
164  std::vector<XMLAttrib> attributes; //<List of attributes.
165  int finish; //<Whether to close the tag.
166 
167 
169  bool RawData() const {return finish == 5;}
171  bool BlockData() const {return finish == 4;}
173  bool Failure() const {return finish == 0;}
175  bool Process() const {return finish == 1;}
177  bool Stub() const {return finish == 2;}
179  bool Finalize() const {return finish == 3;}
181  const XMLAttrib & GetAttrib(int n) const {return attributes[n];}
183  XMLAttrib & GetAttrib(int n) {return attributes[n];}
185  int NumAttrib() const {return (int)attributes.size();}
187  std::string GetName() const {return name;}
188  };
189 
190  XMLTag OpenTag();
191  bool CloseTag(XMLTag & tag);
192 
194  struct XMLTree
195  {
196  XMLTag tag; //< tag information, such as name and attributes.
197  std::vector<XMLTree> children; //< Children inside XML tag.
198  std::string contents; //< Text inside of the tag.
199 
202  int FindChild(std::string name, int offset = -1) const;
205  int FindAttrib(std::string name, int offset = -1) const;
207  const XMLTree & GetChild(int n) const {return children[n];}
210  const XMLTree * GetChild(std::string name) const;
213  const XMLTree * GetChildWithAttrib(std::string name, std::string value) const;
215  int NumChildren() const {return (int)children.size();}
217  const XMLAttrib & GetAttrib(int n) const {return tag.GetAttrib(n);}
220  const std::string & GetAttrib(std::string name) const;
222  int NumAttrib() const {return tag.NumAttrib();}
224  std::string GetName() const {return tag.GetName();}
226  const std::string & GetContents() const {return contents;}
227  };
228 
229 
230 private:
231  std::string ReadUntil(std::string stop);
232  int ReadXMLSub(XMLTree & root);
233 public:
238  };
239 
240  void WriteXML(const XMLReader::XMLTree & t, std::ostream & output, int offset = 0);
241 
242  typedef std::vector<XMLReader::XMLTree>::iterator xml_reader_tree_iterator_t;
243  typedef std::vector<XMLReader::XMLAttrib>::iterator xml_reader_attrib_iterator_t;
244 }
245 
246 
247 #endif //INMOST_XML_INCLUDED
void SetVerbosity(int verbosity)
2 - lot's of output 1 - output key steps, currently in ReadXML
Definition: inmost_xml.h:90
XMLTree ReadXML()
Read entire XML file into structure, it may be more efficient to read the file incrementally,...
A class that represents a variable with multiple first order variations.
Structure for xml attribute.
Definition: inmost_xml.h:155
Structure for xml tag with attributes.
Definition: inmost_xml.h:162
const XMLAttrib & GetAttrib(int n) const
Retrieve attribute number n.
Definition: inmost_xml.h:181
std::string GetName() const
Retrieve the name of the tag.
Definition: inmost_xml.h:187
bool Failure() const
Was not able to read the tag.
Definition: inmost_xml.h:173
bool Stub() const
Tag was read but do not have internal contents.
Definition: inmost_xml.h:177
XMLAttrib & GetAttrib(int n)
Retrieve attribute number n.
Definition: inmost_xml.h:183
int NumAttrib() const
Retrieve number of attributes.
Definition: inmost_xml.h:185
bool Finalize() const
Tag was not red, finish of enclosing tag was encountered.
Definition: inmost_xml.h:179
bool BlockData() const
This is data within ![CDATA[ wrap.
Definition: inmost_xml.h:171
bool Process() const
Tag was read and have internal contents, can process the contents.
Definition: inmost_xml.h:175
bool RawData() const
This is data without ![CDATA[ wrap.
Definition: inmost_xml.h:169
Structure defining entire XML file.
Definition: inmost_xml.h:195
const XMLTree * GetChildWithAttrib(std::string name, std::string value) const
Retrieve a child of current XML tag with attribute Returns NULL if not found.
const std::string & GetAttrib(std::string name) const
Retrieve attribute of current XML tag with name.
const std::string & GetContents() const
Retrieve contents of the tag.
Definition: inmost_xml.h:226
int FindChild(std::string name, int offset=-1) const
Return next occurence of XML tag with the specified name.
int FindAttrib(std::string name, int offset=-1) const
Return next occurence of XML attribute with the specified name.
const XMLTree & GetChild(int n) const
Retrieve a child of current XML tag with number n.
Definition: inmost_xml.h:207
int NumAttrib() const
Retrieve number of attributes.
Definition: inmost_xml.h:222
const XMLAttrib & GetAttrib(int n) const
Retrieve attribute of current XML tag with number n.
Definition: inmost_xml.h:217
std::string GetName() const
Retrieve the name of the tag.
Definition: inmost_xml.h:224
const XMLTree * GetChild(std::string name) const
Retrieve a child of current XML tag with name Returns NULL if not found.
int NumChildren() const
Retrieve number of children.
Definition: inmost_xml.h:215