main.c
author viric <viriketo@gmail.com>
Fri, 30 Mar 2012 18:54:29 +0200
changeset 30 d30178b2a9e6
parent 5 c87681fff7d3
permissions -rw-r--r--
Making the makefile not depend on /usr.
#include <stdio.h>
#include <sys/stat.h>

#include "dictre.h"

extern int nwords;
extern int ndefs;

int main(int argn, char **argv)
{
    char tmpname[500];
    FILE *i, *d;
    int remove_tmp_data = 0;

    if (argn < 3)
    {
        fprintf(stderr, "usage: %s <dict_basename> "
                "<dict_basename_out> [filter]\n",
                argv[0]);
        return 1;
    }
    strcpy(tmpname, argv[1]);
    strcat(tmpname, ".index");
    i = fopen(tmpname, "r");
    if(i == NULL)
    {
        fprintf(stderr, "File: %s ", tmpname);
        perror("- cannot open file.");
        exit(-1);
    }

    strcpy(tmpname, argv[1]);
    strcat(tmpname, ".dict");
    d = fopen(tmpname, "r");
    if(d == NULL)
    {
        struct stat st;
        int res;
        char tmp[500];
        strcat(tmpname, ".dz");
        res = stat(tmpname, &st);
        if (res == -1)
        {
            fprintf(stderr, "File: %s ", tmpname);
            perror("- cannot open file.");
            exit(-1);
        }
        sprintf(tmp, "gzip -cd %s > /tmp/tmp.dict",
                tmpname);
        printf("Gunzipping...\n");
        res = system(tmp);
        d = fopen("/tmp/tmp.dict", "r");
        if(d == NULL || res != 0)
        {
            fprintf(stderr, "Error gunzipping file: %s ", tmpname);
            perror("- something happened to /tmp/tmp.dict.");
            exit(-1);
        }
        remove_tmp_data = 1;
    }

    init_load();
    init_repeated();
    /* Always line buffered on stdout, for 'ts' */
    setlinebuf(stdout);

    load_dictionary(i, d);

    fclose(i);
    fclose(d);

    if (remove_tmp_data)
        unlink("/tmp/tmp.dict");

    sort_words();

    if (0)
        print_words();

    if (argn >= 4)
        filter_all(argv[3]);

    write_dictionary(argv[2]);

    return 0;
}