reflect renaming of g_string_sprintfa to g_string_printfa
[platform/upstream/glib.git] / gconvert.c
index 344902f..035eefa 100644 (file)
 #include "glib.h"
 #include "config.h"
 
-#ifdef G_OS_WIN32
+#ifdef G_PLATFORM_WIN32
+#define STRICT
 #include <windows.h>
+#undef STRICT
 #endif
 
-#define _(s) (s)
+#include "glibintl.h"
 
 GQuark 
 g_convert_error_quark()
@@ -51,6 +53,20 @@ g_convert_error_quark()
 #error libiconv not in use but included iconv.h is from libiconv
 #endif
 
+/**
+ * g_iconv_open:
+ * @to_codeset: destination codeset
+ * @from_codeset: source codeset
+ * 
+ * Same as the standard UNIX routine iconv_open(), but
+ * may be implemented via libiconv on UNIX flavors that lack
+ * a native implementation.
+ * 
+ * GLib provides g_convert() and g_locale_to_utf8() which are likely
+ * more convenient than the raw iconv wrappers.
+ * 
+ * Return value: a "conversion descriptor"
+ **/
 GIConv
 g_iconv_open (const gchar  *to_codeset,
              const gchar  *from_codeset)
@@ -60,6 +76,23 @@ g_iconv_open (const gchar  *to_codeset,
   return (GIConv)cd;
 }
 
