source: other-projects/gs2-export-cdrom-installer/trunk/stringArray.cpp@ 21709

Last change on this file since 21709 was 11664, checked in by mdewsnip, 18 years ago

Fixed the line endings... for real this time, I hope.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1#include "stringArray.h"
2
3/**
4 * Constructor: create a new string array
5 */
6stringArray::stringArray()
7{
8}
9
10stringArray::stringArray(char *instring, char *divider)
11{ char *parse, *next;
12 int count = 1;
13
14 parse = instring;
15 while ((next = strstr(parse, divider)) != NULL)
16 { if (next != parse)
17 { string newString(parse, 0, next-parse);
18 this->array.push_back(newString);
19 }
20 parse = next + strlen(divider);
21 }
22 if (strlen(parse) > 0)
23 { string newString(parse);
24 this->array.push_back(parse);
25 }
26}
27
28void stringArray::permitDuplication(bool permit)
29{ this->permitDups = permit;
30}
31
32/**
33 * Add a new string to the array
34 */
35void stringArray::add(string member)
36{ if (this->permitDups || !this->includes(member))
37 { this->array.push_back(member);
38 }
39}
40
41void stringArray::add(char *member)
42{ string newString(member);
43 this->add(newString);
44}
45
46void stringArray::add(stringArray &other)
47{ vector<string>::iterator here = other.array.begin();
48 vector<string>::iterator end = other.array.end();
49
50 while (here != end)
51 { this->add(*here);
52 here ++;
53 }
54 for (int i = 0; i < other.size(); i ++)
55 { this->add(other[i]);
56 }
57}
58
59/**
60 * Insert a string at a given location
61 */
62void stringArray::insertAt(string s, unsigned int at)
63{ if (at >= this->size())
64 { this->add(s);
65 }
66 else
67 { this->array.insert(this->array.begin() + at, s);
68 }
69}
70
71/**
72 * Remove a string from the array
73 */
74void stringArray::remove(string member)
75{ int memberPos;
76
77 memberPos = this->indexOf(member);
78 if (memberPos >= 0)
79 { this->array.erase(this->array.begin() + memberPos);
80 }
81}
82
83void stringArray::remove(char *member)
84{ string removeMember(member);
85 this->remove(removeMember);
86}
87
88/**
89 * Check if the array includes the given string
90 */
91bool stringArray::includes(string member)
92{ return (this->indexOf(member) >= 0);
93}
94
95bool stringArray::includes(char *member)
96{ return (this->indexOf(member) >= 0);
97}
98
99/**
100 * Return an index of the location of the given string into the array.
101 * -1 indicates that it does not exist
102 */
103int stringArray::indexOf(string member)
104{ vector<string>::iterator here = this->array.begin();
105 vector<string>::iterator end = this->array.end();
106
107 int i = 0;
108 while (here != end)
109 { if (here->compare(member) == 0)
110 { return i;
111 }
112 here ++;
113 i ++;
114 }
115 return -1;
116}
117
118int stringArray::indexOf(char *member)
119{ string memberString(member);
120 return this->indexOf(memberString);
121}
122
123/**
124 * Write the array into a c string, separated by commas
125 */
126int stringArray::writeToCString(char *buffer, char *divider, int bufflen)
127{ int i = 0;
128 vector<string>::iterator here = this->array.begin();
129 vector<string>::iterator end = this->array.end();
130 buffer[0] = '\0';
131
132 while (here != end)
133 { const char *hereCString;
134
135 hereCString = here->c_str();
136
137 if ((strlen(hereCString) + (i > 0 ? 1 : 0)) >= bufflen)
138 break;
139
140 if (i > 0)
141 { strcat(buffer, divider);
142 bufflen -= strlen(divider);
143 }
144 strcat(buffer, hereCString);
145 bufflen -= strlen(hereCString);
146 i ++;
147 here ++;
148 }
149 return i;
150}
151
152string stringArray::toString(string separator)
153{ string reply;
154
155 vector<string>::iterator here = this->array.begin();
156 vector<string>::iterator end = this->array.end();
157 while (here != end)
158 { if (here != this->array.begin())
159 { reply += separator;
160 }
161 reply += *here;
162 }
163 return reply;
164}
165
166stringArray stringArray::words(char *buffer)
167{ stringArray reply;
168 unsigned int at = 0;
169 unsigned int start;
170
171 while (buffer[at] != '\0' && buffer[at] <= ' ')
172 { at ++;
173 }
174
175 while (buffer[at] != '\0')
176 { start = at;
177 while (buffer[at] > ' ' && buffer[at] != '\0')
178 { at ++;
179 }
180 string newWord(buffer, start, at - start);
181 reply.add(newWord);
182
183 while (buffer[at] <= ' ' && buffer[at] != '\0')
184 { at ++;
185 }
186 }
187 return reply;
188}
189
190string &stringArray::operator[] (const unsigned int a)
191{ return this->array[a];
192}
193
194void stringArray::clear()
195{ this->array.empty();
196 this->array.erase(this->array.begin(), this->array.end());
197}
198
Note: See TracBrowser for help on using the repository browser.