Code

strbuf: Added a string buffer class for dynamically growing strings.
[libjunos.git] / src / strbuf.c
1 /*
2  * libJUNOS - src/strbuf.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * String buffer object.
30  */
32 #include "junos.h"
34 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
40 /*
41  * private data structures
42  */
44 struct junos_strbuf {
45         char  *string;
46         size_t size;
47         size_t pos;
48 };
50 /*
51  * private helper functions
52  */
54 static int
55 strbuf_resize(junos_strbuf_t *strbuf)
56 {
57         char *tmp;
59         tmp = realloc(strbuf->string, 2 * strbuf->size);
60         if (! tmp)
61                 return -1;
63         strbuf->string = tmp;
64         strbuf->size *= 2;
66         strbuf->string[strbuf->pos] = '\0';
67         return 0;
68 } /* strbuf_resize */
70 /*
71  * public API
72  */
74 junos_strbuf_t *
75 junos_strbuf_new(size_t size)
76 {
77         junos_strbuf_t *strbuf;
79         strbuf = calloc(1, sizeof(*strbuf));
80         if (! strbuf)
81                 return NULL;
83         if (! size)
84                 return strbuf;
86         strbuf->string = malloc(size);
87         if (strbuf->string) {
88                 strbuf->string[0] = '\0';
89                 strbuf->size = size;
90         }
92         return strbuf;
93 } /* junos_strbuf_new */
95 void
96 junos_strbuf_free(junos_strbuf_t *strbuf)
97 {
98         if (! strbuf)
99                 return;
101         free(strbuf->string);
102         free(strbuf);
103 } /* junos_strbuf_free */
105 ssize_t
106 junos_strbuf_vsprintf(junos_strbuf_t *strbuf, const char *fmt, va_list ap)
108         int status;
110         if (! strbuf)
111                 return -1;
113         assert(strbuf->string[strbuf->pos] == '\0');
115         if (strbuf->pos >= strbuf->size)
116                 if (strbuf_resize(strbuf))
117                         return -1;
119         status = vsnprintf(strbuf->string + strbuf->pos,
120                         strbuf->size - strbuf->pos, fmt, ap);
122         if (status < 0)
123                 return status;
125         if ((size_t)status >= strbuf->size - strbuf->pos) {
126                 strbuf_resize(strbuf);
127                 return junos_strbuf_vsprintf(strbuf, fmt, ap);
128         }
130         strbuf->pos += (size_t)status;
131         return (ssize_t)strbuf->pos;
132 } /* junos_strbuf_vsprintf */
134 ssize_t
135 junos_strbuf_sprintf(junos_strbuf_t *strbuf, const char *fmt, ...)
137         va_list ap;
138         ssize_t status;
140         va_start(ap, fmt);
141         status = junos_strbuf_vsprintf(strbuf, fmt, ap);
142         va_end(ap);
144         return status;
145 } /* junos_strbuf_sprintf */
147 char *
148 junos_strbuf_string(junos_strbuf_t *strbuf)
150         if (! strbuf)
151                 return NULL;
152         return strbuf->string;
153 } /* junos_strbuf_string */
155 size_t
156 junos_strbuf_len(junos_strbuf_t *strbuf)
158         if (! strbuf)
159                 return 0;
160         return strbuf->pos;
161 } /* junos_strbuf_string */
163 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */