X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f780dc864273916c30a3986a0de0c3801db0aacf..fd69e87d2586726e57446d7208ceb8854bc74166:/src/common/wxexpr.cpp diff --git a/src/common/wxexpr.cpp b/src/common/wxexpr.cpp index 33fcade86c..9db80bb028 100644 --- a/src/common/wxexpr.cpp +++ b/src/common/wxexpr.cpp @@ -6,7 +6,7 @@ // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart and Markus Holzem -// Licence: wxWindows license +// Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ @@ -20,21 +20,11 @@ #pragma hdrstop #endif -#if wxUSE_IOSTREAMH -#include -#else -#include -# ifdef _MSC_VER - using namespace std; -# endif -#endif - #include #include #include #include "wx/utils.h" - #include "wx/expr.h" #include "wx/wxexpr.h" @@ -42,10 +32,6 @@ extern "C" void add_expr(char *); extern "C" void LexFromFile(FILE *fd); extern "C" void LexFromString(char *buf); -#ifndef USE_SHARED_LIBRARY -IMPLEMENT_DYNAMIC_CLASS(wxExprDatabase, wxList) -#endif - wxExprDatabase *thewxExprDatabase = NULL; wxExprErrorHandler currentwxExprErrorHandler; @@ -69,10 +55,10 @@ wxExpr::wxExpr(wxExprType the_type, const wxString& word_or_string) switch (the_type) { case wxExprWord: - value.word = copystring((const char *)word_or_string); + value.word = copystring((const wxChar *)word_or_string); break; case wxExprString: - value.string = copystring((const char *)word_or_string); + value.string = copystring((const wxChar *)word_or_string); break; case wxExprList: last = NULL; @@ -87,7 +73,7 @@ wxExpr::wxExpr(wxExprType the_type, const wxString& word_or_string) next = NULL; } -wxExpr::wxExpr(wxExprType the_type, char *word_or_string, bool allocate) +wxExpr::wxExpr(wxExprType the_type, wxChar *word_or_string, bool allocate) { type = the_type; @@ -262,7 +248,7 @@ wxExpr *wxExpr::GetAttributeValueNode(const wxString& word) const // Use only fo { wxExpr *secondNode = firstNode->next; if ((secondNode->type == wxExprWord) && - (strcmp((const char *)word, secondNode->value.word) == 0)) + (wxStrcmp((const wxChar *)word, secondNode->value.word) == 0)) { return expr; } @@ -289,12 +275,12 @@ wxExpr *wxExpr::AttributeValue(const wxString& word) const // Use only for a cla wxString wxExpr::Functor(void) const // Use only for a clause { if ((type != wxExprList) || !value.first) - return wxString(""); + return wxString(wxT("")); if (value.first->type == wxExprWord) return wxString(value.first->value.word); else - return wxString(""); + return wxString(wxT("")); } bool wxExpr::IsFunctor(const wxString& f) const // Use only for a clause @@ -303,7 +289,7 @@ bool wxExpr::IsFunctor(const wxString& f) const // Use only for a clause return FALSE; return (value.first->type == wxExprWord && - (strcmp((const char *)f, value.first->value.word) == 0)); + (wxStrcmp((const wxChar *)f, value.first->value.word) == 0)); } // Return nth argument of a clause (starting from 1) @@ -372,7 +358,7 @@ void wxExpr::DeleteAttributeValue(const wxString& attribute) { wxExpr *secondNode = firstNode->next; if ((secondNode->type == wxExprWord) && - (strcmp((const char *)attribute, secondNode->value.word) == 0)) + (wxStrcmp((const wxChar *)attribute, secondNode->value.word) == 0)) { wxExpr *nextExpr = expr->next; delete expr; @@ -404,7 +390,7 @@ void wxExpr::AddAttributeValue(const wxString& attribute, wxExpr *val) // DeleteAttributeValue(attribute); wxExpr *patt = new wxExpr(wxExprWord, attribute); - wxExpr *pequals = new wxExpr(wxExprWord, "="); + wxExpr *pequals = new wxExpr(wxExprWord, wxT("=")); wxExpr *listExpr = new wxExpr(wxExprList); @@ -428,7 +414,7 @@ void wxExpr::AddAttributeValue(const wxString& attribute, long val) wxExpr *patt = new wxExpr(wxExprWord, attribute); wxExpr *pval = new wxExpr(val); - wxExpr *pequals = new wxExpr(wxExprWord, "="); + wxExpr *pequals = new wxExpr(wxExprWord, wxT("=")); wxExpr *listExpr = new wxExpr(wxExprList); @@ -450,7 +436,7 @@ void wxExpr::AddAttributeValue(const wxString& attribute, double val) // DeleteAttributeValue(attribute); wxExpr *patt = new wxExpr(wxExprWord, attribute); wxExpr *pval = new wxExpr(val); - wxExpr *pequals = new wxExpr(wxExprWord, "="); + wxExpr *pequals = new wxExpr(wxExprWord, wxT("=")); wxExpr *listExpr = new wxExpr(wxExprList); @@ -473,7 +459,7 @@ void wxExpr::AddAttributeValueString(const wxString& attribute, const wxString& wxExpr *patt = new wxExpr(wxExprWord, attribute); wxExpr *pval = new wxExpr(wxExprString, val); - wxExpr *pequals = new wxExpr(wxExprWord, "="); + wxExpr *pequals = new wxExpr(wxExprWord, wxT("=")); wxExpr *listExpr = new wxExpr(wxExprList); @@ -496,7 +482,7 @@ void wxExpr::AddAttributeValueWord(const wxString& attribute, const wxString& va wxExpr *patt = new wxExpr(wxExprWord, attribute); wxExpr *pval = new wxExpr(wxExprWord, val); - wxExpr *pequals = new wxExpr(wxExprWord, "="); + wxExpr *pequals = new wxExpr(wxExprWord, wxT("=")); wxExpr *listExpr = new wxExpr(wxExprList); @@ -521,7 +507,7 @@ void wxExpr::AddAttributeValue(const wxString& attribute, wxList *val) wxExpr *patt = new wxExpr(wxExprWord, attribute); wxExpr *pval = new wxExpr(val); - wxExpr *pequals = new wxExpr(wxExprWord, "="); + wxExpr *pequals = new wxExpr(wxExprWord, wxT("=")); wxExpr *listExpr = new wxExpr(wxExprList); @@ -557,7 +543,7 @@ void wxExpr::AddAttributeValueStringList(const wxString& attribute, wxList *stri // Now make an (=, Att, Value) triple wxExpr *patt = new wxExpr(wxExprWord, attribute); - wxExpr *pequals = new wxExpr(wxExprWord, "="); + wxExpr *pequals = new wxExpr(wxExprWord, wxT("=")); wxExpr *listExpr2 = new wxExpr(wxExprList); @@ -667,18 +653,18 @@ bool wxExpr::GetAttributeValueStringList(const wxString& att, wxList *var) const } // Compatibility -void wxExpr::AssignAttributeValue(char *att, char **var) const +void wxExpr::AssignAttributeValue(wxChar *att, wxChar **var) const { wxString str; if (GetAttributeValue(att, str)) { if (*var) delete[] *var; - *var = copystring((const char *) str); + *var = copystring((const wxChar *) str); } } -void wxExpr::WriteClause(ostream& stream) // Write this expression as a top-level clause +void wxExpr::WriteClause(FILE* stream) // Write this expression as a top-level clause { if (type != wxExprList) return; @@ -687,23 +673,24 @@ void wxExpr::WriteClause(ostream& stream) // Write this expression as a top-lev if (node) { node->WriteExpr(stream); - stream << "("; + fprintf( stream, "(" ); node = node->next; bool first = TRUE; while (node) { if (!first) - stream << " "; + fprintf( stream, " " ); node->WriteExpr(stream); node = node->next; - if (node) stream << ",\n"; + if (node) + fprintf( stream, ",\n" ); first = FALSE; } - stream << ").\n\n"; + fprintf( stream, ").\n\n" ); } } -void wxExpr::WriteExpr(ostream& stream) // Write as any other subexpression +void wxExpr::WriteExpr(FILE* stream) // Write as any other subexpression { // This seems to get round an optimizer bug when // using Watcom C++ 10a in WIN32 compilation mode. @@ -720,89 +707,87 @@ void wxExpr::WriteExpr(ostream& stream) // Write as any other subexpression { case wxExprInteger: { - stream << value.integer; + fprintf( stream, "%ld", value.integer ); break; } case wxExprReal: { double f = value.real; -/* Now the parser can cope with this. - // Prevent printing in 'e' notation. Any better way? - if (fabs(f) < 0.00001) - f = 0.0; -*/ - char buf[40]; - sprintf(buf, "%.6g", f); - stream << buf; + fprintf( stream, "%.6g", f); break; } case wxExprString: { - stream << "\""; + fprintf( stream, "\"" ); int i; - int len = strlen(value.string); + const wxWX2MBbuf val = wxConvLibc.cWX2MB(value.string); + int len = strlen(val); for (i = 0; i < len; i++) { - char ch = value.string[i]; + char ch = val[i]; if (ch == '"' || ch == '\\') - stream << "\\"; - stream << ch; + fprintf( stream, "\\" ); + char tmp[2]; + tmp[0] = ch; + tmp[1] = 0; + fprintf( stream, tmp ); } - - stream << "\""; + fprintf( stream, "\"" ); break; } case wxExprWord: { bool quote_it = FALSE; - int len = strlen(value.word); - if ((len == 0) || (len > 0 && (value.word[0] > 64 && value.word[0] < 91))) + const wxWX2MBbuf val = wxConvLibc.cWX2MB(value.word); + int len = strlen(val); + if ((len == 0) || (len > 0 && (val[0] > 64 && val[0] < 91))) quote_it = TRUE; else { int i; for (i = 0; i < len; i++) - if ((!isalpha(value.word[i])) && (!isdigit(value.word[i])) && - (value.word[i] != '_')) + if ((!isalpha(val[i])) && (!isdigit(val[i])) && + (val[i] != '_')) { quote_it = TRUE; i = len; } } if (quote_it) - stream << "'"; + fprintf( stream ,"'" ); - stream << value.word; + fprintf( stream, (const char*) val ); if (quote_it) - stream << "'"; + fprintf( stream, "'" ); break; } case wxExprList: { if (!value.first) - stream << "[]"; + fprintf( stream, "[]" ); else { wxExpr *expr = value.first; - if ((expr->Type() == wxExprWord) && (strcmp(expr->WordValue(), "=") == 0)) + if ((expr->Type() == wxExprWord) && (wxStrcmp(expr->WordValue(), wxT("=")) == 0)) { wxExpr *arg1 = expr->next; wxExpr *arg2 = arg1->next; arg1->WriteExpr(stream); - stream << " = "; + fprintf( stream, " = " ); arg2->WriteExpr(stream); } else { - stream << "["; + fprintf( stream, "[" ); while (expr) { expr->WriteExpr(stream); expr = expr->next; - if (expr) stream << ", "; + if (expr) + fprintf( stream, ", " ); } - stream << "]"; + fprintf( stream, "]" ); } } break; @@ -811,49 +796,6 @@ void wxExpr::WriteExpr(ostream& stream) // Write as any other subexpression } } -void wxExpr::WriteLispExpr(ostream& stream) -{ - switch (type) - { - case wxExprInteger: - { - stream << value.integer; - break; - } - case wxExprReal: - { - stream << value.real; - break; - } - case wxExprString: - { - stream << "\"" << value.string << "\""; - break; - } - case wxExprWord: - { - stream << value.word; - break; - } - case wxExprList: - { - wxExpr *expr = value.first; - - stream << "("; - while (expr) - { - expr->WriteLispExpr(stream); - expr = expr->next; - if (expr) stream << " "; - } - - stream << ")"; - break; - } - case wxExprNull: break; - } -} - /* * wxExpr 'database' (list of expressions) */ @@ -999,12 +941,12 @@ void wxExprDatabase::Append(wxExpr *clause) wxExpr *expr = clause->AttributeValue(attribute_to_hash); if (expr) { - long functor_key = hash_table->MakeKey((char *)(const char *)functor); + long functor_key = hash_table->MakeKey(WXSTRINGCAST functor); long value_key = 0; if (expr && expr->Type() == wxExprString) { - value_key = hash_table->MakeKey((char *)(const char *)expr->StringValue()); - hash_table->Put(functor_key + value_key, (char *)(const char *)expr->StringValue(), (wxObject *)clause); + value_key = hash_table->MakeKey(WXSTRINGCAST expr->StringValue()); + hash_table->Put(functor_key + value_key, WXSTRINGCAST expr->StringValue(), (wxObject *)clause); } else if (expr && expr->Type() == wxExprInteger) { @@ -1018,7 +960,7 @@ void wxExprDatabase::Append(wxExpr *clause) wxExpr *wxExprDatabase::HashFind(const wxString& functor, long value) const { - long key = hash_table->MakeKey((char *)(const char *)functor) + value; + long key = hash_table->MakeKey(WXSTRINGCAST functor) + value; // The key alone isn't guaranteed to be unique: // must supply value too. Let's assume the value of the @@ -1028,8 +970,8 @@ wxExpr *wxExprDatabase::HashFind(const wxString& functor, long value) const wxExpr *wxExprDatabase::HashFind(const wxString& functor, const wxString& value) const { - long key = hash_table->MakeKey((char *)(const char *)functor) + hash_table->MakeKey((char *)(const char *)value); - return (wxExpr *)hash_table->Get(key, (char *)(const char *)value); + long key = hash_table->MakeKey(WXSTRINGCAST functor) + hash_table->MakeKey(WXSTRINGCAST value); + return (wxExpr *)hash_table->Get(key, WXSTRINGCAST value); } void wxExprDatabase::ClearDatabase(void) @@ -1052,7 +994,7 @@ bool wxExprDatabase::Read(const wxString& filename) { noErrors = 0; - FILE *f = fopen((const char *)filename, "r"); + FILE *f = fopen(filename.fn_str(), "r"); if (f) { thewxExprDatabase = this; @@ -1075,7 +1017,8 @@ bool wxExprDatabase::ReadFromString(const wxString& buffer) noErrors = 0; thewxExprDatabase = this; - LexFromString((char *)(const char *)buffer); + const wxWX2MBbuf buf = buffer.mb_str(); + LexFromString(wxMBSTRINGCAST buf); yyparse(); wxExprCleanUp(); return (noErrors == 0); @@ -1083,13 +1026,17 @@ bool wxExprDatabase::ReadFromString(const wxString& buffer) bool wxExprDatabase::Write(const wxString& fileName) { - ofstream str((char *)(const char *)fileName); - if (str.bad()) + FILE *stream = fopen( fileName.fn_str(), "w+" ); + + if (!stream) return FALSE; - return Write(str); + + bool success = Write(stream); + fclose(stream); + return success; } -bool wxExprDatabase::Write(ostream& stream) +bool wxExprDatabase::Write(FILE *stream) { noErrors = 0; wxNode *node = First(); @@ -1102,19 +1049,6 @@ bool wxExprDatabase::Write(ostream& stream) return (noErrors == 0); } -void wxExprDatabase::WriteLisp(ostream& stream) -{ - noErrors = 0; - wxNode *node = First(); - while (node) - { - wxExpr *expr = (wxExpr *)node->Data(); - expr->WriteLispExpr(stream); - stream << "\n\n"; - node = node->Next(); - } -} - void add_expr(wxExpr * expr) { thewxExprDatabase->Append(expr); @@ -1142,14 +1076,14 @@ bool wxExprIsFunctor(wxExpr *expr, const wxString& functor) * */ -char *make_integer(char *str) +char *wxmake_integer(char *str) { wxExpr *x = new wxExpr(atol(str)); return (char *)x; } -char *make_real(char *str1, char *str2) +char *wxmake_real(char *str1, char *str2) { char buf[50]; @@ -1162,7 +1096,7 @@ char *make_real(char *str1, char *str2) // extern "C" double exp10(double); -char *make_exp(char *str1, char *str2) +char *wxmake_exp(char *str1, char *str2) { double mantissa = (double)atoi(str1); double exponent = (double)atoi(str2); @@ -1174,7 +1108,7 @@ char *make_exp(char *str1, char *str2) return (char *)x; } -char *make_exp2(char *str1, char *str2, char *str3) +char *wxmake_exp2(char *str1, char *str2, char *str3) { char buf[50]; @@ -1189,40 +1123,41 @@ char *make_exp2(char *str1, char *str2, char *str3) return (char *)x; } -char *make_word(char *str) +char *wxmake_word(char *str) { wxExpr *x = new wxExpr(wxExprWord, str); return (char *)x; } -char *make_string(char *str) +char *wxmake_string(char *str) { - char *s, *t; + wxChar *s, *t; int len, i; + const wxMB2WXbuf sbuf = wxConvLibc.cMB2WX(str); - str++; /* skip leading quote */ - len = strlen(str) - 1; /* ignore trailing quote */ +// str++; /* skip leading quote */ + len = wxStrlen(sbuf) - 1; /* ignore trailing quote */ - s = new char[len + 1]; + s = new wxChar[len + 1]; t = s; - for(i=0; i