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