diff --git a/src/support.c b/src/support.c
index bde8d1914b012fe7cb4a5cf35bb5d26679832b05..8d9d0d01079fa61b8069d14f9f074911f1ebccc3 100644 (file)
--- a/src/support.c
+++ b/src/support.c
-/*
- * $Id$
- *
+/*
* (c) 2004 by Kalle Wallin <kaw@linux.se>
*
* This program is free software; you can redistribute it and/or modify
*/
#include "support.h"
-#include "ncmpc.h"
+#include "charset.h"
+#include "config.h"
#include <assert.h>
-#include <time.h>
#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
-#define BUFSIZE 1024
-
-extern void screen_status_printf(const char *format, ...);
-
-static gboolean noconvert = TRUE;
-
-size_t
-my_strlen(const char *str)
-{
- assert(str != NULL);
-
- if (g_utf8_validate(str, -1, NULL)) {
- size_t len = g_utf8_strlen(str, -1);
- size_t width = 0;
- gunichar c;
-
- while (len--) {
- c = g_utf8_get_char(str);
- width += g_unichar_iswide(c) ? 2 : 1;
- str += g_unichar_to_utf8(c, NULL);
- }
-
- return width;
- } else
- return strlen(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)
-{
- gsize i;
- gsize 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;
-
- assert(path != NULL);
-
- 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 *
+const char *
strcasestr(const char *haystack, const char *needle)
{
+ char *haystack2 = g_utf8_strdown(haystack, -1);
+ char *needle2 = g_utf8_strdown(needle, -1);
+ char *result;
+
assert(haystack != NULL);
assert(needle != NULL);
- return strstr(lowerstr(haystack), lowerstr(needle));
+ result = strstr(haystack2, needle2);
+ g_free(haystack2);
+ g_free(needle2);
+
+ return haystack + (result - haystack2);
}
#endif /* HAVE_STRCASESTR */
tmp = g_malloc(size);
g_strlcpy(tmp, str, size);
g_strlcat(tmp, separator, size);
- len = my_strlen(tmp);
+ len = utf8_width(tmp);
if (st->offset >= len)
st->offset = 0;
g_free(tmp);
return buf;
}
-
-void
-charset_init(gboolean disable)
-{
- noconvert = disable;
-}
-
-char *
-utf8_to_locale(const char *utf8str)
-{
- gchar *str;
- gsize rb, wb;
- GError *error;
-
- assert(utf8str != NULL);
-
- 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) {
- const char *charset;
-
- g_get_charset(&charset);
- screen_status_printf(_("Error: Unable to convert characters to %s"),
- charset);
- g_error_free(error);
- return g_strdup(utf8str);
- }
-
- return str;
-}
-
-char *
-locale_to_utf8(const char *localestr)
-{
- gchar *str;
- gsize rb, wb;
- GError *error;
-
- assert(localestr != NULL);
-
- 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"));
- g_error_free(error);
- return g_strdup(localestr);
- }
-
- return str;
-}