~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSString.cc

  • Committer: Monty Taylor
  • Date: 2010-07-06 00:44:32 UTC
  • mfrom: (1643.1.13 build)
  • Revision ID: mordred@inaugust.com-20100706004432-843uftc92rc2497l
Merged in PBMS, translation updates, a few build fixes and a few bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 * Original author: Paul McCullagh (H&G2JCtL)
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-06-15
 
23
 *
 
24
 * CORE SYSTEM STORAGE
 
25
 * Basic storage structures.
 
26
 *
 
27
 */
 
28
 
 
29
#include "CSConfig.h"
 
30
#include <inttypes.h>
 
31
 
 
32
#include <assert.h>
 
33
#include <string.h>
 
34
#include <ctype.h>
 
35
 
 
36
#include "CSGlobal.h"
 
37
#include "CSUTF8.h"
 
38
#include "CSStorage.h"
 
39
#include "CSMemory.h"
 
40
#include "CSString.h"
 
41
#include "CSStrUtil.h"
 
42
#include "CSGlobal.h"
 
43
 
 
44
 
 
45
/*
 
46
 * ---------------------------------------------------------------
 
47
 * Core System String Buffers
 
48
 */
 
49
 
 
50
CSStringBuffer_::CSStringBuffer_():
 
51
iBuffer(NULL),
 
52
iGrow(0),
 
53
iSize(0),
 
54
myStrLen(0)
 
55
{
 
56
        iGrow = 20;
 
57
}
 
58
 
 
59
CSStringBuffer_::CSStringBuffer_(uint32_t grow):
 
60
iBuffer(NULL),
 
61
iGrow(0),
 
62
iSize(0),
 
63
myStrLen(0)
 
64
{
 
65
        iGrow = grow;
 
66
}
 
67
 
 
68
CSStringBuffer_::~CSStringBuffer_()
 
69
{
 
70
        clear();
 
71
}
 
72
 
 
73
void CSStringBuffer_::clear()
 
74
{
 
75
        if (iBuffer)
 
76
                cs_free(iBuffer);
 
77
        iBuffer = NULL;
 
78
        iSize = 0;
 
79
        myStrLen = 0; 
 
80
}
 
81
 
 
82
void CSStringBuffer_::append(char ch)
 
83
{
 
84
        if (iSize == myStrLen) {
 
85
                cs_realloc((void **) &iBuffer, iSize + iGrow);
 
86
                iSize += iGrow;
 
87
        }
 
88
        iBuffer[myStrLen] = ch;
 
89
        myStrLen++;
 
90
}
 
91
 
 
92
void CSStringBuffer_::append(const char *str, size_t len)
 
93
{
 
94
        if (myStrLen + len > iSize) {
 
95
                size_t add = len;
 
96
                
 
97
                if (add < iGrow)
 
98
                        add = iGrow;
 
99
                cs_realloc((void **) &iBuffer, iSize + add);
 
100
                iSize += add;
 
101
        }
 
102
        memcpy(iBuffer + myStrLen, str, len);
 
103
        myStrLen += len;
 
104
}
 
105
 
 
106
void CSStringBuffer_::append(int value)
 
107
{
 
108
        char buffer[100];
 
109
 
 
110
        snprintf(buffer, 100, "%"PRId32"", value);
 
111
        append(buffer);
 
112
}
 
113
 
 
114
void CSStringBuffer_::append(uint32_t value)
 
115
{
 
116
        char buffer[100];
 
117
 
 
118
        snprintf(buffer, 100, "%"PRIu32"", value);
 
119
        append(buffer);
 
120
}
 
121
 
 
122
char *CSStringBuffer_::getCString()
 
123
{
 
124
        if (iSize == myStrLen) {
 
125
                cs_realloc((void **) &iBuffer, iSize + 1);
 
126
                iSize++;
 
127
        }
 
128
        iBuffer[myStrLen] = 0;
 
129
        return iBuffer;
 
130
}
 
131
 
 
132
char *CSStringBuffer_::take()
 
133
{
 
134
        char *buf;
 
135
 
 
136
        cs_realloc((void **) &iBuffer, myStrLen + 1);
 
137
        iSize = myStrLen + 1;
 
138
        iBuffer[myStrLen] = 0;
 
139
 
 
140
        buf = iBuffer;
 
141
        iBuffer = NULL;
 
142
        iSize = 0;
 
143
        myStrLen = 0; 
 
144
        return buf;
 
145
}
 
146
 
 
147
void CSStringBuffer_::setLength(uint32_t len)
 
148
{
 
149
        if (len > iSize) {
 
150
                cs_realloc((void **) &iBuffer, len + 1);
 
151
                iSize = len+1;
 
152
        }
 
153
        myStrLen = len;
 
154
}
 
