Code

Removed nested functions
[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 <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 size_t
39 my_strlen(char *str)
40 {
41   if( g_utf8_validate(str,-1,NULL) )
42     return g_utf8_strlen(str,-1);
43   else
44     return strlen(str);
45 }
47 char *
48 remove_trailing_slash(char *path)
49 {
50   int len;
52   if( path==NULL )
53     return NULL;
55   len=strlen(path);
56   if( len>1 && path[len-1] == '/' )
57     path[len-1] = '\0';
59   return path;
60 }
62 char *
63 lowerstr(char *str)
64 {
65   gsize i;
66   gsize len = strlen(str);
68   if( str==NULL )
69     return NULL;
71   i=0;
72   while(i<len && str[i])
73     {
74       str[i] = tolower(str[i]);
75       i++;
76     }
77   return str;
78 }
81 #ifndef HAVE_BASENAME
82 char *
83 basename(char *path)
84 {
85   char *end;
87   path = remove_trailing_slash(path);
88   end = path + strlen(path);
90   while( end>path && *end!='/' )
91     end--;
93   if( *end=='/' && end!=path )
94     return end+1;
96   return path;
97 }
98 #endif /* HAVE_BASENAME */
101 #ifndef HAVE_STRCASESTR
102 char *
103 strcasestr(const char *haystack, const char *needle)
105   return strstr(lowerstr(haystack), lowerstr(needle));
107 #endif /* HAVE_STRCASESTR */
109 // FIXME: utf-8 length
110 char *
111 strscroll(char *str, char *separator, int width, scroll_state_t *st)
113   gchar *tmp, *buf;
114   gsize len, size;
116   if( st->offset==0 )
117     {
118       st->offset++;
119       return g_strdup(str);
120     }
121   
122   /* create a buffer containing the string and the separator */
123   size = strlen(str)+strlen(separator)+1;
124   tmp = g_malloc(size);
125   g_strlcpy(tmp, str, size);
126   g_strlcat(tmp, separator, size);
127   len = my_strlen(tmp);
129   if( st->offset >= len )
130     st->offset = 0;
131   
132   /* create the new scrolled string */
133   size = width+1;
134   if (g_utf8_validate(tmp, -1, NULL) )
135     {
136       int ulen;
137       buf = g_malloc(size*6);// max length of utf8 char is 6
138       g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
139       if( (ulen = g_utf8_strlen(buf, -1)) < width )
140         g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
141     }
142   else
143     {
144       buf = g_malloc(size);
145       g_strlcpy(buf, tmp+st->offset, size);
146       if( strlen(buf) < width )
147         g_strlcat(buf, tmp, size);
148     }
149   if( time(NULL)-st->t >= 1 )
150     {
151       st->t = time(NULL);
152       st->offset++;
153     }
154   g_free(tmp);
155   return buf;
156   
159 void
160 charset_init(gboolean disable)
162   noconvert = disable;
165 char *
166 utf8_to_locale(char *utf8str)
168   gchar *str;
169   gsize rb, wb;
170   GError *error;
172   if( noconvert )
173     return g_strdup(utf8str);
175   rb = 0; /* bytes read */
176   wb = 0; /* bytes written */
177   error = NULL;
178   str=g_locale_from_utf8(utf8str, 
179                          strlen(utf8str),
180                          &wb, &rb,
181                          &error);
182   if( error )
183     {
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     }
193   
194   return str;
197 char *
198 locale_to_utf8(char *localestr)
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     {
216       screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
217       D("locale_to_utf8: %s\n", error->message);
218       g_error_free(error);
219       return g_strdup(localestr);
220     }
222   return str;