1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "wxexpr.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/deprecated/setup.h"
32 #include "wx/deprecated/expr.h"
33 #include "wx/deprecated/wxexpr.h"
35 #if !WXWIN_COMPATIBILITY_2_4
36 static inline wxChar
* copystring(const wxChar
* s
)
37 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
40 extern "C" void add_expr(char *);
41 extern "C" void LexFromFile(FILE *fd
);
42 extern "C" void LexFromString(char *buf
);
46 /* Rename all YACC/LEX stuff or we'll conflict with other
50 #define yyback PROIO_yyback
51 #define yylook PROIO_yylook
52 #define yywrap PROIO_yywrap
53 #define yyoutput PROIO_yyoutput
54 #define yylex PROIO_yylex
55 #define yyerror PROIO_yyerror
56 #define yyleng PROIO_yyleng
57 #define yytext PROIO_yytext
58 #define yymorfg PROIO_yymorfg
59 #define yylineno PROIO_yylineno
60 #define yytchar PROIO_yytchar
61 #define yyin PROIO_yyin
62 #define yyout PROIO_yyout
63 #define yysvf PROIO_yysvf
64 #define yyestate PROIO_yyestate
65 #define yysvec PROIO_yysvec
66 #define yybgin PROIO_yybgin
67 #define yyprevious PROIO_yyprevious
68 #define yylhs PROIO_yylhs
69 #define yylen PROIO_yylen
70 #define yydefred PROIO_yydefred
71 #define yydgoto PROIO_yydgoto
72 #define yysindex PROIO_yysindex
73 #define yyrindex PROIO_yyrindex
74 #define yygindex PROIO_yygindex
75 #define yytable PROIO_yytable
76 #define yycheck PROIO_yycheck
77 #define yyname PROIO_yyname
78 #define yyrule PROIO_yyrule
79 #define yydebug PROIO_yydebug
80 #define yynerrs PROIO_yynerrs
81 #define yyerrflag PROIO_yyerrflag
82 #define yychar PROIO_yychar
83 #define yyvsp PROIO_yyvsp
84 #define yyssp PROIO_yyssp
85 #define yyval PROIO_yyval
86 #define yylval PROIO_yylval
87 #define yyss PROIO_yyss
88 #define yyvs PROIO_yyvs
89 #define yyparse PROIO_yyparse
91 /* +++steve162e: more defines necessary */
92 #define yy_init_buffer PROIO_yy_init_buffer
93 #define yy_create_buffer PROIO_yy_create_buffer
94 #define yy_load_buffer_state PROIO_yy_load_buffer_state
95 #define yyrestart PROIO_yyrestart
96 #define yy_switch_to_buffer PROIO_yy_switch_to_buffer
97 #define yy_delete_buffer PROIO_yy_delete_buffer
100 /* WG 1/96: still more for flex 2.5 */
101 #define yy_scan_buffer PROIO_scan_buffer
102 #define yy_scan_string PROIO_scan_string
103 #define yy_scan_bytes PROIO_scan_bytes
104 #define yy_flex_debug PROIO_flex_debug
105 #define yy_flush_buffer PROIO_flush_buffer
106 #if !defined(__VISAGECPP__)
107 /* multiply defined??? */
108 #define yyleng PROIO_yyleng
109 #define yytext PROIO_yytext
112 extern "C" WXDLLIMPEXP_DATA_DEPRECATED(FILE*) yyin
;
113 extern "C" WXDLLIMPEXP_DEPRECATED
int yyparse(void);
116 wxExprDatabase
*thewxExprDatabase
= NULL
;
117 wxExprErrorHandler currentwxExprErrorHandler
;
119 wxExpr::wxExpr(const wxString
& functor
)
126 wxExpr
*pfunctor
= new wxExpr(wxExprWord
, functor
);
131 wxExpr::wxExpr(wxExprType the_type
, const wxString
& word_or_string
)
138 value
.word
= copystring((const wxChar
*)word_or_string
);
141 value
.string
= copystring((const wxChar
*)word_or_string
);
156 wxExpr::wxExpr(wxExprType the_type
, wxChar
*word_or_string
, bool allocate
)
163 value
.word
= allocate
? copystring(word_or_string
) : word_or_string
;
166 value
.string
= allocate
? copystring(word_or_string
) : word_or_string
;
181 wxExpr::wxExpr(long the_integer
)
183 type
= wxExprInteger
;
184 value
.integer
= the_integer
;
189 wxExpr::wxExpr(double the_real
)
192 value
.real
= the_real
;
197 wxExpr::wxExpr(wxList
*the_list
)
204 wxExpr
*listExpr
= new wxExpr(wxExprList
);
206 wxNode
*node
= the_list
->GetFirst();
209 wxExpr
*expr
= (wxExpr
*)node
->GetData();
210 listExpr
->Append(expr
);
211 node
= node
->GetNext();
218 wxExpr::~wxExpr(void)
229 delete[] value
.string
;
239 wxExpr
*expr
= value
.first
;
242 wxExpr
*expr1
= expr
->next
;
249 case wxExprNull
: break;
253 void wxExpr::Append(wxExpr
*expr
)
263 void wxExpr::Insert(wxExpr
*expr
)
265 expr
->next
= value
.first
;
272 wxExpr
*wxExpr::Copy(void) const
274 // This seems to get round an optimizer bug when
275 // using Watcom C++ 10a in WIN32 compilation mode.
276 // If these lines not present, the type seems to be
277 // interpreted wrongly as an integer.
278 // I don't want to turn optimization off since it's needed
279 // for reading in files quickly.
280 #if defined(__WATCOMC__)
288 return new wxExpr(value
.integer
);
290 return new wxExpr(value
.real
);
292 return new wxExpr(wxExprString
, wxString(value
.string
));
294 return new wxExpr(wxExprWord
, wxString(value
.word
));
297 wxExpr
*expr
= value
.first
;
298 wxExpr
*new_list
= new wxExpr(wxExprList
);
301 wxExpr
*expr2
= expr
->Copy();
302 new_list
->Append(expr2
);
314 // Get the wxExpr (containing (= wxExpr Value) form) for the given word
315 // or string, assuming that we have Attribute=Value, ...
316 wxExpr
*wxExpr::GetAttributeValueNode(const wxString
& word
) const // Use only for a clause or list
318 if (type
!= wxExprList
)
321 wxExpr
*expr
= value
.first
;
324 if (expr
->type
== wxExprList
)
326 wxExpr
*firstNode
= expr
->value
.first
;
327 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
329 wxExpr
*secondNode
= firstNode
->next
;
330 if ((secondNode
->type
== wxExprWord
) &&
331 (wxStrcmp((const wxChar
*)word
, secondNode
->value
.word
) == 0))
342 // Get the value (in wxExpr form) for the given word or string, assuming
343 // that we have Attribute=Value, ...
344 wxExpr
*wxExpr::AttributeValue(const wxString
& word
) const // Use only for a clause or list
346 if (type
!= wxExprList
)
349 wxExpr
*attExpr
= GetAttributeValueNode(word
);
350 if (attExpr
&& attExpr
->value
.first
&& attExpr
->value
.first
->next
)
351 return attExpr
->value
.first
->next
->next
;
355 wxString
wxExpr::Functor(void) const // Use only for a clause
357 if ((type
!= wxExprList
) || !value
.first
)
358 return wxString(wxT(""));
360 if (value
.first
->type
== wxExprWord
)
361 return wxString(value
.first
->value
.word
);
363 return wxString(wxT(""));
366 bool wxExpr::IsFunctor(const wxString
& f
) const // Use only for a clause
368 if ((type
!= wxExprList
) || !value
.first
)
371 return (value
.first
->type
== wxExprWord
&&
372 (wxStrcmp((const wxChar
*)f
, value
.first
->value
.word
) == 0));
375 // Return nth argument of a clause (starting from 1)
376 wxExpr
*wxExpr::Arg(wxExprType theType
, int arg
) const
378 wxExpr
*expr
= value
.first
;
380 for (i
= 1; i
< arg
; i
++)
384 if (expr
&& (expr
->type
== theType
))
390 // Return nth argument of a list expression (starting from zero)
391 wxExpr
*wxExpr::Nth(int arg
) const
393 if (type
!= wxExprList
)
396 wxExpr
*expr
= value
.first
;
398 for (i
= 0; i
< arg
; i
++)
409 // Returns the number of elements in a list expression
410 int wxExpr::Number(void) const
412 if (type
!= wxExprList
)
416 wxExpr
*expr
= value
.first
;
425 void wxExpr::DeleteAttributeValue(const wxString
& attribute
)
427 if (type
!= wxExprList
)
430 wxExpr
*expr
= value
.first
;
431 wxExpr
*lastExpr
= this;
434 if (expr
->type
== wxExprList
)
436 wxExpr
*firstNode
= expr
->value
.first
;
437 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
439 wxExpr
*secondNode
= firstNode
->next
;
440 if ((secondNode
->type
== wxExprWord
) &&
441 (wxStrcmp((const wxChar
*)attribute
, secondNode
->value
.word
) == 0))
443 wxExpr
*nextExpr
= expr
->next
;
446 lastExpr
->next
= nextExpr
;
461 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxExpr
*val
)
463 if (type
!= wxExprList
)
465 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
468 // Warning - existing code may assume that any existing value
469 // is deleted first. For efficiency, we leave this to the application.
470 // DeleteAttributeValue(attribute);
472 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
473 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
475 wxExpr
*listExpr
= new wxExpr(wxExprList
);
477 listExpr
->Append(pequals
);
478 listExpr
->Append(patt
);
479 listExpr
->Append(val
);
484 void wxExpr::AddAttributeValue(const wxString
& attribute
, long val
)
486 if (type
!= wxExprList
)
488 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
491 // Warning - existing code may assume that any existing value
492 // is deleted first. For efficiency, we leave this to the application.
493 // DeleteAttributeValue(attribute);
495 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
496 wxExpr
*pval
= new wxExpr(val
);
497 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
499 wxExpr
*listExpr
= new wxExpr(wxExprList
);
501 listExpr
->Append(pequals
);
502 listExpr
->Append(patt
);
503 listExpr
->Append(pval
);
508 void wxExpr::AddAttributeValue(const wxString
& attribute
, double val
)
510 if (type
!= wxExprList
)
512 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
516 // DeleteAttributeValue(attribute);
517 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
518 wxExpr
*pval
= new wxExpr(val
);
519 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
521 wxExpr
*listExpr
= new wxExpr(wxExprList
);
523 listExpr
->Append(pequals
);
524 listExpr
->Append(patt
);
525 listExpr
->Append(pval
);
530 void wxExpr::AddAttributeValueString(const wxString
& attribute
, const wxString
& val
)
532 if (type
!= wxExprList
)
534 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
538 // DeleteAttributeValue(attribute);
540 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
541 wxExpr
*pval
= new wxExpr(wxExprString
, val
);
542 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
544 wxExpr
*listExpr
= new wxExpr(wxExprList
);
546 listExpr
->Append(pequals
);
547 listExpr
->Append(patt
);
548 listExpr
->Append(pval
);
553 void wxExpr::AddAttributeValueWord(const wxString
& attribute
, const wxString
& val
)
555 if (type
!= wxExprList
)
557 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
561 // DeleteAttributeValue(attribute);
563 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
564 wxExpr
*pval
= new wxExpr(wxExprWord
, val
);
565 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
567 wxExpr
*listExpr
= new wxExpr(wxExprList
);
569 listExpr
->Append(pequals
);
570 listExpr
->Append(patt
);
571 listExpr
->Append(pval
);
576 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxList
*val
)
578 if (type
!= wxExprList
)
580 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
586 // DeleteAttributeValue(attribute);
588 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
589 wxExpr
*pval
= new wxExpr(val
);
590 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
592 wxExpr
*listExpr
= new wxExpr(wxExprList
);
594 listExpr
->Append(pequals
);
595 listExpr
->Append(patt
);
596 listExpr
->Append(pval
);
601 void wxExpr::AddAttributeValueStringList(const wxString
& attribute
, wxList
*string_list
)
603 if (type
!= wxExprList
)
605 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
611 // DeleteAttributeValue(attribute);
613 // First make a list of wxExpr strings
614 wxExpr
*listExpr
= new wxExpr(wxExprList
);
615 wxNode
*node
= string_list
->GetFirst();
618 wxChar
*string
= (wxChar
*)node
->GetData();
619 wxExpr
*expr
= new wxExpr(wxExprString
, wxString(string
));
620 listExpr
->Append(expr
);
621 node
= node
->GetNext();
624 // Now make an (=, Att, Value) triple
625 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
626 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
628 wxExpr
*listExpr2
= new wxExpr(wxExprList
);
630 listExpr2
->Append(pequals
);
631 listExpr2
->Append(patt
);
632 listExpr2
->Append(listExpr
);
637 bool wxExpr::GetAttributeValue(const wxString
& att
, int& var
) const
639 wxExpr
*expr
= AttributeValue(att
);
641 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
643 var
= (int)(expr
->IntegerValue());
650 bool wxExpr::GetAttributeValue(const wxString
& att
, long& var
) const
652 wxExpr
*expr
= AttributeValue(att
);
654 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
656 var
= expr
->IntegerValue();
663 bool wxExpr::GetAttributeValue(const wxString
& att
, float& var
) const
665 wxExpr
*expr
= AttributeValue(att
);
666 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
668 var
= (float) expr
->RealValue();
675 bool wxExpr::GetAttributeValue(const wxString
& att
, double& var
) const
677 wxExpr
*expr
= AttributeValue(att
);
678 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
680 var
= expr
->RealValue();
687 bool wxExpr::GetAttributeValue(const wxString
& att
, wxString
& var
) const // Word OR string -> string
689 wxExpr
*expr
= AttributeValue(att
);
690 if (expr
&& expr
->Type() == wxExprWord
)
692 var
= expr
->WordValue();
695 else if (expr
&& expr
->Type() == wxExprString
)
697 var
= expr
->StringValue();
704 bool wxExpr::GetAttributeValue(const wxString
& att
, wxExpr
**var
) const
706 wxExpr
*expr
= AttributeValue(att
);
716 bool wxExpr::GetAttributeValueStringList(const wxString
& att
, wxList
*var
) const
718 wxExpr
*expr
= AttributeValue(att
);
719 if (expr
&& expr
->Type() == wxExprList
)
721 wxExpr
*string_expr
= expr
->value
.first
;
724 if (string_expr
->Type() == wxExprString
)
725 var
->Append((wxObject
*)copystring(string_expr
->StringValue()));
727 string_expr
= string_expr
->next
;
736 void wxExpr::AssignAttributeValue(wxChar
*att
, wxChar
**var
) const
739 if (GetAttributeValue(att
, str
))
743 *var
= copystring((const wxChar
*) str
);
747 void wxExpr::WriteClause(FILE* stream
) // Write this expression as a top-level clause
749 if (type
!= wxExprList
)
752 wxExpr
*node
= value
.first
;
755 node
->WriteExpr(stream
);
756 fprintf( stream
, "(" );
762 fprintf( stream
, " " );
763 node
->WriteExpr(stream
);
766 fprintf( stream
, ",\n" );
769 fprintf( stream
, ").\n\n" );
773 void wxExpr::WriteExpr(FILE* stream
) // Write as any other subexpression
775 // This seems to get round an optimizer bug when
776 // using Watcom C++ 10a in WIN32 compilation mode.
777 // If these lines not present, the type seems to be
778 // interpreted wrongly as an integer.
779 // I don't want to turn optimization off since it's needed
780 // for reading in files quickly.
781 #if defined(__WATCOMC__)
790 fprintf( stream
, "%ld", value
.integer
);
795 double f
= value
.real
;
796 fprintf( stream
, "%.6g", f
);
801 fprintf( stream
, "\"" );
803 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.string
);
804 size_t len
= strlen(val
);
805 for (i
= 0; i
< len
; i
++)
808 if (ch
== '"' || ch
== '\\')
809 fprintf( stream
, "\\" );
813 fprintf( stream
, tmp
);
815 fprintf( stream
, "\"" );
820 bool quote_it
= FALSE
;
821 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.word
);
822 size_t len
= strlen(val
);
823 if ((len
== 0) || (len
> 0 && (val
[(size_t) 0] > 64 && val
[(size_t) 0] < 91)))
828 for (i
= 0; i
< len
; i
++)
829 if ((!isalpha(val
[i
])) && (!isdigit(val
[i
])) &&
831 { quote_it
= TRUE
; i
= len
; }
835 fprintf( stream
,"'" );
837 fprintf( stream
, val
);
840 fprintf( stream
, "'" );
847 fprintf( stream
, "[]" );
850 wxExpr
*expr
= value
.first
;
852 if ((expr
->Type() == wxExprWord
) && (wxStrcmp(expr
->WordValue(), wxT("=")) == 0))
854 wxExpr
*arg1
= expr
->next
;
855 wxExpr
*arg2
= arg1
->next
;
856 arg1
->WriteExpr(stream
);
857 fprintf( stream
, " = " );
858 arg2
->WriteExpr(stream
);
862 fprintf( stream
, "[" );
865 expr
->WriteExpr(stream
);
868 fprintf( stream
, ", " );
870 fprintf( stream
, "]" );
875 case wxExprNull
: break;
880 * wxExpr 'database' (list of expressions)
883 IMPLEMENT_DYNAMIC_CLASS(wxExprDatabase
, wxList
)
885 wxExprDatabase::wxExprDatabase(wxExprErrorHandler handler
)
889 currentwxExprErrorHandler
= handler
;
893 wxExprDatabase::wxExprDatabase(wxExprType type
, const wxString
& attribute
, int size
,
894 wxExprErrorHandler handler
)
897 attribute_to_hash
= attribute
;
898 if (type
== wxExprString
)
899 hash_table
= new wxHashTable(wxKEY_STRING
, size
);
900 else if (type
== wxExprInteger
)
901 hash_table
= new wxHashTable(wxKEY_INTEGER
, size
);
902 else hash_table
= NULL
;
904 currentwxExprErrorHandler
= handler
;
908 wxExprDatabase::~wxExprDatabase(void)
915 void wxExprDatabase::BeginFind(void) // Initialise a search
917 position
= GetFirst();
920 wxExpr
*wxExprDatabase::FindClause(long id
) // Find a term based on an integer id attribute
921 // e.g. node(id=23, type=rectangle, ....).
923 wxExpr
*found
= NULL
;
924 while (position
&& !found
)
926 wxExpr
*term
= (wxExpr
*)position
->GetData();
928 if (term
->Type() == wxExprList
)
930 wxExpr
*value
= term
->AttributeValue(wxT("id"));
931 if (value
->Type() == wxExprInteger
&& value
->IntegerValue() == id
)
934 position
= position
->GetNext();
939 // Find on basis of attribute/value pairs, e.g. type=rectangle
940 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, const wxString
& val
)
942 wxExpr
*found
= NULL
;
943 while (position
&& !found
)
945 wxExpr
*term
= (wxExpr
*)position
->GetData();
947 if (term
->Type() == wxExprList
)
949 wxExpr
*value
= term
->AttributeValue(word
);
950 if ((value
->Type() == wxExprWord
&& value
->WordValue() == val
) ||
951 (value
->Type() == wxExprString
&& value
->StringValue() == val
))
954 position
= position
->GetNext();
959 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, long val
)
961 wxExpr
*found
= NULL
;
962 while (position
&& !found
)
964 wxExpr
*term
= (wxExpr
*)position
->GetData();
966 if (term
->Type() == wxExprList
)
968 wxExpr
*value
= term
->AttributeValue(word
);
969 if ((value
->Type() == wxExprInteger
) && (value
->IntegerValue() == val
))
972 position
= position
->GetNext();
977 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, double val
)
979 wxExpr
*found
= NULL
;
980 while (position
&& !found
)
982 wxExpr
*term
= (wxExpr
*)position
->GetData();
984 if (term
->Type() == wxExprList
)
986 wxExpr
*value
= term
->AttributeValue(word
);
987 if ((value
->Type() == wxExprReal
) && (value
->RealValue() == val
))
990 position
= position
->GetNext();
995 wxExpr
*wxExprDatabase::FindClauseByFunctor(const wxString
& functor
)
997 wxExpr
*found
= NULL
;
998 while (position
&& !found
)
1000 wxExpr
*term
= (wxExpr
*)position
->GetData();
1002 if (term
->Type() == wxExprList
)
1004 if (term
->Functor() == functor
)
1007 position
= position
->GetNext();
1012 // If hashing is on, must store in hash table too
1013 void wxExprDatabase::Append(wxExpr
*clause
)
1015 wxList::Append((wxObject
*)clause
);
1018 wxString
functor(clause
->Functor());
1019 wxExpr
*expr
= clause
->AttributeValue(attribute_to_hash
);
1022 long functor_key
= hash_table
->MakeKey(WXSTRINGCAST functor
);
1024 if (expr
&& expr
->Type() == wxExprString
)
1026 value_key
= hash_table
->MakeKey(WXSTRINGCAST expr
->StringValue());
1027 hash_table
->Put(functor_key
+ value_key
, WXSTRINGCAST expr
->StringValue(), (wxObject
*)clause
);
1029 else if (expr
&& expr
->Type() == wxExprInteger
)
1031 value_key
= expr
->IntegerValue();
1032 hash_table
->Put(functor_key
+ value_key
, expr
->IntegerValue(), (wxObject
*)clause
);
1039 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, long value
) const
1041 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + value
;
1043 // The key alone isn't guaranteed to be unique:
1044 // must supply value too. Let's assume the value of the
1045 // id is going to be reasonably unique.
1046 return (wxExpr
*)hash_table
->Get(key
, value
);
1049 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, const wxString
& value
) const
1051 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + hash_table
->MakeKey(WXSTRINGCAST value
);
1052 return (wxExpr
*)hash_table
->Get(key
, WXSTRINGCAST value
);
1055 void wxExprDatabase::ClearDatabase(void)
1058 wxNode
*node
= GetFirst();
1061 wxExpr
*expr
= (wxExpr
*)node
->GetData();
1068 hash_table
->Clear();
1071 bool wxExprDatabase::Read(const wxString
& filename
)
1075 FILE *f
= wxFopen(filename
, _T("r"));
1078 thewxExprDatabase
= this;
1085 return (noErrors
== 0);
1093 bool wxExprDatabase::ReadFromString(const wxString
& buffer
)
1096 thewxExprDatabase
= this;
1098 const wxWX2MBbuf buf
= buffer
.mb_str();
1099 LexFromString(wxMBSTRINGCAST buf
);
1102 return (noErrors
== 0);
1105 bool wxExprDatabase::Write(const wxString
& fileName
)
1107 FILE *stream
= wxFopen( fileName
, _T("w+"));
1112 bool success
= Write(stream
);
1117 bool wxExprDatabase::Write(FILE *stream
)
1120 wxNode
*node
= GetFirst();
1123 wxExpr
*expr
= (wxExpr
*)node
->GetData();
1124 expr
->WriteClause(stream
);
1125 node
= node
->GetNext();
1127 return (noErrors
== 0);
1130 void add_expr(wxExpr
* expr
)
1132 thewxExprDatabase
->Append(expr
);
1136 bool wxExprIsFunctor(wxExpr
*expr
, const wxString
& functor
)
1138 if (expr
&& (expr
->Type() == wxExprList
))
1140 wxExpr
*first_expr
= expr
->value
.first
;
1142 if (first_expr
&& (first_expr
->Type() == wxExprWord
) &&
1143 (first_expr
->WordValue() == functor
))
1153 * Called from parser
1157 char *wxmake_integer(char *str
)
1159 wxExpr
*x
= new wxExpr(atol(str
));
1164 char *wxmake_real(char *str1
, char *str2
)
1168 sprintf(buf
, "%s.%s", str1
, str2
);
1169 double f
= (double)atof(buf
);
1170 wxExpr
*x
= new wxExpr(f
);
1175 // extern "C" double exp10(double);
1177 char *wxmake_exp(char *str1
, char *str2
)
1179 double mantissa
= (double)atoi(str1
);
1180 double exponent
= (double)atoi(str2
);
1182 double d
= mantissa
* pow(10.0, exponent
);
1184 wxExpr
*x
= new wxExpr(d
);
1189 char *wxmake_exp2(char *str1
, char *str2
, char *str3
)
1193 sprintf(buf
, "%s.%s", str1
, str2
);
1194 double mantissa
= (double)atof(buf
);
1195 double exponent
= (double)atoi(str3
);
1197 double d
= mantissa
* pow(10.0, exponent
);
1199 wxExpr
*x
= new wxExpr(d
);
1204 char *wxmake_word(char *str
)
1206 wxExpr
*x
= new wxExpr(wxExprWord
, wxString(str
, wxConvLibc
).c_str());
1210 char *wxmake_string(char *str
)
1214 const wxMB2WXbuf sbuf
= wxConvLibc
.cMB2WX(str
);
1216 // str++; /* skip leading quote */
1217 len
= wxStrlen(sbuf
) - 1; /* ignore trailing quote */
1219 s
= new wxChar
[len
+ 1];
1222 for(i
=1; i
<len
; i
++) // 1 since we want to skip leading quote
1224 if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('"'))
1229 else if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('\\'))
1240 wxExpr
*x
= new wxExpr(wxExprString
, s
, FALSE
);
1244 char *proio_cons(char * ccar
, char * ccdr
)
1246 wxExpr
*car
= (wxExpr
*)ccar
;
1247 wxExpr
*cdr
= (wxExpr
*)ccdr
;
1251 cdr
= new wxExpr(wxExprList
);
1258 void process_command(char * cexpr
)
1260 wxExpr
*expr
= (wxExpr
*)cexpr
;
1264 void syntax_error(char *WXUNUSED(s
))
1266 if (currentwxExprErrorHandler
)
1267 (void)(*(currentwxExprErrorHandler
))(WXEXPR_ERROR_SYNTAX
, (char *)"syntax error");
1268 if (thewxExprDatabase
) thewxExprDatabase
->noErrors
+= 1;
1273 // char *__cdecl strdup(const char *s)
1274 WXDLLEXPORT
char *strdup(const char *s
)
1276 int len
= strlen(s
);
1277 char *new_s
= (char *)malloc(sizeof(char)*(len
+1));