zip-reader: Switch to a more usual error reporting mechanism.
[pspp] / tests / libpspp / i18n-test.c
index b433756b0d21b2a1d9e455f1833de3b43eba006b..a4993a8d0280b13a6c4b80a70371cec5863ab2c1 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 2010, 2011, 2012 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
 
 #include "libpspp/i18n.h"
 
+#include "gl/xalloc.h"
+
 #undef NDEBUG
 #include <assert.h>
 
+/* Returns a malloc()'d copy of INPUT with \ooo octal escapes replaced by their
+   values. */
+static char *
+backslash_decode (const char *input)
+{
+  char *output = xmalloc (strlen (input) + 1);
+  char *p = output;
+  for (; *input; input++)
+    {
+      if (*input == '\\' && input[1] >= '0' && input[1] <= '7')
+        {
+          uint8_t c = 0;
+          while (input[1] >= '0' && input[1] <= '7')
+            c = c * 8 + (*++input - '0');
+          *p++ = c;
+        }
+      else
+        *p++ = *input;
+    }
+  *p = '\0';
+
+  return output;
+}
+
 int
 main (int argc, char *argv[])
 {
   i18n_init ();
 
+  if (argc > 1 && !strcmp (argv[1], "supports_encodings"))
+    {
+      int status = 0;
+      int i;
+
+      for (i = 2; i < argc; i++)
+        if (!is_encoding_supported (argv[i]))
+          {
+            printf ("encoding \"%s\" is NOT supported\n", argv[i]);
+            status = 77;
+          }
+      i18n_done ();
+      exit (status);
+    }
   if (argc == 5 && !strcmp (argv[1], "recode"))
     {
       const char *from = argv[2];
       const char *to = argv[3];
-      const char *string = argv[4];
+      char *string = backslash_decode (argv[4]);
       char *result = recode_string (to, from, string, -1);
       puts (result);
       assert (strlen (result) == recode_string_len (to, from, string, -1));
+      free (string);
       free (result);
     }
   else if (argc == 6 && !strcmp (argv[1], "concat"))
     {
-      const char *head = argv[2];
-      const char *tail = argv[3];
+      char *head = backslash_decode (argv[2]);
+      char *tail = backslash_decode (argv[3]);
       const char *encoding = argv[4];
       int max_len = atoi (argv[5]);
       char *result;
@@ -64,10 +105,16 @@ main (int argc, char *argv[])
         }
 
       free (result);
+      free (head);
+      free (tail);
     }
   else
     {
       fprintf (stderr, "\
+usage: %s supports_encodings ENCODING...\n\
+where ENCODING is the name of an encoding.\n\
+Exits with status 0 if all the encodings are supported, 77 otherwise.\n\
+\n\
 usage: %s recode FROM TO STRING\n\
 where FROM is the source encoding,\n\
       TO is the target encoding,\n\
@@ -78,7 +125,7 @@ where HEAD is the first string to concatenate\n\
       TAIL is the second string to concatenate\n\
       ENCODING is the encoding in which to measure the result's length\n\
       MAX_LEN is the maximum length of the result in ENCODING.\n",
-               argv[0], argv[0]);
+               argv[0], argv[0], argv[0]);
       return EXIT_FAILURE;
     }