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