[4597] | 1 | /* |
---|
[3941] | 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 |
---|
[4597] | 13 | co-programmer: Benjamin Grauer |
---|
| 14 | |
---|
| 15 | 2005-06-10: some naming conventions |
---|
[4220] | 16 | */ |
---|
| 17 | |
---|
| 18 | |
---|
[3941] | 19 | /** |
---|
[4836] | 20 | * breaks a string into parts that were initially seperated by comma |
---|
| 21 | * @param string the string to break into substrings |
---|
[4220] | 22 | */ |
---|
[3941] | 23 | |
---|
[4220] | 24 | #include "substring.h" |
---|
| 25 | |
---|
[4833] | 26 | #include "debug.h" |
---|
[4220] | 27 | #include <string.h> |
---|
| 28 | #include <assert.h> |
---|
| 29 | |
---|
[4734] | 30 | SubString::SubString( const char* string, char splitter) |
---|
[4220] | 31 | { |
---|
[4830] | 32 | this->splittersCount = 0; |
---|
[5150] | 33 | if (string == NULL) |
---|
| 34 | { |
---|
| 35 | this->strings = NULL; |
---|
| 36 | return; |
---|
| 37 | } |
---|
[4597] | 38 | |
---|
[5150] | 39 | for( int i = 0; i < strlen(string); i++) |
---|
| 40 | if( string[i] == splitter) |
---|
| 41 | this->splittersCount++; |
---|
[4597] | 42 | |
---|
[4830] | 43 | this->splittersCount += 1; |
---|
[4597] | 44 | |
---|
[4830] | 45 | this->strings = new char*[this->splittersCount]; |
---|
[4220] | 46 | assert (strings != NULL); |
---|
[4597] | 47 | |
---|
[4220] | 48 | int i = 0; |
---|
| 49 | int l = 0; |
---|
[4597] | 50 | |
---|
[5150] | 51 | if( this->splittersCount > 1) |
---|
| 52 | { |
---|
| 53 | const char* offset = string; |
---|
[5183] | 54 | const char* end = strchr( string, splitter); |
---|
[5150] | 55 | while( end != NULL) |
---|
[4220] | 56 | { |
---|
[4830] | 57 | assert( i < this->splittersCount); |
---|
[4220] | 58 | l = end - offset; |
---|
[4830] | 59 | this->strings[i] = new char[l + 1]; |
---|
[4220] | 60 | assert( strings[i] != NULL); |
---|
| 61 | strncpy( strings[i], offset, l); |
---|
[5183] | 62 | strings[i][l] = '\0'; |
---|
[4220] | 63 | i++; |
---|
| 64 | end++; |
---|
| 65 | offset = end; |
---|
[4734] | 66 | end = strchr( offset, splitter); |
---|
[4220] | 67 | } |
---|
[4597] | 68 | |
---|
[5150] | 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 | { |
---|
[5183] | 76 | this->strings[0] = new char[strlen(string)+1]; |
---|
[5150] | 77 | strcpy(this->strings[0], string); |
---|
| 78 | } |
---|
[4220] | 79 | } |
---|
| 80 | |
---|
[5183] | 81 | |
---|
[3941] | 82 | /** |
---|
[5183] | 83 | * Splits a String into a Substring removing all whiteSpaces |
---|
| 84 | * @param string the String to Split |
---|
| 85 | * @param whiteSpaces MUST BE __TRUE__ |
---|
| 86 | * |
---|
| 87 | */ |
---|
| 88 | SubString::SubString(const char* string, bool whiteSpaces) |
---|
| 89 | { |
---|
| 90 | this->splittersCount = 0; |
---|
| 91 | if (string == NULL || whiteSpaces == false) |
---|
| 92 | { |
---|
| 93 | this->strings = NULL; |
---|
| 94 | return; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | // chop the input to the beginning of something usefull |
---|
| 98 | if (strlen(string) > 0) |
---|
| 99 | string = string + strspn(string, " \t\n"); |
---|
| 100 | |
---|
| 101 | // count the Splitters |
---|
| 102 | bool lastWasWhiteSpace = false; |
---|
| 103 | for(unsigned int i = 0; i < strlen(string); i++) |
---|
| 104 | if( string[i] == ' ' || string[i] == '\t' || string[i] == '\n' ) |
---|
| 105 | lastWasWhiteSpace = true; |
---|
| 106 | else |
---|
| 107 | { |
---|
| 108 | if (lastWasWhiteSpace) |
---|
| 109 | this->splittersCount ++; |
---|
| 110 | lastWasWhiteSpace = false; |
---|
| 111 | } |
---|
| 112 | this->splittersCount += 1; |
---|
| 113 | |
---|
[5184] | 114 | // allocate memory |
---|
[5183] | 115 | this->strings = new char*[this->splittersCount]; |
---|
| 116 | assert (strings != NULL); |
---|
| 117 | |
---|
[5184] | 118 | // split the String into substrings |
---|
[5183] | 119 | int l = 0; |
---|
| 120 | unsigned int i = 0; |
---|
| 121 | if( this->splittersCount > 1) |
---|
| 122 | { |
---|
| 123 | const char* offset = string; |
---|
| 124 | const char* end = offset + strcspn(offset, " \t\n"); |
---|
| 125 | for (i = 0; i < this->splittersCount; i++) |
---|
| 126 | { |
---|
| 127 | assert( i < this->splittersCount); |
---|
| 128 | l = end - offset; |
---|
| 129 | this->strings[i] = new char[l + 1]; |
---|
| 130 | assert( strings[i] != NULL); |
---|
| 131 | strncpy( strings[i], offset, l); |
---|
| 132 | strings[i][l] = '\0'; |
---|
| 133 | end += strspn(end, " \t\n"); |
---|
| 134 | offset = end; |
---|
| 135 | end = offset + strcspn(offset, " \t\n"); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | else |
---|
| 139 | { |
---|
| 140 | unsigned int length = strcspn(string, " \t\n"); |
---|
| 141 | this->strings[0] = new char[length+1]; |
---|
| 142 | strncpy(this->strings[0], string, length); |
---|
| 143 | this->strings[0][length] = '\0'; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
[4836] | 148 | * removes the object from memory |
---|
[3941] | 149 | */ |
---|
[4220] | 150 | SubString::~SubString() |
---|
| 151 | { |
---|
[5150] | 152 | if (this->strings) |
---|
| 153 | { |
---|
[5183] | 154 | for(unsigned int i = 0; i < this->splittersCount; i++) |
---|
[5150] | 155 | delete[] this->strings[i]; |
---|
| 156 | delete[] this->strings; |
---|
| 157 | } |
---|
[4220] | 158 | } |
---|
[3941] | 159 | |
---|
| 160 | /** |
---|
[4836] | 161 | * get a particular substring |
---|
| 162 | * @param i the ID of the substring to return |
---|
| 163 | * @returns the designated substring or NULL if an invalid ID was given |
---|
[4220] | 164 | */ |
---|
[5150] | 165 | const char* SubString::getString(unsigned int i) |
---|
[4220] | 166 | { |
---|
[4830] | 167 | if( i < this->splittersCount && i >= 0) |
---|
| 168 | return this->strings[i]; |
---|
| 169 | else |
---|
| 170 | return NULL; |
---|
[4220] | 171 | } |
---|
[4833] | 172 | |
---|
| 173 | /** |
---|
| 174 | * Some nice debug information about this SubString |
---|
| 175 | */ |
---|
| 176 | void SubString::debug() const |
---|
| 177 | { |
---|
| 178 | PRINT(0)("Substring-information::count=%d ::", this->splittersCount); |
---|
[5183] | 179 | if (this->strings != NULL) |
---|
| 180 | for (unsigned int i = 0; i < this->splittersCount; i++) |
---|
| 181 | PRINT(0)("s%d='%s'::", i, this->strings[i]); |
---|
[4833] | 182 | PRINT(0)("\n"); |
---|
| 183 | } |
---|