]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxexpr.cpp
menu sample added
[wxWidgets.git] / src / common / wxexpr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxexpr.cpp
3 // Purpose: wxExpr
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wxexpr.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include <stdarg.h>
24 #include <ctype.h>
25 #include <string.h>
26
27 #include "wx/utils.h"
28 #include "wx/expr.h"
29 #include "wx/wxexpr.h"
30
31 extern "C" void add_expr(char *);
32 extern "C" void LexFromFile(FILE *fd);
33 extern "C" void LexFromString(char *buf);
34
35
36 wxExprDatabase *thewxExprDatabase = NULL;
37 wxExprErrorHandler currentwxExprErrorHandler;
38
39 wxExpr::wxExpr(const wxString& functor)
40 {
41 type = wxExprList;
42 next = NULL;
43 last = NULL;
44 value.first = NULL;
45
46 wxExpr *pfunctor = new wxExpr(wxExprWord, functor);
47 Append(pfunctor);
48 client_data = NULL;
49 }
50
51 wxExpr::wxExpr(wxExprType the_type, const wxString& word_or_string)
52 {
53 type = the_type;
54
55 switch (the_type)
56 {
57 case wxExprWord:
58 value.word = copystring((const wxChar *)word_or_string);
59 break;
60 case wxExprString:
61 value.string = copystring((const wxChar *)word_or_string);
62 break;
63 case wxExprList:
64 last = NULL;
65 value.first = NULL;
66 break;
67 case wxExprReal:
68 case wxExprInteger:
69 case wxExprNull:
70 break;
71 }
72 client_data = NULL;
73 next = NULL;
74 }
75
76 wxExpr::wxExpr(wxExprType the_type, wxChar *word_or_string, bool allocate)
77 {
78 type = the_type;
79
80 switch (the_type)
81 {
82 case wxExprWord:
83 value.word = allocate ? copystring(word_or_string) : word_or_string;
84 break;
85 case wxExprString:
86 value.string = allocate ? copystring(word_or_string) : word_or_string;
87 break;
88 case wxExprList:
89 last = NULL;
90 value.first = NULL;
91 break;
92 case wxExprReal:
93 case wxExprInteger:
94 case wxExprNull:
95 break;
96 }
97 client_data = NULL;
98 next = NULL;
99 }
100
101 wxExpr::wxExpr(long the_integer)
102 {
103 type = wxExprInteger;
104 value.integer = the_integer;
105 client_data = NULL;
106 next = NULL;
107 }
108
109 wxExpr::wxExpr(double the_real)
110 {
111 type = wxExprReal;
112 value.real = the_real;
113 client_data = NULL;
114 next = NULL;
115 }
116
117 wxExpr::wxExpr(wxList *the_list)
118 {
119 type = wxExprList;
120 client_data = NULL;
121 last = NULL;
122 value.first = NULL;
123
124 wxExpr *listExpr = new wxExpr(wxExprList);
125
126 wxNode *node = the_list->First();
127 while (node)
128 {
129 wxExpr *expr = (wxExpr *)node->Data();
130 listExpr->Append(expr);
131 node = node->Next();
132 }
133 Append(listExpr);
134
135 delete the_list;
136 }
137
138 wxExpr::~wxExpr(void)
139 {
140 switch (type)
141 {
142 case wxExprInteger:
143 case wxExprReal:
144 {
145 break;
146 }
147 case wxExprString:
148 {
149 delete[] value.string;
150 break;
151 }
152 case wxExprWord:
153 {
154 delete[] value.word;
155 break;
156 }
157 case wxExprList:
158 {
159 wxExpr *expr = value.first;
160 while (expr)
161 {
162 wxExpr *expr1 = expr->next;
163
164 delete expr;
165 expr = expr1;
166 }
167 break;
168 }
169 case wxExprNull: break;
170 }
171 }
172
173 void wxExpr::Append(wxExpr *expr)
174 {
175 if (!value.first)
176 value.first = expr;
177
178 if (last)
179 last->next = expr;
180 last = expr;
181 }
182
183 void wxExpr::Insert(wxExpr *expr)
184 {
185 expr->next = value.first;
186 value.first = expr;
187
188 if (!last)
189 last = expr;
190 }
191
192 wxExpr *wxExpr::Copy(void) const
193 {
194 // This seems to get round an optimizer bug when
195 // using Watcom C++ 10a in WIN32 compilation mode.
196 // If these lines not present, the type seems to be
197 // interpreted wrongly as an integer.
198 // I don't want to turn optimization off since it's needed
199 // for reading in files quickly.
200 #if defined(__WATCOMC__)
201 char buf[2];
202 sprintf(buf, "");
203 #endif
204
205 switch (type)
206 {
207 case wxExprInteger:
208 return new wxExpr(value.integer);
209 case wxExprReal:
210 return new wxExpr(value.real);
211 case wxExprString:
212 return new wxExpr(wxExprString, wxString(value.string));
213 case wxExprWord:
214 return new wxExpr(wxExprWord, wxString(value.word));
215 case wxExprList:
216 {
217 wxExpr *expr = value.first;
218 wxExpr *new_list = new wxExpr(wxExprList);
219 while (expr)
220 {
221 wxExpr *expr2 = expr->Copy();
222 new_list->Append(expr2);
223 expr = expr->next;
224 }
225 return new_list;
226 }
227 case wxExprNull:
228 break;
229 }
230 return NULL;
231 }
232
233
234 // Get the wxExpr (containing (= wxExpr Value) form) for the given word
235 // or string, assuming that we have Attribute=Value, ...
236 wxExpr *wxExpr::GetAttributeValueNode(const wxString& word) const // Use only for a clause or list
237 {
238 if (type != wxExprList)
239 return NULL;
240
241 wxExpr *expr = value.first;
242 while (expr)
243 {
244 if (expr->type == wxExprList)
245 {
246 wxExpr *firstNode = expr->value.first;
247 if ((firstNode->type == wxExprWord) && (firstNode->value.word[0] == '='))
248 {
249 wxExpr *secondNode = firstNode->next;
250 if ((secondNode->type == wxExprWord) &&
251 (wxStrcmp((const wxChar *)word, secondNode->value.word) == 0))
252 {
253 return expr;
254 }
255 }
256 }
257 expr = expr->next;
258 }
259 return NULL;
260 }
261
262 // Get the value (in wxExpr form) for the given word or string, assuming
263 // that we have Attribute=Value, ...
264 wxExpr *wxExpr::AttributeValue(const wxString& word) const // Use only for a clause or list
265 {
266 if (type != wxExprList)
267 return NULL;
268
269 wxExpr *attExpr = GetAttributeValueNode(word);
270 if (attExpr && attExpr->value.first && attExpr->value.first->next)
271 return attExpr->value.first->next->next;
272 else return NULL;
273 }
274
275 wxString wxExpr::Functor(void) const // Use only for a clause
276 {
277 if ((type != wxExprList) || !value.first)
278 return wxString(wxT(""));
279
280 if (value.first->type == wxExprWord)
281 return wxString(value.first->value.word);
282 else
283 return wxString(wxT(""));
284 }
285
286 bool wxExpr::IsFunctor(const wxString& f) const // Use only for a clause
287 {
288 if ((type != wxExprList) || !value.first)
289 return FALSE;
290
291 return (value.first->type == wxExprWord &&
292 (wxStrcmp((const wxChar *)f, value.first->value.word) == 0));
293 }
294
295 // Return nth argument of a clause (starting from 1)
296 wxExpr *wxExpr::Arg(wxExprType theType, int arg) const
297 {
298 wxExpr *expr = value.first;
299 int i;
300 for (i = 1; i < arg; i++)
301 if (expr)
302 expr = expr->next;
303
304 if (expr && (expr->type == theType))
305 return expr;
306 else
307 return NULL;
308 }
309
310 // Return nth argument of a list expression (starting from zero)
311 wxExpr *wxExpr::Nth(int arg) const
312 {
313 if (type != wxExprList)
314 return NULL;
315
316 wxExpr *expr = value.first;
317 int i;
318 for (i = 0; i < arg; i++)
319 if (expr)
320 expr = expr->next;
321 else return NULL;
322
323 if (expr)
324 return expr;
325 else
326 return NULL;
327 }
328
329 // Returns the number of elements in a list expression
330 int wxExpr::Number(void) const
331 {
332 if (type != wxExprList)
333 return 0;
334
335 int i = 0;
336 wxExpr *expr = value.first;
337 while (expr)
338 {
339 expr = expr->next;
340 i ++;
341 }
342 return i;
343 }
344
345 void wxExpr::DeleteAttributeValue(const wxString& attribute)
346 {
347 if (type != wxExprList)
348 return;
349
350 wxExpr *expr = value.first;
351 wxExpr *lastExpr = this;
352 while (expr)
353 {
354 if (expr->type == wxExprList)
355 {
356 wxExpr *firstNode = expr->value.first;
357 if ((firstNode->type == wxExprWord) && (firstNode->value.word[0] == '='))
358 {
359 wxExpr *secondNode = firstNode->next;
360 if ((secondNode->type == wxExprWord) &&
361 (wxStrcmp((const wxChar *)attribute, secondNode->value.word) == 0))
362 {
363 wxExpr *nextExpr = expr->next;
364 delete expr;
365
366 lastExpr->next = nextExpr;
367
368 if (last == expr)
369 last = lastExpr;
370
371 return;
372 }
373 }
374 }
375 lastExpr = expr;
376 expr = expr->next;
377 }
378 return;
379 }
380
381 void wxExpr::AddAttributeValue(const wxString& attribute, wxExpr *val)
382 {
383 if (type != wxExprList)
384 {
385 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
386 return;
387 }
388 // Warning - existing code may assume that any existing value
389 // is deleted first. For efficiency, we leave this to the application.
390 // DeleteAttributeValue(attribute);
391
392 wxExpr *patt = new wxExpr(wxExprWord, attribute);
393 wxExpr *pequals = new wxExpr(wxExprWord, wxT("="));
394
395 wxExpr *listExpr = new wxExpr(wxExprList);
396
397 listExpr->Append(pequals);
398 listExpr->Append(patt);
399 listExpr->Append(val);
400
401 Append(listExpr);
402 }
403
404 void wxExpr::AddAttributeValue(const wxString& attribute, long val)
405 {
406 if (type != wxExprList)
407 {
408 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
409 return;
410 }
411 // Warning - existing code may assume that any existing value
412 // is deleted first. For efficiency, we leave this to the application.
413 // DeleteAttributeValue(attribute);
414
415 wxExpr *patt = new wxExpr(wxExprWord, attribute);
416 wxExpr *pval = new wxExpr(val);
417 wxExpr *pequals = new wxExpr(wxExprWord, wxT("="));
418
419 wxExpr *listExpr = new wxExpr(wxExprList);
420
421 listExpr->Append(pequals);
422 listExpr->Append(patt);
423 listExpr->Append(pval);
424
425 Append(listExpr);
426 }
427
428 void wxExpr::AddAttributeValue(const wxString& attribute, double val)
429 {
430 if (type != wxExprList)
431 {
432 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
433 return;
434 }
435
436 // DeleteAttributeValue(attribute);
437 wxExpr *patt = new wxExpr(wxExprWord, attribute);
438 wxExpr *pval = new wxExpr(val);
439 wxExpr *pequals = new wxExpr(wxExprWord, wxT("="));
440
441 wxExpr *listExpr = new wxExpr(wxExprList);
442
443 listExpr->Append(pequals);
444 listExpr->Append(patt);
445 listExpr->Append(pval);
446
447 Append(listExpr);
448 }
449
450 void wxExpr::AddAttributeValueString(const wxString& attribute, const wxString& val)
451 {
452 if (type != wxExprList)
453 {
454 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
455 return;
456 }
457
458 // DeleteAttributeValue(attribute);
459
460 wxExpr *patt = new wxExpr(wxExprWord, attribute);
461 wxExpr *pval = new wxExpr(wxExprString, val);
462 wxExpr *pequals = new wxExpr(wxExprWord, wxT("="));
463
464 wxExpr *listExpr = new wxExpr(wxExprList);
465
466 listExpr->Append(pequals);
467 listExpr->Append(patt);
468 listExpr->Append(pval);
469
470 Append(listExpr);
471 }
472
473 void wxExpr::AddAttributeValueWord(const wxString& attribute, const wxString& val)
474 {
475 if (type != wxExprList)
476 {
477 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
478 return;
479 }
480
481 // DeleteAttributeValue(attribute);
482
483 wxExpr *patt = new wxExpr(wxExprWord, attribute);
484 wxExpr *pval = new wxExpr(wxExprWord, val);
485 wxExpr *pequals = new wxExpr(wxExprWord, wxT("="));
486
487 wxExpr *listExpr = new wxExpr(wxExprList);
488
489 listExpr->Append(pequals);
490 listExpr->Append(patt);
491 listExpr->Append(pval);
492
493 Append(listExpr);
494 }
495
496 void wxExpr::AddAttributeValue(const wxString& attribute, wxList *val)
497 {
498 if (type != wxExprList)
499 {
500 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
501 return;
502 }
503 if (!val)
504 return;
505
506 // DeleteAttributeValue(attribute);
507
508 wxExpr *patt = new wxExpr(wxExprWord, attribute);
509 wxExpr *pval = new wxExpr(val);
510 wxExpr *pequals = new wxExpr(wxExprWord, wxT("="));
511
512 wxExpr *listExpr = new wxExpr(wxExprList);
513
514 listExpr->Append(pequals);
515 listExpr->Append(patt);
516 listExpr->Append(pval);
517
518 Append(listExpr);
519 }
520
521 void wxExpr::AddAttributeValueStringList(const wxString& attribute, wxList *string_list)
522 {
523 if (type != wxExprList)
524 {
525 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
526 return;
527 }
528 if (!string_list)
529 return;
530
531 // DeleteAttributeValue(attribute);
532
533 // First make a list of wxExpr strings
534 wxExpr *listExpr = new wxExpr(wxExprList);
535 wxNode *node = string_list->First();
536 while (node)
537 {
538 char *string = (char *)node->Data();
539 wxExpr *expr = new wxExpr(wxExprString, wxString(string));
540 listExpr->Append(expr);
541 node = node->Next();
542 }
543
544 // Now make an (=, Att, Value) triple
545 wxExpr *patt = new wxExpr(wxExprWord, attribute);
546 wxExpr *pequals = new wxExpr(wxExprWord, wxT("="));
547
548 wxExpr *listExpr2 = new wxExpr(wxExprList);
549
550 listExpr2->Append(pequals);
551 listExpr2->Append(patt);
552 listExpr2->Append(listExpr);
553
554 Append(listExpr2);
555 }
556
557 bool wxExpr::GetAttributeValue(const wxString& att, int& var) const
558 {
559 wxExpr *expr = AttributeValue(att);
560
561 if (expr && (expr->Type() == wxExprInteger || expr->Type() == wxExprReal))
562 {
563 var = (int)(expr->IntegerValue());
564 return TRUE;
565 }
566 else
567 return FALSE;
568 }
569
570 bool wxExpr::GetAttributeValue(const wxString& att, long& var) const
571 {
572 wxExpr *expr = AttributeValue(att);
573
574 if (expr && (expr->Type() == wxExprInteger || expr->Type() == wxExprReal))
575 {
576 var = expr->IntegerValue();
577 return TRUE;
578 }
579 else
580 return FALSE;
581 }
582
583 bool wxExpr::GetAttributeValue(const wxString& att, float& var) const
584 {
585 wxExpr *expr = AttributeValue(att);
586 if (expr && (expr->Type() == wxExprInteger || expr->Type() == wxExprReal))
587 {
588 var = (float) expr->RealValue();
589 return TRUE;
590 }
591 else
592 return FALSE;
593 }
594
595 bool wxExpr::GetAttributeValue(const wxString& att, double& var) const
596 {
597 wxExpr *expr = AttributeValue(att);
598 if (expr && (expr->Type() == wxExprInteger || expr->Type() == wxExprReal))
599 {
600 var = expr->RealValue();
601 return TRUE;
602 }
603 else
604 return FALSE;
605 }
606
607 bool wxExpr::GetAttributeValue(const wxString& att, wxString& var) const // Word OR string -> string
608 {
609 wxExpr *expr = AttributeValue(att);
610 if (expr && expr->Type() == wxExprWord)
611 {
612 var = expr->WordValue();
613 return TRUE;
614 }
615 else if (expr && expr->Type() == wxExprString)
616 {
617 var = expr->StringValue();
618 return TRUE;
619 }
620 else
621 return FALSE;
622 }
623
624 bool wxExpr::GetAttributeValue(const wxString& att, wxExpr **var) const
625 {
626 wxExpr *expr = AttributeValue(att);
627 if (expr)
628 {
629 *var = expr;
630 return TRUE;
631 }
632 else
633 return FALSE;
634 }
635
636 bool wxExpr::GetAttributeValueStringList(const wxString& att, wxList *var) const
637 {
638 wxExpr *expr = AttributeValue(att);
639 if (expr && expr->Type() == wxExprList)
640 {
641 wxExpr *string_expr = expr->value.first;
642 while (string_expr)
643 {
644 if (string_expr->Type() == wxExprString)
645 var->Append((wxObject *)copystring(string_expr->StringValue()));
646
647 string_expr = string_expr->next;
648 }
649 return TRUE;
650 }
651 else
652 return FALSE;
653 }
654
655 // Compatibility
656 void wxExpr::AssignAttributeValue(wxChar *att, wxChar **var) const
657 {
658 wxString str;
659 if (GetAttributeValue(att, str))
660 {
661 if (*var)
662 delete[] *var;
663 *var = copystring((const wxChar *) str);
664 }
665 }
666
667 void wxExpr::WriteClause(FILE* stream) // Write this expression as a top-level clause
668 {
669 if (type != wxExprList)
670 return;
671
672 wxExpr *node = value.first;
673 if (node)
674 {
675 node->WriteExpr(stream);
676 fprintf( stream, "(" );
677 node = node->next;
678 bool first = TRUE;
679 while (node)
680 {
681 if (!first)
682 fprintf( stream, " " );
683 node->WriteExpr(stream);
684 node = node->next;
685 if (node)
686 fprintf( stream, ",\n" );
687 first = FALSE;
688 }
689 fprintf( stream, ").\n\n" );
690 }
691 }
692
693 void wxExpr::WriteExpr(FILE* stream) // Write as any other subexpression
694 {
695 // This seems to get round an optimizer bug when
696 // using Watcom C++ 10a in WIN32 compilation mode.
697 // If these lines not present, the type seems to be
698 // interpreted wrongly as an integer.
699 // I don't want to turn optimization off since it's needed
700 // for reading in files quickly.
701 #if defined(__WATCOMC__)
702 char buf[2];
703 sprintf(buf, "");
704 #endif
705
706 switch (type)
707 {
708 case wxExprInteger:
709 {
710 fprintf( stream, "%ld", value.integer );
711 break;
712 }
713 case wxExprReal:
714 {
715 double f = value.real;
716 fprintf( stream, "%.6g", f);
717 break;
718 }
719 case wxExprString:
720 {
721 fprintf( stream, "\"" );
722 int i;
723 const wxWX2MBbuf val = wxConvLibc.cWX2MB(value.string);
724 int len = strlen(val);
725 for (i = 0; i < len; i++)
726 {
727 char ch = val[i];
728 if (ch == '"' || ch == '\\')
729 fprintf( stream, "\\" );
730 char tmp[2];
731 tmp[0] = ch;
732 tmp[1] = 0;
733 fprintf( stream, tmp );
734 }
735 fprintf( stream, "\"" );
736 break;
737 }
738 case wxExprWord:
739 {
740 bool quote_it = FALSE;
741 const wxWX2MBbuf val = wxConvLibc.cWX2MB(value.word);
742 int len = strlen(val);
743 if ((len == 0) || (len > 0 && (val[0] > 64 && val[0] < 91)))
744 quote_it = TRUE;
745 else
746 {
747 int i;
748 for (i = 0; i < len; i++)
749 if ((!isalpha(val[i])) && (!isdigit(val[i])) &&
750 (val[i] != '_'))
751 { quote_it = TRUE; i = len; }
752 }
753
754 if (quote_it)
755 fprintf( stream ,"'" );
756
757 fprintf( stream, (const char*) val );
758
759 if (quote_it)
760 fprintf( stream, "'" );
761
762 break;
763 }
764 case wxExprList:
765 {
766 if (!value.first)
767 fprintf( stream, "[]" );
768 else
769 {
770 wxExpr *expr = value.first;
771
772 if ((expr->Type() == wxExprWord) && (wxStrcmp(expr->WordValue(), wxT("=")) == 0))
773 {
774 wxExpr *arg1 = expr->next;
775 wxExpr *arg2 = arg1->next;
776 arg1->WriteExpr(stream);
777 fprintf( stream, " = " );
778 arg2->WriteExpr(stream);
779 }
780 else
781 {
782 fprintf( stream, "[" );
783 while (expr)
784 {
785 expr->WriteExpr(stream);
786 expr = expr->next;
787 if (expr)
788 fprintf( stream, ", " );
789 }
790 fprintf( stream, "]" );
791 }
792 }
793 break;
794 }
795 case wxExprNull: break;
796 }
797 }
798
799 /*
800 * wxExpr 'database' (list of expressions)
801 */
802
803 IMPLEMENT_DYNAMIC_CLASS(wxExprDatabase, wxList)
804
805 wxExprDatabase::wxExprDatabase(wxExprErrorHandler handler)
806 {
807 position = NULL;
808 hash_table = NULL;
809 currentwxExprErrorHandler = handler;
810 noErrors = 0;
811 }
812
813 wxExprDatabase::wxExprDatabase(wxExprType type, const wxString& attribute, int size,
814 wxExprErrorHandler handler)
815 {
816 position = NULL;
817 attribute_to_hash = attribute;
818 if (type == wxExprString)
819 hash_table = new wxHashTable(wxKEY_STRING, size);
820 else if (type == wxExprInteger)
821 hash_table = new wxHashTable(wxKEY_INTEGER, size);
822 else hash_table = NULL;
823
824 currentwxExprErrorHandler = handler;
825 noErrors = 0;
826 }
827
828 wxExprDatabase::~wxExprDatabase(void)
829 {
830 ClearDatabase();
831 if (hash_table)
832 delete hash_table;
833 }
834
835 void wxExprDatabase::BeginFind(void) // Initialise a search
836 {
837 position = First();
838 }
839
840 wxExpr *wxExprDatabase::FindClause(long id) // Find a term based on an integer id attribute
841 // e.g. node(id=23, type=rectangle, ....).
842 {
843 wxExpr *found = NULL;
844 while (position && !found)
845 {
846 wxExpr *term = (wxExpr *)position->Data();
847
848 if (term->Type() == wxExprList)
849 {
850 wxExpr *value = term->AttributeValue("id");
851 if (value->Type() == wxExprInteger && value->IntegerValue() == id)
852 found = term;
853 }
854 position = position->Next();
855 }
856 return found;
857 }
858
859 // Find on basis of attribute/value pairs, e.g. type=rectangle
860 wxExpr *wxExprDatabase::FindClause(const wxString& word, const wxString& val)
861 {
862 wxExpr *found = NULL;
863 while (position && !found)
864 {
865 wxExpr *term = (wxExpr *)position->Data();
866
867 if (term->Type() == wxExprList)
868 {
869 wxExpr *value = term->AttributeValue(word);
870 if ((value->Type() == wxExprWord && value->WordValue() == val) ||
871 (value->Type() == wxExprString && value->StringValue() == val))
872 found = term;
873 }
874 position = position->Next();
875 }
876 return found;
877 }
878
879 wxExpr *wxExprDatabase::FindClause(const wxString& word, long val)
880 {
881 wxExpr *found = NULL;
882 while (position && !found)
883 {
884 wxExpr *term = (wxExpr *)position->Data();
885
886 if (term->Type() == wxExprList)
887 {
888 wxExpr *value = term->AttributeValue(word);
889 if ((value->Type() == wxExprInteger) && (value->IntegerValue() == val))
890 found = term;
891 }
892 position = position->Next();
893 }
894 return found;
895 }
896
897 wxExpr *wxExprDatabase::FindClause(const wxString& word, double val)
898 {
899 wxExpr *found = NULL;
900 while (position && !found)
901 {
902 wxExpr *term = (wxExpr *)position->Data();
903
904 if (term->Type() == wxExprList)
905 {
906 wxExpr *value = term->AttributeValue(word);
907 if ((value->Type() == wxExprReal) && (value->RealValue() == val))
908 found = term;
909 }
910 position = position->Next();
911 }
912 return found;
913 }
914
915 wxExpr *wxExprDatabase::FindClauseByFunctor(const wxString& functor)
916 {
917 wxExpr *found = NULL;
918 while (position && !found)
919 {
920 wxExpr *term = (wxExpr *)position->Data();
921
922 if (term->Type() == wxExprList)
923 {
924 if (term->Functor() == functor)
925 found = term;
926 }
927 position = position->Next();
928 }
929 return found;
930 }
931
932 // If hashing is on, must store in hash table too
933 void wxExprDatabase::Append(wxExpr *clause)
934 {
935 wxList::Append((wxObject *)clause);
936 if (hash_table)
937 {
938 wxString functor(clause->Functor());
939 wxExpr *expr = clause->AttributeValue(attribute_to_hash);
940 if (expr)
941 {
942 long functor_key = hash_table->MakeKey(WXSTRINGCAST functor);
943 long value_key = 0;
944 if (expr && expr->Type() == wxExprString)
945 {
946 value_key = hash_table->MakeKey(WXSTRINGCAST expr->StringValue());
947 hash_table->Put(functor_key + value_key, WXSTRINGCAST expr->StringValue(), (wxObject *)clause);
948 }
949 else if (expr && expr->Type() == wxExprInteger)
950 {
951 value_key = expr->IntegerValue();
952 hash_table->Put(functor_key + value_key, expr->IntegerValue(), (wxObject *)clause);
953 }
954
955 }
956 }
957 }
958
959 wxExpr *wxExprDatabase::HashFind(const wxString& functor, long value) const
960 {
961 long key = hash_table->MakeKey(WXSTRINGCAST functor) + value;
962
963 // The key alone isn't guaranteed to be unique:
964 // must supply value too. Let's assume the value of the
965 // id is going to be reasonably unique.
966 return (wxExpr *)hash_table->Get(key, value);
967 }
968
969 wxExpr *wxExprDatabase::HashFind(const wxString& functor, const wxString& value) const
970 {
971 long key = hash_table->MakeKey(WXSTRINGCAST functor) + hash_table->MakeKey(WXSTRINGCAST value);
972 return (wxExpr *)hash_table->Get(key, WXSTRINGCAST value);
973 }
974
975 void wxExprDatabase::ClearDatabase(void)
976 {
977 noErrors = 0;
978 wxNode *node = First();
979 while (node)
980 {
981 wxExpr *expr = (wxExpr *)node->Data();
982 delete expr;
983 delete node;
984 node = First();
985 }
986
987 if (hash_table)
988 hash_table->Clear();
989 }
990
991 bool wxExprDatabase::Read(const wxString& filename)
992 {
993 noErrors = 0;
994
995 FILE *f = fopen(filename.fn_str(), "r");
996 if (f)
997 {
998 thewxExprDatabase = this;
999
1000 LexFromFile(f);
1001 yyparse();
1002 fclose(f);
1003
1004 wxExprCleanUp();
1005 return (noErrors == 0);
1006 }
1007 else
1008 {
1009 return FALSE;
1010 }
1011 }
1012
1013 bool wxExprDatabase::ReadFromString(const wxString& buffer)
1014 {
1015 noErrors = 0;
1016 thewxExprDatabase = this;
1017
1018 const wxWX2MBbuf buf = buffer.mb_str();
1019 LexFromString(wxMBSTRINGCAST buf);
1020 yyparse();
1021 wxExprCleanUp();
1022 return (noErrors == 0);
1023 }
1024
1025 bool wxExprDatabase::Write(const wxString& fileName)
1026 {
1027 FILE *stream = fopen( fileName.fn_str(), "w+" );
1028
1029 if (!stream)
1030 return FALSE;
1031
1032 bool success = Write(stream);
1033 fclose(stream);
1034 return success;
1035 }
1036
1037 bool wxExprDatabase::Write(FILE *stream)
1038 {
1039 noErrors = 0;
1040 wxNode *node = First();
1041 while (node)
1042 {
1043 wxExpr *expr = (wxExpr *)node->Data();
1044 expr->WriteClause(stream);
1045 node = node->Next();
1046 }
1047 return (noErrors == 0);
1048 }
1049
1050 void add_expr(wxExpr * expr)
1051 {
1052 thewxExprDatabase->Append(expr);
1053 }
1054
1055 // Checks functor
1056 bool wxExprIsFunctor(wxExpr *expr, const wxString& functor)
1057 {
1058 if (expr && (expr->Type() == wxExprList))
1059 {
1060 wxExpr *first_expr = expr->value.first;
1061
1062 if (first_expr && (first_expr->Type() == wxExprWord) &&
1063 (first_expr->WordValue() == functor))
1064 return TRUE;
1065 else
1066 return FALSE;
1067 }
1068 else
1069 return FALSE;
1070 }
1071
1072 /*
1073 * Called from parser
1074 *
1075 */
1076
1077 char *wxmake_integer(char *str)
1078 {
1079 wxExpr *x = new wxExpr(atol(str));
1080
1081 return (char *)x;
1082 }
1083
1084 char *wxmake_real(char *str1, char *str2)
1085 {
1086 char buf[50];
1087
1088 sprintf(buf, "%s.%s", str1, str2);
1089 double f = (double)atof(buf);
1090 wxExpr *x = new wxExpr(f);
1091
1092 return (char *)x;
1093 }
1094
1095 // extern "C" double exp10(double);
1096
1097 char *wxmake_exp(char *str1, char *str2)
1098 {
1099 double mantissa = (double)atoi(str1);
1100 double exponent = (double)atoi(str2);
1101
1102 double d = mantissa * pow(10.0, exponent);
1103
1104 wxExpr *x = new wxExpr(d);
1105
1106 return (char *)x;
1107 }
1108
1109 char *wxmake_exp2(char *str1, char *str2, char *str3)
1110 {
1111 char buf[50];
1112
1113 sprintf(buf, "%s.%s", str1, str2);
1114 double mantissa = (double)atof(buf);
1115 double exponent = (double)atoi(str3);
1116
1117 double d = mantissa * pow(10.0, exponent);
1118
1119 wxExpr *x = new wxExpr(d);
1120
1121 return (char *)x;
1122 }
1123
1124 char *wxmake_word(char *str)
1125 {
1126 wxExpr *x = new wxExpr(wxExprWord, str);
1127 return (char *)x;
1128 }
1129
1130 char *wxmake_string(char *str)
1131 {
1132 wxChar *s, *t;
1133 int len, i;
1134 const wxMB2WXbuf sbuf = wxConvLibc.cMB2WX(str);
1135
1136 // str++; /* skip leading quote */
1137 len = wxStrlen(sbuf) - 1; /* ignore trailing quote */
1138
1139 s = new wxChar[len + 1];
1140
1141 t = s;
1142 for(i=1; i<len; i++) // 1 since we want to skip leading quote
1143 {
1144 if (sbuf[i] == wxT('\\') && sbuf[i+1] == wxT('"'))
1145 {
1146 *t++ = wxT('"');
1147 i ++;
1148 }
1149 else if (sbuf[i] == wxT('\\') && sbuf[i+1] == wxT('\\'))
1150 {
1151 *t++ = wxT('\\');
1152 i ++;
1153 }
1154 else
1155 *t++ = sbuf[i];
1156 }
1157
1158 *t = wxT('\0');
1159
1160 wxExpr *x = new wxExpr(wxExprString, s, FALSE);
1161 return (char *)x;
1162 }
1163
1164 char *proio_cons(char * ccar, char * ccdr)
1165 {
1166 wxExpr *car = (wxExpr *)ccar;
1167 wxExpr *cdr = (wxExpr *)ccdr;
1168
1169 if (cdr == NULL)
1170 {
1171 cdr = new wxExpr(wxExprList);
1172 }
1173 if (car)
1174 cdr->Insert(car);
1175 return (char *)cdr;
1176 }
1177
1178 void process_command(char * cexpr)
1179 {
1180 wxExpr *expr = (wxExpr *)cexpr;
1181 add_expr(expr);
1182 }
1183
1184 void syntax_error(char *WXUNUSED(s))
1185 {
1186 if (currentwxExprErrorHandler)
1187 (void)(*(currentwxExprErrorHandler))(WXEXPR_ERROR_SYNTAX, "syntax error");
1188 if (thewxExprDatabase) thewxExprDatabase->noErrors += 1;
1189 }
1190
1191 #if 0
1192 #ifdef _WINDLL
1193 // char *__cdecl strdup(const char *s)
1194 WXDLLEXPORT char *strdup(const char *s)
1195 {
1196 int len = strlen(s);
1197 char *new_s = (char *)malloc(sizeof(char)*(len+1));
1198 strcpy(new_s, s);
1199 return new_s;
1200 }
1201 #endif
1202 #endif
1203