1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Prolog-like file I/O, used by resource system.
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "wxexpr.h"
22 #include "wx/string.h"
39 #define PrologExpr wxExpr
40 #define PrologDatabase wxExprDatabase
41 #define proioErrorHandler wxExprErrorHandler
42 #define PROIO_ERROR_GENERAL 1
43 #define PROIO_ERROR_SYNTAX 2
44 #define PrologNull wxExprNull
45 #define PrologInteger wxExprInteger
46 #define PrologReal wxExprReal
47 #define PrologWord wxExprWord
48 #define PrologString wxExprString
49 #define PrologList wxExprList
50 #define PrologType wxExprType
53 #define WXEXPR_ERROR_GENERAL 1
54 #define WXEXPR_ERROR_SYNTAX 2
56 // Error handler function definition. If app returns TRUE,
57 // carry on processing.
58 typedef bool (*wxExprErrorHandler
) (int errorType
, char *msg
);
60 WXDLLEXPORT_DATA(extern wxExprErrorHandler
) currentwxExprErrorHandler
;
62 extern "C" WXDLLEXPORT_DATA(FILE*) yyin
;
64 extern "C" WXDLLEXPORT
int yyparse(void);
75 class WXDLLEXPORT wxExprDatabase
;
77 class WXDLLEXPORT wxExpr
80 wxObject
*client_data
;
87 wxExpr
*first
; // If is a list expr, points to the first node
90 wxExpr
*next
; // If this is a node in a list, points to the next node
91 wxExpr
*last
; // If is a list expr, points to the last node
93 wxExpr(wxExprType the_type
, char *word_or_string
, bool allocate
);
94 wxExpr(const wxString
& functor
); // Assume this is a new clause - pass functor
95 wxExpr(wxExprType the_type
, const wxString
& word_or_string
= "");
96 wxExpr(long the_integer
);
97 wxExpr(double the_real
);
98 wxExpr(wxList
*the_list
);
101 inline wxExprType
Type(void) const { return type
; }
102 inline long IntegerValue(void) const
104 if (type
== wxExprInteger
)
105 return value
.integer
;
106 else if (type
== wxExprReal
)
107 return (long)value
.real
;
111 inline double RealValue(void) const {
112 if (type
== wxExprReal
)
114 else if (type
== wxExprInteger
)
115 return (double)value
.integer
;
116 else return (double)0.0;
119 inline wxString
WordValue(void) const {
120 if (type
== wxExprWord
)
122 else if (type
== wxExprString
)
123 return wxString(value
.string
);
124 else return wxString("");
127 inline wxString
StringValue(void) const {
128 if (type
== wxExprString
)
129 return wxString(value
.string
);
130 else if (type
== wxExprWord
)
131 return wxString(value
.word
);
132 else return wxString("");
135 // Get nth arg of clause (starting from 1)
136 wxExpr
*Arg(wxExprType type
, int arg
) const;
138 // Return nth argument of a list expression (starting from zero)
139 wxExpr
*Nth(int arg
) const;
141 // Returns the number of elements in a list expression
142 int Number(void) const;
145 wxExpr
*Copy(void) const;
147 wxExpr
*GetAttributeValueNode(const wxString
& word
) const; // Use only for a clause or list
148 wxExpr
*AttributeValue(const wxString
& word
) const; // Use only for a clause
149 wxString
Functor(void) const; // Only for a clause
150 bool IsFunctor(const wxString
& s
) const; // Only for a clause
151 void WriteClause(ostream
& stream
); // Write this expression as a top-level clause
152 void WriteExpr(ostream
& stream
); // Write as any other subexpression
153 void WriteLispExpr(ostream
& stream
);
155 // Append an expression to a list
156 void Append(wxExpr
*expr
);
157 // Insert at beginning of list
158 void Insert(wxExpr
*expr
);
160 // Get first expr in list
161 inline wxExpr
*GetFirst(void) const { return ((type
== wxExprList
) ? value
.first
: (wxExpr
*)NULL
); }
163 // Get next expr if this is a node in a list
164 inline wxExpr
*GetNext(void) const { return next
; }
166 // Get last expr in list
167 inline wxExpr
*GetLast(void) const { return ((type
== wxExprList
) ? last
: (wxExpr
*)NULL
); }
169 // This should really be called SetAttributeValue since any existing
170 // attribute-value is deleted first.
171 void AddAttributeValue(const wxString
& attribute
, long value
);
172 void AddAttributeValue(const wxString
& attribute
, double value
);
173 void AddAttributeValueWord(const wxString
& attribute
, const wxString
& value
);
174 void AddAttributeValueString(const wxString
& attribute
, const wxString
& value
);
175 void AddAttributeValue(const wxString
& attribute
, wxList
*value
);
176 void AddAttributeValue(const wxString
& attribute
, wxExpr
*value
);
177 void AddAttributeValueStringList(const wxString
& attribute
, wxList
*string_list
);
179 void DeleteAttributeValue(const wxString
& attribute
);
181 bool GetAttributeValue(const wxString
& att
, int& var
) const;
182 bool GetAttributeValue(const wxString
& att
, long& var
) const;
183 bool GetAttributeValue(const wxString
& att
, float& var
) const;
184 bool GetAttributeValue(const wxString
& att
, double& var
) const;
185 bool GetAttributeValue(const wxString
& att
, wxString
& var
) const; // Word OR string -> string
186 bool GetAttributeValue(const wxString
& att
, wxExpr
**var
) const;
188 // Compatibility with old PrologIO
189 inline void AssignAttributeValue(char *att
, int *var
) const { GetAttributeValue(att
, *var
); }
190 inline void AssignAttributeValue(char *att
, long *var
) const { GetAttributeValue(att
, *var
); }
191 inline void AssignAttributeValue(char *att
, float *var
) const { GetAttributeValue(att
, *var
); }
192 inline void AssignAttributeValue(char *att
, double *var
) const { GetAttributeValue(att
, *var
); }
193 inline void AssignAttributeValue(char *att
, wxExpr
**var
) const { GetAttributeValue(att
, var
); }
194 void AssignAttributeValue(char *att
, char **var
) const ; // Word OR string -> string
196 // Add string items to list if the list attribute exists
197 bool GetAttributeValueStringList(const wxString
& att
, wxList
*var
) const;
199 // Associate other data with this expression, e.g. when reading in a
200 // number of linked items - store C++ object pointer with the expression
201 // so we can index into the wxExpr database and fish out the pointer.
202 inline void SetClientData(wxObject
*data
) { client_data
= data
; }
203 inline wxObject
*GetClientData(void) const { return client_data
; }
206 class WXDLLEXPORT wxExprDatabase
: public wxList
208 DECLARE_DYNAMIC_CLASS(wxExprDatabase
)
210 wxNode
*position
; // Where we are in a search
211 wxHashTable
*hash_table
;
212 wxString attribute_to_hash
;
216 wxExprDatabase(wxExprErrorHandler handler
= 0);
218 // Use hashing on both the functor, and the attribute of
219 // specified type (wxExprString or wxExprInteger) and name.
220 // So to find node 45
221 // (i.e. match the clause node(id=45, ...))
222 // it usually requires 1 look-up: the keys for functor and attribute
223 // are added together.
224 // Obviously if the attribute was missing in a clause, it would
225 // fail to be found by this method, but could be retrieved by a
226 // linear search using BeginFind and FindClauseByFunctor,
227 // or just searching through the list as per usual.
229 wxExprDatabase(wxExprType type
, const wxString
& attribute
, int size
= 500,
230 wxExprErrorHandler handler
= 0);
232 ~wxExprDatabase(void);
234 void BeginFind(void) ; // Initialise a search
235 wxExpr
*FindClause(long id
) ; // Find a term based on an integer id attribute
236 // e.g. node(id=23, type=rectangle, ....).
238 // Find on basis of attribute/value pairs, e.g. type=rectangle
239 // This doesn't use hashing; it's a linear search.
240 wxExpr
*FindClause(const wxString
& word
, const wxString
& value
);
241 wxExpr
*FindClause(const wxString
& word
, long value
);
242 wxExpr
*FindClause(const wxString
& word
, double value
);
243 wxExpr
*FindClauseByFunctor(const wxString
& functor
);
245 wxExpr
*HashFind(const wxString
& functor
, const wxString
& value
) const;
246 wxExpr
*HashFind(const wxString
& functor
, long value
) const;
248 void Append(wxExpr
*expr
); // Does cleverer things if hashing is on
249 void ClearDatabase(void);
250 inline int GetErrorCount() const { return noErrors
; }
251 bool Read(const wxString
& filename
);
252 bool ReadFromString(const wxString
& buffer
);
253 bool Write(const wxString
& fileName
);
254 bool Write(ostream
& stream
);
255 void WriteLisp(ostream
& stream
);
258 inline bool ReadProlog(char *filename
) { return Read(wxString(filename
)); }
259 inline bool ReadPrologFromString(char *buffer
) { return ReadFromString(wxString(buffer
)); }
260 inline void WriteProlog(ostream
& stream
) { Write(stream
); }
263 // Function call-style interface - some more convenience wrappers/unwrappers
266 WXDLLEXPORT wxExpr
* wxExprMakeCall(const wxString
& functor
...);
268 #define wxExprMakeInteger(x) (new wxExpr((long)x))
269 #define wxExprMakeReal(x) (new wxExpr((double)x))
270 #define wxExprMakeString(x) (new wxExpr(wxExprString, x))
271 #define wxExprMakeWord(x) (new wxExpr(wxExprWord, x))
272 #define wxExprMake(x) (new wxExpr(x))
275 WXDLLEXPORT
bool wxExprIsFunctor(wxExpr
*expr
, const wxString
& functor
);
277 // Temporary variable for communicating between wxexpr.cpp and YACC/LEX
278 WXDLLEXPORT_DATA(extern wxExprDatabase
*) thewxExprDatabase
;
280 // YACC/LEX can leave memory lying around...
281 extern "C" WXDLLEXPORT
int wxExprCleanUp();