diff options
author | Kalle Wallin <kaw@linux.se> | 2004-06-05 11:21:43 +0000 |
---|---|---|
committer | Kalle Wallin <kaw@linux.se> | 2004-06-05 11:21:43 +0000 |
commit | f55a67b3f882641abe5a9b14b045d7ce71964af7 (patch) | |
tree | 181c15b1c59df30b2e28058f2648e5e701e57c4f /src/support.c | |
parent | 677eb1ad30321d83f6196672ea1798c0e1712870 (diff) | |
download | mpd-f55a67b3f882641abe5a9b14b045d7ce71964af7.tar.gz mpd-f55a67b3f882641abe5a9b14b045d7ce71964af7.tar.xz mpd-f55a67b3f882641abe5a9b14b045d7ce71964af7.zip |
Changed directory layout (for future use of gettext)
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@1342 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/support.c')
-rw-r--r-- | src/support.c | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/src/support.c b/src/support.c new file mode 100644 index 000000000..d292e8462 --- /dev/null +++ b/src/support.c @@ -0,0 +1,216 @@ +/* + * (c) 2004 by Kalle Wallin (kaw@linux.se) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <glib.h> + +#include "config.h" +#include "support.h" + +#ifdef HAVE_LOCALE_H +#include <locale.h> +#endif + +#ifdef DEBUG +#define D(x) x +#else +#define D(x) +#endif + +#define BUFSIZE 1024 + +extern void screen_status_printf(char *format, ...); + +static const char *charset = NULL; +static const char *locale = NULL; +static gboolean noconvert = TRUE; + +char * +trim(char *str) +{ + char *end; + + if( str==NULL ) + return NULL; + + while( IS_WHITESPACE(*str) ) + str++; + + end=str+strlen(str)-1; + while( end>str && IS_WHITESPACE(*end) ) + { + *end = '\0'; + end--; + } + return str; +} + +char * +remove_trailing_slash(char *path) +{ + int len; + + if( path==NULL ) + return NULL; + + len=strlen(path); + if( len>1 && path[len-1] == '/' ) + path[len-1] = '\0'; + + return path; +} + +char * +lowerstr(char *str) +{ + size_t i; + size_t len = strlen(str); + + if( str==NULL ) + return NULL; + + i=0; + while(i<len && str[i]) + { + str[i] = tolower(str[i]); + i++; + } + return str; +} + + +#ifndef HAVE_BASENAME +char * +basename(char *path) +{ + char *end; + + path = remove_trailing_slash(path); + end = path + strlen(path); + + while( end>path && *end!='/' ) + end--; + + if( *end=='/' && end!=path ) + return end+1; + + return path; +} +#endif /* HAVE_BASENAME */ + + +#ifndef HAVE_STRCASESTR +char * +strcasestr(const char *haystack, const char *needle) +{ + return strstr(lowerstr(haystack), lowerstr(needle)); +} +#endif /* HAVE_STRCASESTR */ + + +int +charset_init(void) +{ +#ifdef HAVE_LOCALE_H + /* get current locale */ + if( (locale=setlocale(LC_CTYPE,"")) == NULL ) + { + g_printerr("setlocale() - failed!\n"); + return -1; + } +#endif + + /* get charset */ + noconvert = g_get_charset(&charset); + +#ifdef DEBUG + g_printerr("charset: %s [%d]\n", charset, noconvert); + fflush(stderr); +#endif + + return 0; +} + +int +charset_close(void) +{ + return 0; +} + +char * +utf8_to_locale(char *utf8str) +{ + gchar *str; + gsize rb, wb; + GError *error; + + if( noconvert ) + return g_strdup(utf8str); + + rb = 0; /* bytes read */ + wb = 0; /* bytes written */ + error = NULL; + str=g_locale_from_utf8(utf8str, + strlen(utf8str), + &wb, &rb, + &error); + if( error ) + { + screen_status_printf("Error: Unable to convert characters to %s", + charset); + D(g_printerr("utf8_to_locale(): %s\n", error->message)); + g_error_free(error); + return g_strdup(utf8str); + } + + return str; +} + +char * +locale_to_utf8(char *localestr) +{ + gchar *str; + gsize rb, wb; + GError *error; + + if( noconvert ) + return g_strdup(localestr); + + rb = 0; /* bytes read */ + wb = 0; /* bytes written */ + error = NULL; + str=g_locale_to_utf8(localestr, + strlen(localestr), + &wb, &rb, + &error); + if( error ) + { + screen_status_printf("Error: Unable to convert characters to UTF-8"); + D(g_printerr("locale_to_utf8: %s\n", error->message)); + g_error_free(error); + return g_strdup(localestr); + } + + return str; +} + + + |