+/**
+ * g_iconv:
+ * @converter: conversion descriptor from g_iconv_open()
+ * @inbuf: bytes to convert
+ * @inbytes_left: inout parameter, bytes remaining to convert in @inbuf
+ * @outbuf: converted output bytes
+ * @outbytes_left: inout parameter, bytes available to fill in @outbuf
+ * 
+ * Same as the standard UNIX routine iconv(), but
+ * may be implemented via libiconv on UNIX flavors that lack
+ * a native implementation.
+ *
+ * GLib provides g_convert() and g_locale_to_utf8() which are likely
+ * more convenient than the raw iconv wrappers.
+ * 
+ * Return value: count of non-reversible conversions, or -1 on error
+ **/
 size_t 
 g_iconv (GIConv   converter,
         gchar  **inbuf,
@@ -72,6 +105,21 @@ g_iconv (GIConv   converter,
   return iconv (cd, inbuf, inbytes_left, outbuf, outbytes_left);
 }
 
+/**
+ * g_iconv_close:
+ * @converter: a conversion descriptor from g_iconv_open()
+ *
+ * Same as the standard UNIX routine iconv_close(), but
+ * may be implemented via libiconv on UNIX flavors that lack
+ * a native implementation. Should be called to clean up
+ * the conversion descriptor from iconv_open() when
+ * you are done converting things.
+ *
+ * GLib provides g_convert() and g_locale_to_utf8() which are likely
+ * more convenient than the raw iconv wrappers.
+ * 
+ * Return value: -1 on error, 0 on success
+ **/
 gint
 g_iconv_close (GIConv converter)
 {
@@ -92,7 +140,7 @@ open_converter (const gchar *to_codeset,
       /* Something went wrong.  */
       if (errno == EINVAL)
         g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
-                     _("Conversion from character set `%s' to `%s' is not suppo\rted"),
+                     _("Conversion from character set `%s' to `%s' is not supported"),
                      from_codeset, to_codeset);
       else
         g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
@@ -233,9 +281,12 @@ g_convert (const gchar *str,
     {
       if ((p - str) != len) 
        {
-         g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
-                      _("Partial character sequence at end of input"));
-         have_error = TRUE;
+          if (!have_error)
+            {
+              g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
+                           _("Partial character sequence at end of input"));
+              have_error = TRUE;
+            }
        }
     }
 
@@ -337,6 +388,8 @@ g_convert_with_fallback (const gchar *str,
   else
     g_error_free (local_error);
 
+  local_error = NULL;
+  
   /* No go; to proceed, we need a converter from "UTF-8" to
    * to_codeset, and the string as UTF-8.
    */
@@ -516,14 +569,16 @@ g_locale_to_utf8 (const gchar  *opsysstring,
                  gint         *bytes_written,
                  GError      **error)
 {
-#ifdef G_OS_WIN32
+#ifdef G_PLATFORM_WIN32
 
   gint i, clen, total_len, wclen, first;
-  const gint len = len < 0 ? strlen (opsysstring) : len;
   wchar_t *wcs, wc;
   gchar *result, *bp;
   const wchar_t *wcp;
 
+  if (len == -1)
+    len = strlen (opsysstring);
+  
   wcs = g_new (wchar_t, len);
   wclen = MultiByteToWideChar (CP_ACP, 0, opsysstring, len, wcs, len);
 
@@ -610,7 +665,7 @@ g_locale_to_utf8 (const gchar  *opsysstring,
   
   return result;
 
-#else
+#else  /* !G_PLATFORM_WIN32 */
 
   char *charset, *str;
 
@@ -621,7 +676,7 @@ g_locale_to_utf8 (const gchar  *opsysstring,
                   "UTF-8", charset, bytes_read, bytes_written, error);
   
   return str;
-#endif
+#endif /* !G_PLATFORM_WIN32 */
 }
 
 /**
@@ -655,15 +710,17 @@ g_locale_from_utf8 (const gchar *utf8string,
                    gint        *bytes_written,
                    GError     **error)
 {
-#ifdef G_OS_WIN32
+#ifdef G_PLATFORM_WIN32
 
   gint i, mask, clen, mblen;
-  const gint len = len < 0 ? strlen (utf8string) : len;
   wchar_t *wcs, *wcp;
   gchar *result;
   guchar *cp, *end, c;
   gint n;
   
+  if (len == -1)
+    len = strlen (utf8string);
+  
   /* First convert to wide chars */
   cp = (guchar *) utf8string;
   end = cp + len;
@@ -755,7 +812,7 @@ g_locale_from_utf8 (const gchar *utf8string,
   
   return result;
 
-#else
+#else  /* !G_PLATFORM_WIN32 */
 
   gchar *charset, *str;
 
@@ -767,7 +824,7 @@ g_locale_from_utf8 (const gchar *utf8string,
 
   return str;
   
-#endif
+#endif /* !G_PLATFORM_WIN32 */
 }
 
 /**
@@ -800,11 +857,11 @@ g_filename_to_utf8 (const gchar *opsysstring,
                    gint        *bytes_written,
                    GError     **error)
 {
-#ifdef G_OS_WIN32
+#ifdef G_PLATFORM_WIN32
   return g_locale_to_utf8 (opsysstring, len,
                           bytes_read, bytes_written,
                           error);
-#else
+#else  /* !G_PLATFORM_WIN32 */
   if (getenv ("G_BROKEN_FILENAMES"))
     return g_locale_to_utf8 (opsysstring, len,
                             bytes_read, bytes_written,
@@ -824,7 +881,7 @@ g_filename_to_utf8 (const gchar *opsysstring,
     return g_strdup (opsysstring);
   else
     return g_strndup (opsysstring, len);
-#endif
+#endif /* !G_PLATFORM_WIN32 */
 }
 
 /**
@@ -856,11 +913,11 @@ g_filename_from_utf8 (const gchar *utf8string,
                      gint        *bytes_written,
                      GError     **error)
 {
-#ifdef G_OS_WIN32
+#ifdef G_PLATFORM_WIN32
   return g_locale_from_utf8 (utf8string, len,
                             bytes_read, bytes_written,
                             error);
-#else
+#else  /* !G_PLATFORM_WIN32 */
   if (getenv ("G_BROKEN_FILENAMES"))
     return g_locale_from_utf8 (utf8string, len,
                               bytes_read, bytes_written,
@@ -880,7 +937,5 @@ g_filename_from_utf8 (const gchar *utf8string,
     return g_strdup (utf8string);
   else
     return g_strndup (utf8string, len);
-#endif
+#endif /* !G_PLATFORM_WIN32 */
 }
-
-