2007-05-19 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / jnlib / strlist.c
blob01f483a8abaadebb724e1e83f4ca7837c752094b
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 2.1 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
22 #include <config.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <ctype.h>
28 #include "libjnlib-config.h"
29 #include "strlist.h"
30 #ifdef JNLIB_NEED_UTF8CONV
31 #include "utf8conv.h"
32 #endif
34 void
35 free_strlist( strlist_t sl )
37 strlist_t sl2;
39 for(; sl; sl = sl2 ) {
40 sl2 = sl->next;
41 jnlib_free(sl);
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 /* Same as add_to_strlist() but if is_utf8 is *not* set, a conversion
61 to UTF-8 is done. */
62 #ifdef JNLIB_NEED_UTF8CONV
63 strlist_t
64 add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
66 strlist_t sl;
68 if (is_utf8)
69 sl = add_to_strlist( list, string );
70 else
72 char *p = native_to_utf8( string );
73 sl = add_to_strlist( list, p );
74 jnlib_free ( p );
76 return sl;
78 #endif /* JNLIB_NEED_UTF8CONV*/
80 strlist_t
81 append_to_strlist( strlist_t *list, const char *string )
83 strlist_t r, sl;
85 sl = jnlib_xmalloc( sizeof *sl + strlen(string));
86 sl->flags = 0;
87 strcpy(sl->d, string);
88 sl->next = NULL;
89 if( !*list )
90 *list = sl;
91 else {
92 for( r = *list; r->next; r = r->next )
94 r->next = sl;
96 return sl;
100 #ifdef JNLIB_NEED_UTF8CONV
101 strlist_t
102 append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
104 strlist_t sl;
106 if( is_utf8 )
107 sl = append_to_strlist( list, string );
108 else
110 char *p = native_to_utf8 (string);
111 sl = append_to_strlist( list, p );
112 jnlib_free( p );
114 return sl;
116 #endif /* JNLIB_NEED_UTF8CONV */
119 /* Return a copy of LIST. */
120 strlist_t
121 strlist_copy (strlist_t list)
123 strlist_t newlist = NULL, sl, *last;
125 last = &newlist;
126 for (; list; list = list->next)
128 sl = jnlib_xmalloc (sizeof *sl + strlen (list->d));
129 sl->flags = list->flags;
130 strcpy(sl->d, list->d);
131 sl->next = NULL;
132 *last = sl;
133 last = &sl;
135 return newlist;
140 strlist_t
141 strlist_prev( strlist_t head, strlist_t node )
143 strlist_t n;
145 for(n=NULL; head && head != node; head = head->next )
146 n = head;
147 return n;
150 strlist_t
151 strlist_last( strlist_t node )
153 if( node )
154 for( ; node->next ; node = node->next )
156 return node;
160 char *
161 strlist_pop (strlist_t *list)
163 char *str=NULL;
164 strlist_t sl=*list;
166 if(sl)
168 str=jnlib_xmalloc(strlen(sl->d)+1);
169 strcpy(str,sl->d);
171 *list=sl->next;
172 jnlib_free(sl);
175 return str;