summaryrefslogtreecommitdiffstats
path: root/lib/qstring.c
blob: 5ca4d8683e04d05dd247b0eb75fa08ee60e7694d (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
/* Some string handling
 * Copyright (C) 2010 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your
 * option) any later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "qstring.h"

#include "memory.h"
#include "zassert.h"

/*==============================================================================
 */

/*------------------------------------------------------------------------------
 * Initialise qstring -- allocate if required.
 *
 * If non-zero len is given, a body is allocated (for at least len + 1).
 *
 * Returns: address of qstring
 *
 * NB: assumes initialising a new structure.  If not, then caller should
 *     use qs_reset() or qs_clear().
 */
extern qstring
qs_init_new(qstring qs, size_t len)
{
  if (qs == NULL)
    qs = qs_new() ;
  else
    memset(qs, 0, sizeof(qstring_t)) ;  /* see qs_new()         */

  if (len != 0)
    return qs_make_to_length(qs, len) ;

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Allocate or reallocate so that string is big enough for the given length.
 *
 * Allocate qstring if required.  Returns with a body with size > 0.
 *
 * Allocates to 16 byte boundaries with space for '\0' beyond given length.
 *
 * Returns: address of qstring
 *
 * NB: allocates new body if the size == 0.
 *
 *     If the qstring is a "dummy", its contents are now copied to the new
 *     real qstring body -- up to a maximum of the new length.
 */
extern qstring
qs_make_to_length(qstring qs, size_t len)
{
  size_t size = (len + 0x10) & ~(size_t)(0x10 - 1) ;

  if (qs == NULL)
    qs = qs_new() ;

  if (size > qs->size)
    {
      if (qs->size == 0)
        {
          void* old ;
          old = qs->body ;

          qs->size = size ;
          qs->body = XMALLOC(MTYPE_QSTRING_BODY, qs->size) ;

          if ((qs->len != 0) && (old != NULL))
            memcpy(qs->body, old, (qs->len <= len) ? qs->len : len) ;
        }
      else
        {
          qs->size *= 2 ;
          if (qs->size < size)
            qs->size = size ;
          qs->body = XREALLOC(MTYPE_QSTRING_BODY, qs->body, qs->size) ;
        } ;
    };

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Add 'n' to the current string length, allocating or extending the body as
 * required.
 *
 * Allocate qstring if required.  Returns with a body with size > 0.
 *
 * Allocates to 16 byte boundaries with space for '\0' beyond new length.
 *
 * Returns: address of qstring
 *
 *    also: sets char** p_ep to point at the *end* of the new len.
 *
 * NB: allocates new body if the size == 0.
 *
 *     If the qstring is a "dummy", its contents are now copied to the new
 *     real qstring body -- up to a maximum of the new length.
 */
extern qstring
qs_add_len(qstring qs, size_t n, char** p_ep)
{
  size_t len ;
  len = (qs != NULL) ? qs->len + n : n ;

  qs = qs_make_to_length(qs, len) ;

  qs->len = len ;

  *p_ep = (char*)qs->body + len ;

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Free body of qstring -- zeroise size, len and cp
 *
 * Does nothing if qstring is NULL
 *
 * NB: frees the body if the size != 0.  So, a "dummy" qstring will not retain
 *     the old body.
 */
extern void
qs_free_body(qstring qs)
{
  if (qs != NULL)
    {
      if (qs->size != 0)
        XFREE(MTYPE_QSTRING_BODY, qs->body) ; /* sets qs->body = NULL */

      qs->size = 0 ;
      qs->len  = 0 ;
      qs->cp   = 0 ;
    } ;
} ;

/*------------------------------------------------------------------------------
 * Reset qstring -- free body and, if required, free the structure.
 *
 * If not freeing the structure, zeroise size, len and cp -- qs_free_body()
 *
 * Returns: NULL if freed the structure
 *          address of structure (if any), otherwise
 *
 * NB: frees the body if the size != 0.  So, a "dummy" qstring will not retain
 *     the old body.
 */
extern qstring
qs_reset(qstring qs, int free_structure)
{
  if (qs != NULL)
    {
      if (qs->size != 0)
        XFREE(MTYPE_QSTRING_BODY, qs->body) ;   /* sets qs->body = NULL */

      if (free_structure)
        XFREE(MTYPE_QSTRING, qs) ;              /* sets qs = NULL       */
      else
        {
          qs->size = 0 ;
          qs->len  = 0 ;
          qs->cp   = 0 ;
        } ;
    } ;
  return qs ;
} ;

/*==============================================================================
 * printf() and vprintf() type functions
 */

/*------------------------------------------------------------------------------
 * Formatted print to qstring -- cf printf()
 *
 * Allocate qstring if required.
 *
 * Returns: address of qstring if OK
 *          NULL if failed (unlikely though that is)
 */
extern qstring
qs_printf(qstring qs, const char* format, ...)
{
  va_list args;

  va_start (args, format);
  qs = qs_vprintf(qs, format, args);
  va_end (args);

  return qs;
} ;

/*------------------------------------------------------------------------------
 * Formatted print to qstring -- cf vprintf()
 *
 * Allocate qstring if required.
 *
 * Returns: address of qstring if OK
 *          NULL if failed (unlikely though that is)
 */
extern qstring
qs_vprintf(qstring qs, const char *format, va_list args)
{
  va_list  ac ;
  int      len ;
  qstring qqs ;

  qqs = qs ;
  if (qs == NULL)
    qs = qs_new() ;

  while (1)
    {
     /* Note that vsnprintf() returns the length of what it would like to have
      * produced, if it had the space.  That length does not include the
      * trailing '\0'.
      *
      * Also note that given a zero length the string address may be NULL, and
      * the result is still the length required.
      */
      va_copy(ac, args);
      qs->len = len = vsnprintf (qs->body, qs->size, format, ac) ;
      va_end(ac);

      if (len < 0)
        break ;

      if (len < (int)qs->size)
        return qs ;

      qs_make_to_length(qs, len) ;
    } ;

  if (qqs == NULL)
    qs_reset_free(qs) ;         /* discard what was allocated   */
  else
    qs->len = 0 ;

  return NULL ;
} ;

/*==============================================================================
 * Other operations
 */

/*------------------------------------------------------------------------------
 * Set qstring to be copy of the given string.
 *
 * Allocates a qstring, if required.
 *
 * Sets qs->len to the length of the string (excluding trailing '\0')
 *
 * NB: if src == NULL, sets qstring to be zero length string.
 *
 * Returns: address of the qstring copied to.
 *
 * NB: if copying to a dummy qstring, the old body is simply discarded.
 */
extern qstring
qs_set(qstring qs, const char* src)
{
  qs = qs_set_len(qs, (src != NULL) ? strlen(src) : 0) ;
  if (qs->len != 0)
    memcpy(qs->body, src, qs->len + 1) ;
  else
    *((char*)qs->body) = '\0' ;

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Set qstring to be leading 'n' bytes of given string.
 *
 * Allocates qstring if required.
 *
 * Inserts '\0' terminator after the 'n' bytes copied.
 *
 * Returns: address of the qstring copied to.
 *
 * NB: src string MUST be at least 'n' bytes long.
 *
 * NB: src may not be NULL unless n == 0.
 *
 * NB: if copying to a dummy qstring, the old body is simply discarded.
 */
extern qstring
qs_set_n(qstring qs, const char* src, size_t n)
{
  qs = qs_set_len(qs, n) ;      /* ensures have body > n        */
  if (n != 0)
    memcpy(qs->body, src, n) ;

  *((char*)qs->body + n) = '\0' ;

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Append given string to a qstring.
 *
 * Allocates a qstring, if required.
 *
 * Sets qs->len to the length of the result (excluding trailing '\0')
 *
 * NB: if src == NULL, appends nothing -- but result will be '\0' terminated.
 *
 * Returns: address of the qstring copied to.
 *
 * NB: if copying to a dummy qstring, the old body is simply discarded.
 */
extern qstring qs_append(qstring qs, const char* src)
{
  size_t n ;
  char* ep ;

  n = (src != NULL) ? strlen(src) : 0 ;

  qs = qs_add_len(qs, n, &ep) ;
  ep = (char*)qs->body + qs->len ;

  if (n != 0)
    memcpy(ep - n, src, n + 1) ;
  else
    *ep = '\0' ;

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Set qstring to be leading 'n' bytes of given string.
 *
 * Allocates qstring if required.
 *
 * Returns: address of the qstring copied to.
 *
 * NB: src string MUST be at least 'n' bytes long.
 *
 * NB: src may not be NULL unless n == 0.
 *
 * NB: if n == 0, appends nothing -- but result will be '\0' terminated.
 *
 * NB: if copying to a dummy qstring, the old body is simply discarded.
 *
 * NB: if copying to a dummy qstring, the old body is simply discarded.
 */
extern qstring
qs_append_n(qstring qs, const char* src, size_t n)
{
  char* ep ;

  qs = qs_add_len(qs, n, &ep) ;

  if (n != 0)
    memcpy(ep - n, src, n) ;

  *ep = '\0' ;

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Copy one qstring to another
 *
 * If both are NULL, returns NULL.
 *
 * Otherwise if dst is NULL, creates a new qstring.
 *
 * Sets dst:  body = copy of src->len bytes of src->body -- '\0' terminated.
 *            cp   = src->cp
 *            len  = src->len
 *
 * Where a NULL src has zero cp and len.
 *
 * If not NULL, the destination is guaranteed to have a body, and that will be
 * '\0' terminated.
 *
 * Returns: the destination qstring
 *
 * NB: if copying to a dummy qstring, the old body is simply discarded.
 */
extern qstring
qs_copy(qstring dst, qstring src)
{
  size_t n ;

  if (src == NULL)
    {
      if (dst == NULL)
        return dst ;

      n       = 0 ;
      dst->cp = 0 ;
    }
  else
    {
      if (dst == NULL)
        dst = qs_new() ;

      n       = src->len ;
      dst->cp = src->cp ;
    } ;

  qs_set_len(dst, n) ;

  if (n > 0)
    memcpy(dst->body, src->body, n) ;

  *((char*)dst->body + n) = '\0' ;

  return dst ;
} ;

/*------------------------------------------------------------------------------
 * Compare significant parts of two qstrings.
 *
 * By significant, mean excluding leading/trailing isspace() and treating
 * multiple isspace() as single isspace().
 *
 * Compares the 'len' portions of the strings.
 *
 * If either is NULL, it is deemed to be an empty string.
 *
 * Returns: -1 => a <  b
 *           0 => a == b
 *          +1 => a >  b
 */
extern int
qs_cmp_sig(qstring a, qstring b)
{
  const unsigned char* p_a ;
  const unsigned char* e_a ;
  const unsigned char* p_b ;
  const unsigned char* e_b ;

  /* Set up pointers and dispense with leading and trailing isspace()
   *
   * Dummy up if NULL
   */
  if (a != NULL)
    {
      p_a = a->body ;
      e_a = p_a + a->len ;

      while ((p_a < e_a) && isspace(*p_a))
        ++p_a ;
      while ((p_a < e_a) && isspace(*(e_a - 1)))
        --e_a ;
    }
  else
    {
      p_a = NULL ;
      e_a = NULL ;
    }

  if (b != NULL)
    {
      p_b = b->body ;
      e_b = p_b + b->len ;

      while ((p_b < e_b) && isspace(*p_b))
        ++p_b ;
      while ((p_b < e_b) && isspace(*(e_b - 1)))
        --e_b ;
    }
  else
    {
      p_b = NULL ;
      e_b = NULL ;
    } ;

  /* Now set about finding the first difference                         */
  while ((p_a != e_a) && (p_b != e_b))
    {
      if (isspace(*p_a) && isspace(*p_b))
        {
          do { ++p_a ; } while (isspace(*p_a)) ;
          do { ++p_b ; } while (isspace(*p_b)) ;
        } ;

      if (*p_a != *p_b)
        return (*p_a < *p_b) ? -1 : +1 ;

      ++p_a ;
      ++p_b ;
    } ;

  /* No difference before ran out of one or both                        */
  if      (p_a != e_a)
    return +1 ;
  else if (p_b != e_b)
    return -1 ;
  else
    return  0 ;
} ;