1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include <time.h>
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
28 #include "config.h"
29 #include "ncmpc.h"
30 #include "support.h"
32 #define BUFSIZE 1024
34 extern void screen_status_printf(char *format, ...);
36 static gboolean noconvert = TRUE;
38 size_t
39 my_strlen(char *str)
40 {
41 if( g_utf8_validate(str,-1,NULL) )
42 return g_utf8_strlen(str,-1);
43 else
44 return strlen(str);
45 }
47 char *
48 remove_trailing_slash(char *path)
49 {
50 int len;
52 if( path==NULL )
53 return NULL;
55 len=strlen(path);
56 if( len>1 && path[len-1] == '/' )
57 path[len-1] = '\0';
59 return path;
60 }
62 char *
63 lowerstr(char *str)
64 {
65 gsize i;
66 gsize len = strlen(str);
68 if( str==NULL )
69 return NULL;
71 i=0;
72 while(i<len && str[i])
73 {
74 str[i] = tolower(str[i]);
75 i++;
76 }
77 return str;
78 }
81 #ifndef HAVE_BASENAME
82 char *
83 basename(char *path)
84 {
85 char *end;
87 path = remove_trailing_slash(path);
88 end = path + strlen(path);
90 while( end>path && *end!='/' )
91 end--;
93 if( *end=='/' && end!=path )
94 return end+1;
96 return path;
97 }
98 #endif /* HAVE_BASENAME */
101 #ifndef HAVE_STRCASESTR
102 char *
103 strcasestr(const char *haystack, const char *needle)
104 {
105 return strstr(lowerstr(haystack), lowerstr(needle));
106 }
107 #endif /* HAVE_STRCASESTR */
109 char *
110 strscroll(char *str, char *separator, int width, scroll_state_t *st)
111 {
112 gchar *tmp, *buf;
113 gsize len, size;
115 if( st->offset==0 )
116 {
117 st->offset++;
118 return g_strdup(str);
119 }
121 /* create a buffer containing the string and the separator */
122 size = strlen(str)+strlen(separator)+1;
123 tmp = g_malloc(size);
124 g_strlcpy(tmp, str, size);
125 g_strlcat(tmp, separator, size);
126 len = strlen(tmp);
128 if( st->offset >= len )
129 st->offset = 0;
131 /* create the new scrolled string */
132 size = width+1;
133 buf = g_malloc(size);
134 g_strlcpy(buf, tmp+st->offset, size);
135 if( strlen(buf) < width )
136 g_strlcat(buf, tmp, size);
138 if( time(NULL)-st->t >= 1 )
139 {
140 st->t = time(NULL);
141 st->offset++;
142 }
143 g_free(tmp);
144 return buf;
146 }
148 void
149 charset_init(gboolean disable)
150 {
151 noconvert = disable;
152 }
154 char *
155 utf8_to_locale(char *utf8str)
156 {
157 gchar *str;
158 gsize rb, wb;
159 GError *error;
161 if( noconvert )
162 return g_strdup(utf8str);
164 rb = 0; /* bytes read */
165 wb = 0; /* bytes written */
166 error = NULL;
167 str=g_locale_from_utf8(utf8str,
168 strlen(utf8str),
169 &wb, &rb,
170 &error);
171 if( error )
172 {
173 const char *charset;
175 g_get_charset(&charset);
176 screen_status_printf(_("Error: Unable to convert characters to %s"),
177 charset);
178 D("utf8_to_locale(): %s\n", error->message);
179 g_error_free(error);
180 return g_strdup(utf8str);
181 }
183 return str;
184 }
186 char *
187 locale_to_utf8(char *localestr)
188 {
189 gchar *str;
190 gsize rb, wb;
191 GError *error;
193 if( noconvert )
194 return g_strdup(localestr);
196 rb = 0; /* bytes read */
197 wb = 0; /* bytes written */
198 error = NULL;
199 str=g_locale_to_utf8(localestr,
200 strlen(localestr),
201 &wb, &rb,
202 &error);
203 if( error )
204 {
205 screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
206 D("locale_to_utf8: %s\n", error->message);
207 g_error_free(error);
208 return g_strdup(localestr);
209 }
211 return str;
212 }