summaryrefslogtreecommitdiff
path: root/src/pdflib/pdcore/pc_string.c
blob: f0e5824adfbbcd7f5ea0c27d94192e58e93f543b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*---------------------------------------------------------------------------*
 |              PDFlib - A library for generating PDF on the fly             |
 +---------------------------------------------------------------------------+
 | Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
 +---------------------------------------------------------------------------+
 |                                                                           |
 |    This software is subject to the PDFlib license. It is NOT in the       |
 |    public domain. Extended versions and commercial licenses are           |
 |    available, please check http://www.pdflib.com.                         |
 |                                                                           |
 *---------------------------------------------------------------------------*/

/* $Id: pc_string.c,v 1.2 2009/10/20 18:12:26 scuri Exp $
 *
 * The core string classes.
 *
 */

#include <string.h>

#include "pc_util.h"
#include "pc_string.h"
#include "pc_ctype.h"

#undef	SBUF_RESERVE
#define	SBUF_RESERVE	PDC_STR_INLINE_CAP


/* TODO:

    - think over the pdcore temporary memory model.

    - s->buf should be reference-counted (delayed copy).
*/

void pdc_init_strings(pdc_core *pdc)
{
    pdc->bstr_pool = pdc_mp_new(pdc, sizeof (pdc_bstr));
    pdc->ustr_pool = pdc_mp_new(pdc, sizeof (pdc_ustr));
} /* pdc_init_strings */

void pdc_cleanup_strings(pdc_core *pdc)
{
    pdc_mp_delete(pdc->bstr_pool);
    pdc_mp_delete(pdc->ustr_pool);
} /* pdc_cleanup_strings */


/************************************************************************/
/*									*/
/*  string object construction and deletion.				*/
/*									*/
/************************************************************************/

void
pdc_bs_boot(pdc_core *pdc, pdc_bstr *s)
{
    s->pdc = pdc;
    s->buf = (pdc_byte *) 0;
    s->len = 0;
    s->cap = PDC_STR_INLINE_CAP;
} /* pdc_bs_boot */

void
pdc_us_boot(pdc_core *pdc, pdc_ustr *s)
{
    s->pdc = pdc;
    s->buf = (pdc_ucval *) 0;
    s->len = 0;
    s->cap = PDC_STR_INLINE_CAP;
} /* pdc_us_boot */


void
pdc_bs_shutdown(pdc_bstr *s)
{
    if (s->buf != (pdc_byte *) 0)
	pdc_free(s->pdc, s->buf);

    pdc_bs_boot(s->pdc, s);
} /* pdc_bs_shutdown */

void
pdc_us_shutdown(pdc_ustr *s)
{
    if (s->buf != (pdc_ucval *) 0)
	pdc_free(s->pdc, s->buf);

    pdc_us_boot(s->pdc, s);
} /* pdc_us_shutdown */


#undef	USE_POOL
#define	USE_POOL

pdc_bstr *
pdc_bs_new(pdc_core *pdc)
{
#ifndef	USE_POOL
    static const char fn[] = "pdc_bs_new";

    pdc_bstr *result = (pdc_bstr *) pdc_malloc(pdc, sizeof (pdc_bstr), fn);
#else
    pdc_bstr *result = (pdc_bstr *) pdc_mp_alloc(pdc->bstr_pool);
#endif

    pdc_bs_boot(pdc, result);
    return result;
} /* pdc_bs_new */

pdc_ustr *
pdc_us_new(pdc_core *pdc, const pdc_ucval *src, size_t len)
{
#ifndef	USE_POOL
    static const char fn[] = "pdc_us_new";

    pdc_ustr *result = (pdc_ustr *) pdc_malloc(pdc, sizeof (pdc_ustr), fn);
#else
    pdc_ustr *result = (pdc_ustr *) pdc_mp_alloc(pdc->ustr_pool);
#endif

    pdc_us_boot(pdc, result);
    pdc_us_write(result, src, len);
    return result;
} /* pdc_us_new */


pdc_bstr *
pdc_bs_dup(const pdc_bstr *src)
{
    const pdc_byte *	buf = src->buf ? src->buf : src->buf0;
    pdc_bstr *		result = pdc_bs_new(src->pdc);

    pdc_bs_write(result, buf, src->len);
    return result;
} /* pdc_bs_dup */

pdc_ustr *
pdc_us_dup(const pdc_ustr *src)
{
    const pdc_ucval *buf = src->buf ? src->buf : src->buf0;

    return pdc_us_new(src->pdc, buf, src->len);
} /* pdc_us_dup */


void
pdc_bs_delete(pdc_bstr *s)
{
    pdc_bs_shutdown(s);
#ifndef	USE_POOL
    pdc_free(s->pdc, s);
#else
    pdc_mp_free(s->pdc->bstr_pool, s);
#endif
} /* pdc_bs_delete */

