Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5150 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Aug 27, 2005, 12:48:47 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: SubString is now Safe also for NULL and 1 parameters

Location:
trunk/src/lib/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/substring.cc

    r5149 r5150  
    3131{
    3232  this->splittersCount = 0;
     33  if (string == NULL)
     34  {
     35    this->strings = NULL;
     36    return;
     37  }
    3338
    34   assert( string != NULL);
    35 
    36   for( int i = 0; i < strlen(string); i++) if( string[i] == splitter)
    37       splittersCount++;
     39  for( int i = 0; i < strlen(string); i++)
     40    if( string[i] == splitter)
     41      this->splittersCount++;
    3842
    3943  this->splittersCount += 1;
     
    4549  int l = 0;
    4650
    47   const char* offset = string;
    48   char* end = strchr( string, splitter);
    49   while( end != NULL)
     51  if( this->splittersCount > 1)
     52  {
     53    const char* offset = string;
     54    char* end = strchr( string, splitter);
     55    while( end != NULL)
    5056    {
    5157      assert( i < this->splittersCount);
     
    6167    }
    6268
    63   strings[i] = new char[l + 1];
    64   l = strlen( offset);
    65   strncpy( strings[i], offset, l);
    66   strings[i][l] = '\0';
     69    strings[i] = new char[l + 1];
     70    l = strlen( offset);
     71    strncpy( strings[i], offset, l);
     72    strings[i][l] = '\0';
     73  }
     74  else
     75  {
     76    this->strings[0] = new char[strlen(string)];
     77    strcpy(this->strings[0], string);
     78  }
    6779}
    6880
     
    7284SubString::~SubString()
    7385{
    74   for( int i = 0; i < this->splittersCount; i++)
    75     delete this->strings[i];
    76   delete this->strings;
     86  if (this->strings)
     87  {
     88    for( int i = 0; i < this->splittersCount; i++)
     89      delete[] this->strings[i];
     90    delete[] this->strings;
     91  }
    7792}
    7893
     
    8297 * @returns the designated substring or NULL if an invalid ID was given
    8398*/
    84 const char* SubString::getString( int i)
     99const char* SubString::getString(unsigned int i)
    85100{
    86101  if( i < this->splittersCount && i >= 0)
  • trunk/src/lib/util/substring.h

    r4833 r5150  
    1414  ~SubString();
    1515
    16   inline int getCount() { return this->splittersCount; };
    17   const char* getString( int i);
     16  inline unsigned int getCount() { return this->splittersCount; };
     17  const char* getString(unsigned int i);
    1818
    1919  void debug() const;
    2020
    2121 private:
    22   char**     strings;                      //!< strings produced from a single string splitted in multiple strings
    23   int        splittersCount;               //!< how many splitted parts
     22  char**          strings;                      //!< strings produced from a single string splitted in multiple strings
     23  unsigned int    splittersCount;               //!< how many splitted parts
    2424};
    2525
Note: See TracChangeset for help on using the changeset viewer.