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 | * Remove a string from the array
|
---|
57 | */
|
---|
58 | void stringArray::remove(string member)
|
---|
59 | { int memberPos;
|
---|
60 |
|
---|
61 | memberPos = this->indexOf(member);
|
---|
62 | if (memberPos >= 0)
|
---|
63 | { this->array.erase(this->array.begin() + memberPos);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | void stringArray::remove(char *member)
|
---|
68 | { string removeMember(member);
|
---|
69 | this->remove(removeMember);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Check if the array includes the given string
|
---|
74 | */
|
---|
75 | bool stringArray::includes(string member)
|
---|
76 | { return (this->indexOf(member) >= 0);
|
---|
77 | }
|
---|
78 |
|
---|
79 | bool stringArray::includes(char *member)
|
---|
80 | { return (this->indexOf(member) >= 0);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Return an index of the location of the given string into the array.
|
---|
85 | * -1 indicates that it does not exist
|
---|
86 | */
|
---|
87 | int stringArray::indexOf(string member)
|
---|
88 | { vector<string>::iterator here = this->array.begin();
|
---|
89 | vector<string>::iterator end = this->array.end();
|
---|
90 |
|
---|
91 | int i = 0;
|
---|
92 | while (here != end)
|
---|
93 | { if (here->compare(member) == 0)
|
---|
94 | { return i;
|
---|
95 | }
|
---|
96 | here ++;
|
---|
97 | i ++;
|
---|
98 | }
|
---|
99 | return -1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | int stringArray::indexOf(char *member)
|
---|
103 | { string memberString(member);
|
---|
104 | return this->indexOf(memberString);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Write the array into a c string, separated by commas
|
---|
109 | */
|
---|
110 | int stringArray::writeToCString(char *buffer, char *divider, int bufflen)
|
---|
111 | { int i = 0;
|
---|
112 | vector<string>::iterator here = this->array.begin();
|
---|
113 | vector<string>::iterator end = this->array.end();
|
---|
114 | buffer[0] = '\0';
|
---|
115 |
|
---|
116 | while (here != end)
|
---|
117 | { const char *hereCString;
|
---|
118 |
|
---|
119 | hereCString = here->c_str();
|
---|
120 |
|
---|
121 | if ((strlen(hereCString) + (i > 0 ? 1 : 0)) >= bufflen)
|
---|
122 | break;
|
---|
123 |
|
---|
124 | if (i > 0)
|
---|
125 | { strcat(buffer, divider);
|
---|
126 | bufflen -= strlen(divider);
|
---|
127 | }
|
---|
128 | strcat(buffer, hereCString);
|
---|
129 | bufflen -= strlen(hereCString);
|
---|
130 | i ++;
|
---|
131 | here ++;
|
---|
132 | }
|
---|
133 | return i;
|
---|
134 | }
|
---|
135 |
|
---|
136 | stringArray stringArray::words(char *buffer)
|
---|
137 | { stringArray reply;
|
---|
138 | unsigned int at = 0;
|
---|
139 | unsigned int start;
|
---|
140 |
|
---|
141 | while (buffer[at] != '\0' && buffer[at] <= ' ')
|
---|
142 | { at ++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | while (buffer[at] != '\0')
|
---|
146 | { start = at;
|
---|
147 | while (buffer[at] > ' ' && buffer[at] != '\0')
|
---|
148 | { at ++;
|
---|
149 | }
|
---|
150 | string newWord(buffer, start, at - start);
|
---|
151 | reply.add(newWord);
|
---|
152 |
|
---|
153 | while (buffer[at] <= ' ' && buffer[at] != '\0')
|
---|
154 | { at ++;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | return reply;
|
---|
158 | }
|
---|
159 |
|
---|
160 | string &stringArray::operator[] (const unsigned int a)
|
---|
161 | { return this->array[a];
|
---|
162 | }
|
---|
163 |
|
---|
164 | void stringArray::clear()
|
---|
165 | { this->array.empty();
|
---|
166 | }
|
---|
167 |
|
---|