File: lou_translate.c

package info (click to toggle)
liblouis 3.0.0-3+deb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68,336 kB
  • sloc: ansic: 26,525; sh: 5,113; makefile: 739; perl: 69; python: 42
file content (198 lines) | stat: -rw-r--r-- 5,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* liblouis Braille Translation and Back-Translation Library

   Based on the Linux screenreader BRLTTY, copyright (C) 1999-2006 by
   The BRLTTY Team

   Copyright (C) 2004, 2005, 2006, 2009
   ViewPlus Technologies, Inc. www.viewplus.com and
   JJB Software, Inc. www.jjb-software.com

   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
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   */

#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include "liblouis.h"
#include "louis.h"
#include "progname.h"
#include "unistr.h"
#include "version-etc.h"

static int forward_flag = 0;
static int backward_flag = 0;

static FILE *input;

static const struct option longopts[] =
{
  { "help", no_argument, NULL, 'h' },
  { "version", no_argument, NULL, 'v' },
  { "forward", no_argument, NULL, 'f' },
  { "backward", no_argument, NULL, 'b' },
  { NULL, 0, NULL, 0 }
};

const char version_etc_copyright[] =
  "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc.";

#define AUTHORS "John J. Boyer"

static void 
translate_input (int forward_translation, char *table_name)
{
  char charbuf[MAXSTRING];
  char *outputbuf;
  size_t outlen;
  widechar inbuf[MAXSTRING];
  widechar transbuf[MAXSTRING];
  int inlen;
  int translen;
  int k;
  int ch = 0;
  int result;
  while (1)
    {
      translen = MAXSTRING;
      k = 0;
      while ((ch = fgetc(input)) != '\n' && ch != EOF && k < MAXSTRING - 1)
	charbuf[k++] = ch;
      if (ch == EOF && k == 0)
	break;
      charbuf[k] = 0;
      inlen = extParseChars (charbuf, inbuf);
      if (forward_translation) 
	  result = lou_translateString (table_name, inbuf, &inlen,
				      transbuf, &translen, NULL, NULL, 0);
      else 
	result = lou_backTranslateString (table_name, inbuf, &inlen,
					  transbuf, &translen, NULL, NULL, 0);
      if (!result)
	break;
#ifdef WIDECHARS_ARE_UCS4
      outputbuf = u32_to_u8(transbuf, translen, NULL, &outlen);
#else
      outputbuf = u16_to_u8(transbuf, translen, NULL, &outlen);
#endif
      printf ("%.*s\n", (int)outlen, outputbuf);
      free(outputbuf);
    }
  lou_free ();
}

static void
print_help (void)
{
  printf ("\
Usage: %s [OPTIONS] TABLE[,TABLE,...]\n", program_name);
  
  fputs ("\
Translate whatever is on standard input and print it on standard\n\
output. It is intended for large-scale testing of the accuracy of\n\
Braille translation and back-translation.\n\n", stdout);

  fputs ("\
  -h, --help          display this help and exit\n\
  -v, --version       display version information and exit\n\
  -f, --forward       forward translation using the given table\n\
  -b, --backward      backward translation using the given table\n\
                      If neither -f nor -b are specified forward translation\n\
                      is assumed\n", stdout);
  printf ("\n");
  printf ("Report bugs to %s.\n", PACKAGE_BUGREPORT);

#ifdef PACKAGE_PACKAGER_BUG_REPORTS
  printf ("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS);
#endif
#ifdef PACKAGE_URL
  printf ("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
#endif
}

int
main (int argc, char **argv)
{
  int optc;
  
  set_program_name (argv[0]);

  while ((optc = getopt_long (argc, argv, "hvfb", longopts, NULL)) != -1)
    switch (optc)
      {
      /* --help and --version exit immediately, per GNU coding standards.  */
      case 'v':
        version_etc (stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *) NULL);
        exit (EXIT_SUCCESS);
        break;
      case 'h':
        print_help ();
        exit (EXIT_SUCCESS);
        break;
      case 'f':
	forward_flag = 1;
        break;
      case 'b':
	backward_flag = 1;
        break;
      default:
	fprintf (stderr, "Try `%s --help' for more information.\n",
		 program_name);
	exit (EXIT_FAILURE);
        break;
      }

  if (forward_flag && backward_flag)
    {
      fprintf (stderr, "%s: specify either -f or -b but not both\n", 
	       program_name);
      fprintf (stderr, "Try `%s --help' for more information.\n",
               program_name);
      exit (EXIT_FAILURE);
    }

  /*if (optind != argc - 1)
    {
      /* Print error message and exit.  *
      if (optind < argc - 1)
	fprintf (stderr, "%s: extra operand: %s\n",
		 program_name, argv[optind + 1]);
      else
	fprintf (stderr, "%s: no table specified\n", 
		 program_name);
      fprintf (stderr, "Try `%s --help' for more information.\n",
               program_name);
      exit (EXIT_FAILURE);
    }*/
	if(!argv[optind + 0])
	{
	fprintf (stderr, "%s: no table specified\n", 
		 program_name);
      fprintf (stderr, "Try `%s --help' for more information.\n",
               program_name);
      exit (EXIT_FAILURE);
      }
	if(argv[optind + 1])
	{
		input = fopen(argv[optind + 1], "r");
	}
	else
		input = stdin;

  /* assume forward translation by default */
  translate_input (!backward_flag, argv[optind]);
  exit (EXIT_SUCCESS);
}