Changeset 1118 for code/branches/input/src/util
- Timestamp:
- Apr 20, 2008, 4:31:03 PM (17 years ago)
- Location:
- code/branches/input/src/util
- Files:
-
- 1 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/util/CMakeLists.txt
r1114 r1118 7 7 Clipboard.cc 8 8 SubString.cc 9 FloatParser.cc9 ExprParser.cc 10 10 MultiTypePrimitive.cc 11 11 MultiTypeString.cc -
code/branches/input/src/util/ExprParser.cc
r1114 r1118 1 //FloatParser.cpp 2 3 #include "FloatParser.h" 4 #include <string> 1 /* 2 * ORXONOX - the hottest 3D action shooter ever to exist 3 * > www.orxonox.net < 4 * 5 * 6 * License notice: 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21 * 22 * Author: 23 * Reto Grieder 24 * Co-authors: 25 * ... 26 * 27 */ 28 29 /** 30 @file 31 @brief Declaration of FloatParser 32 */ 33 34 #include "ExprParser.h" 5 35 #include <cmath> 6 36 #include <cstring> 7 37 8 using namespace std; 9 10 //Makros, um Funktionen einfacher parser zu können 38 // macros for easier if, else statements 11 39 #define CASE_1(var) if (!strcmp(SWITCH,var)) 12 40 #define CASE(var) else if (!strcmp(SWITCH,var)) 13 41 #define CASE_ELSE else 14 42 43 //! skip white spaces 15 44 #define PARSE_BLANKS while (*reading_stream == ' ') ++reading_stream; 16 45 17 //static enumerations and variables 18 static enum binary_operator { b_plus, b_minus, mal, durch, modulo, hoch, undef, oder, und, gleich, b_nicht, kleiner, groesser, ungleich, kleinergleich, groessergleich}; 19 static enum unary_operator { u_plus, u_minus, u_nicht }; 20 static binary_operator op; 21 static char* reading_stream; 22 static bool parse_float_failed = false; 23 24 //static funtions 25 static double parse_expr_1(); 26 static double parse_expr_2(); 27 static double parse_expr_3(); 28 static double parse_expr_4(); 29 static double parse_expr_5(); 30 static double parse_expr_6(); 31 static double parse_expr_7(); 32 static double parse_expr_8(); 33 static inline char* parse_word(char* str); 34 static inline binary_operator parse_binary_operator(); 35 static inline unary_operator parse_unary_operator(); 36 static double parse_argument(); 37 static double parse_last_argument(); 38 39 //Public functions: 40 /******************/ 41 bool parse_float(char* const string, char **endptr, double* result) 42 { 43 parse_float_failed = false; 44 reading_stream = string; 45 double value = parse_expr_8(); 46 if ( !parse_float_failed && ( 47 *reading_stream == ')' || \ 48 *reading_stream == '}' || \ 49 *reading_stream == ']' || \ 50 *reading_stream == ',' || \ 51 *reading_stream == ';' || \ 52 *reading_stream == '_' || \ 53 *reading_stream == '\0' || \ 54 *reading_stream > 64 && *reading_stream < 91 || \ 55 *reading_stream > 96 && *reading_stream < 123)) 56 { 57 endptr = &reading_stream; 58 *result = value; 59 return true; 60 } 61 else 62 { 63 *result = 0; 64 return false; 65 } 66 } 67 68 bool parse_float(char* const string, char** endptr, char delimiter, double* result) 69 { 70 parse_float_failed = false; 71 reading_stream = string; 72 double value = parse_expr_8(); 73 if (*reading_stream == delimiter && !parse_float_failed) 74 { 75 endptr = &reading_stream; 76 *result = value; 77 return true; 78 } 79 else 80 { 81 *result = 0; 82 return false; 83 } 84 } 85 86 bool parse_vector_float(char* const string, char** endptr, bool last_float, double* result) 87 { 88 parse_float_failed = false; 89 reading_stream = string; 90 double value = parse_expr_4(); 91 if (last_float) 92 { 93 if (*reading_stream == '>') 94 { 95 endptr = &reading_stream; 96 *result = value; 97 } 98 else 99 parse_float_failed = true; 100 } 101 else 102 { 103 if (*reading_stream == ',') 104 { 105 *endptr = reading_stream; 106 *result = value; 107 } 108 else 109 parse_float_failed = true; 110 } 111 if (parse_float_failed) 112 { 113 *result = 0; 114 return false; 115 } 116 else 117 return true; 46 ExprParser::ExprParser(std::string& str) 47 { 48 this->failed_ = false; 49 this->reading_stream = str.c_str(); 50 if (str.size() == 0 || *reading_stream == '\0') 51 { 52 this->failed_ = true; 53 this->result_ = 0.0; 54 } 55 else 56 { 57 this->result_ = parse_expr_8(); 58 this->remains_ = reading_stream; 59 } 118 60 } 119 61 120 62 //Private functions: 121 63 /******************/ 122 static doubleparse_argument()64 double ExprParser::parse_argument() 123 65 { 124 66 double value = parse_expr_8(); … … 130 72 else 131 73 { 132 parse_float_failed= true;74 this->failed_ = true; 133 75 return 0; 134 76 } 135 77 } 136 78 137 static doubleparse_last_argument()79 double ExprParser::parse_last_argument() 138 80 { 139 81 double value = parse_expr_8(); … … 145 87 else 146 88 { 147 parse_float_failed= true;89 this->failed_ = true; 148 90 return 0; 149 91 } 150 92 } 151 93 152 static doubleparse_expr_8()94 double ExprParser::parse_expr_8() 153 95 { 154 96 double value = parse_expr_7(); … … 166 108 167 109 168 static doubleparse_expr_7()110 double ExprParser::parse_expr_7() 169 111 { 170 112 double value = parse_expr_6(); … … 181 123 } 182 124 183 static doubleparse_expr_6()125 double ExprParser::parse_expr_6() 184 126 { 185 127 double value = parse_expr_5(); … … 200 142 } 201 143 202 static doubleparse_expr_5()144 double ExprParser::parse_expr_5() 203 145 { 204 146 double value = parse_expr_4(); … … 225 167 } 226 168 227 static doubleparse_expr_4()169 double ExprParser::parse_expr_4() 228 170 { 229 171 double value = parse_expr_3(); … … 244 186 } 245 187 246 static doubleparse_expr_3()188 double ExprParser::parse_expr_3() 247 189 { 248 190 double value = parse_expr_2(); … … 269 211 } 270 212 271 static doubleparse_expr_2()213 double ExprParser::parse_expr_2() 272 214 { 273 215 double value = parse_expr_1(); … … 288 230 } 289 231 290 static doubleparse_expr_1()232 double ExprParser::parse_expr_1() 291 233 { 292 234 PARSE_BLANKS … … 299 241 { 300 242 // end of string 301 parse_float_failed= true;243 this->failed_ = true; 302 244 return 0; 303 245 } 304 246 else if (*reading_stream > 47 && *reading_stream < 59 || *reading_stream == 46) 305 { // Zahl306 value = strtod(reading_stream, &reading_stream);247 { // number 248 value = strtod(reading_stream, const_cast<char**>(&reading_stream)); 307 249 } 308 250 else if (*reading_stream > 64 && *reading_stream < 91 || *reading_stream > 96 && *reading_stream < 123 || *reading_stream == 46) 309 { // Variable oder Funktion251 { // variable or function 310 252 char* word = new char[256]; 311 253 parse_word(word); … … 386 328 value = floor(parse_argument()/parse_last_argument()); 387 329 CASE("max") 388 value =max(parse_argument(),parse_last_argument());330 value = std::max(parse_argument(),parse_last_argument()); 389 331 CASE("min") 390 value =min(parse_argument(),parse_last_argument());332 value = std::min(parse_argument(),parse_last_argument()); 391 333 CASE_ELSE 392 334 { 393 parse_float_failed = true; 335 this->failed_ = true; 336 delete[] word; 394 337 return 0; 395 338 } … … 397 340 else 398 341 { 399 //TODO: Variablen-Liste durchsuchen 400 //Momentaner Ersatzwert für jede Variable: 401 //value = 0; 402 parse_float_failed = true; 403 return 0; 342 #define SWITCH word 343 CASE_1("pi") 344 value = 3.1415926535897932; 345 CASE("e") 346 value = 2.7182818284590452; 347 CASE_ELSE 348 { 349 this->failed_ = true; 350 delete[] word; 351 return 0; 352 } 404 353 } 405 354 delete[] word; 406 355 } 407 356 else if (*reading_stream == 40) 408 { // Audruck in Klammern357 { // expresion in paranthesis 409 358 ++reading_stream; 410 359 value = parse_last_argument(); … … 412 361 else 413 362 { 414 parse_float_failed= true;363 this->failed_ = true; 415 364 return 0; 416 365 } … … 424 373 default: 425 374 { 426 parse_float_failed= true;375 this->failed_ = true; 427 376 return 0; 428 377 } … … 430 379 } 431 380 432 static inline char*parse_word(char* str)381 char* ExprParser::parse_word(char* str) 433 382 { 434 383 char* word = str; … … 440 389 if (counter > 255) 441 390 { 442 parse_float_failed= true;391 this->failed_ = true; 443 392 return '\0'; 444 393 } … … 448 397 } 449 398 450 static inline binary_operatorparse_binary_operator()399 ExprParser::binary_operator ExprParser::parse_binary_operator() 451 400 { 452 401 binary_operator op; … … 484 433 } 485 434 486 static inline unary_operatorparse_unary_operator()435 ExprParser::unary_operator ExprParser::parse_unary_operator() 487 436 { 488 437 switch (*reading_stream) -
code/branches/input/src/util/ExprParser.h
r1113 r1118 1 //FloatParser.h 1 /* 2 * ORXONOX - the hottest 3D action shooter ever to exist 3 * > www.orxonox.net < 4 * 5 * 6 * License notice: 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21 * 22 * Author: 23 * Reto Grieder 24 * Co-authors: 25 * ... 26 * 27 */ 28 29 /** 30 @file 31 @brief Declaration of FloatParser 32 */ 2 33 3 34 #ifndef _FloatParser_H__ … … 5 36 6 37 #include "UtilPrereqs.h" 38 #include <string> 39 40 class _UtilExport ExprParser 41 { 42 public: 43 enum binary_operator 44 { 45 b_plus, 46 b_minus, 47 mal, 48 durch, 49 modulo, 50 hoch, 51 undef, 52 oder, 53 und, 54 gleich, 55 b_nicht, 56 kleiner, 57 groesser, 58 ungleich, 59 kleinergleich, 60 groessergleich 61 }; 62 63 enum unary_operator 64 { 65 u_plus, 66 u_minus, 67 u_nicht 68 }; 69 70 71 ExprParser(std::string& str); 72 std::string& getRemains() { return this->remains_; } 73 double getResult() { return this->result_; } 74 bool getSuccess() { return !this->failed_; } 75 76 private: 77 double parse_expr_1(); 78 double parse_expr_2(); 79 double parse_expr_3(); 80 double parse_expr_4(); 81 double parse_expr_5(); 82 double parse_expr_6(); 83 double parse_expr_7(); 84 double parse_expr_8(); 85 char* parse_word(char* str); 86 binary_operator parse_binary_operator(); 87 unary_operator parse_unary_operator(); 88 89 double parse_argument(); 90 double parse_last_argument(); 91 92 binary_operator op; 93 const char* reading_stream; 94 bool failed_; 95 double result_; 96 std::string remains_; 97 98 }; 7 99 8 100 //Endzeichen für float expression: ')', '}', ']', ',', ';'
Note: See TracChangeset
for help on using the changeset viewer.