[go: up one dir, main page]

File: lt_expand.cc

package info (click to toggle)
lttoolbox 3.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,328 kB
  • sloc: cpp: 12,761; ansic: 3,589; python: 391; makefile: 71; sh: 24
file content (146 lines) | stat: -rw-r--r-- 3,324 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
/*
 * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
 *
 * 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 2 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 <lttoolbox/expander.h>
#include <lttoolbox/lttoolbox_config.h>

#include <cstdlib>
#include <iostream>
#include <libgen.h>
#include <string>
#include <getopt.h>

#ifdef _MSC_VER
#include <io.h>
#include <fcntl.h>
#endif

using namespace std;

void endProgram(char *name)
{
  if(name != NULL)
  {
    cout << basename(name) << " v" << PACKAGE_VERSION <<": expand the contents of a dictionary file" << endl;
    cout << "USAGE: " << basename(name) << " [-avlrh] dictionary_file [output_file]" << endl;
  }
  exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
  FILE *input = NULL, *output = NULL;
  Expander e;

#if HAVE_GETOPT_LONG
  int option_index=0;
#endif

  while (true) {
#if HAVE_GETOPT_LONG
    static struct option long_options[] =
    {
      {"alt",       required_argument, 0, 'a'},
      {"var",       required_argument, 0, 'v'},
      {"var-left",  required_argument, 0, 'l'},
      {"var-right", required_argument, 0, 'r'},
      {"help",      no_argument,       0, 'h'},
      {0, 0, 0, 0}
    };

    int cnt=getopt_long(argc, argv, "a:v:l:r:h", long_options, &option_index);
#else
    int cnt=getopt(argc, argv, "a:v:l:r:h");
#endif
    if (cnt==-1)
      break;

    switch (cnt)
    {
      case 'a':
        e.setAltValue(optarg);
        break;

      case 'v':
        e.setVariantValue(optarg);
        break;

      case 'l':
        e.setVariantLeftValue(optarg);
        break;

      case 'r':
        e.setVariantRightValue(optarg);
        break;

      case 'h':
      default:
        endProgram(argv[0]);
        break;
    }
  }

  string infile;
  string outfile;

  switch(argc - optind + 1)
  {
    case 2:
      infile = argv[argc-1];
      input = fopen(infile.c_str(), "rb");
      if(input == NULL)
      {
        wcerr << "Error: Cannot open file '" << infile << "'." << endl;
        exit(EXIT_FAILURE);
      }
      fclose(input);
      output = stdout;
      break;

    case 3:
      infile = argv[argc-2];
      input = fopen(infile.c_str(), "rb");
      if(input == NULL)
      {
        wcerr << "Error: Cannot open file '" << infile << "'." << endl;
        exit(EXIT_FAILURE);
      }
      fclose(input);

      outfile = argv[argc-1];
      output = fopen(argv[argc-1], "wb");
      if(output == NULL)
      {
        wcerr << "Error: Cannot open file '" << outfile << "'." << endl;
        exit(EXIT_FAILURE);
      }
      break;

    default:
      endProgram(argv[0]);
      break;
  }

#ifdef _MSC_VER
  _setmode(_fileno(output), _O_U8TEXT);
#endif

  e.expand(infile, output);
  fclose(output);

  return EXIT_SUCCESS;
}