155
 
 
156
uint32_t CSStringBuffer_::ignore(uint32_t pos, char ch)
 
157
{
 
158
        while (pos < myStrLen && iBuffer[pos] == ch)
 
159
                pos++;
 
160
        return pos;
 
161
}
 
162
 
 
163
uint32_t CSStringBuffer_::find(uint32_t pos, char ch)
 
164
{
 
165
        while (pos < myStrLen && iBuffer[pos] != ch)
 
166
                pos++;
 
167
        return pos;
 
168
}
 
169
 
 
170
uint32_t CSStringBuffer_::trim(uint32_t pos, char ch)
 
171
{
 
172
        while (pos > 0 && iBuffer[pos-1] == ch)
 
173
                pos--;
 
174
        return pos;
 
175
}
 
176
 
 
177
CSString *CSStringBuffer_::substr(uint32_t pos, uint32_t len)
 
178
{
 
179
        CSString *s = CSString::newString(iBuffer + pos, len);
 
180
 
 
181
        return s;
 
182
}
 
183
 
 
184
/*
 
185
 * ---------------------------------------------------------------
 
186
 * Generic Strings
 
187
 */
 
188
 
 
189
CSString *CSString::newString(const char *cstr)
 
190
{
 
191
        return CSCString::newString(cstr);
 
192
}
 
193
 
 
194
CSString *CSString::newString(const char *bytes, uint32_t len)
 
195
{
 
196
        return CSCString::newString(bytes, len);
 
197
}
 
198
 
 
199
CSString *CSString::newString(CSStringBuffer *sb)
 
200
{
 
201
        return CSCString::newString(sb);
 
202
}
 
203
 
 
204
CSString *CSString::concat(CSString *cat_str)
 
205
{
 
206
        CSString *new_str = NULL;
 
207
        uint32_t len_a, len_b;
 
208
        
 
209
        enter_();
 
210
        len_a = length();
 
211
        len_b = cat_str->length();
 
212
        new_str = clone(len_a + len_b);
 
213
        push_(new_str);
 
214
        
 
215
        for (uint32_t i=0; i<len_b; i++)
 
216
                new_str->setCharAt(len_a+i, cat_str->charAt(i));
 
217
 
 
218
        pop_(new_str);
 
219
        return_(new_str);
 
220
}
 
221
 
 
222
CSString *CSString::concat(const char *cat_str)
 
223
{
 
224
        CSString *new_str = NULL;
 
225
        uint32_t len_a, len_b;
 
226
        
 
227
        enter_();
 
228
        len_a = length();
 
229
        len_b = strlen(cat_str);
 
230
        new_str = clone(len_a + len_b);
 
231
        push_(new_str);
 
232
        
 
233
        for (uint32_t i=0; i<len_b; i++)
 
234
                new_str->setCharAt(len_a+i, cat_str[i]);
 
235
 
 
236
        pop_(new_str);
 
237
        return_(new_str);
 
238
}
 
239
 
 
240
CSString *CSString::toUpper()
 
241
{
 
242
        CSString *new_str = NULL;
 
243
        uint32_t len;
 
244
 
 
245
        enter_();
 
246
        new_str = clone();
 
247
        push_(new_str);
 
248
        
 
249
        len = new_str->length();
 
250
        for (uint32_t i=0; i<len; i++)
 
251
                new_str->setCharAt(i, upperCharAt(i));
 
252
 
 
253
        pop_(new_str);
 
254
        return_(new_str);
 
255
}
 
256
 
 
257
uint32_t CSString::hashKey()
 
258
{
 
259
        register uint32_t h = 0, g;
 
260
        
 
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;
 
265
        }
 
266
 
 
267
        return (h);
 
268
}
 
269
 
 
270
uint32_t CSString::locate(const char *w_cstr, s_int count)
 
271
{
 
272
        s_int len = length();
 
273
        s_int i;
 
274
 
 
275
        if (count >= 0) {
 
276
                i = 0;
 
277
                while (i < len) {
 
278
                        if (startsWith((uint32_t) i, w_cstr)) {
 
279
                                count--;
 
280
                                if (!count)
 
281
                                        return i;
 
282
                        }
 
283
                        i++;
 
284
                }
 
285
        }
 
286
        else {
 
287
                count = -count;
 
288
                i = len - (s_int) strlen(w_cstr);
 
289
                while (i >= 0) {
 
290
                        if (startsWith((uint32_t) i, w_cstr)) {
 
291
                                count--;
 
292
                                if (!count)
 
293
                                        return i;
 
294
                        }
 
295
                        i--;
 
296
                }
 
297
        }
 
298
        return i;
 
299
}
 
300
 
 
301
uint32_t CSString::locate(uint32_t pos, const char *w_cstr)
 
