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