1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "wxexpr.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
31 #include "wx/wxexpr.h"
33 extern "C" void add_expr(char *);
34 extern "C" void LexFromFile(FILE *fd
);
35 extern "C" void LexFromString(char *buf
);
39 /* Rename all YACC/LEX stuff or we'll conflict with other
43 #define yyback PROIO_yyback
44 #define yylook PROIO_yylook
45 #define yywrap PROIO_yywrap
46 #define yyoutput PROIO_yyoutput
47 #define yylex PROIO_yylex
48 #define yyerror PROIO_yyerror
49 #define yyleng PROIO_yyleng
50 #define yytext PROIO_yytext
51 #define yymorfg PROIO_yymorfg
52 #define yylineno PROIO_yylineno
53 #define yytchar PROIO_yytchar
54 #define yyin PROIO_yyin
55 #define yyout PROIO_yyout
56 #define yysvf PROIO_yysvf
57 #define yyestate PROIO_yyestate
58 #define yysvec PROIO_yysvec
59 #define yybgin PROIO_yybgin
60 #define yyprevious PROIO_yyprevious
61 #define yylhs PROIO_yylhs
62 #define yylen PROIO_yylen
63 #define yydefred PROIO_yydefred
64 #define yydgoto PROIO_yydgoto
65 #define yysindex PROIO_yysindex
66 #define yyrindex PROIO_yyrindex
67 #define yygindex PROIO_yygindex
68 #define yytable PROIO_yytable
69 #define yycheck PROIO_yycheck
70 #define yyname PROIO_yyname
71 #define yyrule PROIO_yyrule
72 #define yydebug PROIO_yydebug
73 #define yynerrs PROIO_yynerrs
74 #define yyerrflag PROIO_yyerrflag
75 #define yychar PROIO_yychar
76 #define yyvsp PROIO_yyvsp
77 #define yyssp PROIO_yyssp
78 #define yyval PROIO_yyval
79 #define yylval PROIO_yylval
80 #define yyss PROIO_yyss
81 #define yyvs PROIO_yyvs
82 #define yyparse PROIO_yyparse
84 /* +++steve162e: more defines necessary */
85 #define yy_init_buffer PROIO_yy_init_buffer
86 #define yy_create_buffer PROIO_yy_create_buffer
87 #define yy_load_buffer_state PROIO_yy_load_buffer_state
88 #define yyrestart PROIO_yyrestart
89 #define yy_switch_to_buffer PROIO_yy_switch_to_buffer
90 #define yy_delete_buffer PROIO_yy_delete_buffer
93 /* WG 1/96: still more for flex 2.5 */
94 #define yy_scan_buffer PROIO_scan_buffer
95 #define yy_scan_string PROIO_scan_string
96 #define yy_scan_bytes PROIO_scan_bytes
97 #define yy_flex_debug PROIO_flex_debug
98 #define yy_flush_buffer PROIO_flush_buffer
99 #if !defined(__VISAGECPP__)
100 /* multiply defined??? */
101 #define yyleng PROIO_yyleng
102 #define yytext PROIO_yytext
105 extern "C" WXDLLEXPORT_DATA(FILE*) yyin
;
106 extern "C" WXDLLEXPORT
int yyparse(void);
109 wxExprDatabase
*thewxExprDatabase
= NULL
;
110 wxExprErrorHandler currentwxExprErrorHandler
;
112 wxExpr::wxExpr(const wxString
& functor
)
119 wxExpr
*pfunctor
= new wxExpr(wxExprWord
, functor
);
124 wxExpr::wxExpr(wxExprType the_type
, const wxString
& word_or_string
)
131 value
.word
= copystring((const wxChar
*)word_or_string
);
134 value
.string
= copystring((const wxChar
*)word_or_string
);
149 wxExpr::wxExpr(wxExprType the_type
, wxChar
*word_or_string
, bool allocate
)
156 value
.word
= allocate
? copystring(word_or_string
) : word_or_string
;
159 value
.string
= allocate
? copystring(word_or_string
) : word_or_string
;
174 wxExpr::wxExpr(long the_integer
)
176 type
= wxExprInteger
;
177 value
.integer
= the_integer
;
182 wxExpr::wxExpr(double the_real
)
185 value
.real
= the_real
;
190 wxExpr::wxExpr(wxList
*the_list
)
197 wxExpr
*listExpr
= new wxExpr(wxExprList
);
199 wxNode
*node
= the_list
->First();
202 wxExpr
*expr
= (wxExpr
*)node
->Data();
203 listExpr
->Append(expr
);
211 wxExpr::~wxExpr(void)
222 delete[] value
.string
;
232 wxExpr
*expr
= value
.first
;
235 wxExpr
*expr1
= expr
->next
;
242 case wxExprNull
: break;
246 void wxExpr::Append(wxExpr
*expr
)
256 void wxExpr::Insert(wxExpr
*expr
)
258 expr
->next
= value
.first
;
265 wxExpr
*wxExpr::Copy(void) const
267 // This seems to get round an optimizer bug when
268 // using Watcom C++ 10a in WIN32 compilation mode.
269 // If these lines not present, the type seems to be
270 // interpreted wrongly as an integer.
271 // I don't want to turn optimization off since it's needed
272 // for reading in files quickly.
273 #if defined(__WATCOMC__)
281 return new wxExpr(value
.integer
);
283 return new wxExpr(value
.real
);
285 return new wxExpr(wxExprString
, wxString(value
.string
));
287 return new wxExpr(wxExprWord
, wxString(value
.word
));
290 wxExpr
*expr
= value
.first
;
291 wxExpr
*new_list
= new wxExpr(wxExprList
);
294 wxExpr
*expr2
= expr
->Copy();
295 new_list
->Append(expr2
);
307 // Get the wxExpr (containing (= wxExpr Value) form) for the given word
308 // or string, assuming that we have Attribute=Value, ...
309 wxExpr
*wxExpr::GetAttributeValueNode(const wxString
& word
) const // Use only for a clause or list
311 if (type
!= wxExprList
)
314 wxExpr
*expr
= value
.first
;
317 if (expr
->type
== wxExprList
)
319 wxExpr
*firstNode
= expr
->value
.first
;
320 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
322 wxExpr
*secondNode
= firstNode
->next
;
323 if ((secondNode
->type
== wxExprWord
) &&
324 (wxStrcmp((const wxChar
*)word
, secondNode
->value
.word
) == 0))
335 // Get the value (in wxExpr form) for the given word or string, assuming
336 // that we have Attribute=Value, ...
337 wxExpr
*wxExpr::AttributeValue(const wxString
& word
) const // Use only for a clause or list
339 if (type
!= wxExprList
)
342 wxExpr
*attExpr
= GetAttributeValueNode(word
);
343 if (attExpr
&& attExpr
->value
.first
&& attExpr
->value
.first
->next
)
344 return attExpr
->value
.first
->next
->next
;
348 wxString
wxExpr::Functor(void) const // Use only for a clause
350 if ((type
!= wxExprList
) || !value
.first
)
351 return wxString(wxT(""));
353 if (value
.first
->type
== wxExprWord
)
354 return wxString(value
.first
->value
.word
);
356 return wxString(wxT(""));
359 bool wxExpr::IsFunctor(const wxString
& f
) const // Use only for a clause
361 if ((type
!= wxExprList
) || !value
.first
)
364 return (value
.first
->type
== wxExprWord
&&
365 (wxStrcmp((const wxChar
*)f
, value
.first
->value
.word
) == 0));
368 // Return nth argument of a clause (starting from 1)
369 wxExpr
*wxExpr::Arg(wxExprType theType
, int arg
) const
371 wxExpr
*expr
= value
.first
;
373 for (i
= 1; i
< arg
; i
++)
377 if (expr
&& (expr
->type
== theType
))
383 // Return nth argument of a list expression (starting from zero)
384 wxExpr
*wxExpr::Nth(int arg
) const
386 if (type
!= wxExprList
)
389 wxExpr
*expr
= value
.first
;
391 for (i
= 0; i
< arg
; i
++)
402 // Returns the number of elements in a list expression
403 int wxExpr::Number(void) const
405 if (type
!= wxExprList
)
409 wxExpr
*expr
= value
.first
;
418 void wxExpr::DeleteAttributeValue(const wxString
& attribute
)
420 if (type
!= wxExprList
)
423 wxExpr
*expr
= value
.first
;
424 wxExpr
*lastExpr
= this;
427 if (expr
->type
== wxExprList
)
429 wxExpr
*firstNode
= expr
->value
.first
;
430 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
432 wxExpr
*secondNode
= firstNode
->next
;
433 if ((secondNode
->type
== wxExprWord
) &&
434 (wxStrcmp((const wxChar
*)attribute
, secondNode
->value
.word
) == 0))
436 wxExpr
*nextExpr
= expr
->next
;
439 lastExpr
->next
= nextExpr
;
454 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxExpr
*val
)
456 if (type
!= wxExprList
)
458 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
461 // Warning - existing code may assume that any existing value
462 // is deleted first. For efficiency, we leave this to the application.
463 // DeleteAttributeValue(attribute);
465 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
466 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
468 wxExpr
*listExpr
= new wxExpr(wxExprList
);
470 listExpr
->Append(pequals
);
471 listExpr
->Append(patt
);
472 listExpr
->Append(val
);
477 void wxExpr::AddAttributeValue(const wxString
& attribute
, long val
)
479 if (type
!= wxExprList
)
481 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
484 // Warning - existing code may assume that any existing value
485 // is deleted first. For efficiency, we leave this to the application.
486 // DeleteAttributeValue(attribute);
488 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
489 wxExpr
*pval
= new wxExpr(val
);
490 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
492 wxExpr
*listExpr
= new wxExpr(wxExprList
);
494 listExpr
->Append(pequals
);
495 listExpr
->Append(patt
);
496 listExpr
->Append(pval
);
501 void wxExpr::AddAttributeValue(const wxString
& attribute
, double val
)
503 if (type
!= wxExprList
)
505 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
509 // DeleteAttributeValue(attribute);
510 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
511 wxExpr
*pval
= new wxExpr(val
);
512 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
514 wxExpr
*listExpr
= new wxExpr(wxExprList
);
516 listExpr
->Append(pequals
);
517 listExpr
->Append(patt
);
518 listExpr
->Append(pval
);
523 void wxExpr::AddAttributeValueString(const wxString
& attribute
, const wxString
& val
)
525 if (type
!= wxExprList
)
527 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
531 // DeleteAttributeValue(attribute);
533 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
534 wxExpr
*pval
= new wxExpr(wxExprString
, val
);
535 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
537 wxExpr
*listExpr
= new wxExpr(wxExprList
);
539 listExpr
->Append(pequals
);
540 listExpr
->Append(patt
);
541 listExpr
->Append(pval
);
546 void wxExpr::AddAttributeValueWord(const wxString
& attribute
, const wxString
& val
)
548 if (type
!= wxExprList
)
550 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
554 // DeleteAttributeValue(attribute);
556 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
557 wxExpr
*pval
= new wxExpr(wxExprWord
, val
);
558 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
560 wxExpr
*listExpr
= new wxExpr(wxExprList
);
562 listExpr
->Append(pequals
);
563 listExpr
->Append(patt
);
564 listExpr
->Append(pval
);
569 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxList
*val
)
571 if (type
!= wxExprList
)
573 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
579 // DeleteAttributeValue(attribute);
581 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
582 wxExpr
*pval
= new wxExpr(val
);
583 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
585 wxExpr
*listExpr
= new wxExpr(wxExprList
);
587 listExpr
->Append(pequals
);
588 listExpr
->Append(patt
);
589 listExpr
->Append(pval
);
594 void wxExpr::AddAttributeValueStringList(const wxString
& attribute
, wxList
*string_list
)
596 if (type
!= wxExprList
)
598 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
604 // DeleteAttributeValue(attribute);
606 // First make a list of wxExpr strings
607 wxExpr
*listExpr
= new wxExpr(wxExprList
);
608 wxNode
*node
= string_list
->First();
611 char *string
= (char *)node
->Data();
612 wxExpr
*expr
= new wxExpr(wxExprString
, wxString(string
));
613 listExpr
->Append(expr
);
617 // Now make an (=, Att, Value) triple
618 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
619 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
621 wxExpr
*listExpr2
= new wxExpr(wxExprList
);
623 listExpr2
->Append(pequals
);
624 listExpr2
->Append(patt
);
625 listExpr2
->Append(listExpr
);
630 bool wxExpr::GetAttributeValue(const wxString
& att
, int& var
) const
632 wxExpr
*expr
= AttributeValue(att
);
634 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
636 var
= (int)(expr
->IntegerValue());
643 bool wxExpr::GetAttributeValue(const wxString
& att
, long& var
) const
645 wxExpr
*expr
= AttributeValue(att
);
647 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
649 var
= expr
->IntegerValue();
656 bool wxExpr::GetAttributeValue(const wxString
& att
, float& var
) const
658 wxExpr
*expr
= AttributeValue(att
);
659 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
661 var
= (float) expr
->RealValue();
668 bool wxExpr::GetAttributeValue(const wxString
& att
, double& var
) const
670 wxExpr
*expr
= AttributeValue(att
);
671 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
673 var
= expr
->RealValue();
680 bool wxExpr::GetAttributeValue(const wxString
& att
, wxString
& var
) const // Word OR string -> string
682 wxExpr
*expr
= AttributeValue(att
);
683 if (expr
&& expr
->Type() == wxExprWord
)
685 var
= expr
->WordValue();
688 else if (expr
&& expr
->Type() == wxExprString
)
690 var
= expr
->StringValue();
697 bool wxExpr::GetAttributeValue(const wxString
& att
, wxExpr
**var
) const
699 wxExpr
*expr
= AttributeValue(att
);
709 bool wxExpr::GetAttributeValueStringList(const wxString
& att
, wxList
*var
) const
711 wxExpr
*expr
= AttributeValue(att
);
712 if (expr
&& expr
->Type() == wxExprList
)
714 wxExpr
*string_expr
= expr
->value
.first
;
717 if (string_expr
->Type() == wxExprString
)
718 var
->Append((wxObject
*)copystring(string_expr
->StringValue()));
720 string_expr
= string_expr
->next
;
729 void wxExpr::AssignAttributeValue(wxChar
*att
, wxChar
**var
) const
732 if (GetAttributeValue(att
, str
))
736 *var
= copystring((const wxChar
*) str
);
740 void wxExpr::WriteClause(FILE* stream
) // Write this expression as a top-level clause
742 if (type
!= wxExprList
)
745 wxExpr
*node
= value
.first
;
748 node
->WriteExpr(stream
);
749 fprintf( stream
, "(" );
755 fprintf( stream
, " " );
756 node
->WriteExpr(stream
);
759 fprintf( stream
, ",\n" );
762 fprintf( stream
, ").\n\n" );
766 void wxExpr::WriteExpr(FILE* stream
) // Write as any other subexpression
768 // This seems to get round an optimizer bug when
769 // using Watcom C++ 10a in WIN32 compilation mode.
770 // If these lines not present, the type seems to be
771 // interpreted wrongly as an integer.
772 // I don't want to turn optimization off since it's needed
773 // for reading in files quickly.
774 #if defined(__WATCOMC__)
783 fprintf( stream
, "%ld", value
.integer
);
788 double f
= value
.real
;
789 fprintf( stream
, "%.6g", f
);
794 fprintf( stream
, "\"" );
796 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.string
);
797 size_t len
= strlen(val
);
798 for (i
= 0; i
< len
; i
++)
801 if (ch
== '"' || ch
== '\\')
802 fprintf( stream
, "\\" );
806 fprintf( stream
, tmp
);
808 fprintf( stream
, "\"" );
813 bool quote_it
= FALSE
;
814 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.word
);
815 size_t len
= strlen(val
);
816 if ((len
== 0) || (len
> 0 && (val
[(size_t) 0] > 64 && val
[(size_t) 0] < 91)))
821 for (i
= 0; i
< len
; i
++)
822 if ((!isalpha(val
[i
])) && (!isdigit(val
[i
])) &&
824 { quote_it
= TRUE
; i
= len
; }
828 fprintf( stream
,"'" );
830 fprintf( stream
, val
);
833 fprintf( stream
, "'" );
840 fprintf( stream
, "[]" );
843 wxExpr
*expr
= value
.first
;
845 if ((expr
->Type() == wxExprWord
) && (wxStrcmp(expr
->WordValue(), wxT("=")) == 0))
847 wxExpr
*arg1
= expr
->next
;
848 wxExpr
*arg2
= arg1
->next
;
849 arg1
->WriteExpr(stream
);
850 fprintf( stream
, " = " );
851 arg2
->WriteExpr(stream
);
855 fprintf( stream
, "[" );
858 expr
->WriteExpr(stream
);
861 fprintf( stream
, ", " );
863 fprintf( stream
, "]" );
868 case wxExprNull
: break;
873 * wxExpr 'database' (list of expressions)
876 IMPLEMENT_DYNAMIC_CLASS(wxExprDatabase
, wxList
)
878 wxExprDatabase::wxExprDatabase(wxExprErrorHandler handler
)
882 currentwxExprErrorHandler
= handler
;
886 wxExprDatabase::wxExprDatabase(wxExprType type
, const wxString
& attribute
, int size
,
887 wxExprErrorHandler handler
)
890 attribute_to_hash
= attribute
;
891 if (type
== wxExprString
)
892 hash_table
= new wxHashTable(wxKEY_STRING
, size
);
893 else if (type
== wxExprInteger
)
894 hash_table
= new wxHashTable(wxKEY_INTEGER
, size
);
895 else hash_table
= NULL
;
897 currentwxExprErrorHandler
= handler
;
901 wxExprDatabase::~wxExprDatabase(void)
908 void wxExprDatabase::BeginFind(void) // Initialise a search
913 wxExpr
*wxExprDatabase::FindClause(long id
) // Find a term based on an integer id attribute
914 // e.g. node(id=23, type=rectangle, ....).
916 wxExpr
*found
= NULL
;
917 while (position
&& !found
)
919 wxExpr
*term
= (wxExpr
*)position
->Data();
921 if (term
->Type() == wxExprList
)
923 wxExpr
*value
= term
->AttributeValue("id");
924 if (value
->Type() == wxExprInteger
&& value
->IntegerValue() == id
)
927 position
= position
->Next();
932 // Find on basis of attribute/value pairs, e.g. type=rectangle
933 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, const wxString
& val
)
935 wxExpr
*found
= NULL
;
936 while (position
&& !found
)
938 wxExpr
*term
= (wxExpr
*)position
->Data();
940 if (term
->Type() == wxExprList
)
942 wxExpr
*value
= term
->AttributeValue(word
);
943 if ((value
->Type() == wxExprWord
&& value
->WordValue() == val
) ||
944 (value
->Type() == wxExprString
&& value
->StringValue() == val
))
947 position
= position
->Next();
952 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, long val
)
954 wxExpr
*found
= NULL
;
955 while (position
&& !found
)
957 wxExpr
*term
= (wxExpr
*)position
->Data();
959 if (term
->Type() == wxExprList
)
961 wxExpr
*value
= term
->AttributeValue(word
);
962 if ((value
->Type() == wxExprInteger
) && (value
->IntegerValue() == val
))
965 position
= position
->Next();
970 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, double val
)
972 wxExpr
*found
= NULL
;
973 while (position
&& !found
)
975 wxExpr
*term
= (wxExpr
*)position
->Data();
977 if (term
->Type() == wxExprList
)
979 wxExpr
*value
= term
->AttributeValue(word
);
980 if ((value
->Type() == wxExprReal
) && (value
->RealValue() == val
))
983 position
= position
->Next();
988 wxExpr
*wxExprDatabase::FindClauseByFunctor(const wxString
& functor
)
990 wxExpr
*found
= NULL
;
991 while (position
&& !found
)
993 wxExpr
*term
= (wxExpr
*)position
->Data();
995 if (term
->Type() == wxExprList
)
997 if (term
->Functor() == functor
)
1000 position
= position
->Next();
1005 // If hashing is on, must store in hash table too
1006 void wxExprDatabase::Append(wxExpr
*clause
)
1008 wxList::Append((wxObject
*)clause
);
1011 wxString
functor(clause
->Functor());
1012 wxExpr
*expr
= clause
->AttributeValue(attribute_to_hash
);
1015 long functor_key
= hash_table
->MakeKey(WXSTRINGCAST functor
);
1017 if (expr
&& expr
->Type() == wxExprString
)
1019 value_key
= hash_table
->MakeKey(WXSTRINGCAST expr
->StringValue());
1020 hash_table
->Put(functor_key
+ value_key
, WXSTRINGCAST expr
->StringValue(), (wxObject
*)clause
);
1022 else if (expr
&& expr
->Type() == wxExprInteger
)
1024 value_key
= expr
->IntegerValue();
1025 hash_table
->Put(functor_key
+ value_key
, expr
->IntegerValue(), (wxObject
*)clause
);
1032 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, long value
) const
1034 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + value
;
1036 // The key alone isn't guaranteed to be unique:
1037 // must supply value too. Let's assume the value of the
1038 // id is going to be reasonably unique.
1039 return (wxExpr
*)hash_table
->Get(key
, value
);
1042 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, const wxString
& value
) const
1044 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + hash_table
->MakeKey(WXSTRINGCAST value
);
1045 return (wxExpr
*)hash_table
->Get(key
, WXSTRINGCAST value
);
1048 void wxExprDatabase::ClearDatabase(void)
1051 wxNode
*node
= First();
1054 wxExpr
*expr
= (wxExpr
*)node
->Data();
1061 hash_table
->Clear();
1064 bool wxExprDatabase::Read(const wxString
& filename
)
1068 FILE *f
= wxFopen(filename
, _T("r"));
1071 thewxExprDatabase
= this;
1078 return (noErrors
== 0);
1086 bool wxExprDatabase::ReadFromString(const wxString
& buffer
)
1089 thewxExprDatabase
= this;
1091 const wxWX2MBbuf buf
= buffer
.mb_str();
1092 LexFromString(wxMBSTRINGCAST buf
);
1095 return (noErrors
== 0);
1098 bool wxExprDatabase::Write(const wxString
& fileName
)
1100 FILE *stream
= wxFopen( fileName
, _T("w+"));
1105 bool success
= Write(stream
);
1110 bool wxExprDatabase::Write(FILE *stream
)
1113 wxNode
*node
= First();
1116 wxExpr
*expr
= (wxExpr
*)node
->Data();
1117 expr
->WriteClause(stream
);
1118 node
= node
->Next();
1120 return (noErrors
== 0);
1123 void add_expr(wxExpr
* expr
)
1125 thewxExprDatabase
->Append(expr
);
1129 bool wxExprIsFunctor(wxExpr
*expr
, const wxString
& functor
)
1131 if (expr
&& (expr
->Type() == wxExprList
))
1133 wxExpr
*first_expr
= expr
->value
.first
;
1135 if (first_expr
&& (first_expr
->Type() == wxExprWord
) &&
1136 (first_expr
->WordValue() == functor
))
1146 * Called from parser
1150 char *wxmake_integer(char *str
)
1152 wxExpr
*x
= new wxExpr(atol(str
));
1157 char *wxmake_real(char *str1
, char *str2
)
1161 sprintf(buf
, "%s.%s", str1
, str2
);
1162 double f
= (double)atof(buf
);
1163 wxExpr
*x
= new wxExpr(f
);
1168 // extern "C" double exp10(double);
1170 char *wxmake_exp(char *str1
, char *str2
)
1172 double mantissa
= (double)atoi(str1
);
1173 double exponent
= (double)atoi(str2
);
1175 double d
= mantissa
* pow(10.0, exponent
);
1177 wxExpr
*x
= new wxExpr(d
);
1182 char *wxmake_exp2(char *str1
, char *str2
, char *str3
)
1186 sprintf(buf
, "%s.%s", str1
, str2
);
1187 double mantissa
= (double)atof(buf
);
1188 double exponent
= (double)atoi(str3
);
1190 double d
= mantissa
* pow(10.0, exponent
);
1192 wxExpr
*x
= new wxExpr(d
);
1197 char *wxmake_word(char *str
)
1199 wxExpr
*x
= new wxExpr(wxExprWord
, str
);
1203 char *wxmake_string(char *str
)
1207 const wxMB2WXbuf sbuf
= wxConvLibc
.cMB2WX(str
);
1209 // str++; /* skip leading quote */
1210 len
= wxStrlen(sbuf
) - 1; /* ignore trailing quote */
1212 s
= new wxChar
[len
+ 1];
1215 for(i
=1; i
<len
; i
++) // 1 since we want to skip leading quote
1217 if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('"'))
1222 else if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('\\'))
1233 wxExpr
*x
= new wxExpr(wxExprString
, s
, FALSE
);
1237 char *proio_cons(char * ccar
, char * ccdr
)
1239 wxExpr
*car
= (wxExpr
*)ccar
;
1240 wxExpr
*cdr
= (wxExpr
*)ccdr
;
1244 cdr
= new wxExpr(wxExprList
);
1251 void process_command(char * cexpr
)
1253 wxExpr
*expr
= (wxExpr
*)cexpr
;
1257 void syntax_error(char *WXUNUSED(s
))
1259 if (currentwxExprErrorHandler
)
1260 (void)(*(currentwxExprErrorHandler
))(WXEXPR_ERROR_SYNTAX
, (char *)"syntax error");
1261 if (thewxExprDatabase
) thewxExprDatabase
->noErrors
+= 1;
1266 // char *__cdecl strdup(const char *s)
1267 WXDLLEXPORT
char *strdup(const char *s
)
1269 int len
= strlen(s
);
1270 char *new_s
= (char *)malloc(sizeof(char)*(len
+1));