Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7211 in orxonox.OLD for branches/std/src/lib/util


Ignore:
Timestamp:
Mar 10, 2006, 4:52:21 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: new SubString class

Location:
branches/std/src/lib/util
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/std/src/lib/util/executor/executor.h

    r7210 r7211  
    1616//! an enumerator for the definition of the Type.
    1717typedef enum {
    18   Executor_Objective         = 0x00000001,
    19   Executor_Static            = 0x00000002,
    20 
    21   Executor_NoLoadString      = 0x00000010,
     18  Executor_Objective         = 1,
     19  Executor_Static            = 2,
     20
     21  Executor_NoLoadString      = 8,
    2222} Executor_Type;
    2323
     
    192192#define ExecutorExecute1(t1) \
    193193   else if (this->paramCount == 1 && this->defaultValue[0] == t1##_PARAM) \
    194     EXECUTOREXECUTER(_1_##t1)(t1##_FUNC((const char*)parameters, t1##_DEFGRAB(0)))
     194    EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)))
    195195
    196196//! execute-macro for functions with two parameters
     
    270270    virtual void execute (BaseObject* object, const void* parameters = NULL)
    271271    {
    272       SubString sub(((const std::string*) parameters)->c_str(), " \n\t,", '\\');
     272      SubString sub((const char*)parameters, " \n\t", '\\');
    273273//! FUNCTOR_LIST is the List of Executive Functions
    274274#define FUNCTOR_LIST(x) ExecutorExecute ## x
     
    332332    virtual void execute (BaseObject* object, const void* parameters = NULL)
    333333    {
    334   SubString sub((const char*)parameters, " \n\t,");
     334      SubString sub(((const char*)parameters), " \n\t,");
    335335//! FUNCTOR_LIST is the List of Executive Functions
    336336#define FUNCTOR_LIST(x) ExecutorExecute ## x
  • branches/std/src/lib/util/helper_functions.cc

    r7203 r7211  
    2727 * @return returns the bool, if BOOL was correct otherwise defaultValue
    2828 */
    29 bool isBool(const char* BOOL, bool defaultValue)
     29bool isBool(const std::string& BOOL, bool defaultValue)
    3030{
    31   if (BOOL == NULL)
     31  if (BOOL.empty())
    3232    return defaultValue;
    33   if(!strcmp(BOOL, "1") || !strcasecmp( BOOL, "true") )
     33  if(BOOL[0] == '1' || BOOL == "true" )
    3434    return true;
    35   else if (!strcmp(BOOL, "0") || !strcasecmp( BOOL, "false"))
     35  else if (BOOL[0] == '0' || BOOL == "false")
    3636    return false;
    3737  else
    3838    return defaultValue;
    39 
    4039}
    4140
     
    4746 * @return returns the contained int, if INT was correct otherwise defaultValue
    4847 */
    49 int isInt(const char* INT, int defaultValue)
     48int isInt(const std::string& INT, int defaultValue)
    5049{
    51   if (INT == NULL)
     50  if (INT.empty())
    5251    return defaultValue;
    5352  char* endPtr = NULL;
    5453
    55   int result = strtol(INT, &endPtr, 10);
     54  int result = strtol(INT.c_str(), &endPtr, 10);
    5655
    57   if ( endPtr >= INT && endPtr < INT + strlen(INT))
     56  if ( endPtr >= INT.c_str() && endPtr < INT.c_str()+ INT.size())
    5857    return defaultValue;
    5958  else
     
    6867 * @return returns the contained float, if FLOAT was correct otherwise defaultValue
    6968 */
    70 float isFloat(const char* FLOAT, float defaultValue)
     69float isFloat(const std::string& FLOAT, float defaultValue)
    7170{
    72   if (FLOAT == NULL)
     71  if (FLOAT.empty())
    7372    return defaultValue;
    7473  char* endPtr = NULL;
    75   double result = strtod(FLOAT, &endPtr);
     74  double result = strtod(FLOAT.c_str(), &endPtr);
    7675
    77   if ( endPtr >= FLOAT && endPtr < FLOAT + strlen(FLOAT))
     76  if ( endPtr >= FLOAT.c_str() && endPtr < FLOAT.c_str() + FLOAT.size())
    7877    return defaultValue;
    7978  else
     
    8887 * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue
    8988 */
    90 const char* isCString(const char* STRING, const char* defaultValue)
     89const char* isCString(const std::string& STRING, const char* defaultValue)
    9190{
    92   if (STRING != NULL && strlen(STRING) > 0)
    93     return STRING;
     91  if (STRING.size() > 0)
     92    return STRING.c_str();
    9493  else
    9594    return defaultValue;
     
    102101 * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue
    103102 */
    104 std::string isString(const char* STRING, const std::string& defaultValue)
     103std::string isString(const std::string& STRING, const std::string& defaultValue)
    105104{
    106   if (STRING != NULL && strlen(STRING) > 0)
     105  if (STRING.size() > 0)
    107106    return STRING;
    108107  else
  • branches/std/src/lib/util/helper_functions.h

    r7203 r7211  
    1414*** HELPER FUNCTIONS ***
    1515***********************/
    16 bool          isBool(const char* BOOL, bool defaultValue);
    17 int           isInt(const char* INT, int defaultValue);
    18 float         isFloat(const char* FLOAT, float defaultValue);
    19 const char*   isCString(const char* STRING, const char* defaultValue);
    20 std::string   isString(const char* STRING, const std::string& defaultValue);
     16bool          isBool(const std::string& BOOL, bool defaultValue);
     17int           isInt(const std::string& INT, int defaultValue);
     18float         isFloat(const std::string& FLOAT, float defaultValue);
     19const char*   isCString(const std::string& STRING, const char* defaultValue);
     20std::string   isString(const std::string& STRING, const std::string& defaultValue);
    2121
    2222
  • branches/std/src/lib/util/loading/load_param.cc

    r7209 r7211  
    6767    {
    6868      PRINTF(4)("Loading '%s' with Parameters '%s' onto: '%s'(%s)\n", this->paramName, loadString.c_str(), this->object->getName(), this->object->getClassName());
    69       this->executor->execute(this->object, &loadString);
     69      this->executor->execute(this->object, (const void*)loadString.c_str());
    7070    }
    7171    delete this->executor;
  • branches/std/src/lib/util/multi_type.cc

    r7201 r7211  
    3939  switch (this->type)
    4040  {
     41    default:
     42      this->value.Float = 0.0f;
     43      break;
    4144    case MT_BOOL:
    4245      this->value.Bool = false;
  • branches/std/src/lib/util/multi_type.h

    r7200 r7211  
    106106//      std::string*      String;
    107107    }                   value;
    108 
     108    std::string         storedString;
    109109    MT_Type             type;
    110 
    111     std::string         storedString;
    112110};
    113111
  • branches/std/src/lib/util/substring.cc

    r5656 r7211  
    2424#include "substring.h"
    2525
    26 #include "debug.h"
    2726#include <string.h>
    28 #include <assert.h>
    29 
    30 SubString::SubString( const char* string, char splitter)
    31 {
    32   this->splittersCount = 0;
    33   if (string == NULL)
    34   {
    35     this->strings = NULL;
    36     this->offsets = NULL;
    37     return;
    38   }
    39 
    40   for( int i = 0; i < strlen(string); i++)
    41     if( string[i] == splitter)
    42       this->splittersCount++;
    43 
    44   this->splittersCount += 1;
    45 
    46   this->strings = new char*[this->splittersCount];
    47   this->offsets = new unsigned int[this->splittersCount];
    48   assert (this->strings != NULL && this->offsets != NULL);
    49 
    50   int i = 0;
    51   int l = 0;
    52 
    53   if( this->splittersCount > 1)
    54   {
    55     const char* offset = string;
    56     const char* end = strchr( string, splitter);
    57     while( end != NULL)
    58     {
    59       assert( i < this->splittersCount);
    60       l = end - offset;
    61       this->strings[i] = new char[l + 1];
    62       assert( strings[i] != NULL);
    63       strncpy( strings[i], offset, l);
    64       strings[i][l] = '\0';
    65       this->offsets[i] = offset - string;
    66       i++;
    67       end++;
    68       offset = end;
    69       end = strchr( offset, splitter);
    70     }
    71 
    72     l = strlen( offset);
    73     strings[i] = new char[l + 1];
    74     strncpy( strings[i], offset, l);
    75     strings[i][l] = '\0';
    76     this->offsets[i] = offset - string;
    77   }
    78   else
    79   {
    80     this->strings[0] = new char[strlen(string)+1];
    81     strcpy(this->strings[0], string);
    82     this->offsets[0] = 0;
    83   }
     27#include <cassert>
     28
     29SubString::SubString(const std::string& string, char splitter)
     30{
     31  char split[2];
     32  split[0] = splitter;
     33  split[1] = '\0';
     34  SubString::splitLine(this->strings, this->offsets,
     35                       string, split);
    8436}
    8537
     
    9143 *
    9244 */
    93 SubString::SubString(const char* string, bool whiteSpaces)
    94 {
    95   this->splittersCount = 0;
    96   if (string == NULL || whiteSpaces == false)
     45SubString::SubString(const std::string& string, bool whiteSpaces)
     46{
     47  SubString::splitLine(this->strings, this->offsets,
     48                      string);
     49}
     50
     51SubString::SubString(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char)
     52{
     53  SubString::splitLine(this->strings, this->offsets,
     54                       string, splitters, escapeChar, safemode_char);
     55}
     56
     57
     58/**
     59 * splits line into tokens and stores them in ret. Supports delimiters, escape characters,
     60 * ignores special  characters between safemode_char and between comment_char and linend '\n'.
     61 *
     62 * @returns SPLIT_LINE_STATE the parser was in when returning
     63 */
     64SPLIT_LINE_STATE SubString::splitLine(std::vector<std::string>& ret, std::vector<unsigned int>& offsets,
     65                                      const std::string& line, const std::string& delimiters,
     66                                      char escape_char, char safemode_char, char comment_char,
     67                                      SPLIT_LINE_STATE start_state)
     68{
     69  SPLIT_LINE_STATE state = start_state;
     70  unsigned int i = 0;
     71  std::string token;
     72
     73  if(start_state != SL_NORMAL && ret.size() > 0)
    9774  {
    98     this->strings = NULL;
    99     this->offsets = NULL;
    100     return;
     75    token = ret[ret.size()-1];
     76    ret.pop_back();
    10177  }
    10278
    103   // chop the input to the beginning of something usefull
    104   if (strlen(string) > 0)
    105     string = string + strspn(string, " \t\n");
    106 
    107   // count the Splitters
    108   bool lastWasWhiteSpace = false;
    109   for(unsigned int i = 0; i < strlen(string); i++)
    110     if( string[i] == ' ' || string[i] == '\t' || string[i] == '\n' )
    111       lastWasWhiteSpace = true;
    112     else
     79  while(i < line.size())
     80  {
     81    switch(state)
    11382    {
    114       if (lastWasWhiteSpace)
    115         this->splittersCount ++;
    116       lastWasWhiteSpace = false;
     83    case SL_NORMAL:
     84      if(line[i] == escape_char)
     85      {
     86        state = SL_ESCAPE;
     87      }
     88      else if(line[i] == safemode_char)
     89      {
     90        state = SL_SAFEMODE;
     91      }
     92      else if(line[i] == comment_char)
     93      {
     94        /// FINISH
     95        if(token.size() > 0)
     96        {
     97          ret.push_back(token);
     98          offsets.push_back(i);
     99          token.clear();
     100        }
     101        token += line[i];       // EAT
     102        state = SL_COMMENT;
     103      }
     104      else if(delimiters.find(line[i]) != std::string::npos)
     105      {
     106        // line[i] is a delimiter
     107        /// FINISH
     108        if(token.size() > 0)
     109        {
     110          ret.push_back(token);
     111          offsets.push_back(i);
     112          token.clear();
     113        }
     114      }
     115      else
     116      {
     117        token += line[i];       // EAT
     118      }
     119      break;
     120    case SL_ESCAPE:
     121      if(line[i] == 'n') token += '\n';
     122      else if(line[i] == 't') token += '\t';
     123      else if(line[i] == 'v') token += '\v';
     124      else if(line[i] == 'b') token += '\b';
     125      else if(line[i] == 'r') token += '\r';
     126      else if(line[i] == 'f') token += '\f';
     127      else if(line[i] == 'a') token += '\a';
     128      else if(line[i] == '?') token += '\?';
     129      else token += line[i];  // EAT
     130      state = SL_NORMAL;
     131      break;
     132    case SL_SAFEMODE:
     133      if(line[i] == safemode_char)
     134      {
     135        state = SL_NORMAL;
     136      }
     137      else if(line[i] == escape_char)
     138      {
     139        state = SL_SAFEESCAPE;
     140      }
     141      else
     142      {
     143        token += line[i];       // EAT
     144      }
     145      break;
     146    case SL_SAFEESCAPE:
     147      if(line[i] == 'n') token += '\n';
     148      else if(line[i] == 't') token += '\t';
     149      else if(line[i] == 'v') token += '\v';
     150      else if(line[i] == 'b') token += '\b';
     151      else if(line[i] == 'r') token += '\r';
     152      else if(line[i] == 'f') token += '\f';
     153      else if(line[i] == 'a') token += '\a';
     154      else if(line[i] == '?') token += '\?';
     155      else token += line[i];  // EAT
     156      state = SL_SAFEMODE;
     157      break;
     158    case SL_COMMENT:
     159      if(line[i] == '\n')
     160      {
     161        /// FINISH
     162        if(token.size() > 0)
     163        {
     164          ret.push_back(token);
     165          offsets.push_back(i);
     166          token.clear();
     167        }
     168        state = SL_NORMAL;
     169      }
     170      else
     171      {
     172        token += line[i];       // EAT
     173      }
     174      break;
     175    default:
     176      // nothing
     177      break;
    117178    }
    118   this->splittersCount += 1;
    119 
    120   // allocate memory
    121   this->strings = new char*[this->splittersCount];
    122   this->offsets = new unsigned int[this->splittersCount];
    123   assert (this->strings != NULL && this->offsets != NULL);
    124 
    125 
    126   // split the String into substrings
    127   int l = 0;
    128   unsigned int i = 0;
    129   if( this->splittersCount > 1)
     179    i++;
     180  }
     181
     182  /// FINISH
     183  if(token.size() > 0)
    130184  {
    131     const char* offset = string;
    132     const char* end = offset + strcspn(offset, " \t\n");
    133     for (i = 0; i < this->splittersCount; i++)
    134     {
    135       assert( i < this->splittersCount);
    136       l = end - offset;
    137       this->strings[i] = new char[l + 1];
    138       assert( strings[i] != NULL);
    139       strncpy( strings[i], offset, l);
    140       strings[i][l] = '\0';
    141       this->offsets[i] = offset - string;
    142       end += strspn(end, " \t\n");
    143       offset = end;
    144       end = offset + strcspn(offset, " \t\n");
    145     }
     185    ret.push_back(token);
     186    offsets.push_back(i);
     187    token.clear();
    146188  }
    147   else
    148   {
    149     unsigned int length = strcspn(string, " \t\n");
    150     this->strings[0] = new char[length+1];
    151     strncpy(this->strings[0], string, length);
    152     this->strings[0][length] = '\0';
    153     offsets[0] = 0;
    154   }
    155 }
    156 
    157 SubString::SubString(const char* string, const char* splitters, char escapeChar)
    158 {
    159   this->splittersCount = 0;
    160   if (string == NULL || splitters == NULL)
    161   {
    162     this->strings = NULL;
    163     this->offsets = NULL;
    164     return;
    165   }
    166 
    167   // chop the input to the beginning of something usefull
    168   if (strlen(string) > 0)
    169     string = string + strspn(string, splitters);
    170 
    171   // count the Splitters
    172   bool lastWasSplitter = false;
    173   for(unsigned int i = 0; i < strlen(string); i++)
    174   {
    175 
    176     if( strchr(splitters, string[i] ))
    177       lastWasSplitter = true;
    178     else
    179     {
    180       if (lastWasSplitter)
    181       {
    182         this->splittersCount ++;
    183         lastWasSplitter = false;
    184       }
    185     }
    186   }
    187   this->splittersCount += 1;
    188 
    189   // allocate memory
    190   this->strings = new char*[this->splittersCount];
    191   this->offsets = new unsigned int[this->splittersCount];
    192   assert (this->strings != NULL && this->offsets != NULL);
    193 
    194 
    195   // split the String into substrings
    196   int l = 0;
    197   unsigned int i = 0;
    198   if( this->splittersCount > 1)
    199   {
    200     const char* offset = string;
    201     const char* end = offset + strcspn(offset, splitters);
    202     for (i = 0; i < this->splittersCount; i++)
    203     {
    204       assert( i < this->splittersCount);
    205       l = end - offset;
    206       this->strings[i] = new char[l + 1];
    207       assert( strings[i] != NULL);
    208       strncpy( strings[i], offset, l);
    209       strings[i][l] = '\0';
    210       this->offsets[i] = offset - string;
    211       end += strspn(end, splitters);
    212       offset = end;
    213       end = offset + strcspn(offset, splitters);
    214     }
    215   }
    216   else
    217   {
    218     unsigned int length = strcspn(string, splitters);
    219     this->strings[0] = new char[length+1];
    220     strncpy(this->strings[0], string, length);
    221     this->strings[0][length] = '\0';
    222     offsets[0] = 0;
    223   }
    224 }
    225 
     189  return(state);
     190}
    226191
    227192/**
     
    229194*/
    230195SubString::~SubString()
    231 {
    232   if (this->strings)
    233   {
    234     for(unsigned int i = 0; i < this->splittersCount; i++)
    235       delete[] this->strings[i];
    236     delete[] this->strings;
    237   }
    238   delete[] this->offsets;
    239 }
    240 
    241 /**
    242  *  get a particular substring
    243  * @param i the ID of the substring to return
    244  * @returns the designated substring or NULL if an invalid ID was given
    245 */
    246 const char* SubString::getString(unsigned int i)
    247 {
    248   if( i < this->splittersCount && i >= 0)
    249     return this->strings[i];
    250   else
    251     return NULL;
    252 }
     196{ }
    253197
    254198/**
     
    259203unsigned int SubString::getOffset(unsigned int i)
    260204{
    261   if( i < this->splittersCount && i >= 0)
     205  if( i < this->offsets.size() && i >= 0)
    262206    return this->offsets[i];
    263207  else
     
    270214void SubString::debug() const
    271215{
    272   PRINT(0)("Substring-information::count=%d ::", this->splittersCount);
    273   if (this->strings != NULL)
    274     for (unsigned int i = 0; i < this->splittersCount; i++)
    275      PRINT(0)("s%d='%s'::", i, this->strings[i]);
    276   PRINT(0)("\n");
    277 }
     216  printf("Substring-information::count=%d ::", this->strings.size());
     217  for (unsigned int i = 0; i < this->strings.size(); i++)
     218    printf("s%d='%s'::", i, this->strings[i].c_str());
     219  printf("\n");
     220}
  • branches/std/src/lib/util/substring.h

    r6648 r7211  
    77#define _SUBSTRING_H
    88
     9#include <vector>
     10#include <string>
     11
     12  typedef enum {
     13    SL_NORMAL,
     14    SL_ESCAPE,
     15    SL_SAFEMODE,
     16    SL_SAFEESCAPE,
     17    SL_COMMENT,
     18  } SPLIT_LINE_STATE;
     19
    920//! A class that can load one string and split it in multipe ones
    1021class SubString
    1122{
    1223 public:
    13   SubString(const char* string, char splitter = ',');
    14   SubString(const char* string, bool whiteSpaces);
    15   SubString(const char* string, const char* splitters, char escapeChar ='\\');
     24  SubString(const std::string& string, char splitter = ',');
     25  SubString(const std::string& string, bool whiteSpaces);
     26  SubString(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
    1627  ~SubString();
    1728
    18   const char* operator[](unsigned int i) { return this->getString(i); };
     29  SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,std::vector<unsigned int>& offsets,
     30                             const std::string& line, const std::string& delimiters = " \t\r\n",
     31                             char escape_char = '\\', char safemode_char = '"', char comment_char = '\0',
     32                             SPLIT_LINE_STATE start_state = SL_NORMAL);
    1933
    20   inline unsigned int getCount() { return this->splittersCount; };
    21   const char* getString(unsigned int i);
     34  const std::string& operator[](unsigned int i) { return this->getString(i); };
     35
     36  inline unsigned int getCount() { return this->strings.size(); };
     37  const std::string& getString(unsigned int i) { return this->strings[i]; };
    2238  unsigned int getOffset(unsigned int i);
    2339
     
    2541
    2642 private:
    27   char**          strings;                      //!< strings produced from a single string splitted in multiple strings
    28   unsigned int*   offsets;                      //!< offsets of the beginning of the input-string to the beginning of each substring.
    29   unsigned int    splittersCount;               //!< how many splitted parts
     43   std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
     44   std::vector<unsigned int> offsets;                      //!< offsets of the beginning of the input-string to the beginning of each substring.
    3045};
    3146
Note: See TracChangeset for help on using the changeset viewer.