254c15eb139e6e7cfcdb3c0dac2b5bb4e2b58141
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 char *
39 remove_trailing_slash(char *path)
40 {
41 int len;
43 if( path==NULL )
44 return NULL;
46 len=strlen(path);
47 if( len>1 && path[len-1] == '/' )
48 path[len-1] = '\0';
50 return path;
51 }
53 char *
54 lowerstr(char *str)
55 {
56 gsize i;
57 gsize len = strlen(str);
59 if( str==NULL )
60 return NULL;
62 i=0;
63 while(i<len && str[i])
64 {
65 str[i] = tolower(str[i]);
66 i++;
67 }
68 return str;
69 }
72 #ifndef HAVE_BASENAME
73 char *
74 basename(char *path)
75 {
76 char *end;
78 path = remove_trailing_slash(path);
79 end = path + strlen(path);
81 while( end>path && *end!='/' )
82 end--;
84 if( *end=='/' && end!=path )
85 return end+1;
87 return path;
88 }
89 #endif /* HAVE_BASENAME */
92 #ifndef HAVE_STRCASESTR
93 char *
94 strcasestr(const char *haystack, const char *needle)
95 {
96 return strstr(lowerstr(haystack), lowerstr(needle));
97 }
98 #endif /* HAVE_STRCASESTR */
100 char *
101 strscroll(char *str, char *separator, int width, scroll_state_t *st)
102 {
103 gchar *tmp, *buf;
104 gsize len, size;
106 if( st->offset==0 )
107 {
108 st->offset++;
109 return g_strdup(str);
110 }
112 /* create a buffer containing the string and the separator */
113 size = strlen(str)+strlen(separator)+1;
114 tmp = g_malloc(size);
115 g_strlcpy(tmp, str, size);
116 g_strlcat(tmp, separator, size);
117 len = strlen(tmp);
119 if( st->offset >= len )
120 st->offset = 0;
122 /* create the new scrolled string */
123 size = width+1;
124 buf = g_malloc(size);
125 g_strlcpy(buf, tmp+st->offset, size);
126 if( strlen(buf) < width )
127 g_strlcat(buf, tmp, size);
129 if( time(NULL)-st->t >= 1 )
130 {
131 st->t = time(NULL);
132 st->offset++;
133 }
134 g_free(tmp);
135 return buf;
137 }
140 void
141 charset_init(gboolean disable)
142 {
143 noconvert = disable;
144 }
146 char *
147 utf8_to_locale(char *utf8str)
148 {
149 gchar *str;
150 gsize rb, wb;
151 GError *error;
153 if( noconvert )
154 return g_strdup(utf8str);
156 rb = 0; /* bytes read */
157 wb = 0; /* bytes written */
158 error = NULL;
159 str=g_locale_from_utf8(utf8str,
160 strlen(utf8str),
161 &wb, &rb,
162 &error);
163 if( error )
164 {
165 const char *charset;
167 g_get_charset(&charset);
168 screen_status_printf(_("Error: Unable to convert characters to %s"),
169 charset);
170 D("utf8_to_locale(): %s\n", error->message);
171 g_error_free(error);
172 return g_strdup(utf8str);
173 }
175 return str;
176 }
178 char *
179 locale_to_utf8(char *localestr)
180 {
181 gchar *str;
182 gsize rb, wb;
183 GError *error;
185 if( noconvert )
186 return g_strdup(localestr);
188 rb = 0; /* bytes read */
189 wb = 0; /* bytes written */
190 error = NULL;
191 str=g_locale_to_utf8(localestr,
192 strlen(localestr),
193 &wb, &rb,
194 &error);
195 if( error )
196 {
197 screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
198 D("locale_to_utf8: %s\n", error->message);
199 g_error_free(error);
200 return g_strdup(localestr);
201 }
203 return str;
204 }