zload.c
author viric <viriketo@gmail.com>
Fri, 30 Mar 2012 18:55:30 +0200
branchsql
changeset 32 6a1a709330bf
parent 11 68ea18fe402c
permissions -rw-r--r--
Adding code to dump the words as sql.
viric@11
     1
#include <stdio.h>
viric@11
     2
#include <sys/stat.h>
viric@11
     3
#include "dictre.h"
viric@11
     4
viric@11
     5
static FILE *index, *dict;
viric@11
     6
static remove_tmp_file = 0;
viric@11
     7
viric@11
     8
static new_word(const char *w, const char *defstr)
viric@11
     9
{
viric@11
    10
    printf("'%s': '%s'\n", w, defstr);
viric@11
    11
}
viric@11
    12
viric@11
    13
void zload_words(FILE *index, FILE *fdefs)
viric@11
    14
{
viric@11
    15
    int last_offset = 0;
viric@11
    16
    int def_avoided = 0;
viric@11
    17
    int numword = 0;;
viric@11
    18
    static int dispnwords = 0;
viric@11
    19
    static int nwords = 0;
viric@11
    20
viric@11
    21
    do {
viric@11
    22
        int offset, length;
viric@11
    23
        char *defstr;
viric@11
    24
        char *word;
viric@11
    25
        word = get_word(index);
viric@11
    26
        /*numword++;
viric@11
    27
        printf("words: %i\n", numword);*/
viric@11
    28
        if (word == 0)
viric@11
    29
            break;
viric@11
    30
        /*printf("Word: %s\n", w.w);*/
viric@11
    31
        offset = get_int(index);
viric@11
    32
        length = get_int(index);
viric@11
    33
        defstr = get_def(fdefs, offset, length);
viric@11
    34
viric@11
    35
        /* sizeof -1  instead of strlen() */
viric@11
    36
        /* If the word is not 00database* ... */
viric@11
    37
        if (strncmp(word, "00database", sizeof("00database") - 1) != 0)
viric@11
    38
            zprocess_def(word, defstr);
viric@11
    39
viric@11
    40
        /* stdout Display */
viric@11
    41
        dispnwords++;
viric@11
    42
        nwords++;
viric@11
    43
        if (dispnwords >= 1000)
viric@11
    44
        {
viric@11
    45
            dispnwords = 0;
viric@11
    46
            fprintf(stderr,
viric@11
    47
                    "Loaded: %i Repeated definitions avoided: %i\n", nwords,
viric@11
    48
                    def_avoided);
viric@11
    49
        }
viric@11
    50
viric@11
    51
    } while(1);
viric@11
    52
}
viric@11
    53
viric@11
    54
static void close_files()
viric@11
    55
{
viric@11
    56
    fclose(index);
viric@11
    57
    fclose(dict);
viric@11
    58
viric@11
    59
    if (remove_tmp_file)
viric@11
    60
        unlink("/tmp/tmp.dict");
viric@11
    61
}
viric@11
    62
viric@11
    63
static void open_files(int argn, char **argv)
viric@11
    64
{
viric@11
    65
    char tmpname[500];
viric@11
    66
    if (argn < 2)
viric@11
    67
    {
viric@11
    68
        fprintf(stderr, "usage: %s <dict_basename>\n", argv[0]);
viric@11
    69
        exit(1);
viric@11
    70
    }
viric@11
    71
    strcpy(tmpname, argv[1]);
viric@11
    72
    strcat(tmpname, ".index");
viric@11
    73
    index = fopen(tmpname, "r");
viric@11
    74
    if(index == NULL)
viric@11
    75
    {
viric@11
    76
        fprintf(stderr, "File: %s ", tmpname);
viric@11
    77
        perror("- cannot open file.");
viric@11
    78
        exit(-1);
viric@11
    79
    }
viric@11
    80
viric@11
    81
    strcpy(tmpname, argv[1]);
viric@11
    82
    strcat(tmpname, ".dict");
viric@11
    83
    dict = fopen(tmpname, "r");
viric@11
    84
    if(dict == NULL)
viric@11
    85
    {
viric@11
    86
        struct stat st;
viric@11
    87
        int res;
viric@11
    88
        char tmp[500];
viric@11
    89
        strcat(tmpname, ".dz");
viric@11
    90
        res = stat(tmpname, &st);
viric@11
    91
        if (res == -1)
viric@11
    92
        {
viric@11
    93
            fprintf(stderr, "File: %s ", tmpname);
viric@11
    94
            perror("- cannot open file.");
viric@11
    95
            exit(-1);
viric@11
    96
        }
viric@11
    97
        sprintf(tmp, "gzip -cd %s > /tmp/tmp.dict",
viric@11
    98
                tmpname);
viric@11
    99
        printf("Gunzipping...\n");
viric@11
   100
        res = system(tmp);
viric@11
   101
        dict = fopen("/tmp/tmp.dict", "r");
viric@11
   102
        if(dict == NULL || res != 0)
viric@11
   103
        {
viric@11
   104
            fprintf(stderr, "Error gunzipping file: %s ", tmpname);
viric@11
   105
            perror("- something happened to /tmp/tmp.dict.");
viric@11
   106
            exit(-1);
viric@11
   107
        }
viric@11
   108
        remove_tmp_file = 1;
viric@11
   109
    }
viric@11
   110
}
viric@11
   111
viric@11
   112
int main(int argn, char **argv)
viric@11
   113
{
viric@11
   114
    open_files(argn, argv);
viric@11
   115
    init_wordlist();
viric@11
   116
    zload_words(index, dict);
viric@11
   117
    dump_wordlist();
viric@11
   118
    close_files();
viric@11
   119
}