]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxexpr.h
Unicode support.
[wxWidgets.git] / include / wx / wxexpr.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxexpr.h
3 // Purpose: Prolog-like file I/O, used by resource system.
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXEXPRH__
13 #define _WX_WXEXPRH__
14
15 #ifdef __GNUG__
16 #pragma interface "wxexpr.h"
17 #endif
18
19 #include <stdio.h>
20
21 #include "wx/defs.h"
22 #include "wx/string.h"
23
24 #include "wx/ioswrap.h"
25
26 #include "wx/list.h"
27 #include "wx/hash.h"
28
29 #include "wx/expr.h"
30
31 // Compatibility
32 #define PrologExpr wxExpr
33 #define PrologDatabase wxExprDatabase
34 #define proioErrorHandler wxExprErrorHandler
35 #define PROIO_ERROR_GENERAL 1
36 #define PROIO_ERROR_SYNTAX 2
37 #define PrologNull wxExprNull
38 #define PrologInteger wxExprInteger
39 #define PrologReal wxExprReal
40 #define PrologWord wxExprWord
41 #define PrologString wxExprString
42 #define PrologList wxExprList
43 #define PrologType wxExprType
44
45 // Error types
46 #define WXEXPR_ERROR_GENERAL 1
47 #define WXEXPR_ERROR_SYNTAX 2
48
49 // Error handler function definition. If app returns TRUE,
50 // carry on processing.
51 typedef bool (*wxExprErrorHandler) (int errorType, char *msg);
52
53 WXDLLEXPORT_DATA(extern wxExprErrorHandler) currentwxExprErrorHandler;
54
55 extern "C" WXDLLEXPORT_DATA(FILE*) yyin;
56
57 extern "C" WXDLLEXPORT int yyparse(void);
58
59 typedef enum {
60 wxExprNull,
61 wxExprInteger,
62 wxExprReal,
63 wxExprWord,
64 wxExprString,
65 wxExprList
66 } wxExprType;
67
68 class WXDLLEXPORT wxExprDatabase;
69
70 class WXDLLEXPORT wxExpr
71 {
72 public:
73 wxObject *client_data;
74 wxExprType type;
75 union {
76 long integer;
77 char *word;
78 char *string;
79 double real;
80 wxExpr *first; // If is a list expr, points to the first node
81 } value;
82
83 wxExpr *next; // If this is a node in a list, points to the next node
84 wxExpr *last; // If is a list expr, points to the last node
85
86 wxExpr(wxExprType the_type, char *word_or_string, bool allocate);
87 wxExpr(const wxString& functor); // Assume this is a new clause - pass functor
88 wxExpr(wxExprType the_type, const wxString& word_or_string = "");
89 wxExpr(long the_integer);
90 wxExpr(double the_real);
91 wxExpr(wxList *the_list);
92 ~wxExpr(void);
93
94 inline wxExprType Type(void) const { return type; }
95 inline long IntegerValue(void) const
96 {
97 if (type == wxExprInteger)
98 return value.integer;
99 else if (type == wxExprReal)
100 return (long)value.real;
101 else return 0;
102 }
103
104 inline double RealValue(void) const {
105 if (type == wxExprReal)
106 return value.real;
107 else if (type == wxExprInteger)
108 return (double)value.integer;
109 else return (double)0.0;
110 }
111
112 inline wxString WordValue(void) const {
113 if (type == wxExprWord)
114 return value.word;
115 else if (type == wxExprString)
116 return wxString(value.string);
117 else return wxString("");
118 }
119
120 inline wxString StringValue(void) const {
121 if (type == wxExprString)
122 return wxString(value.string);
123 else if (type == wxExprWord)
124 return wxString(value.word);
125 else return wxString("");
126 }
127
128 // Get nth arg of clause (starting from 1)
129 wxExpr *Arg(wxExprType type, int arg) const;
130
131 // Return nth argument of a list expression (starting from zero)
132 wxExpr *Nth(int arg) const;
133
134 // Returns the number of elements in a list expression
135 int Number(void) const;
136
137 // Make a clone
138 wxExpr *Copy(void) const;
139
140 wxExpr *GetAttributeValueNode(const wxString& word) const; // Use only for a clause or list
141 wxExpr *AttributeValue(const wxString& word) const; // Use only for a clause
142 wxString Functor(void) const; // Only for a clause
143 bool IsFunctor(const wxString& s) const; // Only for a clause
144 void WriteClause(ostream& stream); // Write this expression as a top-level clause
145 void WriteExpr(ostream& stream); // Write as any other subexpression
146 void WriteLispExpr(ostream& stream);
147
148 // Append an expression to a list
149 void Append(wxExpr *expr);
150 // Insert at beginning of list
151 void Insert(wxExpr *expr);
152
153 // Get first expr in list
154 inline wxExpr *GetFirst(void) const { return ((type == wxExprList) ? value.first : (wxExpr*)NULL); }
155
156 // Get next expr if this is a node in a list
157 inline wxExpr *GetNext(void) const { return next; }
158
159 // Get last expr in list
160 inline wxExpr *GetLast(void) const { return ((type == wxExprList) ? last : (wxExpr*)NULL); }
161
162 // This should really be called SetAttributeValue since any existing
163 // attribute-value is deleted first.
164 void AddAttributeValue(const wxString& attribute, long value);
165 void AddAttributeValue(const wxString& attribute, double value);
166 void AddAttributeValueWord(const wxString& attribute, const wxString& value);
167 void AddAttributeValueString(const wxString& attribute, const wxString& value);
168 void AddAttributeValue(const wxString& attribute, wxList *value);
169 void AddAttributeValue(const wxString& attribute, wxExpr *value);
170 void AddAttributeValueStringList(const wxString& attribute, wxList *string_list);
171
172 void DeleteAttributeValue(const wxString& attribute);
173
174 bool GetAttributeValue(const wxString& att, int& var) const;
175 bool GetAttributeValue(const wxString& att, long& var) const;
176 bool GetAttributeValue(const wxString& att, float& var) const;
177 bool GetAttributeValue(const wxString& att, double& var) const;
178 bool GetAttributeValue(const wxString& att, wxString& var) const; // Word OR string -> string
179 bool GetAttributeValue(const wxString& att, wxExpr **var) const;
180
181 // Compatibility with old PrologIO
182 inline void AssignAttributeValue(char *att, int *var) const { GetAttributeValue(att, *var); }
183 inline void AssignAttributeValue(char *att, long *var) const { GetAttributeValue(att, *var); }
184 inline void AssignAttributeValue(char *att, float *var) const { GetAttributeValue(att, *var); }
185 inline void AssignAttributeValue(char *att, double *var) const { GetAttributeValue(att, *var); }
186 inline void AssignAttributeValue(char *att, wxExpr **var) const { GetAttributeValue(att, var); }
187 void AssignAttributeValue(char *att, char **var) const ; // Word OR string -> string
188
189 // Add string items to list if the list attribute exists
190 bool GetAttributeValueStringList(const wxString& att, wxList *var) const;
191
192 // Associate other data with this expression, e.g. when reading in a
193 // number of linked items - store C++ object pointer with the expression
194 // so we can index into the wxExpr database and fish out the pointer.
195 inline void SetClientData(wxObject *data) { client_data = data; }
196 inline wxObject *GetClientData(void) const { return client_data; }
197 };
198
199 class WXDLLEXPORT wxExprDatabase: public wxList
200 {
201 private:
202 wxNode *position; // Where we are in a search
203 wxHashTable *hash_table;
204 wxString attribute_to_hash;
205
206 public:
207 int noErrors;
208
209 wxExprDatabase(wxExprErrorHandler handler = 0);
210
211 // Use hashing on both the functor, and the attribute of
212 // specified type (wxExprString or wxExprInteger) and name.
213 // So to find node 45
214 // (i.e. match the clause node(id=45, ...))
215 // it usually requires 1 look-up: the keys for functor and attribute
216 // are added together.
217 // Obviously if the attribute was missing in a clause, it would
218 // fail to be found by this method, but could be retrieved by a
219 // linear search using BeginFind and FindClauseByFunctor,
220 // or just searching through the list as per usual.
221
222 wxExprDatabase(wxExprType type, const wxString& attribute, int size = 500,
223 wxExprErrorHandler handler = 0);
224
225 ~wxExprDatabase(void);
226
227 void BeginFind(void) ; // Initialise a search
228 wxExpr *FindClause(long id) ; // Find a term based on an integer id attribute
229 // e.g. node(id=23, type=rectangle, ....).
230
231 // Find on basis of attribute/value pairs, e.g. type=rectangle
232 // This doesn't use hashing; it's a linear search.
233 wxExpr *FindClause(const wxString& word, const wxString& value);
234 wxExpr *FindClause(const wxString& word, long value);
235 wxExpr *FindClause(const wxString& word, double value);
236 wxExpr *FindClauseByFunctor(const wxString& functor);
237
238 wxExpr *HashFind(const wxString& functor, const wxString& value) const;
239 wxExpr *HashFind(const wxString& functor, long value) const;
240
241 void Append(wxExpr *expr); // Does cleverer things if hashing is on
242 void ClearDatabase(void);
243 inline int GetErrorCount() const { return noErrors; }
244 bool Read(const wxString& filename);
245 bool ReadFromString(const wxString& buffer);
246 bool Write(const wxString& fileName);
247 bool Write(ostream& stream);
248 void WriteLisp(ostream& stream);
249
250 // Compatibility
251 inline bool ReadProlog(char *filename) { return Read(wxString(filename)); }
252 inline bool ReadPrologFromString(char *buffer) { return ReadFromString(wxString(buffer)); }
253 inline void WriteProlog(ostream& stream) { Write(stream); }
254
255 private:
256 DECLARE_DYNAMIC_CLASS(wxExprDatabase)
257 };
258
259 // Function call-style interface - some more convenience wrappers/unwrappers
260
261 // Make a call
262 WXDLLEXPORT wxExpr* wxExprMakeCall(const wxString& functor ...);
263
264 #define wxExprMakeInteger(x) (new wxExpr((long)x))
265 #define wxExprMakeReal(x) (new wxExpr((double)x))
266 #define wxExprMakeString(x) (new wxExpr(wxExprString, x))
267 #define wxExprMakeWord(x) (new wxExpr(wxExprWord, x))
268 #define wxExprMake(x) (new wxExpr(x))
269
270 // Checks functor
271 WXDLLEXPORT bool wxExprIsFunctor(wxExpr *expr, const wxString& functor);
272
273 // Temporary variable for communicating between wxexpr.cpp and YACC/LEX
274 WXDLLEXPORT_DATA(extern wxExprDatabase*) thewxExprDatabase;
275
276 // YACC/LEX can leave memory lying around...
277 extern "C" WXDLLEXPORT int wxExprCleanUp();
278
279 #endif
280