Changeset 5150 in orxonox.OLD for trunk/src/lib
- Timestamp:
- Aug 27, 2005, 12:48:47 PM (19 years ago)
- Location:
- trunk/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/substring.cc
r5149 r5150 31 31 { 32 32 this->splittersCount = 0; 33 if (string == NULL) 34 { 35 this->strings = NULL; 36 return; 37 } 33 38 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++; 38 42 39 43 this->splittersCount += 1; … … 45 49 int l = 0; 46 50 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) 50 56 { 51 57 assert( i < this->splittersCount); … … 61 67 } 62 68 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 } 67 79 } 68 80 … … 72 84 SubString::~SubString() 73 85 { 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 } 77 92 } 78 93 … … 82 97 * @returns the designated substring or NULL if an invalid ID was given 83 98 */ 84 const char* SubString::getString( int i)99 const char* SubString::getString(unsigned int i) 85 100 { 86 101 if( i < this->splittersCount && i >= 0) -
trunk/src/lib/util/substring.h
r4833 r5150 14 14 ~SubString(); 15 15 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); 18 18 19 19 void debug() const; 20 20 21 21 private: 22 char** strings; //!< strings produced from a single string splitted in multiple strings23 intsplittersCount; //!< how many splitted parts22 char** strings; //!< strings produced from a single string splitted in multiple strings 23 unsigned int splittersCount; //!< how many splitted parts 24 24 }; 25 25
Note: See TracChangeset
for help on using the changeset viewer.