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 | * 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 "debug.h" |
---|
27 | #include <string.h> |
---|
28 | #include <assert.h> |
---|
29 | |
---|
30 | SubString::SubString( const char* string, char splitter) |
---|
31 | { |
---|
32 | this->splittersCount = 0; |
---|
33 | if (string == NULL) |
---|
34 | { |
---|
35 | this->strings = NULL; |
---|
36 | return; |
---|
37 | } |
---|
38 | |
---|
39 | for( int i = 0; i < strlen(string); i++) |
---|
40 | if( string[i] == splitter) |
---|
41 | this->splittersCount++; |
---|
42 | |
---|
43 | this->splittersCount += 1; |
---|
44 | |
---|
45 | this->strings = new char*[this->splittersCount]; |
---|
46 | assert (strings != NULL); |
---|
47 | |
---|
48 | int i = 0; |
---|
49 | int l = 0; |
---|
50 | |
---|
51 | if( this->splittersCount > 1) |
---|
52 | { |
---|
53 | const char* offset = string; |
---|
54 | const char* end = strchr( string, splitter); |
---|
55 | while( end != NULL) |
---|
56 | { |
---|
57 | assert( i < this->splittersCount); |
---|
58 | l = end - offset; |
---|
59 | this->strings[i] = new char[l + 1]; |
---|
60 | assert( strings[i] != NULL); |
---|
61 | strncpy( strings[i], offset, l); |
---|
62 | strings[i][l] = '\0'; |
---|
63 | i++; |
---|
64 | end++; |
---|
65 | offset = end; |
---|
66 | end = strchr( offset, splitter); |
---|
67 | } |
---|
68 | |
---|
69 | strings[i] = new char[l + 1]; |
---|
70 | l = strlen( offset); |
---|
71 | strncpy( strings[i], offset, l); |
---|
72 | strings[i][l] = '\0'; |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | this->strings[0] = new char[strlen(string)+1]; |
---|
77 | strcpy(this->strings[0], string); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | * Splits a String into a Substring removing all whiteSpaces |
---|
84 | * @param string the String to Split |
---|
85 | * @param whiteSpaces MUST BE __TRUE__ |
---|
86 | * |
---|
87 | */ |
---|
88 | SubString::SubString(const char* string, bool whiteSpaces) |
---|
89 | { |
---|
90 | this->splittersCount = 0; |
---|
91 | if (string == NULL || whiteSpaces == false) |
---|
92 | { |
---|
93 | this->strings = NULL; |
---|
94 | return; |
---|
95 | } |
---|
96 | |
---|
97 | // chop the input to the beginning of something usefull |
---|
98 | if (strlen(string) > 0) |
---|
99 | string = string + strspn(string, " \t\n"); |
---|
100 | |
---|
101 | // count the Splitters |
---|
102 | bool lastWasWhiteSpace = false; |
---|
103 | for(unsigned int i = 0; i < strlen(string); i++) |
---|
104 | if( string[i] == ' ' || string[i] == '\t' || string[i] == '\n' ) |
---|
105 | lastWasWhiteSpace = true; |
---|
106 | else |
---|
107 | { |
---|
108 | if (lastWasWhiteSpace) |
---|
109 | this->splittersCount ++; |
---|
110 | lastWasWhiteSpace = false; |
---|
111 | } |
---|
112 | |
---|
113 | this->splittersCount += 1; |
---|
114 | |
---|
115 | printf("splitterCount = %d\n", this->splittersCount); |
---|
116 | |
---|
117 | this->strings = new char*[this->splittersCount]; |
---|
118 | assert (strings != NULL); |
---|
119 | |
---|
120 | int l = 0; |
---|
121 | unsigned int i = 0; |
---|
122 | if( this->splittersCount > 1) |
---|
123 | { |
---|
124 | const char* offset = string; |
---|
125 | const char* end = offset + strcspn(offset, " \t\n"); |
---|
126 | for (i = 0; i < this->splittersCount; i++) |
---|
127 | { |
---|
128 | assert( i < this->splittersCount); |
---|
129 | l = end - offset; |
---|
130 | this->strings[i] = new char[l + 1]; |
---|
131 | assert( strings[i] != NULL); |
---|
132 | strncpy( strings[i], offset, l); |
---|
133 | strings[i][l] = '\0'; |
---|
134 | end += strspn(end, " \t\n"); |
---|
135 | offset = end; |
---|
136 | printf("'%s'\n", end); |
---|
137 | end = offset + strcspn(offset, " \t\n"); |
---|
138 | } |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | unsigned int length = strcspn(string, " \t\n"); |
---|
143 | this->strings[0] = new char[length+1]; |
---|
144 | strncpy(this->strings[0], string, length); |
---|
145 | this->strings[0][length] = '\0'; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * removes the object from memory |
---|
151 | */ |
---|
152 | SubString::~SubString() |
---|
153 | { |
---|
154 | if (this->strings) |
---|
155 | { |
---|
156 | for(unsigned int i = 0; i < this->splittersCount; i++) |
---|
157 | delete[] this->strings[i]; |
---|
158 | delete[] this->strings; |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * get a particular substring |
---|
164 | * @param i the ID of the substring to return |
---|
165 | * @returns the designated substring or NULL if an invalid ID was given |
---|
166 | */ |
---|
167 | const char* SubString::getString(unsigned int i) |
---|
168 | { |
---|
169 | if( i < this->splittersCount && i >= 0) |
---|
170 | return this->strings[i]; |
---|
171 | else |
---|
172 | return NULL; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Some nice debug information about this SubString |
---|
177 | */ |
---|
178 | void SubString::debug() const |
---|
179 | { |
---|
180 | PRINT(0)("Substring-information::count=%d ::", this->splittersCount); |
---|
181 | if (this->strings != NULL) |
---|
182 | for (unsigned int i = 0; i < this->splittersCount; i++) |
---|
183 | PRINT(0)("s%d='%s'::", i, this->strings[i]); |
---|
184 | PRINT(0)("\n"); |
---|
185 | } |
---|