📄 wxexpr.cpp
字号:
else return false;}bool wxExpr::GetAttributeValue(const wxString& att, long& var) const{ wxExpr *expr = AttributeValue(att); if (expr && (expr->Type() == wxExprInteger || expr->Type() == wxExprReal)) { var = expr->IntegerValue(); return true; } else return false;}bool wxExpr::GetAttributeValue(const wxString& att, float& var) const{ wxExpr *expr = AttributeValue(att); if (expr && (expr->Type() == wxExprInteger || expr->Type() == wxExprReal)) { var = (float) expr->RealValue(); return true; } else return false;}bool wxExpr::GetAttributeValue(const wxString& att, double& var) const{ wxExpr *expr = AttributeValue(att); if (expr && (expr->Type() == wxExprInteger || expr->Type() == wxExprReal)) { var = expr->RealValue(); return true; } else return false;}bool wxExpr::GetAttributeValue(const wxString& att, wxString& var) const // Word OR string -> string{ wxExpr *expr = AttributeValue(att); if (expr && expr->Type() == wxExprWord) { var = expr->WordValue(); return true; } else if (expr && expr->Type() == wxExprString) { var = expr->StringValue(); return true; } else return false;}bool wxExpr::GetAttributeValue(const wxString& att, wxExpr **var) const{ wxExpr *expr = AttributeValue(att); if (expr) { *var = expr; return true; } else return false;}bool wxExpr::GetAttributeValueStringList(const wxString& att, wxList *var) const{ wxExpr *expr = AttributeValue(att); if (expr && expr->Type() == wxExprList) { wxExpr *string_expr = expr->value.first; while (string_expr) { if (string_expr->Type() == wxExprString) var->Append((wxObject *)copystring(string_expr->StringValue())); string_expr = string_expr->next; } return true; } else return false;}// Compatibilityvoid wxExpr::AssignAttributeValue(wxChar *att, wxChar **var) const{ wxString str; if (GetAttributeValue(att, str)) { if (*var) delete[] *var; *var = copystring((const wxChar *) str); }}void wxExpr::WriteClause(FILE* stream) // Write this expression as a top-level clause{ if (type != wxExprList) return; wxExpr *node = value.first; if (node) { node->WriteExpr(stream); fprintf( stream, "(" ); node = node->next; bool first = true; while (node) { if (!first) fprintf( stream, " " ); node->WriteExpr(stream); node = node->next; if (node) fprintf( stream, ",\n" ); first = false; } fprintf( stream, ").\n\n" ); }}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. // If these lines not present, the type seems to be // interpreted wrongly as an integer. // I don't want to turn optimization off since it's needed // for reading in files quickly.#if defined(__WATCOMC__) char buf[2]; sprintf(buf, "");#endif switch (type) { case wxExprInteger: { fprintf( stream, "%ld", value.integer ); break; } case wxExprReal: { double f = value.real; fprintf( stream, "%.6g", f); break; } case wxExprString: { fprintf( stream, "\"" ); size_t i; const wxWX2MBbuf val = wxConvLibc.cWX2MB(value.string); size_t len = strlen(val); for (i = 0; i < len; i++) { char ch = val[i]; if (ch == '"' || ch == '\\') fprintf( stream, "\\" ); char tmp[2]; tmp[0] = ch; tmp[1] = 0; fprintf( stream, tmp ); } fprintf( stream, "\"" ); break; } case wxExprWord: { bool quote_it = false; const wxWX2MBbuf val = wxConvLibc.cWX2MB(value.word); size_t len = strlen(val); if ((len == 0) || (len > 0 && (val[(size_t) 0] > 64 && val[(size_t) 0] < 91))) quote_it = true; else { size_t i; for (i = 0; i < len; i++) if ((!isalpha(val[i])) && (!isdigit(val[i])) && (val[i] != '_')) { quote_it = true; i = len; } } if (quote_it) fprintf( stream ,"'" ); fprintf( stream, val ); if (quote_it) fprintf( stream, "'" ); break; } case wxExprList: { if (!value.first) fprintf( stream, "[]" ); else { wxExpr *expr = value.first; if ((expr->Type() == wxExprWord) && (wxStrcmp(expr->WordValue(), wxT("=")) == 0)) { wxExpr *arg1 = expr->next; wxExpr *arg2 = arg1->next; arg1->WriteExpr(stream); fprintf( stream, " = " ); arg2->WriteExpr(stream); } else { fprintf( stream, "[" ); while (expr) { expr->WriteExpr(stream); expr = expr->next; if (expr) fprintf( stream, ", " ); } fprintf( stream, "]" ); } } break; } case wxExprNull: break; }}/* * wxExpr 'database' (list of expressions) */IMPLEMENT_DYNAMIC_CLASS(wxExprDatabase, wxList)wxExprDatabase::wxExprDatabase(wxExprErrorHandler handler){ position = NULL; hash_table = NULL; currentwxExprErrorHandler = handler; noErrors = 0;}wxExprDatabase::wxExprDatabase(wxExprType type, const wxString& attribute, int size, wxExprErrorHandler handler){ position = NULL; attribute_to_hash = attribute; if (type == wxExprString) hash_table = new wxHashTable(wxKEY_STRING, size); else if (type == wxExprInteger) hash_table = new wxHashTable(wxKEY_INTEGER, size); else hash_table = NULL; currentwxExprErrorHandler = handler; noErrors = 0;}wxExprDatabase::~wxExprDatabase(void){ ClearDatabase(); if (hash_table) delete hash_table;}void wxExprDatabase::BeginFind(void) // Initialise a search{ position = GetFirst();}wxExpr *wxExprDatabase::FindClause(long id) // Find a term based on an integer id attribute // e.g. node(id=23, type=rectangle, ....).{ wxExpr *found = NULL; while (position && !found) { wxExpr *term = (wxExpr *)position->GetData(); if (term->Type() == wxExprList) { wxExpr *value = term->AttributeValue(wxT("id")); if (value->Type() == wxExprInteger && value->IntegerValue() == id) found = term; } position = position->GetNext(); } return found;}// Find on basis of attribute/value pairs, e.g. type=rectanglewxExpr *wxExprDatabase::FindClause(const wxString& word, const wxString& val){ wxExpr *found = NULL; while (position && !found) { wxExpr *term = (wxExpr *)position->GetData(); if (term->Type() == wxExprList) { wxExpr *value = term->AttributeValue(word); if ((value->Type() == wxExprWord && value->WordValue() == val) || (value->Type() == wxExprString && value->StringValue() == val)) found = term; } position = position->GetNext(); } return found;}wxExpr *wxExprDatabase::FindClause(const wxString& word, long val){ wxExpr *found = NULL; while (position && !found) { wxExpr *term = (wxExpr *)position->GetData(); if (term->Type() == wxExprList) { wxExpr *value = term->AttributeValue(word); if ((value->Type() == wxExprInteger) && (value->IntegerValue() == val)) found = term; } position = position->GetNext(); } return found;}wxExpr *wxExprDatabase::FindClause(const wxString& word, double val){ wxExpr *found = NULL; while (position && !found) { wxExpr *term = (wxExpr *)position->GetData(); if (term->Type() == wxExprList) { wxExpr *value = term->AttributeValue(word); if ((value->Type() == wxExprReal) && (value->RealValue() == val)) found = term; } position = position->GetNext(); } return found;}wxExpr *wxExprDatabase::FindClauseByFunctor(const wxString& functor){ wxExpr *found = NULL; while (position && !found) { wxExpr *term = (wxExpr *)position->GetData(); if (term->Type() == wxExprList) { if (term->Functor() == functor) found = term; } position = position->GetNext(); } return found;}// If hashing is on, must store in hash table toovoid wxExprDatabase::Append(wxExpr *clause){ wxList::Append((wxObject *)clause); if (hash_table) { wxString functor(clause->Functor()); wxExpr *expr = clause->AttributeValue(attribute_to_hash); if (expr) { long functor_key = hash_table->MakeKey(WXSTRINGCAST functor); long value_key; if (expr && expr->Type() == wxExprString) { 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) { value_key = expr->IntegerValue(); hash_table->Put(functor_key + value_key, expr->IntegerValue(), (wxObject *)clause); } } }}wxExpr *wxExprDatabase::HashFind(const wxString& functor, long value) const{ 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 // id is going to be reasonably unique. return (wxExpr *)hash_table->Get(key, value);}wxExpr *wxExprDatabase::HashFind(const wxString& functor, const wxString& value) const{ long key = hash_table->MakeKey(WXSTRINGCAST functor) + hash_table->MakeKey(WXSTRINGCAST value); return (wxExpr *)hash_table->Get(key, WXSTRINGCAST value);}void wxExprDatabase::ClearDatabase(void){ noErrors = 0; wxNode *node = GetFirst(); while (node) { wxExpr *expr = (wxExpr *)node->GetData(); delete expr; delete node; node = GetFirst(); } if (hash_table) hash_table->Clear();}bool wxExprDatabase::Read(const wxString& filename){ noErrors = 0; FILE *f = wxFopen(filename, _T("r")); if (f) { thewxExprDatabase = this; LexFromFile(f); yyparse(); fclose(f); wxExprCleanUp(); return (noErrors == 0); } else { return false; }}bool wxExprDatabase::ReadFromString(const wxString& buffer){ noErrors = 0; thewxExprDatabase = this; const wxWX2MBbuf buf = buffer.mb_str(); LexFromString(wxMBSTRINGCAST buf); yyparse(); wxExprCleanUp(); return (noErrors == 0);}bool wxExprDatabase::Write(const wxString& fileName){ FILE *stream = wxFopen( fileName, _T("w+")); if (!stream) return false; bool success = Write(stream); fclose(stream); return success;}bool wxExprDatabase::Write(FILE *stream){ noErrors = 0; wxNode *node = GetFirst(); while (node) { wxExpr *expr = (wxExpr *)node->GetData(); expr->WriteClause(stream); node = node->GetNext(); } return (noErrors == 0);}void add_expr(wxExpr * expr){ thewxExprDatabase->Append(expr);}// Checks functorbool wxExprIsFunctor(wxExpr *expr, const wxString& functor){ if (expr && (expr->Type() == wxExprList)) { wxExpr *first_expr = expr->value.first; if (first_expr && (first_expr->Type() == wxExprWord) && (first_expr->WordValue() == functor)) return true; else return false; } else return false;}/* * Called from parser * */char *wxmake_integer(char *str){ wxExpr *x = new wxExpr(atol(str)); return (char *)x;}char *wxmake_real(char *str1, char *str2){ char buf[50]; sprintf(buf, "%s.%s", str1, str2); double f = (double)atof(buf); wxExpr *x = new wxExpr(f); return (char *)x;}// extern "C" double exp10(double);char *wxmake_exp(char *str1, char *str2){ double mantissa = (double)atoi(str1); double exponent = (double)atoi(str2); double d = mantissa * pow(10.0, exponent); wxExpr *x = new wxExpr(d); return (char *)x;}char *wxmake_exp2(char *str1, char *str2, char *str3){ char buf[50]; sprintf(buf, "%s.%s", str1, str2); double mantissa = (double)atof(buf); double exponent = (double)atoi(str3); double d = mantissa * pow(10.0, exponent); wxExpr *x = new wxExpr(d); return (char *)x;}char *wxmake_word(char *str){ wxExpr *x = new wxExpr(wxExprWord, wxString(str, wxConvLibc).c_str()); return (char *)x;}char *wxmake_string(char *str){ wxChar *s, *t; size_t len, i; const wxMB2WXbuf sbuf = wxConvLibc.cMB2WX(str);// str++; /* skip leading quote */ len = wxStrlen(sbuf) - 1; /* ignore trailing quote */ s = new wxChar[len + 1]; t = s; for(i=1; i<len; i++) // 1 since we want to skip leading quote { if (sbuf[i] == wxT('\\') && sbuf[i+1] == wxT('"')) { *t++ = wxT('"'); i ++; } else if (sbuf[i] == wxT('\\') && sbuf[i+1] == wxT('\\')) { *t++ = wxT('\\'); i ++; } else *t++ = sbuf[i]; } *t = wxT('\0'); wxExpr *x = new wxExpr(wxExprString, s, false); return (char *)x;}char *proio_cons(char * ccar, char * ccdr){ wxExpr *car = (wxExpr *)ccar; wxExpr *cdr = (wxExpr *)ccdr; if (cdr == NULL) { cdr = new wxExpr(wxExprList); } if (car) cdr->Insert(car); return (char *)cdr;}void process_command(char * cexpr){ wxExpr *expr = (wxExpr *)cexpr; add_expr(expr);}void syntax_error(char *WXUNUSED(s)){ if (currentwxExprErrorHandler) (void)(*(currentwxExprErrorHandler))(WXEXPR_ERROR_SYNTAX, (char *)"syntax error"); if (thewxExprDatabase) thewxExprDatabase->noErrors += 1;}#if 0#ifdef _WINDLL// char *__cdecl strdup(const char *s)WXDLLEXPORT char *strdup(const char *s){ int len = strlen(s); char *new_s = (char *)malloc(sizeof(char)*(len+1)); strcpy(new_s, s); return new_s;}#endif#endif#endif // wxUSE_PROLOGIO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -