load.c
author viric@llimona
Sun, 05 Aug 2007 23:06:42 +0200
changeset 0 7f37716d4f1e
child 2 57a1fcb0c75c
permissions -rw-r--r--
Initial. Awful makefile.
viric@0
     1
#include <stdio.h>
viric@0
     2
viric@0
     3
#include "dictre.h"
viric@0
     4
viric@0
     5
enum
viric@0
     6
{
viric@0
     7
    MAX=500000
viric@0
     8
};
viric@0
     9
viric@0
    10
struct Word words[MAX];
viric@0
    11
int nwords;
viric@0
    12
struct Def defs[MAX];
viric@0
    13
int ndefs;
viric@0
    14
int dont_touch[20];
viric@0
    15
int ndont_touch;
viric@0
    16
viric@0
    17
void load_init()
viric@0
    18
{
viric@0
    19
    ndefs = 0;
viric@0
    20
    nwords = 0;
viric@0
    21
    ndont_touch = 0;
viric@0
    22
}
viric@0
    23
viric@0
    24
static void new_word(struct Word *from)
viric@0
    25
{
viric@0
    26
    memcpy(&words[nwords], from, sizeof(*from));
viric@0
    27
    nwords++;
viric@0
    28
}
viric@0
    29
viric@0
    30
static void new_dont_touch(int n)
viric@0
    31
{
viric@0
    32
    dont_touch[ndont_touch++] = n;
viric@0
    33
}
viric@0
    34
viric@0
    35
static int new_def(char *def, int offset, int length)
viric@0
    36
{
viric@0
    37
    defs[ndefs].d = def;
viric@0
    38
    defs[ndefs].offset = offset;
viric@0
    39
    defs[ndefs].length = length;
viric@0
    40
    return ndefs++;
viric@0
    41
}
viric@0
    42
viric@0
    43
static int search_def(int offset, int length)
viric@0
    44
{
viric@0
    45
    int i;
viric@0
    46
viric@0
    47
    for(i=0; i < ndefs; ++i)
viric@0
    48
    {
viric@0
    49
        if (defs[i].offset == offset &&
viric@0
    50
                defs[i].length == length)
viric@0
    51
            return i;
viric@0
    52
    }
viric@0
    53
    return -1;
viric@0
    54
}
viric@0
    55
viric@0
    56
static void print_word(struct Word *w)
viric@0
    57
{
viric@0
    58
    printf("%s\t%i\n", w->w, w->def);
viric@0
    59
}
viric@0
    60
viric@0
    61
void load_dictionary(FILE *index, FILE *fdefs)
viric@0
    62
{
viric@0
    63
    struct Word w;
viric@0
    64
    int last_offset = 0;
viric@0
    65
viric@0
    66
    do {
viric@0
    67
        int offset, length;
viric@0
    68
        char *defstr;
viric@0
    69
        w.w = get_word(index);
viric@0
    70
        if (w.w == 0)
viric@0
    71
            break;
viric@0
    72
        offset = get_int(index);
viric@0
    73
        length = get_int(index);
viric@0
    74
        if (offset > last_offset)
viric@0
    75
        {
viric@0
    76
            w.def = -1;
viric@0
    77
            last_offset = offset;
viric@0
    78
        }
viric@0
    79
        else
viric@0
    80
            w.def = search_def(offset, length);
viric@0
    81
        if (w.def == -1)
viric@0
    82
        {
viric@0
    83
            defstr = get_def(fdefs, offset, length);
viric@0
    84
            w.def = new_def(defstr, offset, length);
viric@0
    85
        }
viric@0
    86
        /* sizeof -1  instead of strlen() */
viric@0
    87
        if (strncmp(w.w, "00database", sizeof("00database") - 1) == 0)
viric@0
    88
                new_dont_touch(w.def);
viric@0
    89
        new_word(&w);
viric@0
    90
    } while(1);
viric@0
    91
}
viric@0
    92
viric@0
    93
void print_words()
viric@0
    94
{
viric@0
    95
    int i;
viric@0
    96
viric@0
    97
    for(i=0; i < nwords; ++i)
viric@0
    98
        print_word(&words[i]);
viric@0
    99
}