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 extern "C" void add_expr(char *);
36 extern "C" void LexFromFile(FILE *fd
);
37 extern "C" void LexFromString(char *buf
);
41 /* Rename all YACC/LEX stuff or we'll conflict with other
45 #define yyback PROIO_yyback
46 #define yylook PROIO_yylook
47 #define yywrap PROIO_yywrap
48 #define yyoutput PROIO_yyoutput
49 #define yylex PROIO_yylex
50 #define yyerror PROIO_yyerror
51 #define yyleng PROIO_yyleng
52 #define yytext PROIO_yytext
53 #define yymorfg PROIO_yymorfg
54 #define yylineno PROIO_yylineno
55 #define yytchar PROIO_yytchar
56 #define yyin PROIO_yyin
57 #define yyout PROIO_yyout
58 #define yysvf PROIO_yysvf
59 #define yyestate PROIO_yyestate
60 #define yysvec PROIO_yysvec
61 #define yybgin PROIO_yybgin
62 #define yyprevious PROIO_yyprevious
63 #define yylhs PROIO_yylhs
64 #define yylen PROIO_yylen
65 #define yydefred PROIO_yydefred
66 #define yydgoto PROIO_yydgoto
67 #define yysindex PROIO_yysindex
68 #define yyrindex PROIO_yyrindex
69 #define yygindex PROIO_yygindex
70 #define yytable PROIO_yytable
71 #define yycheck PROIO_yycheck
72 #define yyname PROIO_yyname
73 #define yyrule PROIO_yyrule
74 #define yydebug PROIO_yydebug
75 #define yynerrs PROIO_yynerrs
76 #define yyerrflag PROIO_yyerrflag
77 #define yychar PROIO_yychar
78 #define yyvsp PROIO_yyvsp
79 #define yyssp PROIO_yyssp
80 #define yyval PROIO_yyval
81 #define yylval PROIO_yylval
82 #define yyss PROIO_yyss
83 #define yyvs PROIO_yyvs
84 #define yyparse PROIO_yyparse
86 /* +++steve162e: more defines necessary */
87 #define yy_init_buffer PROIO_yy_init_buffer
88 #define yy_create_buffer PROIO_yy_create_buffer
89 #define yy_load_buffer_state PROIO_yy_load_buffer_state
90 #define yyrestart PROIO_yyrestart
91 #define yy_switch_to_buffer PROIO_yy_switch_to_buffer
92 #define yy_delete_buffer PROIO_yy_delete_buffer
95 /* WG 1/96: still more for flex 2.5 */
96 #define yy_scan_buffer PROIO_scan_buffer
97 #define yy_scan_string PROIO_scan_string
98 #define yy_scan_bytes PROIO_scan_bytes
99 #define yy_flex_debug PROIO_flex_debug
100 #define yy_flush_buffer PROIO_flush_buffer
101 #if !defined(__VISAGECPP__)
102 /* multiply defined??? */
103 #define yyleng PROIO_yyleng
104 #define yytext PROIO_yytext
107 extern "C" WXDLLIMPEXP_DATA_DEPRECATED(FILE*) yyin
;
108 extern "C" WXDLLIMPEXP_DEPRECATED
int yyparse(void);
111 wxExprDatabase
*thewxExprDatabase
= NULL
;
112 wxExprErrorHandler currentwxExprErrorHandler
;
114 wxExpr::wxExpr(const wxString
& functor
)
121 wxExpr
*pfunctor
= new wxExpr(wxExprWord
, functor
);
126 wxExpr::wxExpr(wxExprType the_type
, const wxString
& word_or_string
)
133 value
.word
= copystring((const wxChar
*)word_or_string
);
136 value
.string
= copystring((const wxChar
*)word_or_string
);
151 wxExpr::wxExpr(wxExprType the_type
, wxChar
*word_or_string
, bool allocate
)
158 value
.word
= allocate
? copystring(word_or_string
) : word_or_string
;
161 value
.string
= allocate
? copystring(word_or_string
) : word_or_string
;
176 wxExpr::wxExpr(long the_integer
)
178 type
= wxExprInteger
;
179 value
.integer
= the_integer
;
184 wxExpr::wxExpr(double the_real
)
187 value
.real
= the_real
;
192 wxExpr::wxExpr(wxList
*the_list
)
199 wxExpr
*listExpr
= new wxExpr(wxExprList
);
201 wxNode
*node
= the_list
->GetFirst();
204 wxExpr
*expr
= (wxExpr
*)node
->GetData();
205 listExpr
->Append(expr
);
206 node
= node
->GetNext();
213 wxExpr::~wxExpr(void)
224 delete[] value
.string
;
234 wxExpr
*expr
= value
.first
;
237 wxExpr
*expr1
= expr
->next
;
244 case wxExprNull
: break;
248 void wxExpr::Append(wxExpr
*expr
)
258 void wxExpr::Insert(wxExpr
*expr
)
260 expr
->next
= value
.first
;
267 wxExpr
*wxExpr::Copy(void) const
269 // This seems to get round an optimizer bug when
270 // using Watcom C++ 10a in WIN32 compilation mode.
271 // If these lines not present, the type seems to be
272 // interpreted wrongly as an integer.
273 // I don't want to turn optimization off since it's needed
274 // for reading in files quickly.
275 #if defined(__WATCOMC__)
283 return new wxExpr(value
.integer
);
285 return new wxExpr(value
.real
);
287 return new wxExpr(wxExprString
, wxString(value
.string
));
289 return new wxExpr(wxExprWord
, wxString(value
.word
));
292 wxExpr
*expr
= value
.first
;
293 wxExpr
*new_list
= new wxExpr(wxExprList
);
296 wxExpr
*expr2
= expr
->Copy();
297 new_list
->Append(expr2
);
309 // Get the wxExpr (containing (= wxExpr Value) form) for the given word
310 // or string, assuming that we have Attribute=Value, ...
311 wxExpr
*wxExpr::GetAttributeValueNode(const wxString
& word
) const // Use only for a clause or list
313 if (type
!= wxExprList
)
316 wxExpr
*expr
= value
.first
;
319 if (expr
->type
== wxExprList
)
321 wxExpr
*firstNode
= expr
->value
.first
;
322 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
324 wxExpr
*secondNode
= firstNode
->next
;
325 if ((secondNode
->type
== wxExprWord
) &&
326 (wxStrcmp((const wxChar
*)word
, secondNode
->value
.word
) == 0))
337 // Get the value (in wxExpr form) for the given word or string, assuming
338 // that we have Attribute=Value, ...
339 wxExpr
*wxExpr::AttributeValue(const wxString
& word
) const // Use only for a clause or list
341 if (type
!= wxExprList
)
344 wxExpr
*attExpr
= GetAttributeValueNode(word
);
345 if (attExpr
&& attExpr
->value
.first
&& attExpr
->value
.first
->next
)
346 return attExpr
->value
.first
->next
->next
;
350 wxString
wxExpr::Functor(void) const // Use only for a clause
352 if ((type
!= wxExprList
) || !value
.first
)
353 return wxString(wxT(""));
355 if (value
.first
->type
== wxExprWord
)
356 return wxString(value
.first
->value
.word
);
358 return wxString(wxT(""));
361 bool wxExpr::IsFunctor(const wxString
& f
) const // Use only for a clause
363 if ((type
!= wxExprList
) || !value
.first
)
366 return (value
.first
->type
== wxExprWord
&&
367 (wxStrcmp((const wxChar
*)f
, value
.first
->value
.word
) == 0));
370 // Return nth argument of a clause (starting from 1)
371 wxExpr
*wxExpr::Arg(wxExprType theType
, int arg
) const
373 wxExpr
*expr
= value
.first
;
375 for (i
= 1; i
< arg
; i
++)
379 if (expr
&& (expr
->type
== theType
))
385 // Return nth argument of a list expression (starting from zero)
386 wxExpr
*wxExpr::Nth(int arg
) const
388 if (type
!= wxExprList
)
391 wxExpr
*expr
= value
.first
;
393 for (i
= 0; i
< arg
; i
++)
404 // Returns the number of elements in a list expression
405 int wxExpr::Number(void) const
407 if (type
!= wxExprList
)
411 wxExpr
*expr
= value
.first
;
420 void wxExpr::DeleteAttributeValue(const wxString
& attribute
)
422 if (type
!= wxExprList
)
425 wxExpr
*expr
= value
.first
;
426 wxExpr
*lastExpr
= this;
429 if (expr
->type
== wxExprList
)
431 wxExpr
*firstNode
= expr
->value
.first
;
432 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
434 wxExpr
*secondNode
= firstNode
->next
;
435 if ((secondNode
->type
== wxExprWord
) &&
436 (wxStrcmp((const wxChar
*)attribute
, secondNode
->value
.word
) == 0))
438 wxExpr
*nextExpr
= expr
->next
;
441 lastExpr
->next
= nextExpr
;
456 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxExpr
*val
)
458 if (type
!= wxExprList
)
460 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
463 // Warning - existing code may assume that any existing value
464 // is deleted first. For efficiency, we leave this to the application.
465 // DeleteAttributeValue(attribute);
467 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
468 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
470 wxExpr
*listExpr
= new wxExpr(wxExprList
);
472 listExpr
->Append(pequals
);
473 listExpr
->Append(patt
);
474 listExpr
->Append(val
);
479 void wxExpr::AddAttributeValue(const wxString
& attribute
, long val
)
481 if (type
!= wxExprList
)
483 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
486 // Warning - existing code may assume that any existing value
487 // is deleted first. For efficiency, we leave this to the application.
488 // DeleteAttributeValue(attribute);
490 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
491 wxExpr
*pval
= new wxExpr(val
);
492 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
494 wxExpr
*listExpr
= new wxExpr(wxExprList
);
496 listExpr
->Append(pequals
);
497 listExpr
->Append(patt
);
498 listExpr
->Append(pval
);
503 void wxExpr::AddAttributeValue(const wxString
& attribute
, double val
)
505 if (type
!= wxExprList
)
507 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
511 // DeleteAttributeValue(attribute);
512 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
513 wxExpr
*pval
= new wxExpr(val
);
514 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
516 wxExpr
*listExpr
= new wxExpr(wxExprList
);
518 listExpr
->Append(pequals
);
519 listExpr
->Append(patt
);
520 listExpr
->Append(pval
);
525 void wxExpr::AddAttributeValueString(const wxString
& attribute
, const wxString
& val
)
527 if (type
!= wxExprList
)
529 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
533 // DeleteAttributeValue(attribute);
535 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
536 wxExpr
*pval
= new wxExpr(wxExprString
, val
);
537 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
539 wxExpr
*listExpr
= new wxExpr(wxExprList
);
541 listExpr
->Append(pequals
);
542 listExpr
->Append(patt
);
543 listExpr
->Append(pval
);
548 void wxExpr::AddAttributeValueWord(const wxString
& attribute
, const wxString
& val
)
550 if (type
!= wxExprList
)
552 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
556 // DeleteAttributeValue(attribute);
558 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
559 wxExpr
*pval
= new wxExpr(wxExprWord
, val
);
560 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
562 wxExpr
*listExpr
= new wxExpr(wxExprList
);
564 listExpr
->Append(pequals
);
565 listExpr
->Append(patt
);
566 listExpr
->Append(pval
);
571 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxList
*val
)
573 if (type
!= wxExprList
)
575 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
581 // DeleteAttributeValue(attribute);
583 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
584 wxExpr
*pval
= new wxExpr(val
);
585 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
587 wxExpr
*listExpr
= new wxExpr(wxExprList
);
589 listExpr
->Append(pequals
);
590 listExpr
->Append(patt
);
591 listExpr
->Append(pval
);
596 void wxExpr::AddAttributeValueStringList(const wxString
& attribute
, wxList
*string_list
)
598 if (type
!= wxExprList
)
600 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
606 // DeleteAttributeValue(attribute);
608 // First make a list of wxExpr strings
609 wxExpr
*listExpr
= new wxExpr(wxExprList
);
610 wxNode
*node
= string_list
->GetFirst();
613 wxChar
*string
= (wxChar
*)node
->GetData();
614 wxExpr
*expr
= new wxExpr(wxExprString
, wxString(string
));
615 listExpr
->Append(expr
);
616 node
= node
->GetNext();
619 // Now make an (=, Att, Value) triple
620 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
621 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
623 wxExpr
*listExpr2
= new wxExpr(wxExprList
);
625 listExpr2
->Append(pequals
);
626 listExpr2
->Append(patt
);
627 listExpr2
->Append(listExpr
);
632 bool wxExpr::GetAttributeValue(const wxString
& att
, int& var
) const
634 wxExpr
*expr
= AttributeValue(att
);
636 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
638 var
= (int)(expr
->IntegerValue());
645 bool wxExpr::GetAttributeValue(const wxString
& att
, long& var
) const
647 wxExpr
*expr
= AttributeValue(att
);
649 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
651 var
= expr
->IntegerValue();
658 bool wxExpr::GetAttributeValue(const wxString
& att
, float& var
) const
660 wxExpr
*expr
= AttributeValue(att
);
661 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
663 var
= (float) expr
->RealValue();
670 bool wxExpr::GetAttributeValue(const wxString
& att
, double& var
) const
672 wxExpr
*expr
= AttributeValue(att
);
673 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
675 var
= expr
->RealValue();
682 bool wxExpr::GetAttributeValue(const wxString
& att
, wxString
& var
) const // Word OR string -> string
684 wxExpr
*expr
= AttributeValue(att
);
685 if (expr
&& expr
->Type() == wxExprWord
)
687 var
= expr
->WordValue();
690 else if (expr
&& expr
->Type() == wxExprString
)
692 var
= expr
->StringValue();
699 bool wxExpr::GetAttributeValue(const wxString
& att
, wxExpr
**var
) const
701 wxExpr
*expr
= AttributeValue(att
);
711 bool wxExpr::GetAttributeValueStringList(const wxString
& att
, wxList
*var
) const
713 wxExpr
*expr
= AttributeValue(att
);
714 if (expr
&& expr
->Type() == wxExprList
)
716 wxExpr
*string_expr
= expr
->value
.first
;
719 if (string_expr
->Type() == wxExprString
)
720 var
->Append((wxObject
*)copystring(string_expr
->StringValue()));
722 string_expr
= string_expr
->next
;
731 void wxExpr::AssignAttributeValue(wxChar
*att
, wxChar
**var
) const
734 if (GetAttributeValue(att
, str
))
738 *var
= copystring((const wxChar
*) str
);
742 void wxExpr::WriteClause(FILE* stream
) // Write this expression as a top-level clause
744 if (type
!= wxExprList
)
747 wxExpr
*node
= value
.first
;
750 node
->WriteExpr(stream
);
751 fprintf( stream
, "(" );
757 fprintf( stream
, " " );
758 node
->WriteExpr(stream
);
761 fprintf( stream
, ",\n" );
764 fprintf( stream
, ").\n\n" );
768 void wxExpr::WriteExpr(FILE* stream
) // Write as any other subexpression
770 // This seems to get round an optimizer bug when
771 // using Watcom C++ 10a in WIN32 compilation mode.
772 // If these lines not present, the type seems to be
773 // interpreted wrongly as an integer.
774 // I don't want to turn optimization off since it's needed
775 // for reading in files quickly.
776 #if defined(__WATCOMC__)
785 fprintf( stream
, "%ld", value
.integer
);
790 double f
= value
.real
;
791 fprintf( stream
, "%.6g", f
);
796 fprintf( stream
, "\"" );
798 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.string
);
799 size_t len
= strlen(val
);
800 for (i
= 0; i
< len
; i
++)
803 if (ch
== '"' || ch
== '\\')
804 fprintf( stream
, "\\" );
808 fprintf( stream
, tmp
);
810 fprintf( stream
, "\"" );
815 bool quote_it
= FALSE
;
816 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.word
);
817 size_t len
= strlen(val
);
818 if ((len
== 0) || (len
> 0 && (val
[(size_t) 0] > 64 && val
[(size_t) 0] < 91)))
823 for (i
= 0; i
< len
; i
++)
824 if ((!isalpha(val
[i
])) && (!isdigit(val
[i
])) &&
826 { quote_it
= TRUE
; i
= len
; }
830 fprintf( stream
,"'" );
832 fprintf( stream
, val
);
835 fprintf( stream
, "'" );
842 fprintf( stream
, "[]" );
845 wxExpr
*expr
= value
.first
;
847 if ((expr
->Type() == wxExprWord
) && (wxStrcmp(expr
->WordValue(), wxT("=")) == 0))
849 wxExpr
*arg1
= expr
->next
;
850 wxExpr
*arg2
= arg1
->next
;
851 arg1
->WriteExpr(stream
);
852 fprintf( stream
, " = " );
853 arg2
->WriteExpr(stream
);
857 fprintf( stream
, "[" );
860 expr
->WriteExpr(stream
);
863 fprintf( stream
, ", " );
865 fprintf( stream
, "]" );
870 case wxExprNull
: break;
875 * wxExpr 'database' (list of expressions)
878 IMPLEMENT_DYNAMIC_CLASS(wxExprDatabase
, wxList
)
880 wxExprDatabase::wxExprDatabase(wxExprErrorHandler handler
)
884 currentwxExprErrorHandler
= handler
;
888 wxExprDatabase::wxExprDatabase(wxExprType type
, const wxString
& attribute
, int size
,
889 wxExprErrorHandler handler
)
892 attribute_to_hash
= attribute
;
893 if (type
== wxExprString
)
894 hash_table
= new wxHashTable(wxKEY_STRING
, size
);
895 else if (type
== wxExprInteger
)
896 hash_table
= new wxHashTable(wxKEY_INTEGER
, size
);
897 else hash_table
= NULL
;
899 currentwxExprErrorHandler
= handler
;
903 wxExprDatabase::~wxExprDatabase(void)
910 void wxExprDatabase::BeginFind(void) // Initialise a search
912 position
= GetFirst();
915 wxExpr
*wxExprDatabase::FindClause(long id
) // Find a term based on an integer id attribute
916 // e.g. node(id=23, type=rectangle, ....).
918 wxExpr
*found
= NULL
;
919 while (position
&& !found
)
921 wxExpr
*term
= (wxExpr
*)position
->GetData();
923 if (term
->Type() == wxExprList
)
925 wxExpr
*value
= term
->AttributeValue(wxT("id"));
926 if (value
->Type() == wxExprInteger
&& value
->IntegerValue() == id
)
929 position
= position
->GetNext();
934 // Find on basis of attribute/value pairs, e.g. type=rectangle
935 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, const wxString
& val
)
937 wxExpr
*found
= NULL
;
938 while (position
&& !found
)
940 wxExpr
*term
= (wxExpr
*)position
->GetData();
942 if (term
->Type() == wxExprList
)
944 wxExpr
*value
= term
->AttributeValue(word
);
945 if ((value
->Type() == wxExprWord
&& value
->WordValue() == val
) ||
946 (value
->Type() == wxExprString
&& value
->StringValue() == val
))
949 position
= position
->GetNext();
954 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, long val
)
956 wxExpr
*found
= NULL
;
957 while (position
&& !found
)
959 wxExpr
*term
= (wxExpr
*)position
->GetData();
961 if (term
->Type() == wxExprList
)
963 wxExpr
*value
= term
->AttributeValue(word
);
964 if ((value
->Type() == wxExprInteger
) && (value
->IntegerValue() == val
))
967 position
= position
->GetNext();
972 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, double val
)
974 wxExpr
*found
= NULL
;
975 while (position
&& !found
)
977 wxExpr
*term
= (wxExpr
*)position
->GetData();
979 if (term
->Type() == wxExprList
)
981 wxExpr
*value
= term
->AttributeValue(word
);
982 if ((value
->Type() == wxExprReal
) && (value
->RealValue() == val
))
985 position
= position
->GetNext();
990 wxExpr
*wxExprDatabase::FindClauseByFunctor(const wxString
& functor
)
992 wxExpr
*found
= NULL
;
993 while (position
&& !found
)
995 wxExpr
*term
= (wxExpr
*)position
->GetData();
997 if (term
->Type() == wxExprList
)
999 if (term
->Functor() == functor
)
1002 position
= position
->GetNext();
1007 // If hashing is on, must store in hash table too
1008 void wxExprDatabase::Append(wxExpr
*clause
)
1010 wxList::Append((wxObject
*)clause
);
1013 wxString
functor(clause
->Functor());
1014 wxExpr
*expr
= clause
->AttributeValue(attribute_to_hash
);
1017 long functor_key
= hash_table
->MakeKey(WXSTRINGCAST functor
);
1019 if (expr
&& expr
->Type() == wxExprString
)
1021 value_key
= hash_table
->MakeKey(WXSTRINGCAST expr
->StringValue());
1022 hash_table
->Put(functor_key
+ value_key
, WXSTRINGCAST expr
->StringValue(), (wxObject
*)clause
);
1024 else if (expr
&& expr
->Type() == wxExprInteger
)
1026 value_key
= expr
->IntegerValue();
1027 hash_table
->Put(functor_key
+ value_key
, expr
->IntegerValue(), (wxObject
*)clause
);
1034 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, long value
) const
1036 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + value
;
1038 // The key alone isn't guaranteed to be unique:
1039 // must supply value too. Let's assume the value of the
1040 // id is going to be reasonably unique.
1041 return (wxExpr
*)hash_table
->Get(key
, value
);
1044 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, const wxString
& value
) const
1046 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + hash_table
->MakeKey(WXSTRINGCAST value
);
1047 return (wxExpr
*)hash_table
->Get(key
, WXSTRINGCAST value
);
1050 void wxExprDatabase::ClearDatabase(void)
1053 wxNode
*node
= GetFirst();
1056 wxExpr
*expr
= (wxExpr
*)node
->GetData();
1063 hash_table
->Clear();
1066 bool wxExprDatabase::Read(const wxString
& filename
)
1070 FILE *f
= wxFopen(filename
, _T("r"));
1073 thewxExprDatabase
= this;
1080 return (noErrors
== 0);
1088 bool wxExprDatabase::ReadFromString(const wxString
& buffer
)
1091 thewxExprDatabase
= this;
1093 const wxWX2MBbuf buf
= buffer
.mb_str();
1094 LexFromString(wxMBSTRINGCAST buf
);
1097 return (noErrors
== 0);
1100 bool wxExprDatabase::Write(const wxString
& fileName
)
1102 FILE *stream
= wxFopen( fileName
, _T("w+"));
1107 bool success
= Write(stream
);
1112 bool wxExprDatabase::Write(FILE *stream
)
1115 wxNode
*node
= GetFirst();
1118 wxExpr
*expr
= (wxExpr
*)node
->GetData();
1119 expr
->WriteClause(stream
);
1120 node
= node
->GetNext();
1122 return (noErrors
== 0);
1125 void add_expr(wxExpr
* expr
)
1127 thewxExprDatabase
->Append(expr
);
1131 bool wxExprIsFunctor(wxExpr
*expr
, const wxString
& functor
)
1133 if (expr
&& (expr
->Type() == wxExprList
))
1135 wxExpr
*first_expr
= expr
->value
.first
;
1137 if (first_expr
&& (first_expr
->Type() == wxExprWord
) &&
1138 (first_expr
->WordValue() == functor
))
1148 * Called from parser
1152 char *wxmake_integer(char *str
)
1154 wxExpr
*x
= new wxExpr(atol(str
));
1159 char *wxmake_real(char *str1
, char *str2
)
1163 sprintf(buf
, "%s.%s", str1
, str2
);
1164 double f
= (double)atof(buf
);
1165 wxExpr
*x
= new wxExpr(f
);
1170 // extern "C" double exp10(double);
1172 char *wxmake_exp(char *str1
, char *str2
)
1174 double mantissa
= (double)atoi(str1
);
1175 double exponent
= (double)atoi(str2
);
1177 double d
= mantissa
* pow(10.0, exponent
);
1179 wxExpr
*x
= new wxExpr(d
);
1184 char *wxmake_exp2(char *str1
, char *str2
, char *str3
)
1188 sprintf(buf
, "%s.%s", str1
, str2
);
1189 double mantissa
= (double)atof(buf
);
1190 double exponent
= (double)atoi(str3
);
1192 double d
= mantissa
* pow(10.0, exponent
);
1194 wxExpr
*x
= new wxExpr(d
);
1199 char *wxmake_word(char *str
)
1201 wxExpr
*x
= new wxExpr(wxExprWord
, wxString(str
, wxConvLibc
).c_str());
1205 char *wxmake_string(char *str
)
1209 const wxMB2WXbuf sbuf
= wxConvLibc
.cMB2WX(str
);
1211 // str++; /* skip leading quote */
1212 len
= wxStrlen(sbuf
) - 1; /* ignore trailing quote */
1214 s
= new wxChar
[len
+ 1];
1217 for(i
=1; i
<len
; i
++) // 1 since we want to skip leading quote
1219 if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('"'))
1224 else if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('\\'))
1235 wxExpr
*x
= new wxExpr(wxExprString
, s
, FALSE
);
1239 char *proio_cons(char * ccar
, char * ccdr
)
1241 wxExpr
*car
= (wxExpr
*)ccar
;
1242 wxExpr
*cdr
= (wxExpr
*)ccdr
;
1246 cdr
= new wxExpr(wxExprList
);
1253 void process_command(char * cexpr
)
1255 wxExpr
*expr
= (wxExpr
*)cexpr
;
1259 void syntax_error(char *WXUNUSED(s
))
1261 if (currentwxExprErrorHandler
)
1262 (void)(*(currentwxExprErrorHandler
))(WXEXPR_ERROR_SYNTAX
, (char *)"syntax error");
1263 if (thewxExprDatabase
) thewxExprDatabase
->noErrors
+= 1;
1268 // char *__cdecl strdup(const char *s)
1269 WXDLLEXPORT
char *strdup(const char *s
)
1271 int len
= strlen(s
);
1272 char *new_s
= (char *)malloc(sizeof(char)*(len
+1));