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