1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
25
* Basic storage structures.
38
#include "CSStorage.h"
41
#include "CSStrUtil.h"
46
* ---------------------------------------------------------------
47
* Core System String Buffers
50
CSStringBuffer_::CSStringBuffer_():
59
CSStringBuffer_::CSStringBuffer_(u_int grow):
68
CSStringBuffer_::~CSStringBuffer_()
73
void CSStringBuffer_::clear()
82
void CSStringBuffer_::append(char ch)
84
if (iSize == myStrLen) {
85
cs_realloc((void **) &iBuffer, iSize + iGrow);
88
iBuffer[myStrLen] = ch;
92
void CSStringBuffer_::append(const char *str, size_t len)
94
if (myStrLen + len > iSize) {
99
cs_realloc((void **) &iBuffer, iSize + add);
102
memcpy(iBuffer + myStrLen, str, len);
106
void CSStringBuffer_::append(int value)
110
snprintf(buffer, 100, "%"PRId32"", value);
114
void CSStringBuffer_::append(uint32_t value)
118
snprintf(buffer, 100, "%"PRIu32"", value);
122
char *CSStringBuffer_::getCString()
124
if (iSize == myStrLen) {
125
cs_realloc((void **) &iBuffer, iSize + 1);
128
iBuffer[myStrLen] = 0;
132
char *CSStringBuffer_::take()
136
cs_realloc((void **) &iBuffer, myStrLen + 1);
137
iSize = myStrLen + 1;
138
iBuffer[myStrLen] = 0;
147
void CSStringBuffer_::setLength(u_int len)
150
cs_realloc((void **) &iBuffer, len + 1);
156
u_int CSStringBuffer_::ignore(u_int pos, char ch)
158
while (pos < myStrLen && iBuffer[pos] == ch)
163
u_int CSStringBuffer_::find(u_int pos, char ch)
165
while (pos < myStrLen && iBuffer[pos] != ch)
170
u_int CSStringBuffer_::trim(u_int pos, char ch)
172
while (pos > 0 && iBuffer[pos-1] == ch)
177
CSString *CSStringBuffer_::substr(u_int pos, u_int len)
179
CSString *s = CSString::newString(iBuffer + pos, len);
185
* ---------------------------------------------------------------
189
CSString *CSString::newString(const char *cstr)
191
return CSCString::newString(cstr);
194
CSString *CSString::newString(const char *bytes, u_int len)
196
return CSCString::newString(bytes, len);
199
CSString *CSString::newString(CSStringBuffer *sb)
201
return CSCString::newString(sb);
204
CSString *CSString::concat(CSString *cat_str)
206
CSString *new_str = NULL;
211
len_b = cat_str->length();
212
new_str = clone(len_a + len_b);
214
for (u_int i=0; i<len_b; i++)
215
new_str->setCharAt(len_a+i, cat_str->charAt(i));
225
CSString *CSString::concat(const char *cat_str)
227
CSString *new_str = NULL;
232
len_b = strlen(cat_str);
233
new_str = clone(len_a + len_b);
235
for (u_int i=0; i<len_b; i++)
236
new_str->setCharAt(len_a+i, cat_str[i]);
246
CSString *CSString::toUpper()
248
CSString *new_str = NULL;
252
len = new_str->length();
254
for (u_int i=0; i<len; i++)
255
new_str->setCharAt(i, upperCharAt(i));
265
u_int CSString::hashKey()
267
register u_int h = 0, g;
269
for (u_int i=0; i<length(); i++) {
270
h = (h << 4) + (u_int) upperCharAt(i);
271
if ((g = (h & 0xF0000000)))
272
h = (h ^ (g >> 24)) ^ g;
278
u_int CSString::locate(const char *w_cstr, s_int count)
280
s_int len = length();
286
if (startsWith((u_int) i, w_cstr)) {
296
i = len - (s_int) strlen(w_cstr);
298
if (startsWith((u_int) i, w_cstr)) {
309
u_int CSString::locate(u_int pos, const char *w_cstr)
311
u_int len = length();
318
if (startsWith(i, w_cstr))
325
u_int CSString::locate(u_int pos, CS_CHAR ch)
327
u_int len = length();
341
u_int CSString::skip(u_int pos, CS_CHAR ch)
343
u_int len = length();
357
CSString *CSString::substr(u_int index, u_int size)
359
return clone(index, size);
362
CSString *CSString::substr(u_int index)
364
return clone(index, length() - index);
368
CSString *CSString::left(const char *w_cstr, s_int count)
370
u_int idx = locate(w_cstr, count);
372
if (idx == (u_int)-1)
373
return CSCString::newString("");
374
return substr(0, idx);
377
CSString *CSString::left(const char *w_cstr)
379
return left(w_cstr, 1);
382
CSString *CSString::right(const char *w_cstr, s_int count)
384
u_int idx = locate(w_cstr, count);
386
if (idx == (u_int)-1) {
392
return newString("");
394
return substr(idx + strlen(w_cstr));
397
CSString *CSString::right(const char *w_cstr)
399
return right(w_cstr, 1);
402
bool CSString::startsWith(const char *w_str)
404
return startsWith(0, w_str);
407
bool CSString::endsWith(const char *w_str)
409
return startsWith(length() - strlen(w_str), w_str);
412
u_int CSString::nextPos(u_int pos)
419
CSString *CSString::clone(u_int len)
421
return clone(0, len);
424
CSString *CSString::clone()
426
return clone(0, length());
429
bool CSString::equals(const char *str)
431
u_int len = length();
434
for (i=0; i<len && *str; i++) {
435
if (charAt(i) != *str)
439
return i==len && !*str;
443
* ---------------------------------------------------------------
447
CSCString::CSCString():
454
CSCString::~CSCString()
460
const char *CSCString::getCString()
465
CS_CHAR CSCString::charAt(u_int pos)
468
return (CS_CHAR) (unsigned char) myCString[pos];
472
CS_CHAR CSCString::upperCharAt(u_int pos)
475
return (CS_CHAR) (unsigned char) toupper(myCString[pos]);
479
void CSCString::setCharAt(u_int pos, CS_CHAR ch)
482
myCString[pos] = (unsigned char) ch;
485
int CSCString::compare(const char *val, u_int len)
487
const char *pa = myCString, *pb = val;
493
while (*pa && *pb && len) {
494
r = toupper(*pa) - toupper(*pb);
502
r = toupper(*pa) - toupper(*pb);
508
int CSCString::compare(CSString *val)
510
return compare(val->getCString(), (u_int)-1);
513
bool CSCString::startsWith(u_int index, const char *w_str)
515
u_int len = strlen(w_str);
518
if (index > myStrLen)
520
str = myCString + index;
521
for (u_int i=0; i<len && *str; i++) {
527
return (*w_str == 0);
530
void CSCString::setLength(u_int len)
532
cs_realloc((void **) &myCString, len+1);
537
CSString *CSCString::clone(u_int pos, u_int len)
539
CSCString *str = NULL;
542
new_(str, CSCString());
544
str->myCString = (char *) cs_malloc(len + 1);
548
if (len > myStrLen - pos) {
549
/* More space has been allocated than required.
550
* It may be that this space will be used up.
551
* Set the zero terminator at the end
554
str->myCString[len] = 0;
555
len = myStrLen - pos;
557
memcpy(str->myCString, myCString+pos, len);
558
str->myCString[len] = 0;
568
CSObject *CSCString::getKey()
570
return (CSObject *) this;
573
int CSCString::compareKey(CSObject *key)
575
return compare((CSString *) key);
578
CSString *CSCString::newString(const char *cstr)
583
new_(str, CSCString());
585
str->myCString = cs_strdup(cstr);
586
str->myStrLen = strlen(cstr);
588
return_((CSString *) str);
591
CSString *CSCString::newString(const char *bytes, u_int len)
596
new_(str, CSCString());
599
str->myCString = (char *) cs_malloc(len+1);
600
memcpy(str->myCString, bytes, len);
601
str->myCString[len] = 0;
603
return_((CSString *) str);
606
CSString *CSCString::newString(CSStringBuffer *sb)
612
new_(str, CSCString());
614
str->myCString = sb->take();
615
str->myStrLen = sb->length();
618
return_((CSString *) str);