main.c
author viric <viriketo@gmail.com>
Fri, 30 Mar 2012 18:54:55 +0200
changeset 31 2bcc08d0f1d8
parent 5 c87681fff7d3
permissions -rw-r--r--
Fixing an error message
viric@0
     1
#include <stdio.h>
viric@5
     2
#include <sys/stat.h>
viric@0
     3
viric@0
     4
#include "dictre.h"
viric@0
     5
viric@0
     6
extern int nwords;
viric@0
     7
extern int ndefs;
viric@0
     8
viric@0
     9
int main(int argn, char **argv)
viric@0
    10
{
viric@0
    11
    char tmpname[500];
viric@0
    12
    FILE *i, *d;
viric@5
    13
    int remove_tmp_data = 0;
viric@0
    14
viric@5
    15
    if (argn < 3)
viric@0
    16
    {
viric@0
    17
        fprintf(stderr, "usage: %s <dict_basename> "
viric@5
    18
                "<dict_basename_out> [filter]\n",
viric@0
    19
                argv[0]);
viric@0
    20
        return 1;
viric@0
    21
    }
viric@0
    22
    strcpy(tmpname, argv[1]);
viric@0
    23
    strcat(tmpname, ".index");
viric@0
    24
    i = fopen(tmpname, "r");
viric@0
    25
    if(i == NULL)
viric@0
    26
    {
viric@0
    27
        fprintf(stderr, "File: %s ", tmpname);
viric@0
    28
        perror("- cannot open file.");
viric@0
    29
        exit(-1);
viric@0
    30
    }
viric@0
    31
viric@0
    32
    strcpy(tmpname, argv[1]);
viric@0
    33
    strcat(tmpname, ".dict");
viric@0
    34
    d = fopen(tmpname, "r");
viric@0
    35
    if(d == NULL)
viric@0
    36
    {
viric@5
    37
        struct stat st;
viric@5
    38
        int res;
viric@5
    39
        char tmp[500];
viric@5
    40
        strcat(tmpname, ".dz");
viric@5
    41
        res = stat(tmpname, &st);
viric@5
    42
        if (res == -1)
viric@5
    43
        {
viric@5
    44
            fprintf(stderr, "File: %s ", tmpname);
viric@5
    45
            perror("- cannot open file.");
viric@5
    46
            exit(-1);
viric@5
    47
        }
viric@5
    48
        sprintf(tmp, "gzip -cd %s > /tmp/tmp.dict",
viric@5
    49
                tmpname);
viric@5
    50
        printf("Gunzipping...\n");
viric@5
    51
        res = system(tmp);
viric@5
    52
        d = fopen("/tmp/tmp.dict", "r");
viric@5
    53
        if(d == NULL || res != 0)
viric@5
    54
        {
viric@5
    55
            fprintf(stderr, "Error gunzipping file: %s ", tmpname);
viric@5
    56
            perror("- something happened to /tmp/tmp.dict.");
viric@5
    57
            exit(-1);
viric@5
    58
        }
viric@5
    59
        remove_tmp_data = 1;
viric@0
    60
    }
viric@0
    61
viric@5
    62
    init_load();
viric@5
    63
    init_repeated();
viric@5
    64
    /* Always line buffered on stdout, for 'ts' */
viric@5
    65
    setlinebuf(stdout);
viric@0
    66
viric@0
    67
    load_dictionary(i, d);
viric@0
    68
viric@0
    69
    fclose(i);
viric@0
    70
    fclose(d);
viric@0
    71
viric@5
    72
    if (remove_tmp_data)
viric@5
    73
        unlink("/tmp/tmp.dict");
viric@5
    74
viric@0
    75
    sort_words();
viric@0
    76
viric@0
    77
    if (0)
viric@0
    78
        print_words();
viric@0
    79
viric@5
    80
    if (argn >= 4)
viric@5
    81
        filter_all(argv[3]);
viric@0
    82
viric@0
    83
    write_dictionary(argv[2]);
viric@0
    84
viric@0
    85
    return 0;
viric@0
    86
}