Code

Added initial i18n support
[ncmpc.git] / src / support.c
1 /* 
2  * (c) 2004 by Kalle Wallin (kaw@linux.se)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
25 #include "config.h"
26 #include "ncmpc.h"
27 #include "support.h"
29 #ifdef HAVE_LOCALE_H
30 #include <locale.h>
31 #endif
33 #define BUFSIZE 1024
35 extern void screen_status_printf(char *format, ...);
37 static const char *charset = NULL;
38 static const char *locale = NULL;
39 static gboolean noconvert = TRUE;
41 char *
42 trim(char *str)
43 {
44   char *end;
46   if( str==NULL )
47     return NULL;
49   while( IS_WHITESPACE(*str) )
50     str++;
52   end=str+strlen(str)-1;
53   while( end>str && IS_WHITESPACE(*end) )
54     {
55       *end = '\0';
56       end--;
57     }
58   return str;
59 }
61 char *
62 remove_trailing_slash(char *path)
63 {
64   int len;
66   if( path==NULL )
67     return NULL;
69   len=strlen(path);
70   if( len>1 && path[len-1] == '/' )
71     path[len-1] = '\0';
73   return path;
74 }
76 char *
77 lowerstr(char *str)
78 {
79   size_t i;
80   size_t len = strlen(str);
82   if( str==NULL )
83     return NULL;
85   i=0;
86   while(i<len && str[i])
87     {
88       str[i] = tolower(str[i]);
89       i++;
90     }
91   return str;
92 }
95 #ifndef HAVE_BASENAME
96 char *
97 basename(char *path)
98 {
99   char *end;
101   path = remove_trailing_slash(path);
102   end = path + strlen(path);
104   while( end>path && *end!='/' )
105     end--;
107   if( *end=='/' && end!=path )
108     return end+1;
110   return path;
112 #endif /* HAVE_BASENAME */
115 #ifndef HAVE_STRCASESTR
116 char *
117 strcasestr(const char *haystack, const char *needle)
119   return strstr(lowerstr(haystack), lowerstr(needle));
121 #endif /* HAVE_STRCASESTR */
124 int
125 charset_init(void)
127 #ifdef HAVE_LOCALE_H
128   /* get current locale */
129   if( (locale=setlocale(LC_CTYPE,"")) == NULL )
130     {
131       g_printerr("setlocale() - failed!\n");
132       return -1;
133     }
134 #endif
136   /* get charset */
137   noconvert = g_get_charset(&charset);
139 #ifdef DEBUG
140   g_printerr("charset: %s [%d]\n", charset, noconvert);
141   fflush(stderr);
142 #endif
143   
144   return 0;
147 int
148 charset_close(void)
150   return 0;
153 char *
154 utf8_to_locale(char *utf8str)
156   gchar *str;
157   gsize rb, wb;
158   GError *error;
160   if( noconvert )
161     return g_strdup(utf8str);
163   rb = 0; /* bytes read */
164   wb = 0; /* bytes written */
165   error = NULL;
166   str=g_locale_from_utf8(utf8str, 
167                          strlen(utf8str),
168                          &wb, &rb,
169                          &error);
170   if( error )
171     {
172       screen_status_printf(_("Error: Unable to convert characters to %s"),
173                            charset);
174       D(g_printerr("utf8_to_locale(): %s\n", error->message));
175       g_error_free(error);
176       return g_strdup(utf8str);
177     }
178   
179   return str;
182 char *
183 locale_to_utf8(char *localestr)
185   gchar *str;
186   gsize rb, wb;
187   GError *error;
189   if( noconvert )
190     return g_strdup(localestr);
192   rb = 0; /* bytes read */
193   wb = 0; /* bytes written */
194   error = NULL;
195   str=g_locale_to_utf8(localestr, 
196                        strlen(localestr), 
197                        &wb, &rb,
198                        &error);
199   if( error )
200     {
201       screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
202       D(g_printerr("locale_to_utf8: %s\n", error->message));
203       g_error_free(error);
204       return g_strdup(localestr);
205     }
207   return str;