[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; |
---|
[4597] | 33 | |
---|
[4220] | 34 | assert( string != NULL); |
---|
[4597] | 35 | |
---|
[4830] | 36 | for( int i = 0; i < strlen(string); i++) if( string[i] == splitter) |
---|
| 37 | splittersCount++; |
---|
[4220] | 38 | |
---|
[4830] | 39 | this->splittersCount += 1; |
---|
[4597] | 40 | |
---|
[4830] | 41 | this->strings = new char*[this->splittersCount]; |
---|
[4220] | 42 | assert (strings != NULL); |
---|
[4597] | 43 | |
---|
[4220] | 44 | int i = 0; |
---|
| 45 | int l = 0; |
---|
[4597] | 46 | |
---|
[4220] | 47 | const char* offset = string; |
---|
[4734] | 48 | char* end = strchr( string, splitter); |
---|
[4220] | 49 | while( end != NULL) |
---|
| 50 | { |
---|
[4830] | 51 | assert( i < this->splittersCount); |
---|
[4220] | 52 | l = end - offset; |
---|
[4830] | 53 | this->strings[i] = new char[l + 1]; |
---|
[4220] | 54 | assert( strings[i] != NULL); |
---|
| 55 | strncpy( strings[i], offset, l); |
---|
| 56 | strings[i][l] = 0; |
---|
| 57 | i++; |
---|
| 58 | end++; |
---|
| 59 | offset = end; |
---|
[4734] | 60 | end = strchr( offset, splitter); |
---|
[4220] | 61 | } |
---|
[4597] | 62 | |
---|
[4220] | 63 | strings[i] = new char[l + 1]; |
---|
| 64 | l = strlen( offset); |
---|
| 65 | strncpy( strings[i], offset, l); |
---|
| 66 | strings[i][l] = 0; |
---|
| 67 | } |
---|
| 68 | |
---|
[3941] | 69 | /** |
---|
[4836] | 70 | * removes the object from memory |
---|
[3941] | 71 | */ |
---|
[4220] | 72 | SubString::~SubString() |
---|
| 73 | { |
---|
[4830] | 74 | for( int i = 0; i < this->splittersCount; i++) |
---|
[4220] | 75 | { |
---|
[4830] | 76 | delete this->strings[i]; |
---|
[4220] | 77 | } |
---|
[4597] | 78 | |
---|
[4830] | 79 | delete this->strings; |
---|
[4220] | 80 | } |
---|
[3941] | 81 | |
---|
| 82 | /** |
---|
[4836] | 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 |
---|
[4220] | 86 | */ |
---|
| 87 | const char* SubString::getString( int i) |
---|
| 88 | { |
---|
[4830] | 89 | if( i < this->splittersCount && i >= 0) |
---|
| 90 | return this->strings[i]; |
---|
| 91 | else |
---|
| 92 | return NULL; |
---|
[4220] | 93 | } |
---|
[4833] | 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 | } |
---|