write.c
author viric@llimona
Sun, 02 Sep 2007 15:59:31 +0200
changeset 26 700a68421116
parent 8 09ec33061ff3
permissions -rw-r--r--
Adding .hgignore and the ia5test.
viric@0
     1
#include <stdio.h>
viric@0
     2
#include "dictre.h"
viric@0
     3
viric@0
     4
extern struct Word words[];
viric@0
     5
extern int nwords;
viric@0
     6
extern struct Def defs[];
viric@0
     7
extern int ndefs;
viric@0
     8
viric@0
     9
static int write_dictionary_data(FILE *fdefs)
viric@0
    10
{
viric@0
    11
    int i;
viric@0
    12
viric@0
    13
    int offset = 0;
viric@0
    14
viric@0
    15
    for (i=0; i < ndefs; ++i)
viric@0
    16
    {
viric@0
    17
        fwrite(defs[i].d, defs[i].length, 1, fdefs);
viric@0
    18
        defs[i].offset = offset;
viric@0
    19
        offset += defs[i].length;
viric@0
    20
    }
viric@0
    21
}
viric@0
    22
viric@0
    23
static void write_dictionary_index(FILE *index)
viric@0
    24
{
viric@0
    25
    int i;
viric@0
    26
viric@0
    27
    for (i=0; i < nwords; ++i)
viric@0
    28
    {
viric@0
    29
        char offset_str[50];
viric@0
    30
        char length_str[50];
viric@0
    31
viric@0
    32
        num_to_ia5(offset_str, defs[words[i].def].offset);
viric@0
    33
        num_to_ia5(length_str, defs[words[i].def].length);
viric@0
    34
        fprintf(index, "%s\t%s\t%s\n",
viric@0
    35
                words[i].w, offset_str, length_str);
viric@0
    36
    }
viric@0
    37
}
viric@0
    38
viric@0
    39
void write_dictionary(const char *name)
viric@0
    40
{
viric@0
    41
    FILE *i, *d;
viric@0
    42
    char tmpname[500];
viric@0
    43
viric@0
    44
    strcpy(tmpname, name);
viric@0
    45
    strcat(tmpname, ".dict");
viric@0
    46
    d = fopen(tmpname, "wb");
viric@0
    47
    write_dictionary_data(d);
viric@0
    48
    fclose(d);
viric@0
    49
viric@0
    50
    strcpy(tmpname, name);
viric@0
    51
    strcat(tmpname, ".index");
viric@0
    52
    i = fopen(tmpname, "wb");
viric@0
    53
    write_dictionary_index(i);
viric@0
    54
    fclose(i);
viric@0
    55
}