yaz-icu: input file may be given.
[yaz-moved-to-github.git] / util / yaz-icu.c
index 10fa2f5..fe86bed 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2009 Index Data
+ * Copyright (C) 1995-2011 Index Data
  * See the file LICENSE for details.
  */
 
@@ -11,6 +11,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #include <yaz/options.h>
 
@@ -40,12 +41,12 @@ static struct config_t {
   
 void print_option_error(const struct config_t *p_config)
 {  
-    fprintf(stderr, "Calling error, valid options are :\n");
-    fprintf(stderr, "yaz-icu\n"
-            "   [-c (path/to/config/file.xml)]\n"
-            "   [-p (a|c|l|t)] print ICU info \n"
-            "   [-s] Show sort normalization key\n"
-            "   [-x] XML output\n"
+    fprintf(stderr, "yaz-icu [options] [infile]\n"
+            "Options:\n"
+            "   -c file         XML configuration\n"
+            "   -p a|c|l|t      Print ICU info \n"
+            "   -s              Show sort normalization key\n"
+            "   -x              XML output instread of text\n"
             "\n"
             "Examples:\n"
             "cat hugetextfile.txt | ./yaz-icu -c config.xml \n"
@@ -75,7 +76,7 @@ void read_params(int argc, char **argv, struct config_t *p_config)
     p_config->xmloutput = 0;
     p_config->sortoutput = 0;
     p_config->chain = 0;
-    p_config->infile = stdin;
+    p_config->infile = 0;
     p_config->outfile = stdout;
     
     /* set up command line parameters */
@@ -96,17 +97,30 @@ void read_params(int argc, char **argv, struct config_t *p_config)
         case 'x':
             p_config->xmloutput = 1;
             break;
+        case 0:
+            if (p_config->infile)
+            {
+                fprintf(stderr, "yaz-icu: only one input file may be given\n");
+                print_option_error(p_config);
+            }
+            p_config->infile = fopen(arg, "r");
+            if (!p_config->infile)
+            {
+                fprintf(stderr, "yaz-icu: cannot open %s : %s\n",
+                        arg, strerror(errno));
+                exit(1);
+            }
+            break;
         default:
-            printf("Got %d\n", ret);
+            fprintf(stderr, "yaz_icu: invalid option: %s\n", arg);
             print_option_error(p_config);
         }
     }
-    
-    if ((!strlen(p_config->conffile)
-         && !strlen(p_config->print))
-        || !config.infile
-        || !config.outfile)
-        
+
+    if (p_config->infile == 0)
+        p_config->infile = stdin;
+
+    if (!strlen(p_config->conffile) && !strlen(p_config->print))
         print_option_error(p_config);
 }
 
@@ -138,19 +152,20 @@ static void print_icu_converters(const struct config_t *p_config)
     if (p_config->xmloutput)
         fprintf(config.outfile, "<converters count=\"%d\" default=\"%s\">\n",
                 count, ucnv_getDefaultName());
-    else {    
+    else
+    {    
         fprintf(config.outfile, "Available ICU converters: %d\n", count);
         fprintf(config.outfile, "Default ICU Converter is: '%s'\n", 
                 ucnv_getDefaultName());
     }
     
-    for(i=0;i<count;i++)
+    for (i = 0; i < count; i++)
     {
         if (p_config->xmloutput)
             fprintf(config.outfile, "<converter id=\"%s\"/>\n", 
                     ucnv_getAvailableName(i));
         else     
-            fprintf(config.outfile, "%s ", ucnv_getAvailableName(i));
+            fprintf(config.outfile, "%s\n", ucnv_getAvailableName(i));
     }
     
     if (p_config->xmloutput)
@@ -161,29 +176,27 @@ static void print_icu_converters(const struct config_t *p_config)
 
 static void print_icu_transliterators(const struct config_t *p_config)
 {
-    int32_t buf_cap = 128;
-    char buf[128];
-    int32_t i;
-    int32_t count = utrans_countAvailableIDs();
-    
+    UErrorCode status;
+    UEnumeration *en = utrans_openIDs(&status);
+    int32_t count = uenum_count(en, &status);
+    const char *name;
+    int32_t length;
+
     if (p_config->xmloutput)
         fprintf(config.outfile, "<transliterators count=\"%d\">\n",  count);
     else 
         fprintf(config.outfile, "Available ICU transliterators: %d\n", count);
-    
-    for(i = 0; i <count; i++)
+
+    while ((name = uenum_next(en, &length, &status)))
     {
-        utrans_getAvailableID(i, buf, buf_cap);
         if (p_config->xmloutput)
-            fprintf(config.outfile, "<transliterator id=\"%s\"/>\n", buf);
+            fprintf(config.outfile, "<transliterator id=\"%s\"/>\n", name);
         else
-            fprintf(config.outfile, " %s", buf);
+            fprintf(config.outfile, "%s\n", name);
     }
-    
+    uenum_close(en);
     if (p_config->xmloutput)
-    {
         fprintf(config.outfile, "</transliterators>\n");
-    }
     else
     {
         fprintf(config.outfile, "\n\nUnicode Set Patterns:\n"
@@ -223,10 +236,8 @@ static void print_icu_transliterators(const struct config_t *p_config)
                 "   [A-Za-z]; Lower(); Latin-Katakana; Katakana-Hiragana (transforms latin and katagana to hiragana)\n"
                 "   [[:separator:][:start punctuation:][:initial punctuation:]] Remove \n"
                 "\n"
-                "see http://icu.sourceforge.net/userguide/Transform.html\n"
-                "    http://www.unicode.org/Public/UNIDATA/UCD.html\n"
-                "    http://icu.sourceforge.net/userguide/Transform.html\n"
-                "    http://icu.sourceforge.net/userguide/TransformRule.html\n"
+                "see http://userguide.icu-project.org/transforms/general\n"
+                "    http://www.unicode.org/reports/tr44/\n"
             );
         
         
@@ -283,8 +294,13 @@ static void print_icu_xml_locales(const struct config_t *p_config)
         fprintf(config.outfile, "<locales count=\"%d\" default=\"%s\" collations=\"%d\">\n", 
                 count, uloc_getDefault(), ucol_countAvailable());
     }
+    else
+    {
+        fprintf(config.outfile, "Available ICU locales: %d\n", count);
+        fprintf(config.outfile, "Default locale is: %s\n",  uloc_getDefault());
+    }
   
-    for(i=0;i<count;i++) 
+    for (i = 0; i < count; i++) 
     {
 
         keyword_len 
@@ -390,14 +406,14 @@ static void print_icu_xml_locales(const struct config_t *p_config)
             fprintf(config.outfile, "\n");
         }
         else
-            fprintf(config.outfile, "%s ", uloc_getAvailable(i));
+            fprintf(config.outfile, "%s\n", uloc_getAvailable(i));
     }
     if (p_config->xmloutput)
         fprintf(config.outfile, "</locales>\n");
     else
         fprintf(config.outfile, "\n");
 
-    if(U_FAILURE(status))
+    if (U_FAILURE(status))
     {
         fprintf(stderr, "ICU Error: %d %s\n", status, u_errorName(status));
         exit(2);
@@ -560,7 +576,7 @@ int main(int argc, char **argv)
 #else /* YAZ_HAVE_ICU */
 
     printf("ICU not available on your system.\n"
-           "Please install libicu36-dev and icu-doc or similar, "
+           "Please install libicu-dev and icu-doc or similar, "
            "re-configure and re-compile\n");