| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 |    ### File Specific: | 
|---|
| 12 |    main-programmer: Christian Meyer | 
|---|
| 13 |    co-programmer: Benjamin Grauer | 
|---|
| 14 |  | 
|---|
| 15 |    2005-06-10: some naming conventions | 
|---|
| 16 | */ | 
|---|
| 17 |  | 
|---|
| 18 |  | 
|---|
| 19 | /** | 
|---|
| 20 |  *  breaks a string into parts that were initially seperated by comma | 
|---|
| 21 |  * @param string the string to break into substrings | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | #include "substring.h" | 
|---|
| 25 |  | 
|---|
| 26 | #include "debug.h" | 
|---|
| 27 | #include <string.h> | 
|---|
| 28 | #include <assert.h> | 
|---|
| 29 |  | 
|---|
| 30 | SubString::SubString( const char* string, char splitter) | 
|---|
| 31 | { | 
|---|
| 32 |   this->splittersCount = 0; | 
|---|
| 33 |  | 
|---|
| 34 |   assert( string != NULL); | 
|---|
| 35 |  | 
|---|
| 36 |   for( int i = 0; i < strlen(string); i++) if( string[i] == splitter) | 
|---|
| 37 |       splittersCount++; | 
|---|
| 38 |  | 
|---|
| 39 |   this->splittersCount += 1; | 
|---|
| 40 |  | 
|---|
| 41 |   this->strings = new char*[this->splittersCount]; | 
|---|
| 42 |   assert (strings != NULL); | 
|---|
| 43 |  | 
|---|
| 44 |   int i = 0; | 
|---|
| 45 |   int l = 0; | 
|---|
| 46 |  | 
|---|
| 47 |   const char* offset = string; | 
|---|
| 48 |   char* end = strchr( string, splitter); | 
|---|
| 49 |   while( end != NULL) | 
|---|
| 50 |     { | 
|---|
| 51 |       assert( i < this->splittersCount); | 
|---|
| 52 |       l = end - offset; | 
|---|
| 53 |       this->strings[i] = new char[l + 1]; | 
|---|
| 54 |       assert( strings[i] != NULL); | 
|---|
| 55 |       strncpy( strings[i], offset, l); | 
|---|
| 56 |       strings[i][l] = 0; | 
|---|
| 57 |       i++; | 
|---|
| 58 |       end++; | 
|---|
| 59 |       offset = end; | 
|---|
| 60 |       end = strchr( offset, splitter); | 
|---|
| 61 |     } | 
|---|
| 62 |  | 
|---|
| 63 |   strings[i] = new char[l + 1]; | 
|---|
| 64 |   l = strlen( offset); | 
|---|
| 65 |   strncpy( strings[i], offset, l); | 
|---|
| 66 |   strings[i][l] = 0; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |  *  removes the object from memory | 
|---|
| 71 | */ | 
|---|
| 72 | SubString::~SubString() | 
|---|
| 73 | { | 
|---|
| 74 |   for( int i = 0; i < this->splittersCount; i++) | 
|---|
| 75 |     { | 
|---|
| 76 |       delete this->strings[i]; | 
|---|
| 77 |     } | 
|---|
| 78 |  | 
|---|
| 79 |   delete this->strings; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | /** | 
|---|
| 83 |  *  get a particular substring | 
|---|
| 84 |  * @param i the ID of the substring to return | 
|---|
| 85 |  * @returns the designated substring or NULL if an invalid ID was given | 
|---|
| 86 | */ | 
|---|
| 87 | const char* SubString::getString( int i) | 
|---|
| 88 | { | 
|---|
| 89 |   if( i < this->splittersCount && i >= 0) | 
|---|
| 90 |     return this->strings[i]; | 
|---|
| 91 |   else | 
|---|
| 92 |     return NULL; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | /** | 
|---|
| 98 |  * Some nice debug information about this SubString | 
|---|
| 99 |  */ | 
|---|
| 100 | void SubString::debug() const | 
|---|
| 101 | { | 
|---|
| 102 |   PRINT(0)("Substring-information::count=%d ::", this->splittersCount); | 
|---|
| 103 |   for (int i = 0; i < this->splittersCount; i++) | 
|---|
| 104 |     PRINT(0)("s%d:%s::", i, this->strings[i]); | 
|---|
| 105 |   PRINT(0)("\n"); | 
|---|
| 106 | } | 
|---|