load.c
author viric@mandarina
Sat, 11 Aug 2007 16:01:25 +0200
changeset 3 ba1b3c2fcff2
parent 2 57a1fcb0c75c
child 5 c87681fff7d3
permissions -rw-r--r--
Less memory use, less often output.

#include <stdio.h>

#include "dictre.h"

enum
{
    MAX=500000
};

struct Word words[MAX];
int nwords;
struct Def defs[MAX];
int ndefs;
int dont_touch[20];
int ndont_touch;

void load_init()
{
    ndefs = 0;
    nwords = 0;
    ndont_touch = 0;
}

static void new_word(struct Word *from)
{
    static int dispnwords = 0;
    memcpy(&words[nwords], from, sizeof(*from));
    nwords++;
    dispnwords++;
    if (dispnwords >= 1000)
    {
        dispnwords = 0;
        printf("Loaded: %i\n", nwords);
    }
}

static void new_dont_touch(int n)
{
    dont_touch[ndont_touch++] = n;
}

static int new_def(char *def, int offset, int length)
{
    defs[ndefs].d = def;
    defs[ndefs].offset = offset;
    defs[ndefs].length = length;
    return ndefs++;
}

static int search_def(int offset, int length)
{
    int i;

    for(i=0; i < ndefs; ++i)
    {
        if (defs[i].offset == offset &&
                defs[i].length == length)
            return i;
    }
    return -1;
}

static void print_word(struct Word *w)
{
    printf("%s\t%i\n", w->w, w->def);
}

void load_dictionary(FILE *index, FILE *fdefs)
{
    struct Word w;
    int last_offset = 0;

    do {
        int offset, length;
        char *defstr;
        w.w = get_word(index);
        if (w.w == 0)
            break;
        offset = get_int(index);
        length = get_int(index);
        if (offset > last_offset)
        {
            w.def = -1;
            last_offset = offset;
        }
        else
            w.def = search_def(offset, length);
        if (w.def == -1)
        {
            defstr = get_def(fdefs, offset, length);
            w.def = new_def(defstr, offset, length);
        }
        /* sizeof -1  instead of strlen() */
        if (strncmp(w.w, "00database", sizeof("00database") - 1) == 0)
                new_dont_touch(w.def);
        new_word(&w);
    } while(1);
}

void print_words()
{
    int i;

    for(i=0; i < nwords; ++i)
        print_word(&words[i]);
}