302
{
 
303
        uint32_t len = length();
 
304
        uint32_t i;
 
305
 
 
306
        if (pos > len)
 
307
                return len;
 
308
        i = pos;
 
309
        while (i < len) {
 
310
                if (startsWith(i, w_cstr))
 
311
                        return i;
 
312
                i++;
 
313
        }
 
314
        return i;
 
315
}
 
316
 
 
317
uint32_t CSString::locate(uint32_t pos, CS_CHAR ch)
 
318
{
 
319
        uint32_t len = length();
 
320
        uint32_t i;
 
321
 
 
322
        if (pos > len)
 
323
                return len;
 
324
        i = pos;
 
325
        while (i < len) {
 
326
                if (charAt(i) == ch)
 
327
                        return i;
 
328
                i++;
 
329
        }
 
330
        return i;
 
331
}
 
332
 
 
333
uint32_t CSString::skip(uint32_t pos, CS_CHAR ch)
 
334
{
 
335
        uint32_t len = length();
 
336
        uint32_t i;
 
337
 
 
338
        if (pos > len)
 
339
                return len;
 
340
        i = pos;
 
341
        while (i < len) {
 
342
                if (charAt(i) != ch)
 
343
                        return i;
 
344
                i++;
 
345
        }
 
346
        return i;
 
347
}
 
348
 
 
349
CSString *CSString::substr(uint32_t index, uint32_t size)
 
350
{
 
351
        return clone(index, size);
 
352
}
 
353
 
 
354
CSString *CSString::substr(uint32_t index)
 
355
{
 
356
        return clone(index, length() - index);
 
357
        
 
358
}
 
359
 
 
360
CSString *CSString::left(const char *w_cstr, s_int count)
 
361
{
 
362
        uint32_t idx = locate(w_cstr, count);
 
363
 
 
364
        if (idx == (uint32_t)-1)
 
365
                return CSCString::newString("");
 
366
        return substr(0, idx);
 
367
}
 
368
 
 
369
CSString *CSString::left(const char *w_cstr)
 
370
{
 
371
        return left(w_cstr, 1);
 
372
}
 
373
 
 
374
CSString *CSString::right(const char *w_cstr, s_int count)
 
375
{
 
376
        uint32_t idx = locate(w_cstr, count);
 
377
 
 
378
        if (idx == (uint32_t)-1) {
 
379
                this->retain();
 
380
                return this;
 
381
        }
 
382
        
 
383
        if (idx == length())
 
384
                return newString("");
 
385
                
 
386
        return substr(idx + strlen(w_cstr));
 
387
}
 
388
 
 
389
CSString *CSString::right(const char *w_cstr)
 
390
{
 
391
        return right(w_cstr, 1);
 
392
}
 
393
 
 
394
bool CSString::startsWith(const char *w_str)
 
395
{
 
396
        return startsWith(0, w_str);
 
397
}
 
398
 
 
399
bool CSString::endsWith(const char *w_str)
 
400
{
 
401
        return startsWith(length() - strlen(w_str), w_str);
 
402
}
 
403
 
 
404
uint32_t CSString::nextPos(uint32_t pos)
 
405
{
 
406
        if (pos >= length())
 
407
                return length();
 
408
        return pos + 1;
 
409
}
 
410
 
 
411
CSString *CSString::clone(uint32_t len)
 
412
{
 
413
        return clone(0, len);
 
414
}
 
415
 
 
416
CSString *CSString::clone()
 
417
{
 
418
        return clone(0, length());
 
419
}
 
420
 
 
421
bool CSString::equals(const char *str)
 
422
{
 
423
        uint32_t len = length();
 
424
        uint32_t i;
 
425
        
 
426
        for (i=0; i<len && *str; i++) {
 
427
                if (charAt(i) != *str)
 
428
                        return false;
 
429
                str++;
 
430
        }
 
431
        return i==len && !*str;
 
432
}
 
433
 
 
434
/*
 
435
 * ---------------------------------------------------------------
 
436
 * Standard C String
 
437
 */
 
438
 
 
439
CSCString::CSCString():
 
440
CSString(),
 
441
myCString(NULL),
 
442
myStrLen(0)
 
443
{
 
444
}
 
445
 
 
446
CSCString::~CSCString()
 
447
{
 
448
        if (myCString)
 
449
                cs_free(myCString);
 
450
}
 
451
 
 
452
const char *CSCString::getCString()
 
453
{
 
454
        return myCString;
 
455
}
 
456
 
 
457
CS_CHAR CSCString::charAt(uint32_t pos)
 
458
{
 
459
        if (pos < myStrLen)
 
460
                return (CS_CHAR) (unsigned char) myCString[pos];
 
461
        return (CS_CHAR) 0;
 
462
}
 
