Implemented g_iconv() w/ needing libiconv
[mono.git] / eglib / src / gunicode.c
index 11dc508d9a047e4c97f50210716f03fd50456eee..7ef1874254ee44e2e65efb71de1638892ce4b1d5 100644 (file)
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  *
  */
+#include <config.h>
 #include <stdio.h>
 #include <glib.h>
 #include <unicode-data.h>
 #include <errno.h>
-#ifdef _MSC_VER
-/* FIXME */
-#define CODESET 1
-#include <Windows.h>
 
-typedef int iconv_t;
+#if defined(_MSC_VER) || defined(G_OS_WIN32)
+/* FIXME */
+#  define CODESET 1
+#  include <windows.h>
 #else
-#include <langinfo.h>
-#include <iconv.h>
+#    ifdef HAVE_LANGINFO_H
+#       include <langinfo.h>
+#    endif
+#    ifdef HAVE_LOCALCHARSET_H
+#       include <localcharset.h>
+#    endif
 #endif
 
 static char *my_charset;
@@ -83,7 +87,7 @@ static const gulong offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E208
 GUnicodeType 
 g_unichar_type (gunichar c)
 {
-int i;
+       int i;
 
        guint16 cp = (guint16) c;
        for (i = 0; i < unicode_category_ranges_count; i++) {
@@ -120,6 +124,13 @@ int i;
        return 0;
 }
 
+GUnicodeBreakType
+g_unichar_break_type (gunichar c)
+{
+       // MOONLIGHT_FIXME
+       return G_UNICODE_BREAK_UNKNOWN;
+}
+
 gunichar
 g_unichar_case (gunichar c, gboolean upper)
 {
@@ -193,35 +204,46 @@ g_unichar_xdigit_value (gunichar c)
        return -1;
 }
 
+gboolean
+g_unichar_isspace (gunichar c)
+{
+       GUnicodeType type = g_unichar_type (c);
+       if (type == G_UNICODE_LINE_SEPARATOR ||
+           type == G_UNICODE_PARAGRAPH_SEPARATOR ||
+           type == G_UNICODE_SPACE_SEPARATOR)
+               return TRUE;
+
+       return FALSE;
+}
+
 gchar *
 g_convert (const gchar *str, gssize len,
           const gchar *to_codeset, const gchar *from_codeset,
           gsize *bytes_read, gsize *bytes_written, GError **error)
 {
-       char *result = NULL;
-#ifdef G_OS_WIN32
-#else
-       iconv_t convertor;
-       char *buffer, *output;
-       const char *strptr = (const char *) str;
        size_t str_len = len == -1 ? strlen (str) : len;
-       size_t buffer_size;
-       size_t left, out_left;
+       const char *strptr = (const char *) str;
+       size_t left, out_left, buffer_size;
+       char *buffer, *output;
+       char *result = NULL;
+       GIConv cd;
        
-       convertor = iconv_open (to_codeset, from_codeset);
-       if (convertor == (iconv_t) -1){
-               *bytes_written = 0;
-               *bytes_read = 0;
+       if ((cd = g_iconv_open (to_codeset, from_codeset)) == (GIConv) -1) {
+               if (bytes_written)
+                       *bytes_written = 0;
+               if (bytes_read)
+                       *bytes_read = 0;
                return NULL;
        }
-
+       
        buffer_size = str_len + 1 + 8;
        buffer = g_malloc (buffer_size);
        out_left = str_len;
        output = buffer;
        left = str_len;
+       
        while (left > 0){
-               int res = iconv (convertor, (char **) &strptr, &left, &output, &out_left);
+               int res = g_iconv (cd, (char **) &strptr, &left, &output, &out_left);
                if (res == (size_t) -1){
                        if (errno == E2BIG){
                                char *n;
@@ -264,8 +286,8 @@ g_convert (const gchar *str, gssize len,
        *output = 0;
        result = buffer;
  leave:
-       iconv_close (convertor);
-#endif
+       g_iconv_close (cd);
+       
        return result;
 }
 
@@ -288,21 +310,28 @@ g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gs
 gboolean
 g_get_charset (G_CONST_RETURN char **charset)
 {
+       if (my_charset == NULL) {
 #ifdef G_OS_WIN32
-       static char buf[14];
-       sprintf (buf, "CP%u", GetACP ());
-       *charset = buf;
-       is_utf8 = FALSE;
+               static char buf [14];
+               sprintf (buf, "CP%u", GetACP ());
+               my_charset = buf;
+               is_utf8 = FALSE;
+#else
+               /* These shouldn't be heap allocated */
+#if HAVE_LOCALCHARSET_H
+               my_charset = locale_charset ();
+#elif defined(HAVE_LANGINFO_H)
+               my_charset = nl_langinfo (CODESET);
 #else
-       if (my_charset == NULL){
-               my_charset = g_strdup (nl_langinfo (CODESET));
+               my_charset = "UTF-8";
+#endif
                is_utf8 = strcmp (my_charset, "UTF-8") == 0;
+#endif
        }
        
        if (charset != NULL)
                *charset = my_charset;
 
-#endif
        return is_utf8;
 }