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
38 char *
39 remove_trailing_slash(char *path)
40 {
41 int len;
43 if( path==NULL )
44 return NULL;
46 len=strlen(path);
47 if( len>1 && path[len-1] == '/' )
48 path[len-1] = '\0';
50 return path;
51 }
53 char *
54 lowerstr(char *str)
55 {
56 size_t i;
57 size_t len = strlen(str);
59 if( str==NULL )
60 return NULL;
62 i=0;
63 while(i<len && str[i])
64 {
65 str[i] = tolower(str[i]);
66 i++;
67 }
68 return str;
69 }
72 char *
73 concat_path(char *p1, char *p2)
74 {
75 size_t size;
76 char *path;
77 char append_slash = 0;
79 size = strlen(p1);
80 if( size==0 || p1[size-1]!='/' )
81 {
82 size++;
83 append_slash = 1;
84 }
85 size += strlen(p2);
86 size++;
88 path = calloc(size, sizeof(char));
89 strncpy(path, p1, size);
90 if( append_slash )
91 strncat(path, "/", size);
92 strncat(path, p2, size);
94 return path;
95 }
99 #ifndef HAVE_BASENAME
100 char *
101 basename(char *path)
102 {
103 char *end;
105 path = remove_trailing_slash(path);
106 end = path + strlen(path);
108 while( end>path && *end!='/' )
109 end--;
111 if( *end=='/' && end!=path )
112 return end+1;
114 return path;
115 }
116 #endif /* HAVE_BASENAME */
119 #ifndef HAVE_STRCASESTR
120 char *
121 strcasestr(const char *haystack, const char *needle)
122 {
123 return strstr(lowerstr(haystack), lowerstr(needle));
124 }
125 #endif /* HAVE_STRCASESTR */
128 int
129 charset_init(void)
130 {
131 #ifdef HAVE_LOCALE_H
132 /* get current locale */
133 if( (locale=setlocale(LC_CTYPE,"")) == NULL )
134 {
135 fprintf(stderr,"setlocale() - failed!\n");
136 return -1;
137 }
138 #endif
140 #ifdef HAVE_LANGINFO_CODESET
141 /* get charset */
142 if( (charset=nl_langinfo(CODESET)) == NULL )
143 {
144 fprintf(stderr,
145 "nl_langinfo() failed using default:" DEFAULT_CHARSET "\n");
146 }
147 #endif
149 if( charset==NULL )
150 charset = DEFAULT_CHARSET;
152 #ifdef HAVE_ICONV
153 /* allocate descriptor for character set conversion */
154 iconv_from_uft8 = iconv_open(charset, "UTF-8");
155 if( iconv_from_uft8 == (iconv_t)(-1) )
156 {
157 perror("iconv_open");
158 return -1;
159 }
160 #endif
162 return 0;
163 }
165 int
166 charset_close(void)
167 {
168 #ifdef HAVE_ICONV
169 if( iconv_from_uft8 == (iconv_t)(-1) )
170 {
171 iconv_close(iconv_from_uft8);
172 iconv_from_uft8 = (iconv_t)(-1);
173 }
174 if( iconv_to_uft8 == (iconv_t)(-1) )
175 {
176 iconv_close(iconv_to_uft8);
177 iconv_to_uft8 = (iconv_t)(-1);
178 }
179 #endif
180 return 0;
181 }
185 char *
186 utf8_to_locale(char *str)
187 {
188 #ifdef HAVE_ICONV
189 size_t inleft;
190 size_t retlen;
191 char *ret;
193 if( iconv_from_uft8 == (iconv_t)(-1) )
194 return strdup(str);
196 ret = NULL;
197 retlen = 0;
198 inleft = strlen(str);
199 while( inleft>0 )
200 {
201 char buf[BUFSIZE];
202 size_t outleft = BUFSIZE;
203 char *bufp = buf;
205 if( iconv(iconv_from_uft8, &str, &inleft, &bufp, &outleft) <0 )
206 {
207 perror("iconv");
208 free(ret);
209 return NULL;
210 }
211 ret = realloc(ret, BUFSIZE-outleft+1);
212 memcpy(ret+retlen, buf, BUFSIZE-outleft);
213 retlen += BUFSIZE-outleft;
214 ret[retlen] = '\0';
215 }
216 return ret;
218 #else
219 return strdup(str);
220 #endif
221 }