1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <glib.h>
7 #include "config.h"
8 #include "support.h"
10 #ifdef HAVE_LOCALE_H
11 #include <locale.h>
12 #endif
14 #ifdef DEBUG
15 #define D(x) x
16 #else
17 #define D(x)
18 #endif
20 #define BUFSIZE 1024
22 extern void screen_status_printf(char *format, ...);
24 static const char *charset = NULL;
25 static const char *locale = NULL;
26 static gboolean noconvert = TRUE;
28 char *
29 trim(char *str)
30 {
31 char *end;
33 if( str==NULL )
34 return NULL;
36 while( IS_WHITESPACE(*str) )
37 str++;
39 end=str+strlen(str)-1;
40 while( end>str && IS_WHITESPACE(*end) )
41 {
42 *end = '\0';
43 end--;
44 }
45 return str;
46 }
48 char *
49 remove_trailing_slash(char *path)
50 {
51 int len;
53 if( path==NULL )
54 return NULL;
56 len=strlen(path);
57 if( len>1 && path[len-1] == '/' )
58 path[len-1] = '\0';
60 return path;
61 }
63 char *
64 lowerstr(char *str)
65 {
66 size_t i;
67 size_t len = strlen(str);
69 if( str==NULL )
70 return NULL;
72 i=0;
73 while(i<len && str[i])
74 {
75 str[i] = tolower(str[i]);
76 i++;
77 }
78 return str;
79 }
82 #ifndef HAVE_BASENAME
83 char *
84 basename(char *path)
85 {
86 char *end;
88 path = remove_trailing_slash(path);
89 end = path + strlen(path);
91 while( end>path && *end!='/' )
92 end--;
94 if( *end=='/' && end!=path )
95 return end+1;
97 return path;
98 }
99 #endif /* HAVE_BASENAME */
102 #ifndef HAVE_STRCASESTR
103 char *
104 strcasestr(const char *haystack, const char *needle)
105 {
106 return strstr(lowerstr(haystack), lowerstr(needle));
107 }
108 #endif /* HAVE_STRCASESTR */
111 int
112 charset_init(void)
113 {
114 #ifdef HAVE_LOCALE_H
115 /* get current locale */
116 if( (locale=setlocale(LC_CTYPE,"")) == NULL )
117 {
118 g_printerr("setlocale() - failed!\n");
119 return -1;
120 }
121 #endif
123 /* get charset */
124 noconvert = g_get_charset(&charset);
126 #ifdef DEBUG
127 g_printerr("charset: %s [%d]\n", charset, noconvert);
128 fflush(stderr);
129 #endif
131 return 0;
132 }
134 int
135 charset_close(void)
136 {
137 return 0;
138 }
140 char *
141 utf8_to_locale(char *utf8str)
142 {
143 gchar *str;
144 gsize rb, wb;
145 GError *error;
147 if( noconvert )
148 return g_strdup(utf8str);
150 rb = 0; /* bytes read */
151 wb = 0; /* bytes written */
152 error = NULL;
153 str=g_locale_from_utf8(utf8str,
154 strlen(utf8str),
155 &wb, &rb,
156 &error);
157 if( error )
158 {
159 screen_status_printf("Error: Unable to convert characters to %s",
160 charset);
161 D(g_printerr("utf8_to_locale(): %s\n", error->message));
162 g_error_free(error);
163 return g_strdup(utf8str);
164 }
166 return str;
167 }
169 char *
170 locale_to_utf8(char *localestr)
171 {
172 gchar *str;
173 gsize rb, wb;
174 GError *error;
176 if( noconvert )
177 return g_strdup(localestr);
179 rb = 0; /* bytes read */
180 wb = 0; /* bytes written */
181 error = NULL;
182 str=g_locale_to_utf8(localestr,
183 strlen(localestr),
184 &wb, &rb,
185 &error);
186 if( error )
187 {
188 screen_status_printf("Error: Unable to convert characters to UTF-8");
189 D(g_printerr("locale_to_utf8: %s\n", error->message));
190 g_error_free(error);
191 return g_strdup(localestr);
192 }
194 return str;
195 }