907c5085f6410c0884a759f2ce35f06905371e54
1 #include <ctype.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <glib.h>
8 #include "config.h"
9 #include "support.h"
11 #ifdef HAVE_LOCALE_H
12 #include <locale.h>
13 #endif
15 #ifdef HAVE_ICONV
16 #include <iconv.h>
17 #endif
19 #ifdef HAVE_LANGINFO_CODESET
20 #include <langinfo.h>
21 #endif
23 #define BUFSIZE 1024
26 static char *charset = NULL;
28 #ifdef HAVE_LOCALE_H
29 static char *locale = NULL;
30 #endif
32 #ifdef HAVE_ICONV
33 iconv_t iconv_from_uft8 = (iconv_t)(-1);
34 iconv_t iconv_to_uft8 = (iconv_t)(-1);
35 #endif
37 char *
38 trim(char *str)
39 {
40 char *end;
42 if( str==NULL )
43 return NULL;
45 while( IS_WHITESPACE(*str) )
46 str++;
48 end=str+strlen(str)-1;
49 while( end>str && IS_WHITESPACE(*end) )
50 {
51 *end = '\0';
52 end--;
53 }
54 return str;
55 }
57 char *
58 remove_trailing_slash(char *path)
59 {
60 int len;
62 if( path==NULL )
63 return NULL;
65 len=strlen(path);
66 if( len>1 && path[len-1] == '/' )
67 path[len-1] = '\0';
69 return path;
70 }
72 char *
73 lowerstr(char *str)
74 {
75 size_t i;
76 size_t len = strlen(str);
78 if( str==NULL )
79 return NULL;
81 i=0;
82 while(i<len && str[i])
83 {
84 str[i] = tolower(str[i]);
85 i++;
86 }
87 return str;
88 }
91 char *
92 concat_path(char *p1, char *p2)
93 {
94 size_t size;
95 char *path;
96 char append_slash = 0;
98 size = strlen(p1);
99 if( size==0 || p1[size-1]!='/' )
100 {
101 size++;
102 append_slash = 1;
103 }
104 size += strlen(p2);
105 size++;
107 path = calloc(size, sizeof(char));
108 strncpy(path, p1, size);
109 if( append_slash )
110 strncat(path, "/", size);
111 strncat(path, p2, size);
113 return path;
114 }
118 #ifndef HAVE_BASENAME
119 char *
120 basename(char *path)
121 {
122 char *end;
124 path = remove_trailing_slash(path);
125 end = path + strlen(path);
127 while( end>path && *end!='/' )
128 end--;
130 if( *end=='/' && end!=path )
131 return end+1;
133 return path;
134 }
135 #endif /* HAVE_BASENAME */
138 #ifndef HAVE_STRCASESTR
139 char *
140 strcasestr(const char *haystack, const char *needle)
141 {
142 return strstr(lowerstr(haystack), lowerstr(needle));
143 }
144 #endif /* HAVE_STRCASESTR */
147 int
148 charset_init(void)
149 {
150 #ifdef HAVE_LOCALE_H
151 /* get current locale */
152 if( (locale=setlocale(LC_CTYPE,"")) == NULL )
153 {
154 fprintf(stderr,"setlocale() - failed!\n");
155 return -1;
156 }
157 #endif
159 /* get charset */
160 if( (charset=nl_langinfo(CODESET)) == NULL )
161 {
162 fprintf(stderr,
163 "nl_langinfo() failed using default:" DEFAULT_CHARSET "\n");
164 charset = DEFAULT_CHARSET;
165 }
166 #ifdef DEBUG
167 fprintf(stderr, "charset: %s\n", charset);
168 #endif
170 #ifdef HAVE_ICONV
171 /* allocate descriptor for character set conversion */
172 iconv_from_uft8 = iconv_open(charset, "UTF-8");
173 if( iconv_from_uft8 == (iconv_t)(-1) )
174 {
175 perror("iconv_open");
176 return -1;
177 }
178 iconv_to_uft8 = iconv_open("UTF-8", charset);
179 if( iconv_to_uft8 == (iconv_t)(-1) )
180 {
181 perror("iconv_open");
182 return -1;
183 }
184 #endif
186 return 0;
187 }
189 int
190 charset_close(void)
191 {
192 #ifdef HAVE_ICONV
193 if( iconv_from_uft8 != (iconv_t)(-1) )
194 {
195 iconv_close(iconv_from_uft8);
196 iconv_from_uft8 = (iconv_t)(-1);
197 }
198 if( iconv_to_uft8 != (iconv_t)(-1) )
199 {
200 iconv_close(iconv_to_uft8);
201 iconv_to_uft8 = (iconv_t)(-1);
202 }
203 #endif
204 return 0;
205 }
207 #ifdef HAVE_ICONV
208 static char *
209 charconv(iconv_t iv, char *str)
210 {
211 size_t inleft;
212 size_t retlen;
213 char *ret;
215 if( str==NULL )
216 return NULL;
218 if( iv == (iconv_t)(-1) )
219 return strdup(str);
221 ret = NULL;
222 retlen = 0;
223 inleft = strlen(str);
224 while( inleft>0 )
225 {
226 char buf[BUFSIZE];
227 size_t outleft = BUFSIZE;
228 char *bufp = buf;
230 if( iconv(iv, &str, &inleft, &bufp, &outleft) <0 )
231 {
232 perror("iconv");
233 free(ret);
234 return NULL;
235 }
236 ret = realloc(ret, BUFSIZE-outleft+1);
237 memcpy(ret+retlen, buf, BUFSIZE-outleft);
238 retlen += BUFSIZE-outleft;
239 ret[retlen] = '\0';
240 }
241 return ret;
242 }
243 #endif
245 char *
246 utf8_to_locale(char *str)
247 {
248 #ifdef HAVE_ICONV
249 return charconv(iconv_from_uft8, str);
250 #else
251 return strdup(str);
252 #endif
253 }
255 char *
256 locale_to_utf8(char *str)
257 {
258 #ifdef HAVE_ICONV
259 return charconv(iconv_to_uft8, str);
260 #else
261 return strdup(str);
262 #endif
263 }