]> git.saurik.com Git - wxWidgets.git/blame - include/wx/wxexpr.h
1. suppressed some messages from HasEntry()/HasKeys()
[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
47d67540 24#if wxUSE_IOSTREAMH
10b959e3
JS
25#include <iostream.h>
26#else
27#include <iostream>
fbc535ff
JS
28# ifdef _MSC_VER
29 using namespace std;
30# endif
10b959e3
JS
31#endif
32
33#include "wx/list.h"
34#include "wx/hash.h"
35
36#include "wx/expr.h"
37
38// Compatibility
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
51
52// Error types
53#define WXEXPR_ERROR_GENERAL 1
54#define WXEXPR_ERROR_SYNTAX 2
55
56// Error handler function definition. If app returns TRUE,
57// carry on processing.
58typedef bool (*wxExprErrorHandler) (int errorType, char *msg);
59
60WXDLLEXPORT_DATA(extern wxExprErrorHandler) currentwxExprErrorHandler;
61
184b5d99 62extern "C" WXDLLEXPORT_DATA(FILE*) yyin;
10b959e3 63
184b5d99 64extern "C" WXDLLEXPORT int yyparse(void);
10b959e3
JS
65
66typedef enum {
67 wxExprNull,
68 wxExprInteger,
69 wxExprReal,
70 wxExprWord,
71 wxExprString,
72 wxExprList
73} wxExprType;
74
75class WXDLLEXPORT wxExprDatabase;
76
77class WXDLLEXPORT wxExpr
78{
79 public:
80 wxObject *client_data;
81 wxExprType type;
82 union {
83 long integer;
84 char *word;
85 char *string;
7a11869d 86 double real;
10b959e3
JS
87 wxExpr *first; // If is a list expr, points to the first node
88 } value;
89
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
92
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);
7a11869d 97 wxExpr(double the_real);
10b959e3
JS
98 wxExpr(wxList *the_list);
99 ~wxExpr(void);
100
101 inline wxExprType Type(void) const { return type; }
102 inline long IntegerValue(void) const
103 {
104 if (type == wxExprInteger)
105 return value.integer;
106 else if (type == wxExprReal)
107 return (long)value.real;
108 else return 0;
109 }
110
7a11869d 111 inline double RealValue(void) const {
10b959e3
JS
112 if (type == wxExprReal)
113 return value.real;
114 else if (type == wxExprInteger)
7a11869d
JS
115 return (double)value.integer;
116 else return (double)0.0;
10b959e3
JS
117 }
118
119 inline wxString WordValue(void) const {
120 if (type == wxExprWord)
121 return value.word;
122 else if (type == wxExprString)
123 return wxString(value.string);
124 else return wxString("");
125 }
126
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("");
133 }
134
135 // Get nth arg of clause (starting from 1)
136 wxExpr *Arg(wxExprType type, int arg) const;
137
138 // Return nth argument of a list expression (starting from zero)
139 wxExpr *Nth(int arg) const;
140
141 // Returns the number of elements in a list expression
142 int Number(void) const;
143
144 // Make a clone
145 wxExpr *Copy(void) const;
146
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);
154
155 // Append an expression to a list
156 void Append(wxExpr *expr);
157 // Insert at beginning of list
158 void Insert(wxExpr *expr);
159
160 // Get first expr in list
161 inline wxExpr *GetFirst(void) const { return ((type == wxExprList) ? value.first : (wxExpr*)NULL); }
162
163 // Get next expr if this is a node in a list
164 inline wxExpr *GetNext(void) const { return next; }
165
166 // Get last expr in list
167 inline wxExpr *GetLast(void) const { return ((type == wxExprList) ? last : (wxExpr*)NULL); }
168
169 // This should really be called SetAttributeValue since any existing
170 // attribute-value is deleted first.
171 void AddAttributeValue(const wxString& attribute, long value);
7a11869d 172 void AddAttributeValue(const wxString& attribute, double value);
10b959e3
JS
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);
178
179 void DeleteAttributeValue(const wxString& attribute);
180
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;
7a11869d 184 bool GetAttributeValue(const wxString& att, double& var) const;
10b959e3
JS
185 bool GetAttributeValue(const wxString& att, wxString& var) const; // Word OR string -> string
186 bool GetAttributeValue(const wxString& att, wxExpr **var) const;
187
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); }
7a11869d 192 inline void AssignAttributeValue(char *att, double *var) const { GetAttributeValue(att, *var); }
10b959e3
JS
193 inline void AssignAttributeValue(char *att, wxExpr **var) const { GetAttributeValue(att, var); }
194 void AssignAttributeValue(char *att, char **var) const ; // Word OR string -> string
195
196 // Add string items to list if the list attribute exists
197 bool GetAttributeValueStringList(const wxString& att, wxList *var) const;
198
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; }
204};
205
206class WXDLLEXPORT wxExprDatabase: public wxList
207{
a6f6393c
VZ
208private:
209 wxNode *position; // Where we are in a search
210 wxHashTable *hash_table;
211 wxString attribute_to_hash;
212
213public:
214 int noErrors;
215
216 wxExprDatabase(wxExprErrorHandler handler = 0);
217
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.
228
229 wxExprDatabase(wxExprType type, const wxString& attribute, int size = 500,
230 wxExprErrorHandler handler = 0);
231
232 ~wxExprDatabase(void);
233
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, ....).
237
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);
244
245 wxExpr *HashFind(const wxString& functor, const wxString& value) const;
246 wxExpr *HashFind(const wxString& functor, long value) const;
247
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);
256
257 // Compatibility
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); }
261
262private:
263 DECLARE_DYNAMIC_CLASS(wxExprDatabase)
10b959e3
JS
264};
265
266// Function call-style interface - some more convenience wrappers/unwrappers
267
268// Make a call
184b5d99 269WXDLLEXPORT wxExpr* wxExprMakeCall(const wxString& functor ...);
10b959e3
JS
270
271#define wxExprMakeInteger(x) (new wxExpr((long)x))
7a11869d 272#define wxExprMakeReal(x) (new wxExpr((double)x))
10b959e3
JS
273#define wxExprMakeString(x) (new wxExpr(wxExprString, x))
274#define wxExprMakeWord(x) (new wxExpr(wxExprWord, x))
275#define wxExprMake(x) (new wxExpr(x))
276
277// Checks functor
184b5d99 278WXDLLEXPORT bool wxExprIsFunctor(wxExpr *expr, const wxString& functor);
10b959e3
JS
279
280// Temporary variable for communicating between wxexpr.cpp and YACC/LEX
281WXDLLEXPORT_DATA(extern wxExprDatabase*) thewxExprDatabase;
282
283// YACC/LEX can leave memory lying around...
184b5d99 284extern "C" WXDLLEXPORT int wxExprCleanUp();
10b959e3
JS
285
286#endif
287