i18n: New function recode_string_len().
[pspp-builds.git] / src / libpspp / i18n.c
index 5d03d19a861b1144b496e90c9c24d5a5a61de2cb..29148a0e7a9789cf9cbee7c5d6ac764c83f8ff10 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2006, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
 
    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
@@ -85,6 +85,20 @@ create_iconv (const char* tocode, const char* fromcode)
   return converter->conv;
 }
 
+/* Converts the single byte C from encoding FROM to TO, returning the first
+   byte of the result.
+
+   This function probably shouldn't be used at all, but some code still does
+   use it. */
+char
+recode_byte (const char *to, const char *from, char c)
+{
+  char x;
+  char *s = recode_string (to, from, &c, 1);
+  x = s[0];
+  free (s);
+  return x;
+}
 
 /* Similar to recode_string_pool, but allocates the returned value on the heap
    instead of in a pool.  It is the caller's responsibility to free the
@@ -96,6 +110,17 @@ recode_string (const char *to, const char *from,
   return recode_string_pool (to, from, text, length, NULL);
 }
 
+/* Returns the length, in bytes, of the string that a similar recode_string()
+   call would return. */
+size_t
+recode_string_len (const char *to, const char *from,
+                   const char *text, int length)
+{
+  char *s = recode_string (to, from, text, length);
+  size_t len = strlen (s);
+  free (s);
+  return len;
+}
 
 /* Uses CONV to convert the INBYTES starting at IP into the OUTBYTES starting
    at OP, and appends a null terminator to the output.
@@ -208,7 +233,7 @@ recode_substring_pool (const char *to, const char *from,
   if ( (iconv_t) -1 == conv )
     {
       struct substring out;
-      ss_alloc_substring (&out, text);
+      ss_alloc_substring_pool (&out, text, pool);
       return out;
     }
 
@@ -363,3 +388,12 @@ get_system_decimal (void)
   return radix_char;
 }
 
+const char *
+uc_name (ucs4_t uc, char buffer[16])
+{
+  if (uc >= 0x20 && uc < 0x7f)
+    snprintf (buffer, 16, "`%c'", uc);
+  else
+    snprintf (buffer, 16, "U+%04X", uc);
+  return buffer;
+}