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 "support.h"
22 #include "ncmpc.h"
24 #include <time.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #define BUFSIZE 1024
32 extern void screen_status_printf(const char *format, ...);
34 static gboolean noconvert = TRUE;
36 size_t
37 my_strlen(const char *str)
38 {
39 if (g_utf8_validate(str, -1, NULL)) {
40 size_t len = g_utf8_strlen(str, -1);
41 size_t width = 0;
42 gunichar c;
44 while (len--) {
45 c = g_utf8_get_char(str);
46 width += g_unichar_iswide(c) ? 2 : 1;
47 str += g_unichar_to_utf8(c, NULL);
48 }
50 return width;
51 } else
52 return strlen(str);
53 }
55 char *
56 remove_trailing_slash(char *path)
57 {
58 int len;
60 if (path == NULL)
61 return NULL;
63 len = strlen(path);
64 if (len > 1 && path[len - 1] == '/')
65 path[len - 1] = '\0';
67 return path;
68 }
70 char *
71 lowerstr(char *str)
72 {
73 gsize i;
74 gsize len = strlen(str);
76 if (str == NULL)
77 return NULL;
79 i = 0;
80 while (i < len && str[i]) {
81 str[i] = tolower(str[i]);
82 i++;
83 }
84 return str;
85 }
88 #ifndef HAVE_BASENAME
89 char *
90 basename(char *path)
91 {
92 char *end;
94 path = remove_trailing_slash(path);
95 end = path + strlen(path);
97 while (end > path && *end != '/')
98 end--;
100 if (*end == '/' && end != path)
101 return end+1;
103 return path;
104 }
105 #endif /* HAVE_BASENAME */
108 #ifndef HAVE_STRCASESTR
109 char *
110 strcasestr(const char *haystack, const char *needle)
111 {
112 return strstr(lowerstr(haystack), lowerstr(needle));
113 }
114 #endif /* HAVE_STRCASESTR */
116 // FIXME: utf-8 length
117 char *
118 strscroll(char *str, char *separator, int width, scroll_state_t *st)
119 {
120 gchar *tmp, *buf;
121 gsize len, size;
123 if( st->offset==0 ) {
124 st->offset++;
125 return g_strdup(str);
126 }
128 /* create a buffer containing the string and the separator */
129 size = strlen(str)+strlen(separator)+1;
130 tmp = g_malloc(size);
131 g_strlcpy(tmp, str, size);
132 g_strlcat(tmp, separator, size);
133 len = my_strlen(tmp);
135 if (st->offset >= len)
136 st->offset = 0;
138 /* create the new scrolled string */
139 size = width+1;
140 if (g_utf8_validate(tmp, -1, NULL) ) {
141 int ulen;
142 buf = g_malloc(size*6);// max length of utf8 char is 6
143 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
144 if( (ulen = g_utf8_strlen(buf, -1)) < width )
145 g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
146 } else {
147 buf = g_malloc(size);
148 g_strlcpy(buf, tmp+st->offset, size);
149 if (strlen(buf) < (size_t)width)
150 g_strlcat(buf, tmp, size);
151 }
152 if( time(NULL)-st->t >= 1 ) {
153 st->t = time(NULL);
154 st->offset++;
155 }
156 g_free(tmp);
157 return buf;
158 }
160 void
161 charset_init(gboolean disable)
162 {
163 noconvert = disable;
164 }
166 char *
167 utf8_to_locale(const char *utf8str)
168 {
169 gchar *str;
170 gsize rb, wb;
171 GError *error;
173 if (noconvert)
174 return g_strdup(utf8str);
176 rb = 0; /* bytes read */
177 wb = 0; /* bytes written */
178 error = NULL;
179 str = g_locale_from_utf8(utf8str,
180 strlen(utf8str),
181 &wb, &rb,
182 &error);
183 if (error) {
184 const char *charset;
186 g_get_charset(&charset);
187 screen_status_printf(_("Error: Unable to convert characters to %s"),
188 charset);
189 D("utf8_to_locale(): %s\n", error->message);
190 g_error_free(error);
191 return g_strdup(utf8str);
192 }
194 return str;
195 }
197 char *
198 locale_to_utf8(const char *localestr)
199 {
200 gchar *str;
201 gsize rb, wb;
202 GError *error;
204 if (noconvert)
205 return g_strdup(localestr);
207 rb = 0; /* bytes read */
208 wb = 0; /* bytes written */
209 error = NULL;
210 str = g_locale_to_utf8(localestr,
211 strlen(localestr),
212 &wb, &rb,
213 &error);
214 if (error) {
215 screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
216 D("locale_to_utf8: %s\n", error->message);
217 g_error_free(error);
218 return g_strdup(localestr);
219 }
221 return str;
222 }