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 | 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)]; |
---|
77 | strcpy(this->strings[0], string); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * removes the object from memory |
---|
83 | */ |
---|
84 | SubString::~SubString() |
---|
85 | { |
---|
86 | if (this->strings) |
---|
87 | { |
---|
88 | for( int i = 0; i < this->splittersCount; i++) |
---|
89 | delete[] this->strings[i]; |
---|
90 | delete[] this->strings; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * get a particular substring |
---|
96 | * @param i the ID of the substring to return |
---|
97 | * @returns the designated substring or NULL if an invalid ID was given |
---|
98 | */ |
---|
99 | const char* SubString::getString(unsigned int i) |
---|
100 | { |
---|
101 | if( i < this->splittersCount && i >= 0) |
---|
102 | return this->strings[i]; |
---|
103 | else |
---|
104 | return NULL; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | * Some nice debug information about this SubString |
---|
111 | */ |
---|
112 | void SubString::debug() const |
---|
113 | { |
---|
114 | PRINT(0)("Substring-information::count=%d ::", this->splittersCount); |
---|
115 | for (int i = 0; i < this->splittersCount; i++) |
---|
116 | PRINT(0)("s%d:%s::", i, this->strings[i]); |
---|
117 | PRINT(0)("\n"); |
---|
118 | } |
---|