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