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"
32 string_free (struct string_s
*s
, int with_data
)
44 string_erase (struct string_s
*s
, ssize_t pos
, ssize_t len
)
52 s
->len
= s
->len
- pos
;
58 memmove (&s
->str
[pos
], &s
->str
[pos
+ len
], n
);
65 string_new (const char *str
)
67 struct string_s
*s
= xcalloc (1, sizeof (struct string_s
));
71 s
->str
= str_dup (str
);
78 s
->len
= strlen (s
->str
);
79 s
->allocated
= s
->len
+ 1;
86 string_append (struct string_s
*s
, const char *str
)
88 size_t len
= strlen (str
);
90 if (s
->allocated
< len
+ s
->len
+ 1)
92 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
98 s
->allocated
= s
->len
+ len
+ 1;
101 memcpy (&s
->str
[s
->len
], str
, len
);
108 string_truncate (struct string_s
*s
, size_t n
)
119 string_prepend (struct string_s
*s
, const char *str
)
121 size_t len
= strlen (str
);
123 if (s
->allocated
< s
->len
+ len
+ 1)
125 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
131 s
->allocated
= s
->len
+ len
+ 1;
134 memmove (&s
->str
[len
], s
->str
, s
->len
);
135 memcpy (s
->str
, str
, len
);
142 string_append_printf (struct string_s
*s
, const char *fmt
, ...)
149 len
= str_vasprintf (&buf
, fmt
, ap
);
155 s
= string_append (s
, buf
);
161 string_insert_c (struct string_s
*s
, ssize_t pos
, char c
)
163 size_t len
= s
->len
+ 2;
165 if (pos
>= s
->allocated
)
168 if (s
->allocated
< len
)
170 char *p
= xrealloc (s
->str
, len
* sizeof (char));
179 memmove (&s
->str
[pos
+ 1], &s
->str
[pos
], s
->len
- pos
);
181 s
->len
= pos
+ 1 == s
->allocated
? pos
+ 1 : s
->len
+ 1;
187 strv_length (char **a
)
197 /* 'str' must already be allocated. */
199 strv_cat (char **a
, char *str
)
201 int len
= a
? strv_length (a
) : 0;
204 dst
= xrealloc (a
, (len
+ 2) * sizeof (char **));
214 strv_printf (char ***array
, const char *fmt
, ...)
222 ret
= str_vasprintf (&buf
, fmt
, ap
);
228 a
= strv_cat (*array
, buf
);
244 for (p
= a
; p
&& *p
; p
++)
251 strv_dup (char **src
)
256 for (p
= src
; p
&& *p
; p
++)
259 char *xp
= str_dup (*p
);
267 tmp
= strv_cat (dst
, xp
);
282 strv_catv (char **dst
, char **src
)
294 for (p
= src
; p
&& *p
; p
++)
297 char *xp
= str_dup (*p
);
305 tmp
= strv_cat (d
, xp
);
320 str_split (const char *str
, const char *delim
, int count
)
324 const char *dp
= delim
, *p
;
325 size_t dlen
= strlen (delim
);
326 size_t pos
= 0, lastpos
= 0;
331 for (p
= str
; *p
; p
++)
339 size_t len
= pos
- lastpos
- dlen
+ 1;
341 xp
= xmalloc (len
+ 1);
348 memcpy (xp
, &str
[lastpos
], len
);
351 tmp
= strv_cat (dst
, xp
);
362 if (count
> 0 && index
== count
&& *(p
+ 1))
364 if (!strv_printf (&dst
, "%s", p
+ 1))
385 if (!strv_printf (&dst
, "%s", p
))
395 strv_join (char *delim
, char **a
)
399 struct string_s
*s
= string_new ("");
408 sp
= string_append_printf (s
, "%s%s", *p
, delim
409 && *(p
+ 1) ? delim
: "");
429 for (p
= str
; *p
; p
++)
439 str_chomp (char *str
)
441 int len
= strlen (str
);
442 char *p
= str
+ len
- 1;
444 while (len
&& isspace (*p
))
457 memmove (str
, p
, len
);
463 str_dup (const char *str
)
470 t
= xmalloc ((len
+ 1) * sizeof (char));
474 for (c
= 0; c
< len
; c
++)
482 str_asprintf (const char *fmt
, ...)
489 len
= str_vasprintf (&result
, fmt
, ap
);
491 return len
== -1 ? NULL
: result
;
496 str_vasprintf (char **result
, const char *fmt
, va_list ap
)
498 size_t alloc
= BUFSIZE
;
504 char *p
= xrealloc (buf
, alloc
* sizeof (char));
515 len
= vsnprintf (buf
, alloc
, fmt
, cp
);