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_(uint32_t 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(uint32_t len)
150
cs_realloc((void **) &iBuffer, len + 1);
156
uint32_t CSStringBuffer_::ignore(uint32_t pos, char ch)
158
while (pos < myStrLen && iBuffer[pos] == ch)
163
uint32_t CSStringBuffer_::find(uint32_t pos, char ch)
165
while (pos < myStrLen && iBuffer[pos] != ch)
170
uint32_t CSStringBuffer_::trim(uint32_t pos, char ch)
172
while (pos > 0 && iBuffer[pos-1] == ch)
177
CSString *CSStringBuffer_::substr(uint32_t pos, uint32_t 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, uint32_t 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;
207
uint32_t len_a, len_b;
211
len_b = cat_str->length();
212
new_str = clone(len_a + len_b);
215
for (uint32_t i=0; i<len_b; i++)
216
new_str->setCharAt(len_a+i, cat_str->charAt(i));
222
CSString *CSString::concat(const char *cat_str)
224
CSString *new_str = NULL;
225
uint32_t len_a, len_b;
229
len_b = strlen(cat_str);
230
new_str = clone(len_a + len_b);
233
for (uint32_t i=0; i<len_b; i++)
234
new_str->setCharAt(len_a+i, cat_str[i]);
240
CSString *CSString::toUpper()
242
CSString *new_str = NULL;
249
len = new_str->length();
250
for (uint32_t i=0; i<len; i++)
251
new_str->setCharAt(i, upperCharAt(i));
257
uint32_t CSString::hashKey()
259
register uint32_t h = 0, g;
261
for (uint32_t i=0; i<length(); i++) {
262
h = (h << 4) + (uint32_t) upperCharAt(i);
263
if ((g = (h & 0xF0000000)))
264
h = (h ^ (g >> 24)) ^ g;
270
uint32_t CSString::locate(const char *w_cstr, s_int count)
272
s_int len = length();
278
if (startsWith((uint32_t) i, w_cstr)) {
288
i = len - (s_int) strlen(w_cstr);
290
if (startsWith((uint32_t) i, w_cstr)) {
301
uint32_t CSString::locate(uint32_t pos, const char *w_cstr)
303
uint32_t len = length();
310
if (startsWith(i, w_cstr))
317
uint32_t CSString::locate(uint32_t pos, CS_CHAR ch)
319
uint32_t len = length();
333
uint32_t CSString::skip(uint32_t pos, CS_CHAR ch)
335
uint32_t len = length();
349
CSString *CSString::substr(uint32_t index, uint32_t size)
351
return clone(index, size);
354
CSString *CSString::substr(uint32_t index)
356
return clone(index, length() - index);
360
CSString *CSString::left(const char *w_cstr, s_int count)
362
uint32_t idx = locate(w_cstr, count);
364
if (idx == (uint32_t)-1)
365
return CSCString::newString("");
366
return substr(0, idx);
369
CSString *CSString::left(const char *w_cstr)
371
return left(w_cstr, 1);
374
CSString *CSString::right(const char *w_cstr, s_int count)
376
uint32_t idx = locate(w_cstr, count);
378
if (idx == (uint32_t)-1) {
384
return newString("");
386
return substr(idx + strlen(w_cstr));
389
CSString *CSString::right(const char *w_cstr)
391
return right(w_cstr, 1);
394
bool CSString::startsWith(const char *w_str)
396
return startsWith(0, w_str);
399
bool CSString::endsWith(const char *w_str)
401
return startsWith(length() - strlen(w_str), w_str);
404
uint32_t CSString::nextPos(uint32_t pos)
411
CSString *CSString::clone(uint32_t len)
413
return clone(0, len);
416
CSString *CSString::clone()
418
return clone(0, length());
421
bool CSString::equals(const char *str)
423
uint32_t len = length();
426
for (i=0; i<len && *str; i++) {
427
if (charAt(i) != *str)
431
return i==len && !*str;
435
* ---------------------------------------------------------------
439
CSCString::CSCString():
446
CSCString::~CSCString()
452
const char *CSCString::getCString()
457
CS_CHAR CSCString::charAt(uint32_t pos)
460
return (CS_CHAR) (unsigned char) myCString[pos];
464
CS_CHAR CSCString::upperCharAt(uint32_t pos)
467
return (CS_CHAR) (unsigned char) toupper(myCString[pos]);
471
void CSCString::setCharAt(uint32_t pos, CS_CHAR ch)
474
myCString[pos] = (unsigned char) ch;
477
int CSCString::compare(const char *val, uint32_t len)
479
const char *pa = myCString, *pb = val;
485
while (*pa && *pb && len) {
486
r = toupper(*pa) - toupper(*pb);
494
r = toupper(*pa) - toupper(*pb);
500
int CSCString::compare(CSString *val)
502
return compare(val->getCString(), (uint32_t)-1);
505
bool CSCString::startsWith(uint32_t index, const char *w_str)
507
uint32_t len = strlen(w_str);
510
if (index > myStrLen)
512
str = myCString + index;
513
for (uint32_t i=0; i<len && *str; i++) {
519
return (*w_str == 0);
522
void CSCString::setLength(uint32_t len)
524
cs_realloc((void **) &myCString, len+1);
529
CSString *CSCString::clone(uint32_t pos, uint32_t len)
531
CSCString *str = NULL;
534
new_(str, CSCString());
537
str->myCString = (char *) cs_malloc(len + 1);
541
if (len > myStrLen - pos) {
542
/* More space has been allocated than required.
543
* It may be that this space will be used up.
544
* Set the zero terminator at the end
547
str->myCString[len] = 0;
548
len = myStrLen - pos;
550
memcpy(str->myCString, myCString+pos, len);
551
str->myCString[len] = 0;
557
CSObject *CSCString::getKey()
559
return (CSObject *) this;
562
int CSCString::compareKey(CSObject *key)
564
return compare((CSString *) key);
567
CSCString *CSCString::newString(const char *cstr)
572
new_(str, CSCString());
574
str->myCString = cs_strdup(cstr);
575
str->myStrLen = strlen(cstr);
580
CSCString *CSCString::newString(const char *bytes, uint32_t len)
585
new_(str, CSCString());
588
str->myCString = (char *) cs_malloc(len+1);
589
memcpy(str->myCString, bytes, len);
590
str->myCString[len] = 0;
595
CSCString *CSCString::newString(CSStringBuffer *sb)
601
new_(str, CSCString());
603
str->myCString = sb->take();
604
str->myStrLen = sb->length();