2 Copyright (C) 2012, 2013
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
);
64 /* Be careful about allocations since other string_ functions may
65 * realloc the 'str' pointer. */
67 string_new_content (char *str
)
69 struct string_s
*s
= xcalloc (1, sizeof (struct string_s
));
72 s
->len
= strlen (s
->str
);
73 s
->allocated
= s
->len
+ 1;
78 string_new (const char *str
)
80 struct string_s
*s
= xcalloc (1, sizeof (struct string_s
));
84 s
->str
= str_dup (str
);
91 s
->len
= strlen (s
->str
);
92 s
->allocated
= s
->len
+ 1;
99 string_append (struct string_s
*s
, const char *str
)
101 size_t len
= strlen (str
);
103 if (s
->allocated
< len
+ s
->len
+ 1)
105 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
111 s
->allocated
= s
->len
+ len
+ 1;
114 memcpy (&s
->str
[s
->len
], str
, len
);
121 string_truncate (struct string_s
*s
, size_t n
)
132 string_prepend (struct string_s
*s
, const char *str
)
134 size_t len
= strlen (str
);
136 if (s
->allocated
< s
->len
+ len
+ 1)
138 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
144 s
->allocated
= s
->len
+ len
+ 1;
147 memmove (&s
->str
[len
], s
->str
, s
->len
);
148 memcpy (s
->str
, str
, len
);
155 string_append_printf (struct string_s
*s
, const char *fmt
, ...)
162 len
= str_vasprintf (&buf
, fmt
, ap
);
168 s
= string_append (s
, buf
);
174 string_insert_c (struct string_s
*s
, ssize_t pos
, char c
)
176 size_t len
= s
->len
+ 2;
178 if (pos
>= s
->allocated
)
181 if (s
->allocated
< len
)
183 char *p
= xrealloc (s
->str
, len
* sizeof (char));
192 memmove (&s
->str
[pos
+ 1], &s
->str
[pos
], s
->len
- pos
);
194 s
->len
= pos
+ 1 == s
->allocated
? pos
+ 1 : s
->len
+ 1;
200 strv_length (char **a
)
210 /* 'str' must already be allocated. */
212 strv_cat (char **a
, char *str
)
214 int len
= a
? strv_length (a
) : 0;
217 dst
= xrealloc (a
, (len
+ 2) * sizeof (char **));
227 strv_printf (char ***array
, const char *fmt
, ...)
235 ret
= str_vasprintf (&buf
, fmt
, ap
);
241 a
= strv_cat (*array
, buf
);
257 for (p
= a
; p
&& *p
; p
++)
264 strv_dup (char **src
)
269 for (p
= src
; p
&& *p
; p
++)
272 char *xp
= str_dup (*p
);
280 tmp
= strv_cat (dst
, xp
);
295 strv_catv (char **dst
, char **src
)
307 for (p
= src
; p
&& *p
; p
++)
310 char *xp
= str_dup (*p
);
318 tmp
= strv_cat (d
, xp
);
333 str_split (const char *str
, const char *delim
, int count
)
337 const char *dp
= delim
, *p
;
338 size_t dlen
= strlen (delim
);
339 size_t pos
= 0, lastpos
= 0;
344 for (p
= str
; *p
; p
++)
352 size_t len
= pos
- lastpos
- dlen
+ 1;
354 xp
= xmalloc (len
+ 1);
361 memcpy (xp
, &str
[lastpos
], len
);
364 tmp
= strv_cat (dst
, xp
);
375 if (count
> 0 && index
== count
&& *(p
+ 1))
377 if (!strv_printf (&dst
, "%s", p
+ 1))
398 if (!strv_printf (&dst
, "%s", p
))
408 strv_join (char *delim
, char **a
)
412 struct string_s
*s
= string_new ("");
421 sp
= string_append_printf (s
, "%s%s", *p
, delim
422 && *(p
+ 1) ? delim
: "");
442 for (p
= str
; *p
; p
++)
452 str_chomp (char *str
)
454 int len
= strlen (str
);
455 char *p
= str
+ len
- 1;
457 while (len
&& isspace (*p
))
470 memmove (str
, p
, len
);
476 str_dup (const char *str
)
483 t
= xmalloc ((len
+ 1) * sizeof (char));
487 for (c
= 0; c
< len
; c
++)
495 str_asprintf (const char *fmt
, ...)
502 len
= str_vasprintf (&result
, fmt
, ap
);
504 return len
== -1 ? NULL
: result
;
509 str_vasprintf (char **result
, const char *fmt
, va_list ap
)
511 size_t alloc
= BUFSIZE
;
517 char *p
= xrealloc (buf
, alloc
* sizeof (char));
528 len
= vsnprintf (buf
, alloc
, fmt
, cp
);