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