diff --git a/support.c b/support.c
index ce67dc81e6b507e3db2f42e7bc5bee7449ac69fb..d7c196a9402f55d2eae09db23b69f2fa577227c8 100644 (file)
--- a/support.c
+++ b/support.c
-/*
- * $Id: support.c,v 1.2 2004/03/17 23:17:09 kalle Exp $
- *
- */
-
+#include <ctype.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "support.h"
-#ifndef HAVE_LIBGEN_H
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif
+
+#ifdef HAVE_ICONV
+#include <iconv.h>
+#endif
+
+#ifdef HAVE_LANGINFO_CODESET
+#include <langinfo.h>
+#endif
+
+#define BUFSIZE 1024
+
+
+static char *charset = NULL;
+
+#ifdef HAVE_LOCALE_H
+static char *locale = NULL;
+#endif
+
+#ifdef HAVE_ICONV
+iconv_t iconv_from_uft8 = (iconv_t)(-1);
+iconv_t iconv_to_uft8 = (iconv_t)(-1);
+#endif
+
char *
remove_trailing_slash(char *path)
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;
+}
+
+char *
+concat_path(char *p1, char *p2)
+{
+ size_t size;
+ char *path;
+ char append_slash = 0;
+
+ size = strlen(p1);
+ if( size==0 || p1[size-1]!='/' )
+ {
+ size++;
+ append_slash = 1;
+ }
+ size += strlen(p2);
+ size++;
+
+ path = calloc(size, sizeof(char));
+ strncpy(path, p1, size);
+ if( append_slash )
+ strncat(path, "/", size);
+ strncat(path, p2, size);
+
+ return path;
+}
+
+
+
+#ifndef HAVE_BASENAME
char *
basename(char *path)
{
return path;
}
+#endif /* HAVE_BASENAME */
-#endif /* HAVE_LIBGEN_H */
+#ifndef HAVE_STRCASESTR
char *
-utf8(char *str)
+strcasestr(const char *haystack, const char *needle)
+{
+ return strstr(lowerstr(haystack), lowerstr(needle));
+}
+#endif /* HAVE_STRCASESTR */
+
+
+int
+charset_init(void)
{
- static const gchar *charset = NULL;
- static gboolean locale_is_utf8 = FALSE;
+#ifdef HAVE_LOCALE_H
+ /* get current locale */
+ if( (locale=setlocale(LC_CTYPE,"")) == NULL )
+ {
+ fprintf(stderr,"setlocale() - failed!\n");
+ return -1;
+ }
+#endif
+
+#ifdef HAVE_LANGINFO_CODESET
+ /* get charset */
+ if( (charset=nl_langinfo(CODESET)) == NULL )
+ {
+ fprintf(stderr,
+ "nl_langinfo() failed using default:" DEFAULT_CHARSET "\n");
+ }
+#endif
+
+ if( charset==NULL )
+ charset = DEFAULT_CHARSET;
+
+#ifdef HAVE_ICONV
+ /* allocate descriptor for character set conversion */
+ iconv_from_uft8 = iconv_open(charset, "UTF-8");
+ if( iconv_from_uft8 == (iconv_t)(-1) )
+ {
+ perror("iconv_open");
+ return -1;
+ }
+#endif
+
+ return 0;
+}
- if( !charset )
- locale_is_utf8 = g_get_charset(&charset);
+int
+charset_close(void)
+{
+#ifdef HAVE_ICONV
+ if( iconv_from_uft8 == (iconv_t)(-1) )
+ {
+ iconv_close(iconv_from_uft8);
+ iconv_from_uft8 = (iconv_t)(-1);
+ }
+ if( iconv_to_uft8 == (iconv_t)(-1) )
+ {
+ iconv_close(iconv_to_uft8);
+ iconv_to_uft8 = (iconv_t)(-1);
+ }
+#endif
+ return 0;
+}
- if( locale_is_utf8 )
- return str;
- return g_locale_from_utf8(str, -1, NULL, NULL, NULL);
+
+char *
+utf8_to_locale(char *str)
+{
+#ifdef HAVE_ICONV
+ size_t inleft;
+ size_t retlen;
+ char *ret;
+
+ if( iconv_from_uft8 == (iconv_t)(-1) )
+ return strdup(str);
+
+ ret = NULL;
+ retlen = 0;
+ inleft = strlen(str);
+ while( inleft>0 )
+ {
+ char buf[BUFSIZE];
+ size_t outleft = BUFSIZE;
+ char *bufp = buf;
+
+ if( iconv(iconv_from_uft8, &str, &inleft, &bufp, &outleft) <0 )
+ {
+ perror("iconv");
+ free(ret);
+ return NULL;
+ }
+ ret = realloc(ret, BUFSIZE-outleft+1);
+ memcpy(ret+retlen, buf, BUFSIZE-outleft);
+ retlen += BUFSIZE-outleft;
+ ret[retlen] = '\0';
+ }
+ return ret;
+
+#else
+ return strdup(str);
+#endif
}
+
+
+