void
pdc_us_delete(pdc_ustr *s)
{
    pdc_us_shutdown(s);
#ifndef	USE_POOL
    pdc_free(s->pdc, s);
#else
    pdc_mp_free(s->pdc->ustr_pool, s);
#endif
} /* pdc_bs_delete */


/************************************************************************/
/*									*/
/*  "getters".								*/
/*									*/
/************************************************************************/

size_t
pdc_bs_length(const pdc_bstr *s)
{
    return s->len;
} /* pdc_bs_length */

size_t
pdc_us_length(const pdc_ustr *s)
{
    return s->len;
} /* pdc_us_length */


pdc_byte
pdc_bs_get(const pdc_bstr *s, int idx)
{
    static const char fn[] = "pdc_bs_get";

    const pdc_byte *buf = s->buf ? s->buf : s->buf0;

    if (idx < 0 || s->len <= (size_t) idx)
	pdc_error(s->pdc, PDC_E_INT_ARRIDX,
	    pdc_errprintf(s->pdc, "%d", idx), fn, 0, 0);

    return buf[idx];
} /* pdc_bs_get */

pdc_ucval
pdc_us_get(const pdc_ustr *s, int idx)
{
    static const char fn[] = "pdc_us_get";

    const pdc_ucval *buf = s->buf ? s->buf : s->buf0;

    if (idx < 0 || s->len <= (size_t) idx)
	pdc_error(s->pdc, PDC_E_INT_ARRIDX,
	    pdc_errprintf(s->pdc, "%d", idx), fn, 0, 0);

    return buf[idx];
} /* pdc_us_get */


const pdc_byte *
pdc_bs_get_cptr(const pdc_bstr *s)
{
    static const pdc_byte empty = 0;

    /* TODO: avoid the ugly "un-const" cast. */
    pdc_byte *buf = (pdc_byte *) (s->buf ? s->buf : s->buf0);

    if (!s->len)
	return &empty;

    buf[s->len] = 0;
    return buf;
}

const pdc_byte *
pdc_us_get_cptr(const pdc_ustr *s)
{
    static const pdc_byte empty = 0;

    const pdc_ucval *buf = s->buf ? s->buf : s->buf0;

    if (!s->len)
	return &empty;

    return (const pdc_byte *) buf;
}


/************************************************************************/
/*									*/
/*  "modifiers".							*/
/*									*/
/************************************************************************/

void
pdc_bs_copy(pdc_bstr *dst, const pdc_bstr *src)
{
    const pdc_byte *buf = src->buf ? src->buf : src->buf0;

    dst->len = 0;

    if (src->len)
	pdc_bs_write(dst, buf, src->len);
} /* pdc_bs_copy */

void
pdc_us_copy(pdc_ustr *dst, const pdc_ustr *src)
{
    const pdc_ucval *buf = src->buf ? src->buf : src->buf0;

    dst->len = 0;

    if (src->len)
	pdc_us_write(dst, buf, src->len);
} /* pdc_us_copy */


void
pdc_bs_substr(pdc_bstr *dst, const pdc_bstr *src, size_t pos, size_t len)
{
    static const char fn[] = "pdc_bs_substr";

    const pdc_byte *buf = src->buf ? src->buf : src->buf0;

    if ((pos < 0) || (len < 0) || (pos > src->len) || ((pos + len) > src->len))
	pdc_error(src->pdc, PDC_E_INT_ILLARG, fn, 0, 0, 0);

    dst->len = 0;
    pdc_bs_write(dst, buf + pos, len);
} /* pdc_bs_substr */

void
pdc_us_substr(pdc_ustr *dst, const pdc_ustr *src, size_t pos, size_t len)
{
    static const char fn[] = "pdc_us_substr";

    const pdc_ucval *buf = src->buf ? src->buf : src->buf0;

    if ((pos < 0) || (len < 0) || (pos > src->len) || ((pos + len) > src->len))
	pdc_error(src->pdc, PDC_E_INT_ILLARG, fn, 0, 0, 0);

    dst->len = 0;
    pdc_us_write(dst, buf + pos, len);
} /* pdc_us_substr */


void
pdc_bs_concat(pdc_bstr *dst, const pdc_bstr *src)
{
    const pdc_byte *buf = src->buf ? src->buf : src->buf0;

    if (src->len)
	pdc_bs_write(dst, buf, src->len);
} /* pdc_bs_concat */

void
pdc_us_concat(pdc_ustr *dst, const pdc_ustr *src)
{
    const pdc_ucval *buf = src->buf ? src->buf : src->buf0;

    if (src->len)
	pdc_us_write(dst, buf, src->len);
} /* pdc_us_concat */


void
pdc_bs_set(pdc_bstr *s, int idx, pdc_byte val)
{
    static const char fn[] = "pdc_bs_set";

    pdc_byte *buf = s->buf ? s->buf : s->buf0;

    if (idx < 0 || s->len <= (size_t) idx)
	pdc_error(s->pdc, PDC_E_INT_ARRIDX,
	    pdc_errprintf(s->pdc, "%d", idx), fn, 0, 0);

    buf[idx] = val;
} /* pdc_bs_set */

