18 #include "StringArray.h" 25 int StringArray::alloc = 32;
26 bool StringArray::lazyMemoryManagement =
false;
28 StringArray::StringArray(
int startsize)
31 size = (startsize + alloc) / alloc * alloc;
32 strings =
new String * [size];
33 for (
int i = 0; i < count; i++)
35 for (
int i = count; i < size; i++)
42 size = (rhs.count + alloc) / alloc * alloc;
43 strings =
new String * [size];
45 for (
int i = 0; i < count; i++)
46 strings[i] =
new String(rhs[i]);;
47 for (
int i = count; i < size; i++)
51 StringArray::~StringArray()
53 for (
int i = 0; i < size; i++)
54 if (strings[i] != NULL)
62 int StringArray::CharLength()
65 for (
int i = 0; i < count; i++)
66 charlen += strings[i]->Length();
70 void StringArray::Read(
const char * filename)
73 if (f == NULL)
return;
78 void StringArray::Write(
const char * filename)
80 FILE * f = fopen(filename,
"wt");
81 if (f == NULL)
return;
86 void StringArray::WriteLine(
const char * filename)
88 FILE * f = fopen(filename,
"wt");
89 if (f == NULL)
return;
94 void StringArray::Read(FILE * f)
99 if (strings[count] == NULL)
100 strings[count] =
new String;
101 strings[count]->ReadLine(f);
106 void StringArray::Write(FILE * f)
108 for (
int i = 0; i < count; i++)
109 strings[i]->WriteLine(f);
112 void StringArray::WriteLine(FILE * f)
114 for (
int i = 0; i < count; i++)
115 fprintf(f,
"%s%c", (
const char *)(*strings[i]), i == count-1 ?
'\n' :
'\t');
118 void StringArray::Read(
IFILE & f)
123 if (strings[count] == NULL)
124 strings[count] =
new String;
125 strings[count]->ReadLine(f);
126 if (
ifeof(f) && strings[count]->Length()==0)
134 void StringArray::Grow(
int newsize)
140 if ((newsize >> 1) >= size)
141 size = (newsize + alloc) / alloc * alloc;
145 while (size <= newsize)
149 for (
int i = 0; i < oldsize; i++)
151 for (
int i = oldsize; i < size; i++)
158 void StringArray::Clear()
160 if (!lazyMemoryManagement)
162 for (
int i = 0; i < size; i++)
164 if (strings[i] != NULL)
178 int StringArray::AddColumns(
const String & s,
char ch)
181 for (
int pos = 0; pos <= s.Length(); pos++)
184 pos = s.FindChar(ch, pos);
185 if (pos == -1) pos = s.Length();
188 if (strings[count] == NULL)
190 strings[count] =
new String(pos - oldpos);
192 strings[count]->SetLength(pos - oldpos);
193 memcpy((
char *) *strings[count++], ((
const char *) s) + oldpos, pos - oldpos);
199 int StringArray::AddColumns(
const String & s,
char ch,
int maxColumns)
204 for (
int pos = 0; pos <= s.Length() && maxColumns != count; pos++)
207 pos = s.FindChar(ch, pos);
208 if (pos == -1) pos = s.Length();
211 if (strings[count] == NULL)
212 strings[count] =
new String(pos - oldpos);
213 strings[count]->SetLength(pos - oldpos);
214 memcpy((
char *) *strings[count++], ((
const char *) s) + oldpos, pos - oldpos);
220 int StringArray::AddTokens(
const String & s,
char ch)
222 for (
int pos = 0; pos < s.Length(); pos++)
224 while (pos < s.Length() && s[pos] == ch) pos++;
227 while (pos < s.Length() && s[pos] != ch) pos++;
229 if (oldpos < s.Length())
232 if (strings[count] == NULL)
234 strings[count] =
new String(pos - oldpos);
236 strings[count]->SetLength(pos - oldpos);
237 memcpy((
char *) *strings[count++], (
const char *) s + oldpos, pos - oldpos);
244 int StringArray::AddTokens(
const String & s,
const String & separators)
246 for (
int pos = 0; pos < s.Length(); pos++)
248 while (pos < s.Length() && separators.FindChar(s[pos]) != -1) pos++;
251 while (pos < s.Length() && separators.FindChar(s[pos]) == -1) pos++;
253 if (oldpos < s.Length())
256 if (strings[count] == NULL)
257 strings[count] =
new String(pos - oldpos);
258 strings[count]->SetLength(pos - oldpos);
259 memcpy((
char *) *strings[count++], ((
const char *) s) + oldpos, pos - oldpos);
266 int StringArray::Dimension(
int newcount)
268 if (newcount > count)
271 for (
int i = count; i < newcount; i++)
273 if (strings[i] == NULL)
280 else if (newcount < count)
282 if (!lazyMemoryManagement)
284 for (
int i = newcount; i < size; i++)
286 if (strings[i] != NULL)
303 int StringArray::Find(
const String & s)
const 305 for (
int i = 0; i < count; i++)
306 if (*(strings[i]) == s)
311 int StringArray::FastFind(
const String & s)
const 313 for (
int i = 0; i < count; i++)
314 if (strings[i]->FastCompare(s) == 0)
319 int StringArray::SlowFind(
const String & s)
const 321 for (
int i = 0; i < count; i++)
322 if (strings[i]->SlowCompare(s) == 0)
327 int StringArray::Add(
const String & s)
330 if (strings[count] == NULL)
332 strings[count] =
new String(s);
341 void StringArray::InsertAt(
int position,
const String & s)
345 String * newString = strings[count];
346 if (newString == NULL)
347 newString =
new String(s);
351 for (
int i = count; i > position; i--)
352 strings[i] = strings[i - 1];
353 strings[position] = newString;
357 String & StringArray::Last()
const 359 if (!count) error(
"StringArray: Null String Access");
360 return *(strings[count - 1]);
363 void StringArray::Delete(
int index)
365 String * oldString = strings[index];
368 for (; index < count; index++)
369 strings[index] = strings[index + 1];
370 strings[count] = oldString;
375 Dimension(rhs.count);
376 for (
int i = 0; i < rhs.count; i++)
377 *strings[i] = *rhs.strings[i];
381 bool StringArray::operator == (
const StringArray & rhs)
const 383 if (count != rhs.count)
return false;
384 for (
int i = 0; i < rhs.count; i++)
385 if (*strings[i] != *rhs.strings[i])
390 void StringArray::Sort()
392 QuickSort(strings, count,
sizeof(
String *), ComparisonForSort);
395 int StringArray::ComparisonForSort(
const void * a,
const void * b)
400 return Compare(*string1, *string2);
405 String result = *(strings[count - 1]);
407 Dimension(count - 1);
412 void StringArray::Trim()
414 for (
int i = 0; i < count; i++)
418 void StringArray::Print()
423 void StringArray::Print(FILE * output)
425 for (
int i = 0; i < count; i++)
426 fprintf(output,
"%s\n", (
const char *)(*strings[i]));
429 void StringArray::PrintLine()
434 void StringArray::PrintLine(FILE * output)
436 for (
int i = 0; i < count; i++)
437 fprintf(output,
"%s%c", (
const char *)(*strings[i]), i == count - 1 ?
'\n' :
'\t');
442 String ** temp = s.strings;