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