void
pdc_us_set(pdc_ustr *s, int idx, pdc_ucval val)
{
    static const char fn[] = "pdc_us_set";

    pdc_ucval *buf = s->buf ? s->buf : s->buf0;

    if (idx < 0 || s->len <= (size_t) idx)
	pdc_error(s->pdc, PDC_E_INT_ARRIDX,
	    pdc_errprintf(s->pdc, "%d", idx), fn, 0, 0);

    buf[idx] = val;
} /* pdc_us_set */


void
pdc_bs_tolower(pdc_bstr *s)
{
    pdc_byte *	buf = s->buf ? s->buf : s->buf0;
    int		i;

    for (i = 0; i < (int) s->len; ++i)
	buf[i] = pdc_tolower(buf[i]);
} /* pdc_bs_tolower */

void
pdc_bs_toupper(pdc_bstr *s)
{
    pdc_byte *	buf = s->buf ? s->buf : s->buf0;
    int		i;

    for (i = 0; i < (int) s->len; ++i)
        buf[i] = pdc_toupper(buf[i]);
} /* pdc_bs_toupper */


/************************************************************************/
/*									*/
/*  stream-like functions.						*/
/*									*/
/************************************************************************/

void
pdc_bs_write(pdc_bstr *dst, const pdc_byte *src, size_t len)
{
    static const char fn[] = "pdc_bs_write";

    pdc_byte *buf = dst->buf ? dst->buf : dst->buf0;

    if (!src || !len)
	return;

    if (dst->cap < dst->len + len + 1)
    {
	dst->cap = dst->len + len + 1 + SBUF_RESERVE;

	if (!dst->buf)
	{
	    dst->buf = (pdc_byte *) pdc_malloc(dst->pdc, dst->cap, fn);
	    memcpy(dst->buf, dst->buf0, dst->len);
	}
	else
	{
	    dst->buf = (pdc_byte *) pdc_realloc(dst->pdc,
		dst->buf, dst->cap, fn);
	}

	buf = dst->buf;
    }

    memcpy(buf + dst->len, src, len);
    dst->len += len;
} /* pdc_bs_write */


void
pdc_bs_puts(pdc_bstr *dst, const pdc_byte *src)
{
    if (!src)
	return;

    pdc_bs_write(dst, src, strlen((char *) src));
} /* pdc_bs_puts */


void
pdc_us_write(pdc_ustr *dst, const pdc_ucval *src, size_t len)
{
    static const char fn[] = "pdc_us_write";

    pdc_ucval *buf = dst->buf ? dst->buf : dst->buf0;

    if (!src || len == 0)
	return;

    if (dst->cap < dst->len + len)
    {
	dst->cap = dst->len + len + SBUF_RESERVE;

	if (!dst->buf)
	{
	    dst->buf = (pdc_ucval *)
		pdc_malloc(dst->pdc, dst->cap * sizeof (pdc_ucval), fn);

	    memcpy(dst->buf, dst->buf0, dst->len * sizeof (pdc_ucval));
	}
	else
	{
	    dst->buf = (pdc_ucval *) pdc_realloc(dst->pdc,
		dst->buf, dst->cap * sizeof (pdc_ucval), fn);
	}

	buf = dst->buf;
    }

    memcpy(buf + dst->len, src, len * sizeof (pdc_ucval));
    dst->len += len;
} /* pdc_us_write */


void
pdc_bs_rewrite(pdc_bstr *s)
{
    s->len = 0;
} /* pdc_bs_rewrite */

void
pdc_us_rewrite(pdc_ustr *s)
{
    s->len = 0;
} /* pdc_us_rewrite */


void
pdc_bs_putc(pdc_bstr *s, pdc_byte val)
{
    pdc_bs_write(s, &val, 1);
} /* pdc_bs_putc */

void
pdc_us_putc(pdc_ustr *s, pdc_ucval val)
{
    pdc_us_write(s, &val, 1);
} /* pdc_us_putc */


/************************************************************************/
/*									*/
/*  other utilities.							*/
/*									*/
/************************************************************************/

int
pdc_bs_compare(const pdc_bstr *s1, const pdc_bstr *s2)
{
    const char *buf1 = (const char *) (s1->buf ? s1->buf : s1->buf0);
    const char *buf2 = (const char *) (s2->buf ? s2->buf : s2->buf0);
    int		result;

    if (s1->len < s2->len)
    {
	if ((result = strncmp(buf1, buf2, s1->len)) != 0)
	    return result;

	return -1;
    }

    if (s2->len < s1->len)
    {
	if ((result = strncmp(buf1, buf2, s2->len)) != 0)
	    return result;

	return +1;
    }

    return strncmp(buf1, buf2, s1->len);
} /* pdc_bs_compare */