Support for g++-4.1.
[gf1.git] / xmlite.h
blob8a9bccfb2fb10b0f3af9f04a065d24e41a6bb707
1 /*
2 ** $Id$
3 **
4 ** code for reading and writing data in an xml-like format
5 **
6 ** the output from these routines may look a lot like xml, and may even
7 ** be wel-formed xml, but I don't think you should use it as that.
8 */
9 /*
10 ** Copyright (C) 2000 Kurt Van den Branden
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ** This program is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ** GNU General Public License for more details.
21 **
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 ** the following is an example of what you should be able to store:
30 <gipfgame type="tournament">
32 <whiteplayer type="human" fulltime="1200" timeleft="1197.562988" gipf="yes">
33 White</whiteplayer>
34 <blackplayer type="computer" fulltime="1200" timeleft="1196" gipf="no">
35 Black</blackplayer>
36 <nextplayer>white</nextplayer>
38 <!-- in between state of removing rows or gipf-pieces -->
40 <board>
41 <whitepieces>16</whitepieces>
42 <whitelost>0</whitelost>
43 <blackpieces>16</blackpieces>
44 <blacklost>0</blacklost>
45 <position color="white" type="piece">e2</position>
46 <position color="black" type="gipf">d5</position>
47 </board>
49 <gamelog>
50 <move nr="1" player="white" type="gipf">e1-e2</move>
51 <move nr="1" player="black" type="piece">e1-e3</move>
52 <removerow owner="white">
53 <position color="black" type="piece">e2</position>
54 <position color="white" type="piece">e3</position>
55 <position color="white" type="piece">e4</position>
56 <position color="white" type="gipf">e5</position>
57 </removerow>
58 </gamelog>
60 </gipfgame>
64 #ifndef _XMLITE_H_
65 #define _XMLITE_H_ 1
67 #include <string>
68 #include <ext/hash_map>
69 #include <vector>
71 using namespace std;
72 using namespace __gnu_cxx;
74 struct eqstring
76 bool operator()(const string & s1, const string & s2) const
78 return (s1 == s2);
83 struct stringhash
85 bool operator()(const string & s) const
87 hash<const char *> H;
88 return (H (s.c_str()));
93 class xmlite_entity {
94 public:
95 xmlite_entity (const char * ename);
96 xmlite_entity (const string & ename);
97 xmlite_entity (const xmlite_entity & source)
98 { (*this) = source; };
99 ~xmlite_entity ();
101 xmlite_entity & operator= (const xmlite_entity & source);
103 int addcontent (xmlite_entity * newcontent);
104 int addattribute (const string & aname, const string & avalue);
105 int setvalue (const string & newvalue);
107 int writetofile (const char * filename);
109 string * escapechars (const string & instring);
110 void escapechars (string & instring);
111 string * unescapechars (const string & instring);
112 void unescapechars (string & instring);
114 xmlite_entity * getcontentbynr (const int nr = 0);
115 xmlite_entity * getcontentbyname (const string & name,
116 const int nr = 0);
117 const string & getvalue ()
118 { return (value); };
119 const string & getname ()
120 { return (name); };
121 const string & getattribute (const string key)
122 { return (attributes[key]); };
124 private:
125 string name;
126 string value;
127 vector<xmlite_entity *> contents;
128 hash_map<string, string, stringhash, eqstring> attributes;
130 int writetostream (ostream & ostr);
131 void replaceall (string & input, const string & from, const string & to);
135 class xmlite_parser {
136 public:
137 xmlite_parser (istream * inputstream) :
138 mystream (false), istr (inputstream)
139 { parseerror = "no parsing done yet."; };
140 xmlite_parser (const char * filename);
141 ~xmlite_parser ()
142 { if (mystream) delete (istr); };
144 xmlite_entity * parse ();
145 xmlite_entity * readentity (const string & startname);
146 void getnextc ();
147 void skipwhitespace ();
148 void readstring (string & str, const char * endchar);
149 void readattribute (xmlite_entity * ent);
151 string & geterror ()
152 { return (parseerror); };
154 private:
155 bool mystream;
156 istream * istr;
157 char nextc;
159 string parseerror;
162 #endif