1 /* strlist.c - string helpers
2 * Copyright (C) 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
3 * Copyright (C) 2015 g10 Code GmbH
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify it
8 * under the terms of either
10 * - the GNU Lesser General Public License as published by the Free
11 * Software Foundation; either version 3 of the License, or (at
12 * your option) any later version.
16 * - the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * or both in parallel, as here.
22 * GnuPG is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copies of the GNU General Public License
28 * and the GNU Lesser General Public License along with this program;
29 * if not, see <http://www.gnu.org/licenses/>.
39 #include "common-defs.h"
44 free_strlist( strlist_t sl )
48 for(; sl; sl = sl2 ) {
55 /* Add STRING to the LIST at the front. This function terminates the
56 process on memory shortage. */
58 add_to_strlist( strlist_t *list, const char *string )
62 sl = xmalloc( sizeof *sl + strlen(string));
64 strcpy(sl->d, string);
71 /* Add STRING to the LIST at the front. This function returns NULL
72 and sets ERRNO on memory shortage. */
74 add_to_strlist_try (strlist_t *list, const char *string)
78 sl = xtrymalloc (sizeof *sl + strlen (string));
82 strcpy (sl->d, string);
90 /* Same as add_to_strlist() but if IS_UTF8 is *not* set, a conversion
91 to UTF-8 is done. This function terminates the process on memory
94 add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
99 sl = add_to_strlist( list, string );
102 char *p = native_to_utf8( string );
103 sl = add_to_strlist( list, p );
110 /* Add STRING to the LIST at the end. This function terminates the
111 process on memory shortage. */
113 append_to_strlist( strlist_t *list, const char *string )
117 sl = xmalloc( sizeof *sl + strlen(string));
119 strcpy(sl->d, string);
124 for( r = *list; r->next; r = r->next )
133 append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
138 sl = append_to_strlist( list, string );
141 char *p = native_to_utf8 (string);
142 sl = append_to_strlist( list, p );
149 /* Return a copy of LIST. This function terminates the process on
152 strlist_copy (strlist_t list)
154 strlist_t newlist = NULL, sl, *last;
157 for (; list; list = list->next)
159 sl = xmalloc (sizeof *sl + strlen (list->d));
160 sl->flags = list->flags;
161 strcpy(sl->d, list->d);
172 strlist_prev( strlist_t head, strlist_t node )
176 for(n=NULL; head && head != node; head = head->next )
182 strlist_last( strlist_t node )
185 for( ; node->next ; node = node->next )
191 /* Remove the first item from LIST and return its content in an
192 allocated buffer. This function terminates the process on memory
195 strlist_pop (strlist_t *list)
202 str = xmalloc(strlen(sl->d)+1);
212 /* Return the first element of the string list HAYSTACK whose string
213 matches NEEDLE. If no elements match, return NULL. */
215 strlist_find (strlist_t haystack, const char *needle)
219 haystack = haystack->next)
220 if (strcmp (haystack->d, needle) == 0)