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/>.
26 #include "libjnlib-config.h"
28 #ifdef JNLIB_NEED_UTF8CONV
33 free_strlist( strlist_t sl
)
37 for(; sl
; sl
= sl2
) {
44 /* Add STRING to the LIST at the front. This function terminates the
45 process on memory shortage. */
47 add_to_strlist( strlist_t
*list
, const char *string
)
51 sl
= jnlib_xmalloc( sizeof *sl
+ strlen(string
));
53 strcpy(sl
->d
, string
);
60 /* Same as add_to_strlist() but if IS_UTF8 is *not* set, a conversion
61 to UTF-8 is done. This function terminates the process on memory
63 #ifdef JNLIB_NEED_UTF8CONV
65 add_to_strlist2( strlist_t
*list
, const char *string
, int is_utf8
)
70 sl
= add_to_strlist( list
, string
);
73 char *p
= native_to_utf8( string
);
74 sl
= add_to_strlist( list
, p
);
79 #endif /* JNLIB_NEED_UTF8CONV*/
82 /* Add STRING to the LIST at the end. This function terminates the
83 process on memory shortage. */
85 append_to_strlist( strlist_t
*list
, const char *string
)
89 sl
= jnlib_xmalloc( sizeof *sl
+ strlen(string
));
91 strcpy(sl
->d
, string
);
96 for( r
= *list
; r
->next
; r
= r
->next
)
104 #ifdef JNLIB_NEED_UTF8CONV
106 append_to_strlist2( strlist_t
*list
, const char *string
, int is_utf8
)
111 sl
= append_to_strlist( list
, string
);
114 char *p
= native_to_utf8 (string
);
115 sl
= append_to_strlist( list
, p
);
120 #endif /* JNLIB_NEED_UTF8CONV */
123 /* Return a copy of LIST. This function terminates the process on
126 strlist_copy (strlist_t list
)
128 strlist_t newlist
= NULL
, sl
, *last
;
131 for (; list
; list
= list
->next
)
133 sl
= jnlib_xmalloc (sizeof *sl
+ strlen (list
->d
));
134 sl
->flags
= list
->flags
;
135 strcpy(sl
->d
, list
->d
);
146 strlist_prev( strlist_t head
, strlist_t node
)
150 for(n
=NULL
; head
&& head
!= node
; head
= head
->next
)
156 strlist_last( strlist_t node
)
159 for( ; node
->next
; node
= node
->next
)
165 /* Remove the first item from LIST and return its content in an
166 allocated buffer. This function terminates the process on memory
169 strlist_pop (strlist_t
*list
)
176 str
=jnlib_xmalloc(strlen(sl
->d
)+1);