Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / liblouis / src / tools / lou_translate.c
1 /* liblouis Braille Translation and Back-Translation Library
2
3    Based on the Linux screenreader BRLTTY, copyright (C) 1999-2006 by
4    The BRLTTY Team
5
6    Copyright (C) 2004, 2005, 2006, 2009
7    ViewPlus Technologies, Inc. www.viewplus.com and
8    JJB Software, Inc. www.jjb-software.com
9
10    This program is free software: you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation, either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23    Maintained by John J. Boyer john.boyer@jjb-software.com
24    */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <getopt.h>
34 #include "liblouis.h"
35 #include "louis.h"
36 #include "progname.h"
37 #include "version-etc.h"
38
39 #define BUFSIZE MAXSTRING - 4
40
41 static int forward_flag = 0;
42 static int backward_flag = 0;
43
44 static const struct option longopts[] =
45 {
46   { "help", no_argument, NULL, 'h' },
47   { "version", no_argument, NULL, 'v' },
48   { "forward", no_argument, NULL, 'f' },
49   { "backward", no_argument, NULL, 'b' },
50   { NULL, 0, NULL, 0 }
51 };
52
53 const char version_etc_copyright[] =
54   "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc.";
55
56 #define AUTHORS "John J. Boyer"
57
58 static void 
59 translate_input (int forward_translation, char *table_name)
60 {
61   char charbuf[BUFSIZE];
62   char *outputbuf;
63   widechar inbuf[BUFSIZE];
64   widechar transbuf[BUFSIZE];
65   int inlen;
66   int translen;
67   int k;
68   int ch = 0;
69   int result;
70   while (1)
71     {
72       translen = BUFSIZE;
73       k = 0;
74       while ((ch = getchar ()) != '\n' && ch != EOF && k < BUFSIZE)
75         charbuf[k++] = ch;
76       if (ch == EOF && k == 0)
77         break;
78       charbuf[k] = 0;
79       inlen = extParseChars (charbuf, inbuf);
80       if (forward_translation) 
81           result = lou_translateString (table_name, inbuf, &inlen,
82                                       transbuf, &translen, NULL, NULL, 0);
83       else 
84         result = lou_backTranslateString (table_name, inbuf, &inlen,
85                                           transbuf, &translen, NULL, NULL, 0);
86       if (!result)
87         break;
88       outputbuf = showString (transbuf, translen);
89       k = strlen (outputbuf) - 1;
90       outputbuf[k] = 0;
91       printf ("%s\n", &outputbuf[1]);
92     }
93   lou_free ();
94 }
95
96 static void
97 print_help (void)
98 {
99   printf ("\
100 Usage: %s [OPTIONS] TABLE[,TABLE,...]\n", program_name);
101   
102   fputs ("\
103 Translate whatever is on standard input and print it on standard\n\
104 output. It is intended for large-scale testing of the accuracy of\n\
105 Braille translation and back-translation.\n\n", stdout);
106
107   fputs ("\
108   -h, --help          display this help and exit\n\
109   -v, --version       display version information and exit\n\
110   -f, --forward       forward translation using the given table\n\
111   -b, --backward      backward translation using the given table\n\
112                       If neither -f nor -b are specified forward translation\n\
113                       is assumed\n", stdout);
114   printf ("\n");
115   printf ("Report bugs to %s.\n", PACKAGE_BUGREPORT);
116
117 #ifdef PACKAGE_PACKAGER_BUG_REPORTS
118   printf ("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS);
119 #endif
120 #ifdef PACKAGE_URL
121   printf ("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
122 #endif
123 }
124
125 int
126 main (int argc, char **argv)
127 {
128   int optc;
129   
130   set_program_name (argv[0]);
131
132   while ((optc = getopt_long (argc, argv, "hvfb", longopts, NULL)) != -1)
133     switch (optc)
134       {
135       /* --help and --version exit immediately, per GNU coding standards.  */
136       case 'v':
137         version_etc (stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *) NULL);
138         exit (EXIT_SUCCESS);
139         break;
140       case 'h':
141         print_help ();
142         exit (EXIT_SUCCESS);
143         break;
144       case 'f':
145         forward_flag = 1;
146         break;
147       case 'b':
148         backward_flag = 1;
149         break;
150       default:
151         fprintf (stderr, "Try `%s --help' for more information.\n",
152                  program_name);
153         exit (EXIT_FAILURE);
154         break;
155       }
156
157   if (forward_flag && backward_flag)
158     {
159       fprintf (stderr, "%s: specify either -f or -b but not both\n", 
160                program_name);
161       fprintf (stderr, "Try `%s --help' for more information.\n",
162                program_name);
163       exit (EXIT_FAILURE);
164     }
165
166   if (optind != argc - 1)
167     {
168       /* Print error message and exit.  */
169       if (optind < argc - 1)
170         fprintf (stderr, "%s: extra operand: %s\n",
171                  program_name, argv[optind + 1]);
172       else
173         fprintf (stderr, "%s: no table specified\n", 
174                  program_name);
175       fprintf (stderr, "Try `%s --help' for more information.\n",
176                program_name);
177       exit (EXIT_FAILURE);
178     }
179
180   /* assume forward translation by default */
181   translate_input (!backward_flag, argv[optind]);
182   exit (EXIT_SUCCESS);
183 }