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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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
CSStringBufferImpl::CSStringBufferImpl():
59
CSStringBufferImpl::CSStringBufferImpl(uint32_t grow):
68
CSStringBufferImpl::~CSStringBufferImpl()
73
void CSStringBufferImpl::clear()
82
void CSStringBufferImpl::append(char ch)
84
if (iSize == myStrLen) {
85
cs_realloc((void **) &iBuffer, iSize + iGrow);
88
iBuffer[myStrLen] = ch;
92
void CSStringBufferImpl::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 CSStringBufferImpl::append(int value)
110
snprintf(buffer, 100, "%d", value);
114
void CSStringBufferImpl::append(uint32_t value)
118
snprintf(buffer, 100, "%"PRIu32, value);
122
void CSStringBufferImpl::append(uint64_t value)
126
snprintf(buffer, 100, "%"PRIu64, value);
130
char *CSStringBufferImpl::getCString()
132
if (iSize == myStrLen) {
133
cs_realloc((void **) &iBuffer, iSize + 1);
136
iBuffer[myStrLen] = 0;
140
char *CSStringBufferImpl::take()
144
cs_realloc((void **) &iBuffer, myStrLen + 1);
145
iSize = myStrLen + 1;
146
iBuffer[myStrLen] = 0;
155
void CSStringBufferImpl::setLength(uint32_t len)
158
cs_realloc((void **) &iBuffer, len + 1);
164
uint32_t CSStringBufferImpl::ignore(uint32_t pos, char ch)
166
while (pos < myStrLen && iBuffer[pos] == ch)
171
uint32_t CSStringBufferImpl::find(uint32_t pos, char ch)
173
while (pos < myStrLen && iBuffer[pos] != ch)
178
uint32_t CSStringBufferImpl::trim(uint32_t pos, char ch)
180
while (pos > 0 && iBuffer[pos-1] == ch)
185
CSString *CSStringBufferImpl::substr(uint32_t pos, uint32_t len)
187
CSString *s = CSString::newString(iBuffer + pos, len);
193
* ---------------------------------------------------------------
197
CSString *CSString::concat(CSString *cat_str)
199
CSString *new_str = NULL;
200
uint32_t len_a, len_b;
204
len_b = cat_str->length();
205
new_str = clone(len_a + len_b);
208
for (uint32_t i=0; i<len_b; i++)
209
new_str->setCharAt(len_a+i, cat_str->charAt(i));
215
CSString *CSString::concat(const char *cat_str)
217
CSString *new_str = NULL;
218
uint32_t len_a, len_b;
222
len_b = strlen(cat_str);
223
new_str = clone(len_a + len_b);
226
for (uint32_t i=0; i<len_b; i++)
227
new_str->setCharAt(len_a+i, cat_str[i]);
233
CSString *CSString::toUpper()
235
CSString *new_str = NULL;
242
len = new_str->length();
243
for (uint32_t i=0; i<len; i++)
244
new_str->setCharAt(i, upperCharAt(i));
250
uint32_t CSString::hashKey()
252
register uint32_t h = 0, g;
254
for (uint32_t i=0; i<length(); i++) {
255
h = (h << 4) + (uint32_t) upperCharAt(i);
256
if ((g = (h & 0xF0000000)))
257
h = (h ^ (g >> 24)) ^ g;
263
uint32_t CSString::locate(const char *w_cstr, int32_t count)
265
int32_t len = length();
271
if (startsWith((uint32_t) i, w_cstr)) {
281
i = len - (int32_t) strlen(w_cstr);
283
if (startsWith((uint32_t) i, w_cstr)) {
294
uint32_t CSString::locate(uint32_t pos, const char *w_cstr)
296
uint32_t len = length();
303
if (startsWith(i, w_cstr))
310
uint32_t CSString::locate(uint32_t pos, CS_CHAR ch)
312
uint32_t len = length();
326
uint32_t CSString::skip(uint32_t pos, CS_CHAR ch)
328
uint32_t len = length();
342
CSString *CSString::substr(uint32_t index, uint32_t size)
344
return clone(index, size);
347
CSString *CSString::substr(uint32_t index)
349
return clone(index, length() - index);
353
CSString *CSString::left(const char *w_cstr, int32_t count)
355
uint32_t idx = locate(w_cstr, count);
357
if (idx == (uint32_t)-1)
358
return CSString::newString("");
359
return substr(0, idx);
362
CSString *CSString::left(const char *w_cstr)
364
return left(w_cstr, 1);
367
CSString *CSString::right(const char *w_cstr, int32_t count)
369
uint32_t idx = locate(w_cstr, count);
371
if (idx == (uint32_t)-1) {
376
return newString("");
378
return substr(idx + strlen(w_cstr));
381
CSString *CSString::right(const char *w_cstr)
383
return right(w_cstr, 1);
386
bool CSString::startsWith(const char *w_str)
388
return startsWith(0, w_str);
391
bool CSString::endsWith(const char *w_str)
393
return startsWith(length() - strlen(w_str), w_str);
396
uint32_t CSString::nextPos(uint32_t pos)
403
CSString *CSString::clone(uint32_t len)
405
return clone(0, len);
408
CSString *CSString::clone()
410
return clone(0, length());
413
bool CSString::equals(const char *str)
415
uint32_t len = length();
418
for (i=0; i<len && *str; i++) {
419
if (charAt(i) != *str)
423
return i==len && !*str;
427
* ---------------------------------------------------------------
431
CSString::CSString():
437
CSString::CSString(const char *cstr):
438
myCString(cs_strdup(cstr)),
439
myStrLen(strlen(cstr))
443
CSString::~CSString()
449
const char *CSString::getCString()
454
CS_CHAR CSString::charAt(uint32_t pos)
457
return (CS_CHAR) (unsigned char) myCString[pos];
461
CS_CHAR CSString::upperCharAt(uint32_t pos)
464
return (CS_CHAR) (unsigned char) toupper(myCString[pos]);
468
void CSString::setCharAt(uint32_t pos, CS_CHAR ch)
471
myCString[pos] = (unsigned char) ch;
474
int CSString::compare(const char *val, uint32_t len)
476
const char *pa = myCString, *pb = val;
482
while (*pa && *pb && len) {
483
r = toupper(*pa) - toupper(*pb);
491
r = toupper(*pa) - toupper(*pb);
497
int CSString::compare(CSString *val)
499
return compare(val->getCString(), (uint32_t)-1);
502
bool CSString::startsWith(uint32_t index, const char *w_str)
504
uint32_t len = strlen(w_str);
507
if (index > myStrLen)
509
str = myCString + index;
510
for (uint32_t i=0; i<len && *str; i++) {
516
return (*w_str == 0);
519
void CSString::setLength(uint32_t len)
521
cs_realloc((void **) &myCString, len+1);
526
CSString *CSString::clone(uint32_t pos, uint32_t len)
528
CSString *str = NULL;
531
new_(str, CSString());
534
str->myCString = (char *) cs_malloc(len + 1);
538
if (len > myStrLen - pos) {
539
/* More space has been allocated than required.
540
* It may be that this space will be used up.
541
* Set the zero terminator at the end
544
str->myCString[len] = 0;
545
len = myStrLen - pos;
547
memcpy(str->myCString, myCString+pos, len);
548
str->myCString[len] = 0;
554
CSObject *CSString::getKey()
556
return (CSObject *) this;
559
int CSString::compareKey(CSObject *key)
561
return compare((CSString *) key);
564
CSString *CSString::newString(const char *cstr)
569
new_(str, CSString());
571
str->myCString = cs_strdup(cstr);
572
str->myStrLen = strlen(cstr);
577
CSString *CSString::newString(const char *bytes, uint32_t len)
582
new_(str, CSString());
585
str->myCString = (char *) cs_malloc(len+1);
586
memcpy(str->myCString, bytes, len);
587
str->myCString[len] = 0;
592
CSString *CSString::newString(CSStringBuffer *sb)
598
new_(str, CSString());
600
str->myStrLen = sb->length();
601
str->myCString = sb->take();
603
pop_(sb); // pop this do not release it because CSStringBuffer is NOT a CSRefObject.