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