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 trim(char *str)
40 {
41 char *end;
43 if( str==NULL )
44 return NULL;
46 while( IS_WHITESPACE(*str) )
47 str++;
49 end=str+strlen(str)-1;
50 while( end>str && IS_WHITESPACE(*end) )
51 {
52 *end = '\0';
53 end--;
54 }
55 return 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 size_t i;
77 size_t len = strlen(str);
79 if( str==NULL )
80 return NULL;
82 i=0;
83 while(i<len && str[i])
84 {
85 str[i] = tolower(str[i]);
86 i++;
87 }
88 return str;
89 }
92 #ifndef HAVE_BASENAME
93 char *
94 basename(char *path)
95 {
96 char *end;
98 path = remove_trailing_slash(path);
99 end = path + strlen(path);
101 while( end>path && *end!='/' )
102 end--;
104 if( *end=='/' && end!=path )
105 return end+1;
107 return path;
108 }
109 #endif /* HAVE_BASENAME */
112 #ifndef HAVE_STRCASESTR
113 char *
114 strcasestr(const char *haystack, const char *needle)
115 {
116 return strstr(lowerstr(haystack), lowerstr(needle));
117 }
118 #endif /* HAVE_STRCASESTR */
120 char *
121 strscroll(char *str, char *separator, int width, scroll_state_t *st)
122 {
123 char *tmp, *buf;
124 size_t len;
126 if( st->offset==0 )
127 {
128 st->offset++;
129 return g_strdup(str);
130 }
132 /* create a buffer containing the string and the separator */
133 tmp = g_malloc(strlen(str)+strlen(separator)+1);
134 strcpy(tmp, str);
135 strcat(tmp, separator);
136 len = strlen(tmp);
138 if( st->offset >= len )
139 st->offset = 0;
141 /* create the new scrolled string */
142 buf = g_malloc(width+1);
143 strncpy(buf, tmp+st->offset, width);
144 if( strlen(buf) < width )
145 strncat(buf, tmp, width-strlen(buf));
147 if( time(NULL)-st->t >= 1 )
148 {
149 st->t = time(NULL);
150 st->offset++;
151 }
152 g_free(tmp);
153 return buf;
155 }
158 void
159 charset_init(gboolean disable)
160 {
161 noconvert = disable;
162 }
164 char *
165 utf8_to_locale(char *utf8str)
166 {
167 gchar *str;
168 gsize rb, wb;
169 GError *error;
171 if( noconvert )
172 return g_strdup(utf8str);
174 rb = 0; /* bytes read */
175 wb = 0; /* bytes written */
176 error = NULL;
177 str=g_locale_from_utf8(utf8str,
178 strlen(utf8str),
179 &wb, &rb,
180 &error);
181 if( error )
182 {
183 const char *charset;
185 g_get_charset(&charset);
186 screen_status_printf(_("Error: Unable to convert characters to %s"),
187 charset);
188 D("utf8_to_locale(): %s\n", error->message);
189 g_error_free(error);
190 return g_strdup(utf8str);
191 }
193 return str;
194 }
196 char *
197 locale_to_utf8(char *localestr)
198 {
199 gchar *str;
200 gsize rb, wb;
201 GError *error;
203 if( noconvert )
204 return g_strdup(localestr);
206 rb = 0; /* bytes read */
207 wb = 0; /* bytes written */
208 error = NULL;
209 str=g_locale_to_utf8(localestr,
210 strlen(localestr),
211 &wb, &rb,
212 &error);
213 if( error )
214 {
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 }