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(float 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 float RealValue(void) const {
109 if (type
== wxExprReal
)
111 else if (type
== wxExprInteger
)
112 return (float)value
.integer
;
113 else return (float)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
, float 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
, wxString
& var
) const; // Word OR string -> string
182 bool GetAttributeValue(const wxString
& att
, wxExpr
**var
) const;
184 // Compatibility with old PrologIO
185 inline void AssignAttributeValue(char *att
, int *var
) const { GetAttributeValue(att
, *var
); }
186 inline void AssignAttributeValue(char *att
, long *var
) const { GetAttributeValue(att
, *var
); }
187 inline void AssignAttributeValue(char *att
, float *var
) const { GetAttributeValue(att
, *var
); }
188 inline void AssignAttributeValue(char *att
, wxExpr
**var
) const { GetAttributeValue(att
, var
); }
189 void AssignAttributeValue(char *att
, char **var
) const ; // Word OR string -> string
191 // Add string items to list if the list attribute exists
192 bool GetAttributeValueStringList(const wxString
& att
, wxList
*var
) const;
194 // Associate other data with this expression, e.g. when reading in a
195 // number of linked items - store C++ object pointer with the expression
196 // so we can index into the wxExpr database and fish out the pointer.
197 inline void SetClientData(wxObject
*data
) { client_data
= data
; }
198 inline wxObject
*GetClientData(void) const { return client_data
; }
201 class WXDLLEXPORT wxExprDatabase
: public wxList
203 DECLARE_DYNAMIC_CLASS(wxExprDatabase
)
205 wxNode
*position
; // Where we are in a search
206 wxHashTable
*hash_table
;
207 wxString attribute_to_hash
;
211 wxExprDatabase(wxExprErrorHandler handler
= 0);
213 // Use hashing on both the functor, and the attribute of
214 // specified type (wxExprString or wxExprInteger) and name.
215 // So to find node 45
216 // (i.e. match the clause node(id=45, ...))
217 // it usually requires 1 look-up: the keys for functor and attribute
218 // are added together.
219 // Obviously if the attribute was missing in a clause, it would
220 // fail to be found by this method, but could be retrieved by a
221 // linear search using BeginFind and FindClauseByFunctor,
222 // or just searching through the list as per usual.
224 wxExprDatabase(wxExprType type
, const wxString
& attribute
, int size
= 500,
225 wxExprErrorHandler handler
= 0);
227 ~wxExprDatabase(void);
229 void BeginFind(void) ; // Initialise a search
230 wxExpr
*FindClause(long id
) ; // Find a term based on an integer id attribute
231 // e.g. node(id=23, type=rectangle, ....).
233 // Find on basis of attribute/value pairs, e.g. type=rectangle
234 // This doesn't use hashing; it's a linear search.
235 wxExpr
*FindClause(const wxString
& word
, const wxString
& value
);
236 wxExpr
*FindClause(const wxString
& word
, long value
);
237 wxExpr
*FindClause(const wxString
& word
, float value
);
238 wxExpr
*FindClauseByFunctor(const wxString
& functor
);
240 wxExpr
*HashFind(const wxString
& functor
, const wxString
& value
) const;
241 wxExpr
*HashFind(const wxString
& functor
, long value
) const;
243 void Append(wxExpr
*expr
); // Does cleverer things if hashing is on
244 void ClearDatabase(void);
245 inline int GetErrorCount() const { return noErrors
; }
246 bool Read(const wxString
& filename
);
247 bool ReadFromString(const wxString
& buffer
);
248 bool Write(const wxString
& fileName
);
249 bool Write(ostream
& stream
);
250 void WriteLisp(ostream
& stream
);
253 inline bool ReadProlog(char *filename
) { return Read(wxString(filename
)); }
254 inline bool ReadPrologFromString(char *buffer
) { return ReadFromString(wxString(buffer
)); }
255 inline void WriteProlog(ostream
& stream
) { Write(stream
); }
258 // Function call-style interface - some more convenience wrappers/unwrappers
261 wxExpr
* WXDLLEXPORT
wxExprMakeCall(const wxString
& functor
...);
263 #define wxExprMakeInteger(x) (new wxExpr((long)x))
264 #define wxExprMakeReal(x) (new wxExpr((float)x))
265 #define wxExprMakeString(x) (new wxExpr(wxExprString, x))
266 #define wxExprMakeWord(x) (new wxExpr(wxExprWord, x))
267 #define wxExprMake(x) (new wxExpr(x))
270 bool WXDLLEXPORT
wxExprIsFunctor(wxExpr
*expr
, const wxString
& functor
);
272 // Temporary variable for communicating between wxexpr.cpp and YACC/LEX
273 WXDLLEXPORT_DATA(extern wxExprDatabase
*) thewxExprDatabase
;
275 // YACC/LEX can leave memory lying around...
276 extern "C" WXDLLEXPORT
wxExprCleanUp();