~ubuntu-branches/debian/jessie/liblouis/jessie

« back to all changes in this revision

Viewing changes to tools/lou_translate.c

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Thibault
  • Date: 2010-01-12 23:48:47 UTC
  • mto: (1.1.4 upstream) (5.2.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100112234847-ph9xdk3lrjdox6ks
ImportĀ upstreamĀ versionĀ 1.8.0

Show diffs side-by-side

added added

removed removed

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