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 static inline wxChar
* copystring(const wxChar
* s
)
36 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
38 extern "C" void add_expr(char *);
39 extern "C" void LexFromFile(FILE *fd
);
40 extern "C" void LexFromString(char *buf
);
44 /* Rename all YACC/LEX stuff or we'll conflict with other
48 #define yyback PROIO_yyback
49 #define yylook PROIO_yylook
50 #define yywrap PROIO_yywrap
51 #define yyoutput PROIO_yyoutput
52 #define yylex PROIO_yylex
53 #define yyerror PROIO_yyerror
54 #define yyleng PROIO_yyleng
55 #define yytext PROIO_yytext
56 #define yymorfg PROIO_yymorfg
57 #define yylineno PROIO_yylineno
58 #define yytchar PROIO_yytchar
59 #define yyin PROIO_yyin
60 #define yyout PROIO_yyout
61 #define yysvf PROIO_yysvf
62 #define yyestate PROIO_yyestate
63 #define yysvec PROIO_yysvec
64 #define yybgin PROIO_yybgin
65 #define yyprevious PROIO_yyprevious
66 #define yylhs PROIO_yylhs
67 #define yylen PROIO_yylen
68 #define yydefred PROIO_yydefred
69 #define yydgoto PROIO_yydgoto
70 #define yysindex PROIO_yysindex
71 #define yyrindex PROIO_yyrindex
72 #define yygindex PROIO_yygindex
73 #define yytable PROIO_yytable
74 #define yycheck PROIO_yycheck
75 #define yyname PROIO_yyname
76 #define yyrule PROIO_yyrule
77 #define yydebug PROIO_yydebug
78 #define yynerrs PROIO_yynerrs
79 #define yyerrflag PROIO_yyerrflag
80 #define yychar PROIO_yychar
81 #define yyvsp PROIO_yyvsp
82 #define yyssp PROIO_yyssp
83 #define yyval PROIO_yyval
84 #define yylval PROIO_yylval
85 #define yyss PROIO_yyss
86 #define yyvs PROIO_yyvs
87 #define yyparse PROIO_yyparse
89 /* +++steve162e: more defines necessary */
90 #define yy_init_buffer PROIO_yy_init_buffer
91 #define yy_create_buffer PROIO_yy_create_buffer
92 #define yy_load_buffer_state PROIO_yy_load_buffer_state
93 #define yyrestart PROIO_yyrestart
94 #define yy_switch_to_buffer PROIO_yy_switch_to_buffer
95 #define yy_delete_buffer PROIO_yy_delete_buffer
98 /* WG 1/96: still more for flex 2.5 */
99 #define yy_scan_buffer PROIO_scan_buffer
100 #define yy_scan_string PROIO_scan_string
101 #define yy_scan_bytes PROIO_scan_bytes
102 #define yy_flex_debug PROIO_flex_debug
103 #define yy_flush_buffer PROIO_flush_buffer
104 #if !defined(__VISAGECPP__)
105 /* multiply defined??? */
106 #define yyleng PROIO_yyleng
107 #define yytext PROIO_yytext
110 extern "C" WXDLLIMPEXP_DATA_DEPRECATED(FILE*) yyin
;
111 extern "C" WXDLLIMPEXP_DEPRECATED
int yyparse(void);
114 wxExprDatabase
*thewxExprDatabase
= NULL
;
115 wxExprErrorHandler currentwxExprErrorHandler
;
117 wxExpr::wxExpr(const wxString
& functor
)
124 wxExpr
*pfunctor
= new wxExpr(wxExprWord
, functor
);
129 wxExpr::wxExpr(wxExprType the_type
, const wxString
& word_or_string
)
136 value
.word
= copystring((const wxChar
*)word_or_string
);
139 value
.string
= copystring((const wxChar
*)word_or_string
);
154 wxExpr::wxExpr(wxExprType the_type
, wxChar
*word_or_string
, bool allocate
)
161 value
.word
= allocate
? copystring(word_or_string
) : word_or_string
;
164 value
.string
= allocate
? copystring(word_or_string
) : word_or_string
;
179 wxExpr::wxExpr(long the_integer
)
181 type
= wxExprInteger
;
182 value
.integer
= the_integer
;
187 wxExpr::wxExpr(double the_real
)
190 value
.real
= the_real
;
195 wxExpr::wxExpr(wxList
*the_list
)
202 wxExpr
*listExpr
= new wxExpr(wxExprList
);
204 wxNode
*node
= the_list
->GetFirst();
207 wxExpr
*expr
= (wxExpr
*)node
->GetData();
208 listExpr
->Append(expr
);
209 node
= node
->GetNext();
216 wxExpr::~wxExpr(void)
227 delete[] value
.string
;
237 wxExpr
*expr
= value
.first
;
240 wxExpr
*expr1
= expr
->next
;
247 case wxExprNull
: break;
251 void wxExpr::Append(wxExpr
*expr
)
261 void wxExpr::Insert(wxExpr
*expr
)
263 expr
->next
= value
.first
;
270 wxExpr
*wxExpr::Copy(void) const
272 // This seems to get round an optimizer bug when
273 // using Watcom C++ 10a in WIN32 compilation mode.
274 // If these lines not present, the type seems to be
275 // interpreted wrongly as an integer.
276 // I don't want to turn optimization off since it's needed
277 // for reading in files quickly.
278 #if defined(__WATCOMC__)
286 return new wxExpr(value
.integer
);
288 return new wxExpr(value
.real
);
290 return new wxExpr(wxExprString
, wxString(value
.string
));
292 return new wxExpr(wxExprWord
, wxString(value
.word
));
295 wxExpr
*expr
= value
.first
;
296 wxExpr
*new_list
= new wxExpr(wxExprList
);
299 wxExpr
*expr2
= expr
->Copy();
300 new_list
->Append(expr2
);
312 // Get the wxExpr (containing (= wxExpr Value) form) for the given word
313 // or string, assuming that we have Attribute=Value, ...
314 wxExpr
*wxExpr::GetAttributeValueNode(const wxString
& word
) const // Use only for a clause or list
316 if (type
!= wxExprList
)
319 wxExpr
*expr
= value
.first
;
322 if (expr
->type
== wxExprList
)
324 wxExpr
*firstNode
= expr
->value
.first
;
325 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
327 wxExpr
*secondNode
= firstNode
->next
;
328 if ((secondNode
->type
== wxExprWord
) &&
329 (wxStrcmp((const wxChar
*)word
, secondNode
->value
.word
) == 0))
340 // Get the value (in wxExpr form) for the given word or string, assuming
341 // that we have Attribute=Value, ...
342 wxExpr
*wxExpr::AttributeValue(const wxString
& word
) const // Use only for a clause or list
344 if (type
!= wxExprList
)
347 wxExpr
*attExpr
= GetAttributeValueNode(word
);
348 if (attExpr
&& attExpr
->value
.first
&& attExpr
->value
.first
->next
)
349 return attExpr
->value
.first
->next
->next
;
353 wxString
wxExpr::Functor(void) const // Use only for a clause
355 if ((type
!= wxExprList
) || !value
.first
)
356 return wxString(wxT(""));
358 if (value
.first
->type
== wxExprWord
)
359 return wxString(value
.first
->value
.word
);
361 return wxString(wxT(""));
364 bool wxExpr::IsFunctor(const wxString
& f
) const // Use only for a clause
366 if ((type
!= wxExprList
) || !value
.first
)
369 return (value
.first
->type
== wxExprWord
&&
370 (wxStrcmp((const wxChar
*)f
, value
.first
->value
.word
) == 0));
373 // Return nth argument of a clause (starting from 1)
374 wxExpr
*wxExpr::Arg(wxExprType theType
, int arg
) const
376 wxExpr
*expr
= value
.first
;
378 for (i
= 1; i
< arg
; i
++)
382 if (expr
&& (expr
->type
== theType
))
388 // Return nth argument of a list expression (starting from zero)
389 wxExpr
*wxExpr::Nth(int arg
) const
391 if (type
!= wxExprList
)
394 wxExpr
*expr
= value
.first
;
396 for (i
= 0; i
< arg
; i
++)
407 // Returns the number of elements in a list expression
408 int wxExpr::Number(void) const
410 if (type
!= wxExprList
)
414 wxExpr
*expr
= value
.first
;
423 void wxExpr::DeleteAttributeValue(const wxString
& attribute
)
425 if (type
!= wxExprList
)
428 wxExpr
*expr
= value
.first
;
429 wxExpr
*lastExpr
= this;
432 if (expr
->type
== wxExprList
)
434 wxExpr
*firstNode
= expr
->value
.first
;
435 if ((firstNode
->type
== wxExprWord
) && (firstNode
->value
.word
[0] == '='))
437 wxExpr
*secondNode
= firstNode
->next
;
438 if ((secondNode
->type
== wxExprWord
) &&
439 (wxStrcmp((const wxChar
*)attribute
, secondNode
->value
.word
) == 0))
441 wxExpr
*nextExpr
= expr
->next
;
444 lastExpr
->next
= nextExpr
;
459 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxExpr
*val
)
461 if (type
!= wxExprList
)
463 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
466 // Warning - existing code may assume that any existing value
467 // is deleted first. For efficiency, we leave this to the application.
468 // DeleteAttributeValue(attribute);
470 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
471 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
473 wxExpr
*listExpr
= new wxExpr(wxExprList
);
475 listExpr
->Append(pequals
);
476 listExpr
->Append(patt
);
477 listExpr
->Append(val
);
482 void wxExpr::AddAttributeValue(const wxString
& attribute
, long val
)
484 if (type
!= wxExprList
)
486 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
489 // Warning - existing code may assume that any existing value
490 // is deleted first. For efficiency, we leave this to the application.
491 // DeleteAttributeValue(attribute);
493 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
494 wxExpr
*pval
= new wxExpr(val
);
495 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
497 wxExpr
*listExpr
= new wxExpr(wxExprList
);
499 listExpr
->Append(pequals
);
500 listExpr
->Append(patt
);
501 listExpr
->Append(pval
);
506 void wxExpr::AddAttributeValue(const wxString
& attribute
, double val
)
508 if (type
!= wxExprList
)
510 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
514 // DeleteAttributeValue(attribute);
515 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
516 wxExpr
*pval
= new wxExpr(val
);
517 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
519 wxExpr
*listExpr
= new wxExpr(wxExprList
);
521 listExpr
->Append(pequals
);
522 listExpr
->Append(patt
);
523 listExpr
->Append(pval
);
528 void wxExpr::AddAttributeValueString(const wxString
& attribute
, const wxString
& val
)
530 if (type
!= wxExprList
)
532 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
536 // DeleteAttributeValue(attribute);
538 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
539 wxExpr
*pval
= new wxExpr(wxExprString
, val
);
540 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
542 wxExpr
*listExpr
= new wxExpr(wxExprList
);
544 listExpr
->Append(pequals
);
545 listExpr
->Append(patt
);
546 listExpr
->Append(pval
);
551 void wxExpr::AddAttributeValueWord(const wxString
& attribute
, const wxString
& val
)
553 if (type
!= wxExprList
)
555 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
559 // DeleteAttributeValue(attribute);
561 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
562 wxExpr
*pval
= new wxExpr(wxExprWord
, val
);
563 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
565 wxExpr
*listExpr
= new wxExpr(wxExprList
);
567 listExpr
->Append(pequals
);
568 listExpr
->Append(patt
);
569 listExpr
->Append(pval
);
574 void wxExpr::AddAttributeValue(const wxString
& attribute
, wxList
*val
)
576 if (type
!= wxExprList
)
578 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
584 // DeleteAttributeValue(attribute);
586 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
587 wxExpr
*pval
= new wxExpr(val
);
588 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
590 wxExpr
*listExpr
= new wxExpr(wxExprList
);
592 listExpr
->Append(pequals
);
593 listExpr
->Append(patt
);
594 listExpr
->Append(pval
);
599 void wxExpr::AddAttributeValueStringList(const wxString
& attribute
, wxList
*string_list
)
601 if (type
!= wxExprList
)
603 // cout << "Error! tried to add an attribute-value pair to a nonlist wxExpr expression\n";
609 // DeleteAttributeValue(attribute);
611 // First make a list of wxExpr strings
612 wxExpr
*listExpr
= new wxExpr(wxExprList
);
613 wxNode
*node
= string_list
->GetFirst();
616 wxChar
*string
= (wxChar
*)node
->GetData();
617 wxExpr
*expr
= new wxExpr(wxExprString
, wxString(string
));
618 listExpr
->Append(expr
);
619 node
= node
->GetNext();
622 // Now make an (=, Att, Value) triple
623 wxExpr
*patt
= new wxExpr(wxExprWord
, attribute
);
624 wxExpr
*pequals
= new wxExpr(wxExprWord
, wxT("="));
626 wxExpr
*listExpr2
= new wxExpr(wxExprList
);
628 listExpr2
->Append(pequals
);
629 listExpr2
->Append(patt
);
630 listExpr2
->Append(listExpr
);
635 bool wxExpr::GetAttributeValue(const wxString
& att
, int& var
) const
637 wxExpr
*expr
= AttributeValue(att
);
639 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
641 var
= (int)(expr
->IntegerValue());
648 bool wxExpr::GetAttributeValue(const wxString
& att
, long& var
) const
650 wxExpr
*expr
= AttributeValue(att
);
652 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
654 var
= expr
->IntegerValue();
661 bool wxExpr::GetAttributeValue(const wxString
& att
, float& var
) const
663 wxExpr
*expr
= AttributeValue(att
);
664 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
666 var
= (float) expr
->RealValue();
673 bool wxExpr::GetAttributeValue(const wxString
& att
, double& var
) const
675 wxExpr
*expr
= AttributeValue(att
);
676 if (expr
&& (expr
->Type() == wxExprInteger
|| expr
->Type() == wxExprReal
))
678 var
= expr
->RealValue();
685 bool wxExpr::GetAttributeValue(const wxString
& att
, wxString
& var
) const // Word OR string -> string
687 wxExpr
*expr
= AttributeValue(att
);
688 if (expr
&& expr
->Type() == wxExprWord
)
690 var
= expr
->WordValue();
693 else if (expr
&& expr
->Type() == wxExprString
)
695 var
= expr
->StringValue();
702 bool wxExpr::GetAttributeValue(const wxString
& att
, wxExpr
**var
) const
704 wxExpr
*expr
= AttributeValue(att
);
714 bool wxExpr::GetAttributeValueStringList(const wxString
& att
, wxList
*var
) const
716 wxExpr
*expr
= AttributeValue(att
);
717 if (expr
&& expr
->Type() == wxExprList
)
719 wxExpr
*string_expr
= expr
->value
.first
;
722 if (string_expr
->Type() == wxExprString
)
723 var
->Append((wxObject
*)copystring(string_expr
->StringValue()));
725 string_expr
= string_expr
->next
;
734 void wxExpr::AssignAttributeValue(wxChar
*att
, wxChar
**var
) const
737 if (GetAttributeValue(att
, str
))
741 *var
= copystring((const wxChar
*) str
);
745 void wxExpr::WriteClause(FILE* stream
) // Write this expression as a top-level clause
747 if (type
!= wxExprList
)
750 wxExpr
*node
= value
.first
;
753 node
->WriteExpr(stream
);
754 fprintf( stream
, "(" );
760 fprintf( stream
, " " );
761 node
->WriteExpr(stream
);
764 fprintf( stream
, ",\n" );
767 fprintf( stream
, ").\n\n" );
771 void wxExpr::WriteExpr(FILE* stream
) // Write as any other subexpression
773 // This seems to get round an optimizer bug when
774 // using Watcom C++ 10a in WIN32 compilation mode.
775 // If these lines not present, the type seems to be
776 // interpreted wrongly as an integer.
777 // I don't want to turn optimization off since it's needed
778 // for reading in files quickly.
779 #if defined(__WATCOMC__)
788 fprintf( stream
, "%ld", value
.integer
);
793 double f
= value
.real
;
794 fprintf( stream
, "%.6g", f
);
799 fprintf( stream
, "\"" );
801 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.string
);
802 size_t len
= strlen(val
);
803 for (i
= 0; i
< len
; i
++)
806 if (ch
== '"' || ch
== '\\')
807 fprintf( stream
, "\\" );
811 fprintf( stream
, tmp
);
813 fprintf( stream
, "\"" );
818 bool quote_it
= FALSE
;
819 const wxWX2MBbuf val
= wxConvLibc
.cWX2MB(value
.word
);
820 size_t len
= strlen(val
);
821 if ((len
== 0) || (len
> 0 && (val
[(size_t) 0] > 64 && val
[(size_t) 0] < 91)))
826 for (i
= 0; i
< len
; i
++)
827 if ((!isalpha(val
[i
])) && (!isdigit(val
[i
])) &&
829 { quote_it
= TRUE
; i
= len
; }
833 fprintf( stream
,"'" );
835 fprintf( stream
, val
);
838 fprintf( stream
, "'" );
845 fprintf( stream
, "[]" );
848 wxExpr
*expr
= value
.first
;
850 if ((expr
->Type() == wxExprWord
) && (wxStrcmp(expr
->WordValue(), wxT("=")) == 0))
852 wxExpr
*arg1
= expr
->next
;
853 wxExpr
*arg2
= arg1
->next
;
854 arg1
->WriteExpr(stream
);
855 fprintf( stream
, " = " );
856 arg2
->WriteExpr(stream
);
860 fprintf( stream
, "[" );
863 expr
->WriteExpr(stream
);
866 fprintf( stream
, ", " );
868 fprintf( stream
, "]" );
873 case wxExprNull
: break;
878 * wxExpr 'database' (list of expressions)
881 IMPLEMENT_DYNAMIC_CLASS(wxExprDatabase
, wxList
)
883 wxExprDatabase::wxExprDatabase(wxExprErrorHandler handler
)
887 currentwxExprErrorHandler
= handler
;
891 wxExprDatabase::wxExprDatabase(wxExprType type
, const wxString
& attribute
, int size
,
892 wxExprErrorHandler handler
)
895 attribute_to_hash
= attribute
;
896 if (type
== wxExprString
)
897 hash_table
= new wxHashTable(wxKEY_STRING
, size
);
898 else if (type
== wxExprInteger
)
899 hash_table
= new wxHashTable(wxKEY_INTEGER
, size
);
900 else hash_table
= NULL
;
902 currentwxExprErrorHandler
= handler
;
906 wxExprDatabase::~wxExprDatabase(void)
913 void wxExprDatabase::BeginFind(void) // Initialise a search
915 position
= GetFirst();
918 wxExpr
*wxExprDatabase::FindClause(long id
) // Find a term based on an integer id attribute
919 // e.g. node(id=23, type=rectangle, ....).
921 wxExpr
*found
= NULL
;
922 while (position
&& !found
)
924 wxExpr
*term
= (wxExpr
*)position
->GetData();
926 if (term
->Type() == wxExprList
)
928 wxExpr
*value
= term
->AttributeValue(wxT("id"));
929 if (value
->Type() == wxExprInteger
&& value
->IntegerValue() == id
)
932 position
= position
->GetNext();
937 // Find on basis of attribute/value pairs, e.g. type=rectangle
938 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, const wxString
& val
)
940 wxExpr
*found
= NULL
;
941 while (position
&& !found
)
943 wxExpr
*term
= (wxExpr
*)position
->GetData();
945 if (term
->Type() == wxExprList
)
947 wxExpr
*value
= term
->AttributeValue(word
);
948 if ((value
->Type() == wxExprWord
&& value
->WordValue() == val
) ||
949 (value
->Type() == wxExprString
&& value
->StringValue() == val
))
952 position
= position
->GetNext();
957 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, long val
)
959 wxExpr
*found
= NULL
;
960 while (position
&& !found
)
962 wxExpr
*term
= (wxExpr
*)position
->GetData();
964 if (term
->Type() == wxExprList
)
966 wxExpr
*value
= term
->AttributeValue(word
);
967 if ((value
->Type() == wxExprInteger
) && (value
->IntegerValue() == val
))
970 position
= position
->GetNext();
975 wxExpr
*wxExprDatabase::FindClause(const wxString
& word
, double val
)
977 wxExpr
*found
= NULL
;
978 while (position
&& !found
)
980 wxExpr
*term
= (wxExpr
*)position
->GetData();
982 if (term
->Type() == wxExprList
)
984 wxExpr
*value
= term
->AttributeValue(word
);
985 if ((value
->Type() == wxExprReal
) && (value
->RealValue() == val
))
988 position
= position
->GetNext();
993 wxExpr
*wxExprDatabase::FindClauseByFunctor(const wxString
& functor
)
995 wxExpr
*found
= NULL
;
996 while (position
&& !found
)
998 wxExpr
*term
= (wxExpr
*)position
->GetData();
1000 if (term
->Type() == wxExprList
)
1002 if (term
->Functor() == functor
)
1005 position
= position
->GetNext();
1010 // If hashing is on, must store in hash table too
1011 void wxExprDatabase::Append(wxExpr
*clause
)
1013 wxList::Append((wxObject
*)clause
);
1016 wxString
functor(clause
->Functor());
1017 wxExpr
*expr
= clause
->AttributeValue(attribute_to_hash
);
1020 long functor_key
= hash_table
->MakeKey(WXSTRINGCAST functor
);
1022 if (expr
&& expr
->Type() == wxExprString
)
1024 value_key
= hash_table
->MakeKey(WXSTRINGCAST expr
->StringValue());
1025 hash_table
->Put(functor_key
+ value_key
, WXSTRINGCAST expr
->StringValue(), (wxObject
*)clause
);
1027 else if (expr
&& expr
->Type() == wxExprInteger
)
1029 value_key
= expr
->IntegerValue();
1030 hash_table
->Put(functor_key
+ value_key
, expr
->IntegerValue(), (wxObject
*)clause
);
1037 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, long value
) const
1039 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + value
;
1041 // The key alone isn't guaranteed to be unique:
1042 // must supply value too. Let's assume the value of the
1043 // id is going to be reasonably unique.
1044 return (wxExpr
*)hash_table
->Get(key
, value
);
1047 wxExpr
*wxExprDatabase::HashFind(const wxString
& functor
, const wxString
& value
) const
1049 long key
= hash_table
->MakeKey(WXSTRINGCAST functor
) + hash_table
->MakeKey(WXSTRINGCAST value
);
1050 return (wxExpr
*)hash_table
->Get(key
, WXSTRINGCAST value
);
1053 void wxExprDatabase::ClearDatabase(void)
1056 wxNode
*node
= GetFirst();
1059 wxExpr
*expr
= (wxExpr
*)node
->GetData();
1066 hash_table
->Clear();
1069 bool wxExprDatabase::Read(const wxString
& filename
)
1073 FILE *f
= wxFopen(filename
, _T("r"));
1076 thewxExprDatabase
= this;
1083 return (noErrors
== 0);
1091 bool wxExprDatabase::ReadFromString(const wxString
& buffer
)
1094 thewxExprDatabase
= this;
1096 const wxWX2MBbuf buf
= buffer
.mb_str();
1097 LexFromString(wxMBSTRINGCAST buf
);
1100 return (noErrors
== 0);
1103 bool wxExprDatabase::Write(const wxString
& fileName
)
1105 FILE *stream
= wxFopen( fileName
, _T("w+"));
1110 bool success
= Write(stream
);
1115 bool wxExprDatabase::Write(FILE *stream
)
1118 wxNode
*node
= GetFirst();
1121 wxExpr
*expr
= (wxExpr
*)node
->GetData();
1122 expr
->WriteClause(stream
);
1123 node
= node
->GetNext();
1125 return (noErrors
== 0);
1128 void add_expr(wxExpr
* expr
)
1130 thewxExprDatabase
->Append(expr
);
1134 bool wxExprIsFunctor(wxExpr
*expr
, const wxString
& functor
)
1136 if (expr
&& (expr
->Type() == wxExprList
))
1138 wxExpr
*first_expr
= expr
->value
.first
;
1140 if (first_expr
&& (first_expr
->Type() == wxExprWord
) &&
1141 (first_expr
->WordValue() == functor
))
1151 * Called from parser
1155 char *wxmake_integer(char *str
)
1157 wxExpr
*x
= new wxExpr(atol(str
));
1162 char *wxmake_real(char *str1
, char *str2
)
1166 sprintf(buf
, "%s.%s", str1
, str2
);
1167 double f
= (double)atof(buf
);
1168 wxExpr
*x
= new wxExpr(f
);
1173 // extern "C" double exp10(double);
1175 char *wxmake_exp(char *str1
, char *str2
)
1177 double mantissa
= (double)atoi(str1
);
1178 double exponent
= (double)atoi(str2
);
1180 double d
= mantissa
* pow(10.0, exponent
);
1182 wxExpr
*x
= new wxExpr(d
);
1187 char *wxmake_exp2(char *str1
, char *str2
, char *str3
)
1191 sprintf(buf
, "%s.%s", str1
, str2
);
1192 double mantissa
= (double)atof(buf
);
1193 double exponent
= (double)atoi(str3
);
1195 double d
= mantissa
* pow(10.0, exponent
);
1197 wxExpr
*x
= new wxExpr(d
);
1202 char *wxmake_word(char *str
)
1204 wxExpr
*x
= new wxExpr(wxExprWord
, wxString(str
, wxConvLibc
).c_str());
1208 char *wxmake_string(char *str
)
1212 const wxMB2WXbuf sbuf
= wxConvLibc
.cMB2WX(str
);
1214 // str++; /* skip leading quote */
1215 len
= wxStrlen(sbuf
) - 1; /* ignore trailing quote */
1217 s
= new wxChar
[len
+ 1];
1220 for(i
=1; i
<len
; i
++) // 1 since we want to skip leading quote
1222 if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('"'))
1227 else if (sbuf
[i
] == wxT('\\') && sbuf
[i
+1] == wxT('\\'))
1238 wxExpr
*x
= new wxExpr(wxExprString
, s
, FALSE
);
1242 char *proio_cons(char * ccar
, char * ccdr
)
1244 wxExpr
*car
= (wxExpr
*)ccar
;
1245 wxExpr
*cdr
= (wxExpr
*)ccdr
;
1249 cdr
= new wxExpr(wxExprList
);
1256 void process_command(char * cexpr
)
1258 wxExpr
*expr
= (wxExpr
*)cexpr
;
1262 void syntax_error(char *WXUNUSED(s
))
1264 if (currentwxExprErrorHandler
)
1265 (void)(*(currentwxExprErrorHandler
))(WXEXPR_ERROR_SYNTAX
, (char *)"syntax error");
1266 if (thewxExprDatabase
) thewxExprDatabase
->noErrors
+= 1;
1271 // char *__cdecl strdup(const char *s)
1272 WXDLLEXPORT
char *strdup(const char *s
)
1274 int len
= strlen(s
);
1275 char *new_s
= (char *)malloc(sizeof(char)*(len
+1));