zload.c
author viric@llimona
Sat, 01 Sep 2007 21:49:41 +0200
changeset 21 01fe372188ac
parent 11 68ea18fe402c
permissions -rw-r--r--
Added capabilities to the dictionary finder.

#include <stdio.h>
#include <sys/stat.h>
#include "dictre.h"

static FILE *index, *dict;
static remove_tmp_file = 0;

static new_word(const char *w, const char *defstr)
{
    printf("'%s': '%s'\n", w, defstr);
}

void zload_words(FILE *index, FILE *fdefs)
{
    int last_offset = 0;
    int def_avoided = 0;
    int numword = 0;;
    static int dispnwords = 0;
    static int nwords = 0;

    do {
        int offset, length;
        char *defstr;
        char *word;
        word = get_word(index);
        /*numword++;
        printf("words: %i\n", numword);*/
        if (word == 0)
            break;
        /*printf("Word: %s\n", w.w);*/
        offset = get_int(index);
        length = get_int(index);
        defstr = get_def(fdefs, offset, length);

        /* sizeof -1  instead of strlen() */
        /* If the word is not 00database* ... */
        if (strncmp(word, "00database", sizeof("00database") - 1) != 0)
            zprocess_def(word, defstr);

        /* stdout Display */
        dispnwords++;
        nwords++;
        if (dispnwords >= 1000)
        {
            dispnwords = 0;
            fprintf(stderr,
                    "Loaded: %i Repeated definitions avoided: %i\n", nwords,
                    def_avoided);
        }

    } while(1);
}

static void close_files()
{
    fclose(index);
    fclose(dict);

    if (remove_tmp_file)
        unlink("/tmp/tmp.dict");
}

static void open_files(int argn, char **argv)
{
    char tmpname[500];
    if (argn < 2)
    {
        fprintf(stderr, "usage: %s <dict_basename>\n", argv[0]);
        exit(1);
    }
    strcpy(tmpname, argv[1]);
    strcat(tmpname, ".index");
    index = fopen(tmpname, "r");
    if(index == NULL)
    {
        fprintf(stderr, "File: %s ", tmpname);
        perror("- cannot open file.");
        exit(-1);
    }

    strcpy(tmpname, argv[1]);
    strcat(tmpname, ".dict");
    dict = fopen(tmpname, "r");
    if(dict == 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);
        dict = fopen("/tmp/tmp.dict", "r");
        if(dict == NULL || res != 0)
        {
            fprintf(stderr, "Error gunzipping file: %s ", tmpname);
            perror("- something happened to /tmp/tmp.dict.");
            exit(-1);
        }
        remove_tmp_file = 1;
    }
}

int main(int argn, char **argv)
{
    open_files(argn, argv);
    init_wordlist();
    zload_words(index, dict);
    dump_wordlist();
    close_files();
}