Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 20, 2008, 4:31:03 PM (16 years ago)
Author:
rgrieder
Message:
  • classed FloatParser to ExprParser class
File:
1 moved

Legend:

Unmodified
Added
Removed
  • 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"
    535#include <cmath>
    636#include <cstring>
    737
    8 using namespace std;
    9 
    10 //Makros, um Funktionen einfacher parser zu können
     38// macros for easier if, else statements
    1139#define CASE_1(var) if (!strcmp(SWITCH,var))
    1240#define CASE(var) else if (!strcmp(SWITCH,var))
    1341#define CASE_ELSE else
    1442
     43//! skip white spaces
    1544#define PARSE_BLANKS while (*reading_stream == ' ') ++reading_stream;
    1645
    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;
     46ExprParser::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  }
    11860}
    11961
    12062//Private functions:
    12163/******************/
    122 static double parse_argument()
     64double ExprParser::parse_argument()
    12365{
    12466   double value = parse_expr_8();
     
    13072   else
    13173   {
    132      parse_float_failed = true;
     74     this->failed_ = true;
    13375     return 0;
    13476   }
    13577}
    13678
    137 static double parse_last_argument()
     79double ExprParser::parse_last_argument()
    13880{
    13981   double value = parse_expr_8();
     
    14587   else
    14688   {
    147      parse_float_failed = true;
     89     this->failed_ = true;
    14890     return 0;
    14991   }
    15092}
    15193
    152 static double parse_expr_8()
     94double ExprParser::parse_expr_8()
    15395{
    15496   double value = parse_expr_7();
     
    166108
    167109
    168 static double parse_expr_7()
     110double ExprParser::parse_expr_7()
    169111{
    170112   double value = parse_expr_6();
     
    181123}
    182124
    183 static double parse_expr_6()
     125double ExprParser::parse_expr_6()
    184126{
    185127   double value = parse_expr_5();
     
    200142}
    201143
    202 static double parse_expr_5()
     144double ExprParser::parse_expr_5()
    203145{
    204146   double value = parse_expr_4();
     
    225167}
    226168
    227 static double parse_expr_4()
     169double ExprParser::parse_expr_4()
    228170{
    229171   double value = parse_expr_3();
     
    244186}
    245187
    246 static double parse_expr_3()
     188double ExprParser::parse_expr_3()
    247189{
    248190   double value = parse_expr_2();
     
    269211}
    270212
    271 static double parse_expr_2()
     213double ExprParser::parse_expr_2()
    272214{
    273215   double value = parse_expr_1();
     
    288230}
    289231
    290 static double parse_expr_1()
     232double ExprParser::parse_expr_1()
    291233{
    292234   PARSE_BLANKS
     
    299241   {
    300242     // end of string
    301      parse_float_failed = true;
     243     this->failed_ = true;
    302244     return 0;
    303245   }
    304246   else if (*reading_stream > 47 && *reading_stream < 59 || *reading_stream == 46)
    305    {  //Zahl
    306       value = strtod(reading_stream,&reading_stream);
     247   {  // number
     248      value = strtod(reading_stream, const_cast<char**>(&reading_stream));
    307249   }
    308250   else if (*reading_stream > 64 && *reading_stream < 91 || *reading_stream > 96 && *reading_stream < 123 || *reading_stream == 46)
    309    {  //Variable oder Funktion
     251   {  // variable or function
    310252      char* word = new char[256];
    311253      parse_word(word);
     
    386328            value = floor(parse_argument()/parse_last_argument());
    387329         CASE("max")
    388             value = max(parse_argument(),parse_last_argument());
     330           value = std::max(parse_argument(),parse_last_argument());
    389331         CASE("min")
    390             value = min(parse_argument(),parse_last_argument());
     332           value = std::min(parse_argument(),parse_last_argument());
    391333         CASE_ELSE
    392334         {
    393            parse_float_failed = true;
     335           this->failed_ = true;
     336           delete[] word;
    394337           return 0;
    395338         }
     
    397340      else
    398341      {
    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         }
    404353      }
    405354      delete[] word;
    406355   }
    407356   else if (*reading_stream == 40)
    408    {  //Audruck in Klammern
     357   {  // expresion in paranthesis
    409358      ++reading_stream;
    410359      value = parse_last_argument();
     
    412361   else
    413362   {
    414      parse_float_failed = true;
     363     this->failed_ = true;
    415364     return 0;
    416365   }
     
    424373      default:
    425374        {
    426           parse_float_failed = true;
     375          this->failed_ = true;
    427376          return 0;
    428377        }
     
    430379}
    431380
    432 static inline char* parse_word(char* str)
     381char* ExprParser::parse_word(char* str)
    433382{
    434383   char* word = str;
     
    440389      if (counter > 255)
    441390      {
    442         parse_float_failed = true;
     391        this->failed_ = true;
    443392        return '\0';
    444393      }
     
    448397}
    449398
    450 static inline binary_operator parse_binary_operator()
     399ExprParser::binary_operator ExprParser::parse_binary_operator()
    451400{
    452401   binary_operator op;
     
    484433}
    485434
    486 static inline unary_operator parse_unary_operator()
     435ExprParser::unary_operator ExprParser::parse_unary_operator()
    487436{
    488437   switch (*reading_stream)
Note: See TracChangeset for help on using the changeset viewer.