#include "stringArray.h" /** * Constructor: create a new string array */ stringArray::stringArray() { } stringArray::stringArray(char *instring, char *divider) { char *parse, *next; int count = 1; parse = instring; while ((next = strstr(parse, divider)) != NULL) { if (next != parse) { string newString(parse, 0, next-parse); this->array.push_back(newString); } parse = next + strlen(divider); } if (strlen(parse) > 0) { string newString(parse); this->array.push_back(parse); } } void stringArray::permitDuplication(bool permit) { this->permitDups = permit; } /** * Add a new string to the array */ void stringArray::add(string member) { if (this->permitDups || !this->includes(member)) { this->array.push_back(member); } } void stringArray::add(char *member) { string newString(member); this->add(newString); } void stringArray::add(stringArray &other) { vector::iterator here = other.array.begin(); vector::iterator end = other.array.end(); while (here != end) { this->add(*here); here ++; } for (int i = 0; i < other.size(); i ++) { this->add(other[i]); } } /** * Insert a string at a given location */ void stringArray::insertAt(string s, unsigned int at) { if (at >= this->size()) { this->add(s); } else { this->array.insert(this->array.begin() + at, s); } } /** * Remove a string from the array */ void stringArray::remove(string member) { int memberPos; memberPos = this->indexOf(member); if (memberPos >= 0) { this->array.erase(this->array.begin() + memberPos); } } void stringArray::remove(char *member) { string removeMember(member); this->remove(removeMember); } /** * Check if the array includes the given string */ bool stringArray::includes(string member) { return (this->indexOf(member) >= 0); } bool stringArray::includes(char *member) { return (this->indexOf(member) >= 0); } /** * Return an index of the location of the given string into the array. * -1 indicates that it does not exist */ int stringArray::indexOf(string member) { vector::iterator here = this->array.begin(); vector::iterator end = this->array.end(); int i = 0; while (here != end) { if (here->compare(member) == 0) { return i; } here ++; i ++; } return -1; } int stringArray::indexOf(char *member) { string memberString(member); return this->indexOf(memberString); } /** * Write the array into a c string, separated by commas */ int stringArray::writeToCString(char *buffer, char *divider, int bufflen) { int i = 0; vector::iterator here = this->array.begin(); vector::iterator end = this->array.end(); buffer[0] = '\0'; while (here != end) { const char *hereCString; hereCString = here->c_str(); if ((strlen(hereCString) + (i > 0 ? 1 : 0)) >= bufflen) break; if (i > 0) { strcat(buffer, divider); bufflen -= strlen(divider); } strcat(buffer, hereCString); bufflen -= strlen(hereCString); i ++; here ++; } return i; } string stringArray::toString(string separator) { string reply; vector::iterator here = this->array.begin(); vector::iterator end = this->array.end(); while (here != end) { if (here != this->array.begin()) { reply += separator; } reply += *here; } return reply; } stringArray stringArray::words(char *buffer) { stringArray reply; unsigned int at = 0; unsigned int start; while (buffer[at] != '\0' && buffer[at] <= ' ') { at ++; } while (buffer[at] != '\0') { start = at; while (buffer[at] > ' ' && buffer[at] != '\0') { at ++; } string newWord(buffer, start, at - start); reply.add(newWord); while (buffer[at] <= ' ' && buffer[at] != '\0') { at ++; } } return reply; } string &stringArray::operator[] (const unsigned int a) { return this->array[a]; } void stringArray::clear() { this->array.empty(); this->array.erase(this->array.begin(), this->array.end()); }