[3941] | 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: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | \brief breaks a string into parts that were initially seperated by comma |
---|
| 19 | \param string the string to break into substrings |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "substring.h" |
---|
| 23 | |
---|
| 24 | SubString::SubString( const char* string) |
---|
| 25 | { |
---|
| 26 | n = 0; |
---|
| 27 | |
---|
| 28 | assert( string != NULL); |
---|
| 29 | |
---|
| 30 | for( int i = 0; i < strlen(string); i++) if( string[i] == ',') n++; |
---|
| 31 | |
---|
| 32 | n += 1; |
---|
| 33 | |
---|
| 34 | strings = new char*[n]; |
---|
| 35 | |
---|
| 36 | assert( strings != NULL); |
---|
| 37 | |
---|
| 38 | int i = 0; |
---|
| 39 | int l = 0; |
---|
| 40 | |
---|
| 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; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | \brief removes the object from memory |
---|
| 65 | */ |
---|
| 66 | SubString::~SubString() |
---|
| 67 | { |
---|
| 68 | for( int i = 0; i < n; i++) |
---|
| 69 | { |
---|
| 70 | delete strings[i]; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | delete strings; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | \brief get the amount of substrings |
---|
| 78 | \returns the amount of substrings |
---|
| 79 | */ |
---|
| 80 | int SubString::getN() |
---|
| 81 | { |
---|
| 82 | return n; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | \brief get a particular substring |
---|
| 87 | \param i the ID of the substring to return |
---|
| 88 | \returns the designated substring or NULL if an invalid ID was given |
---|
| 89 | */ |
---|
| 90 | const char* SubString::getString( int i) |
---|
| 91 | { |
---|
| 92 | if( i < n && i >= 0) return strings[i]; |
---|
| 93 | else return NULL; |
---|
| 94 | } |
---|