Code

lyrics: don't check current.lines==NULL
[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 <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     return g_utf8_strlen(str,-1);
41   else
42     return strlen(str);
43 }
45 char *
46 remove_trailing_slash(char *path)
47 {
48   int len;
50   if( path==NULL )
51     return NULL;
53   len=strlen(path);
54   if( len>1 && path[len-1] == '/' )
55     path[len-1] = '\0';
57   return path;
58 }
60 char *
61 lowerstr(char *str)
62 {
63   gsize i;
64   gsize len = strlen(str);
66   if( str==NULL )
67     return NULL;
69   i=0;
70   while(i<len && str[i])
71     {
72       str[i] = tolower(str[i]);
73       i++;
74     }
75   return str;
76 }
79 #ifndef HAVE_BASENAME
80 char *
81 basename(char *path)
82 {
83   char *end;
85   path = remove_trailing_slash(path);
86   end = path + strlen(path);
88   while( end>path && *end!='/' )
89     end--;
91   if( *end=='/' && end!=path )
92     return end+1;
94   return path;
95 }
96 #endif /* HAVE_BASENAME */
99 #ifndef HAVE_STRCASESTR
100 char *
101 strcasestr(const char *haystack, const char *needle)
103   return strstr(lowerstr(haystack), lowerstr(needle));
105 #endif /* HAVE_STRCASESTR */
107 // FIXME: utf-8 length
108 char *
109 strscroll(char *str, char *separator, int width, scroll_state_t *st)
111         gchar *tmp, *buf;
112         gsize len, size;
114         if( st->offset==0 ) {
115                 st->offset++;
116                 return g_strdup(str);
117         }
119         /* create a buffer containing the string and the separator */
120         size = strlen(str)+strlen(separator)+1;
121         tmp = g_malloc(size);
122         g_strlcpy(tmp, str, size);
123         g_strlcat(tmp, separator, size);
124         len = my_strlen(tmp);
126         if( st->offset >= len )
127                 st->offset = 0;
129         /* create the new scrolled string */
130         size = width+1;
131         if (g_utf8_validate(tmp, -1, NULL) ) {
132                 int ulen;
133                 buf = g_malloc(size*6);// max length of utf8 char is 6
134                 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
135                 if( (ulen = g_utf8_strlen(buf, -1)) < width )
136                         g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
137         } else {
138                 buf = g_malloc(size);
139                 g_strlcpy(buf, tmp+st->offset, size);
140                 if (strlen(buf) < (size_t)width)
141                         g_strlcat(buf, tmp, size);
142         }
143         if( time(NULL)-st->t >= 1 ) {
144                 st->t = time(NULL);
145                 st->offset++;
146         }
147         g_free(tmp);
148         return buf;
151 void
152 charset_init(gboolean disable)
154   noconvert = disable;
157 char *
158 utf8_to_locale(const char *utf8str)
160   gchar *str;
161   gsize rb, wb;
162   GError *error;
164   if( noconvert )
165     return g_strdup(utf8str);
167   rb = 0; /* bytes read */
168   wb = 0; /* bytes written */
169   error = NULL;
170   str=g_locale_from_utf8(utf8str, 
171                          strlen(utf8str),
172                          &wb, &rb,
173                          &error);
174   if( error )
175     {
176       const char *charset;
178       g_get_charset(&charset);
179       screen_status_printf(_("Error: Unable to convert characters to %s"),
180                            charset);
181       D("utf8_to_locale(): %s\n", error->message);
182       g_error_free(error);
183       return g_strdup(utf8str);
184     }
185   
186   return str;
189 char *
190 locale_to_utf8(const char *localestr)
192   gchar *str;
193   gsize rb, wb;
194   GError *error;
196   if( noconvert )
197     return g_strdup(localestr);
199   rb = 0; /* bytes read */
200   wb = 0; /* bytes written */
201   error = NULL;
202   str=g_locale_to_utf8(localestr, 
203                        strlen(localestr), 
204                        &wb, &rb,
205                        &error);
206   if( error )
207     {
208       screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
209       D("locale_to_utf8: %s\n", error->message);
210       g_error_free(error);
211       return g_strdup(localestr);
212     }
214   return str;