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 }
71 #ifndef HAVE_BASENAME
72 char *
73 basename(char *path)
74 {
75 char *end;
77 path = remove_trailing_slash(path);
78 end = path + strlen(path);
80 while( end>path && *end!='/' )
81 end--;
83 if( *end=='/' && end!=path )
84 return end+1;
86 return path;
87 }
88 #endif /* HAVE_BASENAME */
91 #ifndef HAVE_STRCASESTR
92 char *
93 strcasestr(const char *haystack, const char *needle)
94 {
95 return strstr(lowerstr(haystack), lowerstr(needle));
96 }
97 #endif /* HAVE_STRCASESTR */
100 int
101 charset_init(void)
102 {
103 #ifdef HAVE_LOCALE_H
104 /* get current locale */
105 if( (locale=setlocale(LC_CTYPE,"")) == NULL )
106 {
107 fprintf(stderr,"setlocale() - failed!\n");
108 return -1;
109 }
110 #endif
112 #ifdef HAVE_LANGINFO_CODESET
113 /* get charset */
114 if( (charset=nl_langinfo(CODESET)) == NULL )
115 {
116 fprintf(stderr,
117 "nl_langinfo() failed using default:" DEFAULT_CHARSET "\n");
118 }
119 #endif
121 if( charset==NULL )
122 charset = DEFAULT_CHARSET;
124 #ifdef HAVE_ICONV
125 /* allocate descriptor for character set conversion */
126 iconv_from_uft8 = iconv_open(charset, "UTF-8");
127 if( iconv_from_uft8 == (iconv_t)(-1) )
128 {
129 perror("iconv_open");
130 return -1;
131 }
132 #endif
134 return 0;
135 }
137 int
138 charset_close(void)
139 {
140 #ifdef HAVE_ICONV
141 if( iconv_from_uft8 == (iconv_t)(-1) )
142 {
143 iconv_close(iconv_from_uft8);
144 iconv_from_uft8 = (iconv_t)(-1);
145 }
146 if( iconv_to_uft8 == (iconv_t)(-1) )
147 {
148 iconv_close(iconv_to_uft8);
149 iconv_to_uft8 = (iconv_t)(-1);
150 }
151 #endif
152 return 0;
153 }
157 char *
158 utf8_to_locale(char *str)
159 {
160 #ifdef HAVE_ICONV
161 size_t inleft;
162 size_t retlen;
163 char *ret;
165 if( iconv_from_uft8 == (iconv_t)(-1) )
166 return strdup(str);
168 ret = NULL;
169 retlen = 0;
170 inleft = strlen(str);
171 while( inleft>0 )
172 {
173 char buf[BUFSIZE];
174 size_t outleft = BUFSIZE;
175 char *bufp = buf;
177 if( iconv(iconv_from_uft8, &str, &inleft, &bufp, &outleft) <0 )
178 {
179 perror("iconv");
180 free(ret);
181 return NULL;
182 }
183 ret = realloc(ret, BUFSIZE-outleft+1);
184 memcpy(ret+retlen, buf, BUFSIZE-outleft);
185 retlen += BUFSIZE-outleft;
186 ret[retlen] = '\0';
187 }
188 return ret;
190 #else
191 return strdup(str);
192 #endif
193 }