aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/apps/makefsa/makefsa.cpp
blob: 08daab970c1734f0a744fd0ef76139981919fd48 (plain) (blame)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>

#include <iostream>
#include <fstream>
#include <memory>

#include <vespa/fsa/base64.h>
#include <vespa/fsa/fsa.h>
#include <vespa/fsa/automaton.h>

using namespace fsa;

enum FSA_Input_Format {
  INPUT_UNDEF,
  INPUT_TEXT,
  INPUT_TEXT_EMPTY,
  INPUT_TEXT_NUM,
  INPUT_BINARY,
  INPUT_BINARY_RAW };

void usage(const char *name, const char *errormsg = NULL)
{
  if(errormsg!=NULL){
    fprintf(stderr,"%s: %s\n",name,errormsg);
  }
  fprintf(stderr,"usage:\n");
  fprintf(stderr,"    %s [OPTIONS] [input_file] output_file\n",name);
  fprintf(stderr,"\n");
  fprintf(stderr,"      Valid options are:\n");
  fprintf(stderr,"      -h         display this help\n");
  fprintf(stderr,"      -b         use binary input format with Base64 encoded info\n");
  fprintf(stderr,"      -B         use binary input format with raw\n");
  fprintf(stderr,"      -e         use text input format with no info (default)\n");
  fprintf(stderr,"      -n         use text input format with (unsigned) numerical info\n");
  fprintf(stderr,"      -s bytes   data size for numerical info: 1,2 or 4(default)\n");
  fprintf(stderr,"      -z bytes   data size for binary info (-B) (0 means NUL terminated)\n");
  fprintf(stderr,"      -t         use text input format\n");
  fprintf(stderr,"      -p         build automaton with perfect hash\n");
  fprintf(stderr,"      -i         ignore info string, regardless of input format\n");
  fprintf(stderr,"      -S serial  serial number\n");
  fprintf(stderr,"      -v         be verbose\n");
  fprintf(stderr,"      -V         display version number\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"      If input_file is not specified, standard input is used.\n");
}

void version()
{
  std::cout << "makefsa "
            << FSA::VER/1000000 << "." << (FSA::VER/1000)%1000 << "." << FSA::VER%1000;
  if(FSA::VER != FSA::libVER()){
    std::cout << " (library "
              << FSA::libVER()/1000000 << "." << (FSA::libVER()/1000)%1000 << "." << FSA::libVER()%1000
              << ")";
  }
  std::cout << std::endl;
}


int main(int argc, char** argv)
{
  FSA_Input_Format  format = INPUT_UNDEF;
  unsigned int      num_size = 4;
  unsigned int      info_size_binary = 0;
  bool build_phash = false;
  const char *input_file;
  const char *output_file;
  uint32_t serial = 0;
  bool ignore_info = false;
  bool verbose = false;
  unsigned int lines=0,count = 0;

  int          opt;
  extern char *optarg;
  extern int   optind;

  while((opt=getopt(argc,argv,"ebBhns:z:tpS:ivV")) != -1){
    switch(opt){
    case 'b':
      format = INPUT_BINARY;
      break;
    case 'B':
      format = INPUT_BINARY_RAW;
      break;
    case 'h':
      usage(argv[0]);
      return 0;
    case 'V':
      version();
      return 0;
    case 't':
      format = INPUT_TEXT;
      break;
    case 'n':
      format = INPUT_TEXT_NUM;
      break;
    case 's':
      num_size = strtoul(optarg,NULL,0);
      if(num_size!=1 && num_size!=2 && num_size!=4){
        usage(argv[0],"invalid numerical info size (-s)");
        return 1;
      }
      break;
    case 'z':
      info_size_binary = strtoul(optarg,NULL,0);
      break;
    case 'S':
      serial = strtoul(optarg,NULL,0);
      break;
    case 'e':
      format = INPUT_TEXT_EMPTY;
      break;
    case 'p':
      build_phash = true;
      break;
    case 'i':
      ignore_info = true;
      break;
    case 'v':
      verbose = true;
      break;
    case '?':
      usage(argv[0],"unrecognized option");
      return 1;
    }
  }

  if(format==INPUT_UNDEF) // use default format (warning?)
    format=INPUT_TEXT_EMPTY;

  if(optind+2==argc){
    input_file = argv[optind];
    output_file = argv[optind+1];
  }
  else if(optind+1==argc){
    input_file = NULL;
    output_file = argv[optind];
  }
  else{
    usage(argv[0],"required parameter(s) missing");
    return 1;
  }

  Automaton automaton;

  std::string input,last_input,meta,temp;
  union{
    uint8_t  u1;
    uint16_t u2;
    uint32_t u4;
  } num_meta;
  std::ifstream infile;
  std::istream *in;
  auto binary_info = std::make_unique<char[]>(info_size_binary);
  size_t split;
  bool empty_meta_str = false;

  if(verbose) version();

  if(verbose) std::cerr << "Initializing automaton ...";
  automaton.init();
  if(verbose) std::cerr << " done." << std::endl;

  if(input_file!=NULL){
    infile.open(input_file);
    if (infile.fail()) {
      std::cerr << "Error: Could not open file \"" << input_file << "\"\n";
      return(1);
    }
    in=&infile;
  }
  else{
    in=&std::cin;
  }
  if(verbose) std::cerr << "Inserting lines ...";
  while(!in->eof()){
    switch(format){
    case INPUT_BINARY:
      getline(*in,input,'\0');
      getline(*in,temp,'\0');
      Base64::decode(temp,meta);
      break;
    case INPUT_BINARY_RAW:
      getline(*in,input,'\0');
      if (info_size_binary) {
        in->read(binary_info.get(), info_size_binary);
        meta.assign(binary_info.get(), info_size_binary);
      }
      else
        getline(*in,meta,'\0');
      break;
    case INPUT_TEXT:
      getline(*in,temp,'\n');
      split = temp.find_first_of('\t');
      input = temp.substr(0, split);
      if (split == std::string::npos) {
        empty_meta_str = true;
        break;
      }
      meta = temp.substr(split + 1);
      meta+='\0';
      break;
    case INPUT_TEXT_NUM:
      getline(*in,temp,'\n');
      split = temp.find_first_of('\t');
      input = temp.substr(0, split);
      if (split == std::string::npos) {
        empty_meta_str = true;
        break;
      }
      temp = temp.substr(split + 1);
      switch(num_size){
      case 1:
        num_meta.u1=strtoul(temp.c_str(),NULL,0);
        meta.assign((const char*)&num_meta,1);
        break;
      case 2:
        num_meta.u2=strtoul(temp.c_str(),NULL,0);
        meta.assign((const char*)&num_meta,2);
        break;
      case 4:
      default:
        num_meta.u4=strtoul(temp.c_str(),NULL,0);
        meta.assign((const char*)&num_meta,4);
        break;
      }
      break;
    case INPUT_TEXT_EMPTY:
      getline(*in,input,'\n');
      break;
    case INPUT_UNDEF:
      assert(0);
      break;
    }

    ++lines;

    if(input.length()>0){
      if(last_input>input){
        std::cerr << "warning: ignoring unsorted line " << lines << ", \"" << input << "\"\n";
      }
      else if(last_input==input){
        std::cerr << "warning: ignoring duplicate line " << lines << ", \"" << input << "\"\n";
      }
      else if(empty_meta_str) {
        std::cerr << "warning: ignoring line " << lines << ", \"" << input << "\" with missing meta info\n";
      }
      else{
        if(format==INPUT_TEXT_EMPTY || ignore_info){
          automaton.insertSortedString(input);
        }
        else{
          automaton.insertSortedString(input,meta);
        }
        if(verbose){
          ++count;
          if(count%1000==0)
            std::cerr << "\rInserting lines ... (inserted " << count << " lines)";
        }
      }
      last_input=input;
    }
    empty_meta_str = false;
  }
  if(verbose) std::cerr << "\rInserting lines ... (inserted " << count << "/" <<  (lines-1) << " lines) ... done.\n";
  if(input_file!=NULL){
    infile.close();
  }


  if(verbose) std::cerr << "Finalizing ...";
  automaton.finalize();
  if(verbose) std::cerr << " done." << std::endl;


  if(build_phash){
    if(verbose) std::cerr << "Adding perfect hash ...";
    automaton.addPerfectHash();
    if(verbose) std::cerr << " done." << std::endl;
  }


  if(verbose) std::cerr << "Writing fsa file ...";
  if (!automaton.write(output_file,serial)) {
    std::cerr << "Failed to write fsa file '" << std::string(output_file) << "'. Please check write permissions" << std::endl;
    return 1;
  }
  if(verbose) std::cerr << " done." << std::endl;


  return 0;
}