muMECH  1.0
tixy2.h
Go to the documentation of this file.
1 #ifndef GELIB_TIXY_H
2 #define GELIB_TIXY_H
3 
4 // There is conflict name XMLDocument in msxml and tinyxml.
5 // Solution 1:
6 // This define have to be before first win32 include.
7 #define WIN32_LEAN_AND_MEAN
8 
9 #include "gelib.h"
10 #include "tinyxml2.h"
11 
12 
18 using namespace tinyxml2;
19 
20 namespace gelibspace {
21 
22 // Solution 2:
23 // #include "gelib.h"
24 // #include "tinyxml2.h"
25 //
26 // namespace TXML = tinyxml2;
27 // XMLDocument -> TXML::XMLDocument
28 
30 
31 
35 
36 XMLDocument* openTXdcPN (const char *path, const char *name);
37 XMLDocument* openTXdcPNSS (const char *path, const char *name, const char *suff, const char *suff2);
38 
39 
40 
45 
46 class Stream
47 {
48 
49  protected:
50  char state;
52  const char *filename;
53 
54  FILE *str_file;
56  const char *str_string;
57 
58  public:
60  Stream (void) { state = '-'; type = STRM_void; filename = NULL; str_file = NULL; str_tixel = NULL; str_string = NULL; }
62  Stream (Stream_type tp) { state = '-'; type = tp; filename = NULL; str_file = NULL; str_tixel = NULL; str_string = NULL; }
64  Stream (FILE *stream) { state = '-'; type = STRM_file; filename = NULL; str_file = stream; str_tixel = NULL; str_string = NULL; }
65  Stream (XMLNode *stream) { state = '-'; type = STRM_tixel; filename = NULL; str_file = NULL; str_tixel = stream; str_string = NULL; }
66  Stream (const char *stream) { state = '-'; type = STRM_string; filename = NULL; str_file = NULL; str_tixel = NULL; str_string = stream; }
68  virtual ~Stream() { delete [] filename; }
69 
71 
75  void open (Stream_type t, const char *rw, const char *&fn, XMLNode *node=NULL)
76  {
77  if (state != '-') errol;
78  //# only these rw are accepted
79  if (rw[1] != '\0') errol;
80  if (*rw != 'r' && *rw != 'w') errol;
81  state = *rw;
82  type = t;
83  if (filename) delete [] filename;
84  filename = fn; fn = NULL;
85 
86  switch (type) {
87  case STRM_file: this->str_file = _openFileN (rw, filename, filename); break;
88  case STRM_tixel:
89  if (state == 'w') this->str_tixel = node;
90  else if (state == 'r') errol;
91  else errol;
92  break;
93  case STRM_void: {
94  if (*rw != 'r' || node) errol;
95 
96  FILE *in = _openFileN("rb", filename, filename);
97  char c = getc(in); ungetc(c, in);
98 
99  if (c=='#') { type = STRM_file; str_file = in; }
100  else if (c=='<') { type = STRM_tixel; str_tixel = new XMLDocument(true, COLLAPSE_WHITESPACE);
101  XMLError err = ((XMLDocument*)str_tixel)->LoadFile(in);
102  if (err!=XML_NO_ERROR && err!=XML_SUCCESS) _errorr2 ("File %s has not valid syntax", filename);
103  fclose (in); }
104  else _errorr2 ("File %s doesnot start neither with character '#' nor '<'", filename);
105 
106  break;
107  }
108  case STRM_string: errol; break;
109  default: _errorr ("unsuported stream type");
110  }
111  }
112 
113  void close (void)
114  {
115  switch (type) {
116  case STRM_file: fclose (str_file); break;
117  case STRM_tixel:
118  if (state == 'w') str_tixel->GetDocument()->SaveFile(filename);
119  else if (state == 'r') ;
120  else errol;
121 
122  delete str_tixel->GetDocument();
123  break;
124  case STRM_string: errol; break;
125  default: _errorr ("unsuported stream type");
126  }
127 
128  state = '-';
129  }
130 
131 
132  //void initiate (XMLNode *stream) { checkin_type(STRM_tixel); if (str_tixel != NULL) _errorr ("not nul"); str_tixel = stream; }
133  //void reinitiate (XMLNode *stream) { checkin_type(STRM_tixel); str_tixel = stream; }
134 
135  void redefine (FILE *stream) { type = STRM_file; str_file = stream; str_tixel = NULL; str_string = NULL; }
136  void redefine (XMLNode *stream) { type = STRM_tixel; str_file = NULL; str_tixel = stream; str_string = NULL; }
137  void redefine (const char *stream) { type = STRM_string; str_file = NULL; str_tixel = NULL; str_string = stream; }
138 
140  FILE * file(void) { check_type(STRM_file); return str_file; }
141  XMLNode * tixnod(void) { check_type(STRM_tixel); return str_tixel; }
142  XMLElement * tixel(void) { check_type(STRM_tixel); return str_tixel->ToElement(); }
143  //XMLDocument* tixdoc(void) { check_type(STRM_tixel); return str_tixel->ToDocument(); }
144  XMLDocument* tix_doc(void) { check_type(STRM_tixel); return str_tixel->GetDocument(); }
145 
146  const char * string(void) { check_type(STRM_string); return str_string;}
147  const char ** pstring(void){ check_type(STRM_string); return &str_string;}
148 
149  Stream_type give_type (void) { return type; }
150 
151  bool isFile (void) { return (type == STRM_file); }
152  bool isTixel (void) { return (type == STRM_tixel); }
153  bool isString (void) { return (type == STRM_string); }
154 
155  bool relink_downF (void) { check_type(STRM_tixel); str_tixel = this->str_tixel->FirstChild(); return (str_tixel) ? true : false; }
156  bool relink_downL (void) { check_type(STRM_tixel); str_tixel = this->str_tixel->LastChild(); return (str_tixel) ? true : false; }
157  bool relink_up (void) { check_type(STRM_tixel); str_tixel = this->str_tixel->Parent(); return (str_tixel) ? true : false; }
158  bool relink_next (void) { check_type(STRM_tixel); if (str_tixel->NextSibling()) { str_tixel = str_tixel->NextSibling(); return true; } else return false; }
159  bool relink_prev (void) { check_type(STRM_tixel); if (str_tixel->PreviousSibling()) { str_tixel = str_tixel->PreviousSibling(); return true; } else return false; }
160 
162  void skip_commnet (void) { check_type(STRM_tixel); while (true) if (str_tixel->ToComment() != NULL) str_tixel = str_tixel->NextSibling(); else break; }
163 
164  private:
165  void check_type (Stream_type tp) {
166  if (type != tp)
167  _errorr ("wrong type of Stream"); }
168  void checkin_type (Stream_type tp) { if (type == STRM_void) type = tp; else if (type != tp) _errorr ("wrong type of Stream"); }
169 };
170 
171 
172 
173 
177 bool ST_scan_number (Stream *src, int &dest);
178 bool ST_scan_number (Stream *src, long &dest);
179 bool ST_scan_number (Stream *src, double &dest);
180 //
182 bool ST_scan_array (Stream *src, int n, int *dest);
183 bool ST_scan_array (Stream *src, int n, long *dest);
184 bool ST_scan_array (Stream *src, int n, double *dest);
185 
187 bool ST_scan_expected_number (Stream *src, long expctd);
188 
190 template <class ArgType>
191 void ST_scan_expected_number_FL (const char* file, int line, Stream *src, ArgType expctd, const char *msg)
192 {
193  if (!ST_scan_expected_number (src, expctd))
194  errorr_expected (file, line, msg, expctd, ""); // misto "" by tam mel byt retezec s prectenym cislem, casem to dodelej, asi budes muset predelat errorr_expected
195 }
196 #define ST_scan_expected_number_exit(_1,_2,_3) ST_scan_expected_number_FL (__FILE__, __LINE__, _1,_2,_3)
197 
202 long GP_scan_line (Stream * stream, char *dest);
204 long GP_scan_word (Stream * stream, char *dest);
206 long GP_scan_line_alloc (Stream * stream, char *&dest);
207 
208 
209 //* ********************************************************************************************
210 //* *** *** *** *** docasne XML FCES *** *** *** ***
211 //* ********************************************************************************************
212 const XMLElement * XP_give_unique_expected_elem (const XMLNode *xelem, const char * name, bool uniq=true);
213 void XP_check_expected_attribute (const XMLNode *xelem, const char *name, const char *value);
214 const char * XP_giveDAtext (const XMLNode *xelem, int n, bool last, const char *format, const char *type, const char *name, int *noc=NULL);
215 
216 
217 } // namespace gelibspace
218 
219 #endif // GELIB_TIXY_H
const char * str_string
Definition: tixy2.h:56
Stream(FILE *stream)
CONSTRUCTOR.
Definition: tixy2.h:64
General functions.
#define _openFileN(_1, _2, _3)
Definition: gelib.h:182
void XP_check_expected_attribute(const XMLNode *xelem, const char *name, const char *value)
Definition: tixy2.cpp:134
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.
Definition: tinyxml2.h:603
long GP_scan_line(Stream *stream, char *dest)
... line; return value is length of the line
Definition: tixy2.cpp:84
Stream(const char *stream)
Definition: tixy2.h:66
const XMLElement * XP_give_unique_expected_elem(const XMLNode *xelem, const char *name, bool uniq)
Definition: tixy2.cpp:125
XMLDocument * tix_doc(void)
Definition: tixy2.h:144
long GP_scan_line_alloc(Stream *stream, char *&dest)
... line; return value is length of the line; dest is NULL and allocated here
Definition: tixy2.cpp:95
XMLNode * tixnod(void)
Definition: tixy2.h:141
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
Definition: tinyxml2.h:671
XMLElement * tixel(void)
Definition: tixy2.h:142
FILE * str_file
Definition: tixy2.h:54
The element is a container class.
Definition: tinyxml2.h:1116
void open(Stream_type t, const char *rw, const char *&fn, XMLNode *node=NULL)
*** SET ***
Definition: tixy2.h:75
bool ST_scan_expected_number(Stream *src, long expctd)
... number and compare with expected one
Definition: tixy2.cpp:75
XMLNode * str_tixel
Definition: tixy2.h:55
void redefine(XMLNode *stream)
Definition: tixy2.h:136
const char * filename
Definition: tixy2.h:52
Stream_type give_type(void)
Definition: tixy2.h:149
#define errol
Definition: gelib.h:151
Stream(Stream_type tp)
CONSTRUCTOR.
Definition: tixy2.h:62
bool relink_downL(void)
Definition: tixy2.h:156
#define _errorr2(_1, _2)
Definition: gelib.h:154
FILE * file(void)
*** GET ***
Definition: tixy2.h:140
bool isString(void)
Definition: tixy2.h:153
bool ST_scan_number(Stream *src, int &dest)
*** *** *** *** TINYXML FCE *** *** *** ***
Definition: tixy2.cpp:36
A Document binds together all the functionality.
Definition: tinyxml2.h:1445
void check_type(Stream_type tp)
Definition: tixy2.h:165
VTKrange
Definition: tixy2.h:29
Stream_type type
Definition: tixy2.h:51
XMLError SaveFile(const char *filename, bool compact=false)
Save the XML file to disk.
Definition: tinyxml2.cpp:1656
const XMLNode * Parent() const
Get the parent of this node on the DOM.
Definition: tinyxml2.h:657
Stream_type
*** *** *** *** CLASS STREAM *** *** *** ***
Definition: tixy2.h:44
XMLNode is a base class for every object that is in the XML Document Object Model (DOM)...
Definition: tinyxml2.h:579
void errorr_expected(const char *file, int line, const char *msg, const char *expctd, const char *src)
*** *** *** *** STRING PROCESSING *** *** *** *** src pointer is always shifted over the skipped/sc...
Definition: librw.cpp:537
bool relink_downF(void)
Definition: tixy2.h:155
bool relink_prev(void)
Definition: tixy2.h:159
const char * string(void)
Definition: tixy2.h:146
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
Definition: tinyxml2.h:586
void ST_scan_expected_number_FL(const char *file, int line, Stream *src, ArgType expctd, const char *msg)
... number and compare with expected one, exit if false
Definition: tixy2.h:191
#define _errorr(_1)
Definition: gelib.h:160
void close(void)
Definition: tixy2.h:113
const XMLNode * PreviousSibling() const
Get the previous (left) sibling node of this node.
Definition: tinyxml2.h:707
virtual ~Stream()
DESTRUCTOR.
Definition: tixy2.h:68
void checkin_type(Stream_type tp)
Definition: tixy2.h:168
bool relink_next(void)
Definition: tixy2.h:158
Stream(XMLNode *stream)
Definition: tixy2.h:65
long GP_scan_word(Stream *stream, char *dest)
... word; return value is length of the word
Definition: tixy2.cpp:112
bool isTixel(void)
Definition: tixy2.h:152
const char * XP_giveDAtext(const XMLNode *xelem, int n, bool last, const char *format, const char *type, const char *name, int *noc)
Definition: tixy2.cpp:145
void redefine(FILE *stream)
Definition: tixy2.h:135
void redefine(const char *stream)
Definition: tixy2.h:137
XMLDocument * openTXdcPNSS(const char *path, const char *name, const char *suff, const char *suff2)
const XMLNode * LastChild() const
Get the last child node, or null if none exists.
Definition: tinyxml2.h:689
bool isFile(void)
Definition: tixy2.h:151
bool relink_up(void)
Definition: tixy2.h:157
XMLDocument * openTXdcPN(const char *path, const char *name)
*** *** *** *** TINYXML FCE *** *** *** ***
Stream(void)
CONSTRUCTOR.
Definition: tixy2.h:60
bool ST_scan_array(Stream *src, int n, int *dest)
scan/copy array of numbers from src to dest, src pointer is shifted over the field ...
Definition: tixy2.cpp:55
const char ** pstring(void)
Definition: tixy2.h:147
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
Definition: tinyxml2.h:595
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
Definition: tinyxml2.h:723
void skip_commnet(void)
if this XMLnode is XMLComment, skip to next noncomment
Definition: tixy2.h:162