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 // FIXME: utf-8 length
110 char *
111 strscroll(char *str, char *separator, int width, scroll_state_t *st)
112 {
113 gchar *tmp, *buf;
114 gsize len, size;
116 if( st->offset==0 ) {
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 = my_strlen(tmp);
128 if( st->offset >= len )
129 st->offset = 0;
131 /* create the new scrolled string */
132 size = width+1;
133 if (g_utf8_validate(tmp, -1, NULL) ) {
134 int ulen;
135 buf = g_malloc(size*6);// max length of utf8 char is 6
136 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
137 if( (ulen = g_utf8_strlen(buf, -1)) < width )
138 g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
139 } else {
140 buf = g_malloc(size);
141 g_strlcpy(buf, tmp+st->offset, size);
142 if (strlen(buf) < width)
143 g_strlcat(buf, tmp, size);
144 }
145 if( time(NULL)-st->t >= 1 ) {
146 st->t = time(NULL);
147 st->offset++;
148 }
149 g_free(tmp);
150 return buf;
151 }
153 void
154 charset_init(gboolean disable)
155 {
156 noconvert = disable;
157 }
159 char *
160 utf8_to_locale(char *utf8str)
161 {
162 gchar *str;
163 gsize rb, wb;
164 GError *error;
166 if( noconvert )
167 return g_strdup(utf8str);
169 rb = 0; /* bytes read */
170 wb = 0; /* bytes written */
171 error = NULL;
172 str=g_locale_from_utf8(utf8str,
173 strlen(utf8str),
174 &wb, &rb,
175 &error);
176 if( error )
177 {
178 const char *charset;
180 g_get_charset(&charset);
181 screen_status_printf(_("Error: Unable to convert characters to %s"),
182 charset);
183 D("utf8_to_locale(): %s\n", error->message);
184 g_error_free(error);
185 return g_strdup(utf8str);
186 }
188 return str;
189 }
191 char *
192 locale_to_utf8(char *localestr)
193 {
194 gchar *str;
195 gsize rb, wb;
196 GError *error;
198 if( noconvert )
199 return g_strdup(localestr);
201 rb = 0; /* bytes read */
202 wb = 0; /* bytes written */
203 error = NULL;
204 str=g_locale_to_utf8(localestr,
205 strlen(localestr),
206 &wb, &rb,
207 &error);
208 if( error )
209 {
210 screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
211 D("locale_to_utf8: %s\n", error->message);
212 g_error_free(error);
213 return g_strdup(localestr);
214 }
216 return str;
217 }