3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
28 #include "util-string.h"
31 void string_free(struct string_s
*s
, int with_data
)
42 struct string_s
*string_erase(struct string_s
*s
, ssize_t pos
, ssize_t len
)
49 s
->len
= s
->len
- pos
;
55 memmove(&s
->str
[pos
], &s
->str
[pos
+len
], n
);
61 struct string_s
*string_new(const char *str
)
63 struct string_s
*s
= xcalloc(1, sizeof(struct string_s
));
66 s
->str
= str_dup(str
);
72 s
->len
= strlen(s
->str
);
73 s
->allocated
= s
->len
+ 1;
79 struct string_s
*string_append(struct string_s
*s
, const char *str
)
81 size_t len
= strlen(str
);
83 if (s
->allocated
< len
+ s
->len
+ 1) {
84 char *p
= xrealloc(s
->str
, (len
+ s
->len
+ 1)*sizeof(char));
90 s
->allocated
= s
->len
+ len
+ 1;
93 memcpy(&s
->str
[s
->len
], str
, len
);
99 struct string_s
*string_truncate(struct string_s
*s
, size_t n
)
109 struct string_s
*string_prepend(struct string_s
*s
, const char *str
)
111 size_t len
= strlen(str
);
113 if (s
->allocated
< s
->len
+ len
+ 1) {
114 char *p
= xrealloc(s
->str
, (len
+ s
->len
+ 1)*sizeof(char));
120 s
->allocated
= s
->len
+ len
+ 1;
123 memmove(&s
->str
[len
], s
->str
, s
->len
);
124 memcpy(s
->str
, str
, len
);
130 struct string_s
*string_append_printf(struct string_s
*s
, const char *fmt
, ...)
137 len
= str_vasprintf(&buf
, fmt
, ap
);
143 s
= string_append(s
, buf
);
148 struct string_s
*string_insert_c(struct string_s
*s
, ssize_t pos
, char c
)
150 size_t len
= s
->len
+ 2;
152 if (pos
>= s
->allocated
)
155 if (s
->allocated
< len
) {
156 char *p
= xrealloc(s
->str
, len
*sizeof(char));
165 memmove(&s
->str
[pos
+1], &s
->str
[pos
], s
->len
- pos
);
167 s
->len
= pos
+1 == s
->allocated
? pos
+1 : s
->len
+1;
172 int strv_length(char **a
)
182 /* 'str' must already be allocated. */
183 char **strv_cat(char **a
, char *str
)
185 int len
= a
? strv_length(a
) : 0;
188 dst
= xrealloc(a
, (len
+2)*sizeof(char **));
197 int strv_printf(char ***array
, const char *fmt
, ...)
205 ret
= str_vasprintf(&buf
, fmt
, ap
);
211 a
= strv_cat(*array
, buf
);
221 void strv_free(char **a
)
225 for (p
= a
; p
&& *p
; p
++)
231 char **strv_dup(char **src
)
236 for (p
= src
; p
&& *p
; p
++) {
238 char *xp
= str_dup(*p
);
245 tmp
= strv_cat(dst
, xp
);
258 char **strv_catv(char **dst
, char **src
)
269 for (p
= src
; p
&& *p
; p
++) {
271 char *xp
= str_dup(*p
);
278 tmp
= strv_cat(d
, xp
);
291 char **str_split(const char *str
, const char *delim
, int count
)
295 const char *dp
= delim
, *p
;
296 size_t dlen
= strlen(delim
);
297 size_t pos
= 0, lastpos
= 0;
302 for (p
= str
; *p
; p
++) {
307 size_t len
= pos
- lastpos
- dlen
+ 1;
315 memcpy(xp
, &str
[lastpos
], len
);
318 tmp
= strv_cat(dst
, xp
);
328 if (count
> 0 && index
== count
&& *(p
+1)) {
329 if (!strv_printf(&dst
, "%s", p
+1)) {
349 if (!strv_printf(&dst
, "%s", p
)) {
357 char *strv_join(char *delim
, char **a
)
361 struct string_s
*s
= string_new("");
366 for (p
= a
; *p
; p
++) {
369 sp
= string_append_printf(s
, "%s%s", *p
, delim
&& *(p
+1) ? delim
: "");
383 char *str_down(char *str
)
387 for (p
= str
; *p
; p
++) {
395 char *str_chomp(char *str
)
397 int len
= strlen(str
);
400 while (len
&& isspace(*p
)) {
406 while (isspace(*p
)) {
411 memmove(str
, p
, len
);
416 char *str_dup(const char *str
)
423 t
= xmalloc((len
+1)*sizeof(char));
427 for (c
= 0; c
< len
; c
++)
434 char *str_asprintf(const char *fmt
, ...)
441 len
= str_vasprintf(&result
, fmt
, ap
);
443 return len
== -1 ? NULL
: result
;
447 int str_vasprintf(char **result
, const char *fmt
, va_list ap
)
449 size_t alloc
= BUFSIZE
;
454 char *p
= xrealloc(buf
, alloc
*sizeof(char));
464 len
= vsnprintf(buf
, alloc
, fmt
, cp
);