Code

screen_lyrics: set current.song
[ncmpc.git] / src / support.c
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 <assert.h>
25 #include <time.h>
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #define BUFSIZE 1024
33 extern void screen_status_printf(const char *format, ...);
35 static gboolean noconvert = TRUE;
37 size_t
38 my_strlen(const char *str)
39 {
40         assert(str != NULL);
42         if (g_utf8_validate(str, -1, NULL)) {
43                 size_t len = g_utf8_strlen(str, -1);
44                 size_t width = 0;
45                 gunichar c;
47                 while (len--) {
48                         c = g_utf8_get_char(str);
49                         width += g_unichar_iswide(c) ? 2 : 1;
50                         str += g_unichar_to_utf8(c, NULL);
51                 }
53                 return width;
54         } else
55                 return strlen(str);
56 }
58 char *
59 remove_trailing_slash(char *path)
60 {
61         int len;
63         if (path == NULL)
64                 return NULL;
66         len = strlen(path);
67         if (len > 1 && path[len - 1] == '/')
68                 path[len - 1] = '\0';
70         return path;
71 }
73 char *
74 lowerstr(char *str)
75 {
76         gsize i;
77         gsize len = strlen(str);
79         if (str == NULL)
80                 return NULL;
82         i = 0;
83         while (i < len && str[i]) {
84                 str[i] = tolower(str[i]);
85                 i++;
86         }
87         return str;
88 }
91 #ifndef HAVE_BASENAME
92 char *
93 basename(char *path)
94 {
95         char *end;
97         assert(path != NULL);
99         path = remove_trailing_slash(path);
100         end = path + strlen(path);
102         while (end > path && *end != '/')
103                 end--;
105         if (*end == '/' && end != path)
106                 return end+1;
108         return path;
110 #endif /* HAVE_BASENAME */
113 #ifndef HAVE_STRCASESTR
114 char *
115 strcasestr(const char *haystack, const char *needle)
117         assert(haystack != NULL);
118         assert(needle != NULL);
120         return strstr(lowerstr(haystack), lowerstr(needle));
122 #endif /* HAVE_STRCASESTR */
124 // FIXME: utf-8 length
125 char *
126 strscroll(char *str, char *separator, int width, scroll_state_t *st)
128         gchar *tmp, *buf;
129         gsize len, size;
131         assert(str != NULL);
132         assert(separator != NULL);
133         assert(st != NULL);
135         if( st->offset==0 ) {
136                 st->offset++;
137                 return g_strdup(str);
138         }
140         /* create a buffer containing the string and the separator */
141         size = strlen(str)+strlen(separator)+1;
142         tmp = g_malloc(size);
143         g_strlcpy(tmp, str, size);
144         g_strlcat(tmp, separator, size);
145         len = my_strlen(tmp);
147         if (st->offset >= len)
148                 st->offset = 0;
150         /* create the new scrolled string */
151         size = width+1;
152         if (g_utf8_validate(tmp, -1, NULL) ) {
153                 int ulen;
154                 buf = g_malloc(size*6);// max length of utf8 char is 6
155                 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
156                 if( (ulen = g_utf8_strlen(buf, -1)) < width )
157                         g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
158         } else {
159                 buf = g_malloc(size);
160                 g_strlcpy(buf, tmp+st->offset, size);
161                 if (strlen(buf) < (size_t)width)
162                         g_strlcat(buf, tmp, size);
163         }
164         if( time(NULL)-st->t >= 1 ) {
165                 st->t = time(NULL);
166                 st->offset++;
167         }
168         g_free(tmp);
169         return buf;
172 void
173 charset_init(gboolean disable)
175   noconvert = disable;
178 char *
179 utf8_to_locale(const char *utf8str)
181         gchar *str;
182         gsize rb, wb;
183         GError *error;
185         assert(utf8str != NULL);
187         if (noconvert)
188                 return g_strdup(utf8str);
190         rb = 0; /* bytes read */
191         wb = 0; /* bytes written */
192         error = NULL;
193         str = g_locale_from_utf8(utf8str,
194                                  strlen(utf8str),
195                                  &wb, &rb,
196                                  &error);
197         if (error) {
198                 const char *charset;
200                 g_get_charset(&charset);
201                 screen_status_printf(_("Error: Unable to convert characters to %s"),
202                                      charset);
203                 D("utf8_to_locale(): %s\n", error->message);
204                 g_error_free(error);
205                 return g_strdup(utf8str);
206         }
208         return str;
211 char *
212 locale_to_utf8(const char *localestr)
214         gchar *str;
215         gsize rb, wb;
216         GError *error;
218         assert(localestr != NULL);
220         if (noconvert)
221                 return g_strdup(localestr);
223         rb = 0; /* bytes read */
224         wb = 0; /* bytes written */
225         error = NULL;
226         str = g_locale_to_utf8(localestr,
227                                strlen(localestr),
228                                &wb, &rb,
229                                &error);
230         if (error) {
231                 screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
232                 D("locale_to_utf8: %s\n", error->message);
233                 g_error_free(error);
234                 return g_strdup(localestr);
235         }
237         return str;