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 /* Add STRING to the LIST at the front. This function returns NULL
61 and sets ERRNO on memory shortage. */
63 add_to_strlist_try (strlist_t
*list
, const char *string
)
67 sl
= jnlib_malloc (sizeof *sl
+ strlen (string
));
71 strcpy (sl
->d
, string
);
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
82 #ifdef JNLIB_NEED_UTF8CONV
84 add_to_strlist2( strlist_t
*list
, const char *string
, int is_utf8
)
89 sl
= add_to_strlist( list
, string
);
92 char *p
= native_to_utf8( string
);
93 sl
= add_to_strlist( list
, p
);
98 #endif /* JNLIB_NEED_UTF8CONV*/
101 /* Add STRING to the LIST at the end. This function terminates the
102 process on memory shortage. */
104 append_to_strlist( strlist_t
*list
, const char *string
)
108 sl
= jnlib_xmalloc( sizeof *sl
+ strlen(string
));
110 strcpy(sl
->d
, string
);
115 for( r
= *list
; r
->next
; r
= r
->next
)
123 #ifdef JNLIB_NEED_UTF8CONV
125 append_to_strlist2( strlist_t
*list
, const char *string
, int is_utf8
)
130 sl
= append_to_strlist( list
, string
);
133 char *p
= native_to_utf8 (string
);
134 sl
= append_to_strlist( list
, p
);
139 #endif /* JNLIB_NEED_UTF8CONV */
142 /* Return a copy of LIST. This function terminates the process on
145 strlist_copy (strlist_t list
)
147 strlist_t newlist
= NULL
, sl
, *last
;
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
);
165 strlist_prev( strlist_t head
, strlist_t node
)
169 for(n
=NULL
; head
&& head
!= node
; head
= head
->next
)
175 strlist_last( strlist_t node
)
178 for( ; node
->next
; node
= node
->next
)
184 /* Remove the first item from LIST and return its content in an
185 allocated buffer. This function terminates the process on memory
188 strlist_pop (strlist_t
*list
)
195 str
=jnlib_xmalloc(strlen(sl
->d
)+1);