Avoid shadowing local variable names.
[sliver-openvswitch.git] / lib / dynamic-string.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "dynamic-string.h"
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23 #include "timeval.h"
24 #include "util.h"
25
26 void
27 ds_init(struct ds *ds)
28 {
29     ds->string = NULL;
30     ds->length = 0;
31     ds->allocated = 0;
32 }
33
34 void
35 ds_clear(struct ds *ds)
36 {
37     ds->length = 0;
38 }
39
40 void
41 ds_truncate(struct ds *ds, size_t new_length)
42 {
43     if (ds->length > new_length) {
44         ds->length = new_length;
45         ds->string[new_length] = '\0';
46     }
47 }
48
49 void
50 ds_reserve(struct ds *ds, size_t min_length)
51 {
52     if (min_length > ds->allocated || !ds->string) {
53         ds->allocated += MAX(min_length, ds->allocated);
54         ds->allocated = MAX(8, ds->allocated);
55         ds->string = xrealloc(ds->string, ds->allocated + 1);
56     }
57 }
58
59 char *
60 ds_put_uninit(struct ds *ds, size_t n)
61 {
62     ds_reserve(ds, ds->length + n);
63     ds->length += n;
64     ds->string[ds->length] = '\0';
65     return &ds->string[ds->length - n];
66 }
67
68 void
69 ds_put_char__(struct ds *ds, char c)
70 {
71     *ds_put_uninit(ds, 1) = c;
72 }
73
74 /* Appends unicode code point 'uc' to 'ds' in UTF-8 encoding. */
75 void
76 ds_put_utf8(struct ds *ds, int uc)
77 {
78     if (uc <= 0x7f) {
79         ds_put_char(ds, uc);
80     } else if (uc <= 0x7ff) {
81         ds_put_char(ds, 0xc0 | (uc >> 6));
82         ds_put_char(ds, 0x80 | (uc & 0x3f));
83     } else if (uc <= 0xffff) {
84         ds_put_char(ds, 0xe0 | (uc >> 12));
85         ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f));
86         ds_put_char(ds, 0x80 | (uc & 0x3f));
87     } else if (uc <= 0x10ffff) {
88         ds_put_char(ds, 0xf0 | (uc >> 18));
89         ds_put_char(ds, 0x80 | ((uc >> 12) & 0x3f));
90         ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f));
91         ds_put_char(ds, 0x80 | (uc & 0x3f));
92     } else {
93         /* Invalid code point.  Insert the Unicode general substitute
94          * REPLACEMENT CHARACTER. */
95         ds_put_utf8(ds, 0xfffd);
96     }
97 }
98
99 void
100 ds_put_char_multiple(struct ds *ds, char c, size_t n)
101 {
102     memset(ds_put_uninit(ds, n), c, n);
103 }
104
105 void
106 ds_put_buffer(struct ds *ds, const char *s, size_t n)
107 {
108     memcpy(ds_put_uninit(ds, n), s, n);
109 }
110
111 void
112 ds_put_cstr(struct ds *ds, const char *s)
113 {
114     size_t s_len = strlen(s);
115     memcpy(ds_put_uninit(ds, s_len), s, s_len);
116 }
117
118 void
119 ds_put_and_free_cstr(struct ds *ds, char *s)
120 {
121     ds_put_cstr(ds, s);
122     free(s);
123 }
124
125 void
126 ds_put_format(struct ds *ds, const char *format, ...)
127 {
128     va_list args;
129
130     va_start(args, format);
131     ds_put_format_valist(ds, format, args);
132     va_end(args);
133 }
134
135 void
136 ds_put_format_valist(struct ds *ds, const char *format, va_list args_)
137 {
138     va_list args;
139     size_t available;
140     int needed;
141
142     va_copy(args, args_);
143     available = ds->string ? ds->allocated - ds->length + 1 : 0;
144     needed = vsnprintf(&ds->string[ds->length], available, format, args);
145     va_end(args);
146
147     if (needed < available) {
148         ds->length += needed;
149     } else {
150         ds_reserve(ds, ds->length + needed);
151
152         va_copy(args, args_);
153         available = ds->allocated - ds->length + 1;
154         needed = vsnprintf(&ds->string[ds->length], available, format, args);
155         va_end(args);
156
157         assert(needed < available);
158         ds->length += needed;
159     }
160 }
161
162 void
163 ds_put_printable(struct ds *ds, const char *s, size_t n)
164 {
165     ds_reserve(ds, ds->length + n);
166     while (n-- > 0) {
167         unsigned char c = *s++;
168         if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') {
169             ds_put_format(ds, "\\%03o", (int) c);
170         } else {
171             ds_put_char(ds, c);
172         }
173     }
174 }
175
176 void
177 ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
178 {
179     if (!tm) {
180         time_t now = time_wall();
181         tm = localtime(&now);
182     }
183     for (;;) {
184         size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
185         size_t used = strftime(&ds->string[ds->length], avail, template, tm);
186         if (used) {
187             ds->length += used;
188             return;
189         }
190         ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail));
191     }
192 }
193
194 int
195 ds_get_line(struct ds *ds, FILE *file)
196 {
197     ds_clear(ds);
198     for (;;) {
199         int c = getc(file);
200         if (c == EOF) {
201             return ds->length ? 0 : EOF;
202         } else if (c == '\n') {
203             return 0;
204         } else {
205             ds_put_char(ds, c);
206         }
207     }
208 }
209
210 char *
211 ds_cstr(struct ds *ds)
212 {
213     if (!ds->string) {
214         ds_reserve(ds, 0);
215     }
216     ds->string[ds->length] = '\0';
217     return ds->string;
218 }
219
220 const char *
221 ds_cstr_ro(const struct ds *ds)
222 {
223     return ds_cstr((struct ds *) ds);
224 }
225
226 /* Returns a null-terminated string representing the current contents of 'ds',
227  * which the caller is expected to free with free(), then clears the contents
228  * of 'ds'. */
229 char *
230 ds_steal_cstr(struct ds *ds)
231 {
232     char *s = ds_cstr(ds);
233     ds_init(ds);
234     return s;
235 }
236
237 void
238 ds_destroy(struct ds *ds)
239 {
240     free(ds->string);
241 }
242
243 /* Swaps the content of 'a' and 'b'. */
244 void
245 ds_swap(struct ds *a, struct ds *b)
246 {
247     struct ds temp = *a;
248     *a = *b;
249     *b = temp;
250 }
251
252 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
253  * line.  Numeric offsets are also included, starting at 'ofs' for the first
254  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
255  * are also rendered alongside. */
256 void
257 ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
258                 uintptr_t ofs, bool ascii)
259 {
260   const uint8_t *buf = buf_;
261   const size_t per_line = 16; /* Maximum bytes per line. */
262
263   while (size > 0)
264     {
265       size_t start, end, n;
266       size_t i;
267
268       /* Number of bytes on this line. */
269       start = ofs % per_line;
270       end = per_line;
271       if (end - start > size)
272         end = start + size;
273       n = end - start;
274
275       /* Print line. */
276       ds_put_format(ds, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
277       for (i = 0; i < start; i++)
278         ds_put_format(ds, "   ");
279       for (; i < end; i++)
280         ds_put_format(ds, "%02hhx%c",
281                 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
282       if (ascii)
283         {
284           for (; i < per_line; i++)
285             ds_put_format(ds, "   ");
286           ds_put_format(ds, "|");
287           for (i = 0; i < start; i++)
288             ds_put_format(ds, " ");
289           for (; i < end; i++) {
290               int c = buf[i - start];
291               ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
292           }
293           for (; i < per_line; i++)
294             ds_put_format(ds, " ");
295           ds_put_format(ds, "|");
296         }
297       ds_put_format(ds, "\n");
298
299       ofs += n;
300       buf += n;
301       size -= n;
302     }
303 }
304
305 int
306 ds_last(const struct ds *ds)
307 {
308     return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
309 }
310
311 void
312 ds_chomp(struct ds *ds, int c)
313 {
314     if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
315         ds->string[--ds->length] = '\0';
316     }
317 }