1 /*
2  * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <apertium/transfer_mult.h>
18 #include <lttoolbox/lt_locale.h>
19 #include <apertium/apertium_config.h>
20 
21 #include <cstdlib>
22 #include <iostream>
23 #include <libgen.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #ifdef _WIN32
28 #include <io.h>
29 #include <fcntl.h>
30 #endif
31 
32 using namespace std;
33 
message(char * progname)34 void message(char *progname)
35 {
36   cerr << "USAGE: " << basename(progname) << " preproc biltrans [input [output]]" << endl;
37   cerr << "  preproc    result of preprocess trules file" << endl;
38   cerr << "  biltrans   bilingual letter transducer file" << endl;
39   cerr << "  input      input file, standard input by default" << endl;
40   cerr << "  output     output file, standard output by default" << endl;
41   exit(EXIT_FAILURE);
42 }
43 
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46   LtLocale::tryToSetLocale();
47 
48   if(argc > 5 || argc <3)
49   {
50     message(argv[0]);
51   }
52 
53   for(unsigned int i = 1; i < 3; i++)
54   {
55     struct stat mybuf;
56     if(stat(argv[i], &mybuf) == -1)
57     {
58       cerr << "Error: can't stat file '";
59       cerr << argv[i] << "'." << endl;
60       exit(EXIT_FAILURE);
61     }
62   }
63 
64   FILE *input = stdin, *output = stdout;
65   if(argc >= 4)
66   {
67     input = fopen(argv[3], "r");
68     if(!input)
69     {
70       cerr << "Error: can't open input file '" << argv[3] << "'." << endl;
71       exit(EXIT_FAILURE);
72     }
73     if(argc == 5)
74     {
75       output = fopen(argv[4], "w");
76       if(!output)
77       {
78 	cerr << "Error: can't open output file '";
79 	cerr << argv[4] << "'." << endl;
80 	exit(EXIT_FAILURE);
81       }
82     }
83   }
84 #ifdef _MSC_VER
85   _setmode(_fileno(input), _O_U8TEXT);
86   _setmode(_fileno(output), _O_U8TEXT);
87 #endif
88 
89   TransferMult t;
90   t.read(argv[1], argv[2]);
91 
92   t.transfer(input, output);
93   return EXIT_SUCCESS;
94 }
95