1 /* strlist.c -  string helpers
2  * Copyright (C) 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
3  *
4  * This file is part of JNLIB.
5  *
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.
10  *
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.
15  *
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.
20  */
21 
22 #include <config.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <ctype.h>
27 
28 #include "libjnlib-config.h"
29 #include "strlist.h"
30 #ifdef JNLIB_NEED_UTF8CONV
31 #include "utf8conv.h"
32 #endif
33 
34 void
free_strlist(strlist_t sl)35 free_strlist( strlist_t sl )
36 {
37     strlist_t sl2;
38 
39     for(; sl; sl = sl2 ) {
40 	sl2 = sl->next;
41 	jnlib_free(sl);
42     }
43 }
44 
45 
46 strlist_t
add_to_strlist(strlist_t * list,const char * string)47 add_to_strlist( strlist_t *list, const char *string )
48 {
49     strlist_t sl;
50 
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;
57 }
58 
59 
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
add_to_strlist2(strlist_t * list,const char * string,int is_utf8)64 add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
65 {
66   strlist_t sl;
67 
68   if (is_utf8)
69     sl = add_to_strlist( list, string );
70   else
71     {
72       char *p = native_to_utf8( string );
73       sl = add_to_strlist( list, p );
74       jnlib_free ( p );
75     }
76   return sl;
77 }
78 #endif /* JNLIB_NEED_UTF8CONV*/
79 
80 strlist_t
append_to_strlist(strlist_t * list,const char * string)81 append_to_strlist( strlist_t *list, const char *string )
82 {
83     strlist_t r, sl;
84 
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 )
93 	    ;
94 	r->next = sl;
95     }
96     return sl;
97 }
98 
99 
100 #ifdef JNLIB_NEED_UTF8CONV
101 strlist_t
append_to_strlist2(strlist_t * list,const char * string,int is_utf8)102 append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
103 {
104   strlist_t sl;
105 
106   if( is_utf8 )
107     sl = append_to_strlist( list, string );
108   else
109     {
110       char *p = native_to_utf8 (string);
111       sl = append_to_strlist( list, p );
112       jnlib_free( p );
113     }
114   return sl;
115 }
116 #endif /* JNLIB_NEED_UTF8CONV */
117 
118 
119 /* Return a copy of LIST. */
120 strlist_t
strlist_copy(strlist_t list)121 strlist_copy (strlist_t list)
122 {
123   strlist_t newlist = NULL, sl, *last;
124 
125   last = &newlist;
126   for (; list; list = list->next)
127     {
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;
134     }
135   return newlist;
136 }
137 
138 
139 
140 strlist_t
strlist_prev(strlist_t head,strlist_t node)141 strlist_prev( strlist_t head, strlist_t node )
142 {
143     strlist_t n;
144 
145     for(n=NULL; head && head != node; head = head->next )
146 	n = head;
147     return n;
148 }
149 
150 strlist_t
strlist_last(strlist_t node)151 strlist_last( strlist_t node )
152 {
153     if( node )
154 	for( ; node->next ; node = node->next )
155 	    ;
156     return node;
157 }
158 
159 
160 char *
strlist_pop(strlist_t * list)161 strlist_pop (strlist_t *list)
162 {
163   char *str=NULL;
164   strlist_t sl=*list;
165 
166   if(sl)
167     {
168       str=jnlib_xmalloc(strlen(sl->d)+1);
169       strcpy(str,sl->d);
170 
171       *list=sl->next;
172       jnlib_free(sl);
173     }
174 
175   return str;
176 }
177 
178