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"
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
50 #define WXEXPR_ERROR_GENERAL 1
51 #define WXEXPR_ERROR_SYNTAX 2
53 // Error handler function definition. If app returns TRUE,
54 // carry on processing.
55 typedef bool (*wxExprErrorHandler
) (int errorType
, char *msg
);
57 WXDLLEXPORT_DATA(extern wxExprErrorHandler
) currentwxExprErrorHandler
;
59 WXDLLEXPORT_DATA(extern "C" FILE*) yyin
;
61 extern "C" int WXDLLEXPORT
yyparse(void);
72 class WXDLLEXPORT wxExprDatabase
;
74 class WXDLLEXPORT wxExpr
77 wxObject
*client_data
;
84 wxExpr
*first
; // If is a list expr, points to the first node
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
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
);
98 inline wxExprType
Type(void) const { return type
; }
99 inline long IntegerValue(void) const
101 if (type
== wxExprInteger
)
102 return value
.integer
;
103 else if (type
== wxExprReal
)
104 return (long)value
.real
;
108 inline double RealValue(void) const {
109 if (type
== wxExprReal
)
111 else if (type
== wxExprInteger
)
112 return (double)value
.integer
;
113 else return (double)0.0;
116 inline wxString
WordValue(void) const {
117 if (type
== wxExprWord
)
119 else if (type
== wxExprString
)
120 return wxString(value
.string
);
121 else return wxString("");
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("");
132 // Get nth arg of clause (starting from 1)
133 wxExpr
*Arg(wxExprType type
, int arg
) const;
135 // Return nth argument of a list expression (starting from zero)
136 wxExpr
*Nth(int arg
) const;
138 // Returns the number of elements in a list expression
139 int Number(void) const;
142 wxExpr
*Copy(void) const;
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
);
152 // Append an expression to a list
153 void Append(wxExpr
*expr
);
154 // Insert at beginning of list
155 void Insert(wxExpr
*expr
);
157 // Get first expr in list
158 inline wxExpr
*GetFirst(void) const { return ((type
== wxExprList
) ? value
.first
: (wxExpr
*)NULL
); }
160 // Get next expr if this is a node in a list
161 inline wxExpr
*GetNext(void) const { return next
; }
163 // Get last expr in list
164 inline wxExpr
*GetLast(void) const { return ((type
== wxExprList
) ? last
: (wxExpr
*)NULL
); }
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
);
176 void DeleteAttributeValue(const wxString
& attribute
);
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;
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
193 // Add string items to list if the list attribute exists
194 bool GetAttributeValueStringList(const wxString
& att
, wxList
*var
) const;
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
; }
203 class WXDLLEXPORT wxExprDatabase
: public wxList
205 DECLARE_DYNAMIC_CLASS(wxExprDatabase
)
207 wxNode
*position
; // Where we are in a search
208 wxHashTable
*hash_table
;
209 wxString attribute_to_hash
;
213 wxExprDatabase(wxExprErrorHandler handler
= 0);
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.
226 wxExprDatabase(wxExprType type
, const wxString
& attribute
, int size
= 500,
227 wxExprErrorHandler handler
= 0);
229 ~wxExprDatabase(void);
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, ....).
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
);
242 wxExpr
*HashFind(const wxString
& functor
, const wxString
& value
) const;
243 wxExpr
*HashFind(const wxString
& functor
, long value
) const;
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
);
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
); }
260 // Function call-style interface - some more convenience wrappers/unwrappers
263 wxExpr
* WXDLLEXPORT
wxExprMakeCall(const wxString
& functor
...);
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))
272 bool WXDLLEXPORT
wxExprIsFunctor(wxExpr
*expr
, const wxString
& functor
);
274 // Temporary variable for communicating between wxexpr.cpp and YACC/LEX
275 WXDLLEXPORT_DATA(extern wxExprDatabase
*) thewxExprDatabase
;
277 // YACC/LEX can leave memory lying around...
278 extern "C" WXDLLEXPORT
wxExprCleanUp();