agent/
[gnupg.git] / jnlib / strlist.c
blobb31e6213a6d877a4e32f8419eaf25935eaa3887a
1 /* strlist.c - string helpers
2 * Copyright (C) 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
4 * This file is part of JNLIB.
6 * JNLIB is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * JNLIB is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <ctype.h>
26 #include "libjnlib-config.h"
27 #include "strlist.h"
28 #ifdef JNLIB_NEED_UTF8CONV
29 #include "utf8conv.h"
30 #endif
32 void
33 free_strlist( strlist_t sl )
35 strlist_t sl2;
37 for(; sl; sl = sl2 ) {
38 sl2 = sl->next;
39 jnlib_free(sl);
44 /* Add STRING to the LIST at the front. This function terminates the
45 process on memory shortage. */
46 strlist_t
47 add_to_strlist( strlist_t *list, const char *string )
49 strlist_t sl;
51 sl = jnlib_xmalloc( sizeof *sl + strlen(string));
52 sl->flags = 0;
53 strcpy(sl->d, string);
54 sl->next = *list;
55 *list = sl;
56 return sl;
60 /* Add STRING to the LIST at the front. This function returns NULL
61 and sets ERRNO on memory shortage. */
62 strlist_t
63 add_to_strlist_try (strlist_t *list, const char *string)
65 strlist_t sl;
67 sl = jnlib_malloc (sizeof *sl + strlen (string));
68 if (sl)
70 sl->flags = 0;
71 strcpy (sl->d, string);
72 sl->next = *list;
73 *list = sl;
75 return sl;
79 /* Same as add_to_strlist() but if IS_UTF8 is *not* set, a conversion
80 to UTF-8 is done. This function terminates the process on memory
81 shortage. */
82 #ifdef JNLIB_NEED_UTF8CONV
83 strlist_t
84 add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
86 strlist_t sl;
88 if (is_utf8)
89 sl = add_to_strlist( list, string );
90 else
92 char *p = native_to_utf8( string );
93 sl = add_to_strlist( list, p );
94 jnlib_free ( p );
96 return sl;
98 #endif /* JNLIB_NEED_UTF8CONV*/
101 /* Add STRING to the LIST at the end. This function terminates the
102 process on memory shortage. */
103 strlist_t
104 append_to_strlist( strlist_t *list, const char *string )
106 strlist_t r, sl;
108 sl = jnlib_xmalloc( sizeof *sl + strlen(string));
109 sl->flags = 0;
110 strcpy(sl->d, string);
111 sl->next = NULL;
112 if( !*list )
113 *list = sl;
114 else {
115 for( r = *list; r->next; r = r->next )
117 r->next = sl;
119 return sl;
123 #ifdef JNLIB_NEED_UTF8CONV
124 strlist_t
125 append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
127 strlist_t sl;
129 if( is_utf8 )
130 sl = append_to_strlist( list, string );
131 else
133 char *p = native_to_utf8 (string);
134 sl = append_to_strlist( list, p );
135 jnlib_free( p );
137 return sl;
139 #endif /* JNLIB_NEED_UTF8CONV */
142 /* Return a copy of LIST. This function terminates the process on
143 memory shortage.*/
144 strlist_t
145 strlist_copy (strlist_t list)
147 strlist_t newlist = NULL, sl, *last;
149 last = &newlist;
150 for (; list; list = list->next)
152 sl = jnlib_xmalloc (sizeof *sl + strlen (list->d));
153 sl->flags = list->flags;
154 strcpy(sl->d, list->d);
155 sl->next = NULL;
156 *last = sl;
157 last = &sl;
159 return newlist;
164 strlist_t
165 strlist_prev( strlist_t head, strlist_t node )
167 strlist_t n;
169 for(n=NULL; head && head != node; head = head->next )
170 n = head;
171 return n;
174 strlist_t
175 strlist_last( strlist_t node )
177 if( node )
178 for( ; node->next ; node = node->next )
180 return node;
184 /* Remove the first item from LIST and return its content in an
185 allocated buffer. This function terminates the process on memory
186 shortage. */
187 char *
188 strlist_pop (strlist_t *list)
190 char *str=NULL;
191 strlist_t sl=*list;
193 if(sl)
195 str=jnlib_xmalloc(strlen(sl->d)+1);
196 strcpy(str,sl->d);
198 *list=sl->next;
199 jnlib_free(sl);
202 return str;