463
 
 
464
CS_CHAR CSCString::upperCharAt(uint32_t pos)
 
465
{
 
466
        if (pos < myStrLen)
 
467
                return (CS_CHAR) (unsigned char) toupper(myCString[pos]);
 
468
        return (CS_CHAR) 0;
 
469
}
 
470
 
 
471
void CSCString::setCharAt(uint32_t pos, CS_CHAR ch)
 
472
{
 
473
        if (pos < myStrLen)
 
474
                myCString[pos] = (unsigned char) ch;
 
475
}
 
476
 
 
477
int CSCString::compare(const char *val, uint32_t len)
 
478
{
 
479
        const char *pa = myCString, *pb = val;
 
480
        int r = 0;
 
481
        
 
482
        enter_();
 
483
        
 
484
        if (pa && pb) {
 
485
                while (*pa && *pb && len) {
 
486
                        r = toupper(*pa) - toupper(*pb);
 
487
                        if (r != 0)
 
488
                                break;
 
489
                        pa++;
 
490
                        pb++;
 
491
                        len--;
 
492
                }
 
493
                if (len)
 
494
                        r = toupper(*pa) - toupper(*pb);
 
495
        }
 
496
 
 
497
        return_(r);
 
498
}
 
499
 
 
500
int CSCString::compare(CSString *val)
 
501
{
 
502
        return compare(val->getCString(), (uint32_t)-1);
 
503
}
 
504
 
 
505
bool CSCString::startsWith(uint32_t index, const char *w_str)
 
506
{
 
507
        uint32_t len = strlen(w_str);
 
508
        char *str;
 
509
        
 
510
        if (index > myStrLen)
 
511
                index = myStrLen;
 
512
        str = myCString + index;
 
513
        for (uint32_t i=0; i<len && *str; i++) {
 
514
                if (*str != *w_str)
 
515
                        return false;
 
516
                str++;
 
517
                w_str++;
 
518
        }
 
519
        return (*w_str == 0);
 
520
}
 
521
 
 
522
void CSCString::setLength(uint32_t len)
 
523
{
 
524
        cs_realloc((void **) &myCString, len+1);
 
525
        myCString[len] = 0;
 
526
        myStrLen = len;
 
527
}
 
528
 
 
529
CSString *CSCString::clone(uint32_t pos, uint32_t len)
 
530
{
 
531
        CSCString *str = NULL;
 
532
        
 
533
        enter_();
 
534
        new_(str, CSCString());
 
535
        push_(str);
 
536
        
 
537
        str->myCString = (char *) cs_malloc(len + 1);
 
538
        str->myStrLen = len;
 
539
        if (pos > myStrLen)
 
540
                pos = myStrLen;
 
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
 
545
                 * of the space!
 
546
                 */
 
547
                str->myCString[len] = 0;
 
548
                len = myStrLen - pos;
 
549
        }
 
550
        memcpy(str->myCString, myCString+pos, len);
 
551
        str->myCString[len] = 0;
 
552
 
 
553
        pop_(str);
 
554
        return_(str);
 
555
}
 
556
 
 
557
CSObject *CSCString::getKey()
 
558
{
 
559
        return (CSObject *) this;
 
560
}
 
561
 
 
562
int CSCString::compareKey(CSObject *key)
 
563
{
 
564
        return compare((CSString *) key);
 
565
}
 
566
 
 
567
CSCString *CSCString::newString(const char *cstr)
 
568
{
 
569
        CSCString *str;
 
570
        
 
571
        enter_();
 
572
        new_(str, CSCString());
 
573
        push_(str);
 
574
        str->myCString = cs_strdup(cstr);
 
575
        str->myStrLen = strlen(cstr);
 
576
        pop_(str);
 
577
        return_(str);
 
578
}
 
579
 
 
580
CSCString *CSCString::newString(const char *bytes, uint32_t len)
 
581
{
 
582
        CSCString *str;
 
583
        
 
584
        enter_();
 
585
        new_(str, CSCString());
 
586
        push_(str);
 
587
        str->myStrLen = len;
 
588
        str->myCString = (char *) cs_malloc(len+1);
 
589
        memcpy(str->myCString, bytes, len);
 
590
        str->myCString[len] = 0;
 
591
        pop_(str);
 
592
        return_(str);
 
593
}
 
594
 
 
595
CSCString *CSCString::newString(CSStringBuffer *sb)
 
596
{
 
597
        CSCString *str;
 
598
        
 
599
        enter_();
 
600
        push_(sb);
 
601
        new_(str, CSCString());
 
602
        push_(str);
 
603
        str->myCString = sb->take();
 
604
        str->myStrLen = sb->length();
 
605
        pop_(str);
 
606
        release_(sb);
 
607
        return_(str);
 
608
}
 
609