Changeset 4220 in orxonox.OLD for orxonox/trunk/src/lib
- Timestamp:
- May 18, 2005, 11:01:59 PM (20 years ago)
- Location:
- orxonox/trunk/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/util/substring.cc
r4010 r4220 22 22 #include "substring.h" 23 23 24 #include <string.h> 25 #include <assert.h> 26 24 27 SubString::SubString( const char* string) 25 28 { 26 29 n = 0; 27 30 28 31 assert( string != NULL); 29 32 30 33 for( int i = 0; i < strlen(string); i++) if( string[i] == ',') n++; 31 34 32 35 n += 1; 33 36 34 strings = new char*[n]; 37 strings = new char*[n]; 38 39 assert (strings != NULL); 35 40 36 assert( strings != NULL); 41 int i = 0; 42 int l = 0; 37 43 38 int i = 0; 39 int l = 0; 44 const char* offset = string; 45 char* end = strchr( string, ','); 46 while( end != NULL) 47 { 48 assert( i < n); 49 l = end - offset; 50 strings[i] = new char[l + 1]; 51 assert( strings[i] != NULL); 52 strncpy( strings[i], offset, l); 53 strings[i][l] = 0; 54 i++; 55 end++; 56 offset = end; 57 end = strchr( offset, ','); 58 } 40 59 41 const char* offset = string; 42 char* end = strchr( string, ','); 43 while( end != NULL) 44 { 45 assert( i < n); 46 l = end - offset; 47 strings[i] = new char[l + 1]; 48 assert( strings[i] != NULL); 49 strncpy( strings[i], offset, l); 50 strings[i][l] = 0; 51 i++; 52 end++; 53 offset = end; 54 end = strchr( offset, ','); 55 } 56 57 strings[i] = new char[l + 1]; 58 l = strlen( offset); 59 strncpy( strings[i], offset, l); 60 strings[i][l] = 0; 60 strings[i] = new char[l + 1]; 61 l = strlen( offset); 62 strncpy( strings[i], offset, l); 63 strings[i][l] = 0; 61 64 } 62 65 … … 66 69 SubString::~SubString() 67 70 { 68 69 70 71 71 for( int i = 0; i < n; i++) 72 { 73 delete strings[i]; 74 } 72 75 73 76 delete strings; 74 77 } 75 78 … … 78 81 \returns the amount of substrings 79 82 */ 80 int SubString::get N()83 int SubString::getCount() 81 84 { 82 85 return n; 83 86 } 84 87 … … 90 93 const char* SubString::getString( int i) 91 94 { 92 93 95 if( i < n && i >= 0) return strings[i]; 96 else return NULL; 94 97 } -
orxonox/trunk/src/lib/util/substring.h
r4010 r4220 1 1 /*! 2 3 2 \file substring.h 3 \brief a small class to get the parts of a string separated by commas 4 4 */ 5 5 … … 7 7 #define _SUBSTRING_H 8 8 9 #include "stdincl.h"10 11 9 class SubString 12 10 { 13 11 public: 14 12 15 16 13 SubString( const char* string); 14 ~SubString(); 17 15 18 int getN();19 16 int getCount(); 17 const char* getString( int i); 20 18 21 22 23 19 private: 20 char** strings; 21 int n; 24 22 }; 25 23
Note: See TracChangeset
for help on using the changeset viewer.