6214c8a106c5f0035ba41a0bd999cc62cfce0d41
[cascardo/ovs.git] / lib / dynamic-string.c
1 /*
2  * Copyright (c) 2008 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 void
75 ds_put_char_multiple(struct ds *ds, char c, size_t n)
76 {
77     memset(ds_put_uninit(ds, n), c, n);
78 }
79
80 void
81 ds_put_buffer(struct ds *ds, const char *s, size_t n)
82 {
83     memcpy(ds_put_uninit(ds, n), s, n);
84 }
85
86 void
87 ds_put_cstr(struct ds *ds, const char *s)
88 {
89     size_t s_len = strlen(s);
90     memcpy(ds_put_uninit(ds, s_len), s, s_len);
91 }
92
93 void
94 ds_put_format(struct ds *ds, const char *format, ...)
95 {
96     va_list args;
97
98     va_start(args, format);
99     ds_put_format_valist(ds, format, args);
100     va_end(args);
101 }
102
103 void
104 ds_put_format_valist(struct ds *ds, const char *format, va_list args_)
105 {
106     va_list args;
107     size_t available;
108     int needed;
109
110     va_copy(args, args_);
111     available = ds->string ? ds->allocated - ds->length + 1 : 0;
112     needed = vsnprintf(&ds->string[ds->length], available, format, args);
113     va_end(args);
114
115     if (needed < available) {
116         ds->length += needed;
117     } else {
118         size_t available;
119
120         ds_reserve(ds, ds->length + needed);
121
122         va_copy(args, args_);
123         available = ds->allocated - ds->length + 1;
124         needed = vsnprintf(&ds->string[ds->length], available, format, args);
125         va_end(args);
126
127         assert(needed < available);
128         ds->length += needed;
129     }
130 }
131
132 void
133 ds_put_printable(struct ds *ds, const char *s, size_t n) 
134 {
135     ds_reserve(ds, ds->length + n);
136     while (n-- > 0) {
137         unsigned char c = *s++;
138         if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') {
139             ds_put_format(ds, "\\%03o", (int) c);
140         } else {
141             ds_put_char(ds, c);
142         }
143     }
144 }
145
146 void
147 ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
148 {
149     if (!tm) {
150         time_t now = time_now();
151         tm = localtime(&now);
152     }
153     for (;;) {
154         size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
155         size_t used = strftime(&ds->string[ds->length], avail, template, tm);
156         if (used) {
157             ds->length += used;
158             return;
159         }
160         ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail)); 
161     }
162 }
163
164 int
165 ds_get_line(struct ds *ds, FILE *file)
166 {
167     ds_clear(ds);
168     for (;;) {
169         int c = getc(file);
170         if (c == EOF) {
171             return ds->length ? 0 : EOF;
172         } else if (c == '\n') {
173             return 0;
174         } else {
175             ds_put_char(ds, c);
176         }
177     }
178 }
179
180 char *
181 ds_cstr(struct ds *ds)
182 {
183     if (!ds->string) {
184         ds_reserve(ds, 0);
185     }
186     ds->string[ds->length] = '\0';
187     return ds->string;
188 }
189
190 void
191 ds_destroy(struct ds *ds)
192 {
193     free(ds->string);
194 }
195
196 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
197  * line.  Numeric offsets are also included, starting at 'ofs' for the first
198  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
199  * are also rendered alongside. */
200 void
201 ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
202                 uintptr_t ofs, bool ascii)
203 {
204   const uint8_t *buf = buf_;
205   const size_t per_line = 16; /* Maximum bytes per line. */
206
207   while (size > 0)
208     {
209       size_t start, end, n;
210       size_t i;
211
212       /* Number of bytes on this line. */
213       start = ofs % per_line;
214       end = per_line;
215       if (end - start > size)
216         end = start + size;
217       n = end - start;
218
219       /* Print line. */
220       ds_put_format(ds, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
221       for (i = 0; i < start; i++)
222         ds_put_format(ds, "   ");
223       for (; i < end; i++)
224         ds_put_format(ds, "%02hhx%c",
225                 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
226       if (ascii)
227         {
228           for (; i < per_line; i++)
229             ds_put_format(ds, "   ");
230           ds_put_format(ds, "|");
231           for (i = 0; i < start; i++)
232             ds_put_format(ds, " ");
233           for (; i < end; i++) {
234               int c = buf[i - start];
235               ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
236           }
237           for (; i < per_line; i++)
238             ds_put_format(ds, " ");
239           ds_put_format(ds, "|");
240         }
241       ds_put_format(ds, "\n");
242
243       ofs += n;
244       buf += n;
245       size -= n;
246     }
247 }
248
249 int
250 ds_last(const struct ds *ds)
251 {
252     return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
253 }
254
255 void
256 ds_chomp(struct ds *ds, int c)
257 {
258     if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
259         ds->string[--ds->length] = '\0';
260     }
261 }