Remove hacks which are not anymore needed since we now require Libgcrypt 1.4
[gnupg.git] / jnlib / strlist.c
blob14506930bbc12fbf390d15a9d393c34c7e539618
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 strlist_t
45 add_to_strlist( strlist_t *list, const char *string )
47 strlist_t sl;
49 sl = jnlib_xmalloc( sizeof *sl + strlen(string));
50 sl->flags = 0;
51 strcpy(sl->d, string);
52 sl->next = *list;
53 *list = sl;
54 return sl;
58 /* Same as add_to_strlist() but if is_utf8 is *not* set, a conversion
59 to UTF-8 is done. */
60 #ifdef JNLIB_NEED_UTF8CONV
61 strlist_t
62 add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
64 strlist_t sl;
66 if (is_utf8)
67 sl = add_to_strlist( list, string );
68 else
70 char *p = native_to_utf8( string );
71 sl = add_to_strlist( list, p );
72 jnlib_free ( p );
74 return sl;
76 #endif /* JNLIB_NEED_UTF8CONV*/
78 strlist_t
79 append_to_strlist( strlist_t *list, const char *string )
81 strlist_t r, sl;
83 sl = jnlib_xmalloc( sizeof *sl + strlen(string));
84 sl->flags = 0;
85 strcpy(sl->d, string);
86 sl->next = NULL;
87 if( !*list )
88 *list = sl;
89 else {
90 for( r = *list; r->next; r = r->next )
92 r->next = sl;
94 return sl;
98 #ifdef JNLIB_NEED_UTF8CONV
99 strlist_t
100 append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
102 strlist_t sl;
104 if( is_utf8 )
105 sl = append_to_strlist( list, string );
106 else
108 char *p = native_to_utf8 (string);
109 sl = append_to_strlist( list, p );
110 jnlib_free( p );
112 return sl;
114 #endif /* JNLIB_NEED_UTF8CONV */
117 /* Return a copy of LIST. */
118 strlist_t
119 strlist_copy (strlist_t list)
121 strlist_t newlist = NULL, sl, *last;
123 last = &newlist;
124 for (; list; list = list->next)
126 sl = jnlib_xmalloc (sizeof *sl + strlen (list->d));
127 sl->flags = list->flags;
128 strcpy(sl->d, list->d);
129 sl->next = NULL;
130 *last = sl;
131 last = &sl;
133 return newlist;
138 strlist_t
139 strlist_prev( strlist_t head, strlist_t node )
141 strlist_t n;
143 for(n=NULL; head && head != node; head = head->next )
144 n = head;
145 return n;
148 strlist_t
149 strlist_last( strlist_t node )
151 if( node )
152 for( ; node->next ; node = node->next )
154 return node;
158 char *
159 strlist_pop (strlist_t *list)
161 char *str=NULL;
162 strlist_t sl=*list;
164 if(sl)
166 str=jnlib_xmalloc(strlen(sl->d)+1);
167 strcpy(str,sl->d);
169 *list=sl->next;
170 jnlib_free(sl);
173 return str;