summaryrefslogtreecommitdiffstats
path: root/lib/qstring.c
blob: f847e0b02c2f096c6e933de3b50bc1cacf45f092 (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
/* 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_set_empty().
 */
extern qstring
qs_init_new(qstring qs, size_t len)
{
  if (qs == NULL)
    qs = XCALLOC(MTYPE_QSTRING, sizeof(qstring_t)) ;
  else
    memset(qs, 0, sizeof(qstring_t)) ;

  /* Zeroising has set:
   *
   *   body   = NULL -- no body
   *   size   = 0    -- no body
   *
   *   len    = 0
   *   cp     = 0
   *
   * Nothing more to do unless initial size != 0
   */

  if (len != 0)
    qs_alloc(qs, len) ;

  return qs ;
} ;

/*------------------------------------------------------------------------------
 * Allocate or reallocate so that string is big enough for the given length.
 *
 * Allocates to 16 byte boundaries.
 *
 * Returns: the number of bytes *allocated*, which includes the byte for
 *          possible trailing '\0'.
 *
 * NB: allocates EXTRA space for trailing '\0' beyond given length.
 */
extern size_t
qs_alloc(qstring qs, size_t len)
{
  len = (len + 0x10) & ~(size_t)(0x10 - 1) ;

  if (qs->body == NULL)
    {
      assert(qs->size == 0) ;
      qs->size = len ;
      qs->body = XMALLOC(MTYPE_QSTRING_BODY, qs->size) ;
    }
  else
    {
      assert(qs->size > 0) ;
      qs->size *= 2 ;
      if (qs->size < len)
        qs->size = len ;
      qs->body = XREALLOC(MTYPE_QSTRING_BODY, qs->body, qs->size) ;
    } ;

  return qs->size ;
} ;

/*------------------------------------------------------------------------------
 * Free body of qstring -- zeroise size, len and cp
 */
extern void
qs_free_body(qstring qs)
{
  if (qs->body != NULL)
    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, otherwise
 */
extern qstring
qs_reset(qstring qs, int free_structure)
{
  if (qs->body != NULL)
    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(0 and vprintf() type functions
 */

/*------------------------------------------------------------------------------
 * Formatted print to qstring -- cf printf()
 */
extern int
qs_printf(qstring qs, const char* format, ...)
{
  va_list args;
  int result ;

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

  return result;
} ;

/*------------------------------------------------------------------------------
 * Formatted print to qstring -- cf vprintf()
 *
 * 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.
 */
extern int
qs_vprintf(qstring qs, const char *format, va_list args)
{
  va_list  ac ;
  int len ;

  while (1)
    {
      va_copy(ac, args);
      qs->len = len = vsnprintf (qs->body, qs->size, format, ac) ;
      va_end(ac);

      if (len < (int)qs->size)
        return len ;            /* quit if done (or error)              */

      qs_alloc(qs, len) ;
    } ;
} ;

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

/*------------------------------------------------------------------------------
 * Set qstring to be copy of the given string.
 *
 * Sets qs->len to the length of the string (excluding trailing '\0')
 *
 * NB: if stc == NULL, sets qstring to be zero length string.
 */
extern size_t
qs_set(qstring qs, const char* src)
{
  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->len ;
} ;

/*------------------------------------------------------------------------------
 * Set qstring to be leading 'n' bytes of given string.
 *
 * NB: src string MUST be at least that long.
 *
 * NB: src may not be NULL unless len == 0.
 */
extern size_t
qs_set_n(qstring qs, const char* src, size_t n)
{
  qs_need(qs, n) ;      /* sets qs->len */
  if (n != 0)
    memcpy(qs->body, src, n) ;

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

  return n ;
} ;