Code

utils error, strbuf: Added sdb_error_chomp(), sdb_strbuf_chomp().
[sysdb.git] / src / utils / strbuf.c
1 /*
2  * SysDB - src/utils/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 #include "utils/strbuf.h"
30 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
36 /*
37  * private data structures
38  */
40 struct sdb_strbuf {
41         char  *string;
42         size_t size;
43         size_t pos;
44 };
46 /*
47  * private helper functions
48  */
50 static int
51 strbuf_resize(sdb_strbuf_t *strbuf)
52 {
53         char *tmp;
55         assert(strbuf->size);
57         tmp = realloc(strbuf->string, 2 * strbuf->size);
58         if (! tmp)
59                 return -1;
61         strbuf->string = tmp;
62         strbuf->size *= 2;
63         return 0;
64 } /* strbuf_resize */
66 /*
67  * public API
68  */
70 sdb_strbuf_t *
71 sdb_strbuf_create(size_t size)
72 {
73         sdb_strbuf_t *strbuf;
75         if (! size)
76                 return NULL;
78         strbuf = calloc(1, sizeof(*strbuf));
79         if (! strbuf)
80                 return NULL;
82         strbuf->string = malloc(size);
83         if (! strbuf->string) {
84                 free(strbuf);
85                 return NULL;
86         }
88         strbuf->string[0] = '\0';
89         strbuf->size = size;
90         strbuf->pos  = 0;
92         return strbuf;
93 } /* sdb_strbuf_create */
95 void
96 sdb_strbuf_destroy(sdb_strbuf_t *strbuf)
97 {
98         if (! strbuf)
99                 return;
101         free(strbuf->string);
102         free(strbuf);
103 } /* sdb_strbuf_destroy */
105 ssize_t
106 sdb_strbuf_vappend(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
108         va_list aq;
109         int status;
111         if ((! strbuf) || (! fmt))
112                 return -1;
114         assert(strbuf->string[strbuf->pos] == '\0');
116         if (strbuf->pos >= strbuf->size)
117                 if (strbuf_resize(strbuf))
118                         return -1;
120         /* 'ap' is invalid after calling vsnprintf; thus copy before using it */
121         va_copy(aq, ap);
122         status = vsnprintf(strbuf->string + strbuf->pos,
123                         strbuf->size - strbuf->pos, fmt, ap);
125         if (status < 0) {
126                 va_end(aq);
127                 return status;
128         }
130         if ((size_t)status >= strbuf->size - strbuf->pos) {
131                 strbuf_resize(strbuf);
133                 /* reset string and try again */
134                 strbuf->string[strbuf->pos] = '\0';
135                 status = (int)sdb_strbuf_vappend(strbuf, fmt, aq);
136         }
137         else
138                 strbuf->pos += (size_t)status;
140         va_end(aq);
141         return (ssize_t)status;
142 } /* sdb_strbuf_vappend */
144 ssize_t
145 sdb_strbuf_append(sdb_strbuf_t *strbuf, const char *fmt, ...)
147         va_list ap;
148         ssize_t status;
150         va_start(ap, fmt);
151         status = sdb_strbuf_vappend(strbuf, fmt, ap);
152         va_end(ap);
154         return status;
155 } /* sdb_strbuf_append */
157 ssize_t
158 sdb_strbuf_vsprintf(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
160         if (! strbuf)
161                 return -1;
163         strbuf->string[0] = '\0';
164         strbuf->pos = 0;
166         return sdb_strbuf_vappend(strbuf, fmt, ap);
167 } /* sdb_strbuf_vsprintf */
169 ssize_t
170 sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...)
172         va_list ap;
173         ssize_t status;
175         va_start(ap, fmt);
176         status = sdb_strbuf_vsprintf(strbuf, fmt, ap);
177         va_end(ap);
179         return status;
180 } /* sdb_strbuf_sprintf */
182 ssize_t
183 sdb_strbuf_chomp(sdb_strbuf_t *strbuf)
185         ssize_t ret = 0;
187         if (! strbuf)
188                 return -1;
190         while ((strbuf->pos > 0)
191                         && (strbuf->string[strbuf->pos - 1] == '\n')) {
192                 --strbuf->pos;
193                 strbuf->string[strbuf->pos] = '\0';
194                 ++ret;
195         }
197         return ret;
198 } /* sdb_strbuf_chomp */
200 const char *
201 sdb_strbuf_string(sdb_strbuf_t *strbuf)
203         if (! strbuf)
204                 return NULL;
205         return strbuf->string;
206 } /* sdb_strbuf_string */
208 size_t
209 sdb_strbuf_len(sdb_strbuf_t *strbuf)
211         if (! strbuf)
212                 return 0;
213         return strbuf->pos;
214 } /* sdb_strbuf_string */
216 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */