| 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 | #include <string.h> |
|---|
| 25 | #include <assert.h> |
|---|
| 26 | |
|---|
| 27 | SubString::SubString( const char* string) |
|---|
| 28 | { |
|---|
| 29 | n = 0; |
|---|
| 30 | |
|---|
| 31 | assert( string != NULL); |
|---|
| 32 | |
|---|
| 33 | for( int i = 0; i < strlen(string); i++) if( string[i] == ',') n++; |
|---|
| 34 | |
|---|
| 35 | n += 1; |
|---|
| 36 | |
|---|
| 37 | strings = new char*[n]; |
|---|
| 38 | |
|---|
| 39 | assert (strings != NULL); |
|---|
| 40 | |
|---|
| 41 | int i = 0; |
|---|
| 42 | int l = 0; |
|---|
| 43 | |
|---|
| 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 | } |
|---|
| 59 | |
|---|
| 60 | strings[i] = new char[l + 1]; |
|---|
| 61 | l = strlen( offset); |
|---|
| 62 | strncpy( strings[i], offset, l); |
|---|
| 63 | strings[i][l] = 0; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | \brief removes the object from memory |
|---|
| 68 | */ |
|---|
| 69 | SubString::~SubString() |
|---|
| 70 | { |
|---|
| 71 | for( int i = 0; i < n; i++) |
|---|
| 72 | { |
|---|
| 73 | delete strings[i]; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | delete strings; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | \brief get the amount of substrings |
|---|
| 81 | \returns the amount of substrings |
|---|
| 82 | */ |
|---|
| 83 | int SubString::getCount() |
|---|
| 84 | { |
|---|
| 85 | return n; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | \brief get a particular substring |
|---|
| 90 | \param i the ID of the substring to return |
|---|
| 91 | \returns the designated substring or NULL if an invalid ID was given |
|---|
| 92 | */ |
|---|
| 93 | const char* SubString::getString( int i) |
|---|
| 94 | { |
|---|
| 95 | if( i < n && i >= 0) return strings[i]; |
|---|
| 96 | else return NULL; |
|---|
| 97 | } |
|---|