find.c
author viric@llimona
Sat, 01 Sep 2007 23:52:38 +0200
changeset 22 0b923f95df16
parent 21 01fe372188ac
child 24 026a2ba0ce16
permissions -rw-r--r--
Added 'signifoj' kreadon
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
     1
#include <stdio.h>
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
     2
#include <sys/stat.h>
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
     3
#include <sys/types.h>
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
     4
#include <sys/mman.h>
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
     5
#include <fcntl.h>
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
     6
#include "dictre.h"
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
     7
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
     8
const static char indexext[] = ".index";
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
     9
const static char dictext[] = ".dict";
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    10
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    11
int get_filesize(const char *fname)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    12
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    13
    struct stat st;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    14
    int res;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    15
    res = stat(fname, &st);
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    16
    if (res == -1)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    17
    {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    18
        fprintf(stderr, "Problem stating the file %s\n", fname);
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    19
        perror("Error:");
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    20
        exit(-1);
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    21
    }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    22
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    23
    return st.st_size;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    24
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    25
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    26
void init_dictionary(struct Dict *d, const char *base)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    27
{
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    28
    char *filename;
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    29
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    30
    filename = (char *) malloc(strlen(base) + 10);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    31
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    32
    /* Prepare .index filename and open it*/
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    33
    strcpy(filename, base);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    34
    strcat(filename, indexext);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    35
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    36
    d->indexsize = get_filesize(filename);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    37
    d->indexfd = open(filename, O_RDONLY);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    38
    if (d->indexfd == -1)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    39
    {
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    40
        fprintf(stderr, "Problem opening the file %s\n", filename);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    41
        perror("Error:");
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    42
        exit(-1);
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    43
    }
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    44
    d->index = (unsigned char *) mmap(0, d->indexsize, PROT_READ, MAP_SHARED,
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    45
            d->indexfd, 0);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    46
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    47
    /* Prepare .dict filename and open it*/
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    48
    strcpy(filename, base);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    49
    strcat(filename, dictext);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    50
    d->defs = fopen(filename, "r");
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    51
    if (d->defs == 0)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    52
    {
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    53
        fprintf(stderr, "Problem opening the file %s\n", filename);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    54
        perror("Error:");
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    55
        exit(-1);
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    56
    }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    57
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    58
    d->trim_first_line = 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    59
    d->trim_last_newlines = 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    60
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    61
    free(filename);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    62
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    63
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    64
void end_dictionary(struct Dict *d)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    65
{
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    66
    munmap(d->index, d->indexsize);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    67
    close(d->indexfd);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
    68
    fclose(d->defs);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    69
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
    70
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    71
static int trim_first_line(char *def, int len)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    72
{
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    73
    int new_line_pos;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    74
    int i,j;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    75
    for(i=0; i < len; ++i)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    76
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    77
        if (def[i] == '\n')
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    78
        {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    79
            /* Break */
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    80
            memmove(def, def + i + 1 /* \n */,
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    81
                    len - i - 1);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    82
            def[len-i-1] = 0;
22
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    83
            return len-i-2/*\n*/+1/*\0*/;
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    84
        }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    85
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    86
    return len;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    87
}
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    88
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    89
static int trim_last_newlines(char *def, int len)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    90
{
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    91
    int new_line_pos;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    92
    int i,j;
22
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    93
    if (len < 2)
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    94
        return len;
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    95
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    96
    for(i=len-2; i >= 0; --i)
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    97
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    98
        if (def[i] != '\n' && def[i] != '\r')
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    99
        {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   100
            def[i+1] = '\0';
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   101
            return i + 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   102
        }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   103
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   104
    return len;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   105
}
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   106
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   107
static void fill_def(struct Dict *d, int offset, int length, char * def)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   108
{
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   109
    fseek(d->defs, offset, SEEK_SET);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   110
    fread(def, 1, length, d->defs);
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   111
    def[length] = 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   112
    if (d->trim_first_line)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   113
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   114
        length = trim_first_line(def, length + 1/*\0*/) - 1 /*\0*/;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   115
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   116
    if (d->trim_last_newlines)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   117
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   118
        length = trim_last_newlines(def, length+1) - 1; /* math as above*/
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   119
    }
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   120
}
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   121
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   122
static int pointer_at_end(struct Dict *d, const unsigned char *ptr)
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   123
{
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   124
    if (ptr >= (d->index + d->indexsize))
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   125
        return 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   126
    return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   127
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   128
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   129
static const char * skip_until_newline(struct Dict *d, const char *from)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   130
{
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   131
    if (pointer_at_end(d, from))
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   132
        return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   133
    while(*from != '\n' && *from != 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   134
    {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   135
        ++from;
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   136
        if(pointer_at_end(d, from))
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   137
            return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   138
    }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   139
    return from;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   140
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   141
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   142
static int compare(const unsigned char *word, const unsigned char *test)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   143
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   144
    int i;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   145
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   146
    /*printf("Comparing %s to %.20s\n", word, test);*/
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   147
    for(i=0; word[i] != 0 && test[i] != 0; ++i)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   148
    {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   149
        if (word[i] != test[i])
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   150
        {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   151
            break;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   152
        }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   153
    }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   154
    if (word[i] == 0 && test[i] == '\t')
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   155
        return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   156
    else if (word[i] == 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   157
        return -1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   158
    else if (test[i] == '\t')
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   159
        return 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   160
    else if (word[i] > test[i])
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   161
        return 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   162
    else if (word[i] < test[i])
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   163
        return -1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   164
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   165
    /* It should never reach this. */
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   166
    return -1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   167
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   168
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   169
static const char * search_next(struct Dict *d, const char *word, const char *from)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   170
{
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   171
    const char *ret;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   172
    ret = skip_until_newline(d, from);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   173
    if (compare(from, word) == 0)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   174
        return ret;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   175
    return 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   176
}
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   177
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   178
static const char * bin_search(struct Dict *d, const char *word)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   179
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   180
    int step, pivot;
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   181
    const char *ret;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   182
    const char *test;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   183
    int comparision;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   184
    int found_once = 0;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   185
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   186
    pivot = d->indexsize / 2;
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   187
    step = d->indexsize / 2;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   188
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   189
    do
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   190
    {
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   191
        test = d->index + pivot;
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   192
        test = skip_until_newline(d, test);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   193
        if (test == 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   194
            return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   195
        test += 1; /* skip exactly the new line */
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   196
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   197
        comparision = compare(word, test);
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   198
        if (comparision <= 0)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   199
        {
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   200
            if (comparision == 0)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   201
                found_once = 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   202
            /* If == 0, we don't know that it's the FIRST
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   203
             * match possible in the dictionary.
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   204
             * We want all possible matches. */
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   205
            step = step / 2;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   206
            pivot = pivot - step;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   207
        } else if (comparision > 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   208
        {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   209
            step = step / 2;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   210
            pivot = pivot + step;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   211
        }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   212
    } while(step > 0);
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   213
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   214
    if (!found_once)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   215
        return 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   216
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   217
    if (comparision == 0) /* last comparision */
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   218
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   219
        ret = skip_until_newline(d, d->index + pivot) + 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   220
    } else
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   221
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   222
        ret = skip_until_newline(d, test) + 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   223
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   224
    return ret;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   225
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   226
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   227
static int my_get_int(const char **pos)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   228
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   229
    int i;
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   230
    const char *start;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   231
    int val;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   232
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   233
    start = *pos;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   234
    for(i=0; start[i] != '\t' && start[i] != '\n'; ++i)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   235
        ;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   236
    val = str2int_len(start, i);
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   237
    *pos += i + 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   238
    return val;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   239
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   240
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   241
void find_def(struct Dict *d, const char *word, char * def)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   242
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   243
    int offset, len;
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   244
    const char *found, *pos;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   245
    int wordlen;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   246
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   247
    def[0] = 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   248
    /* we will get a pointer to the offset for the ints*/
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   249
    found = bin_search(d, word);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   250
    if (found == 0)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   251
        return;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   252
    wordlen = strlen(word);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   253
    do
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   254
    {
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   255
        found += wordlen+1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   256
        pos = found;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   257
        offset = my_get_int(&pos); /* increments pos */
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   258
        len = my_get_int(&pos); /* increments pos */
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   259
        fill_def(d, offset, len, def);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   260
        found = search_next(d, word, found);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   261
        if (!found)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   262
            break;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   263
        strcat(def, ", ");
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   264
        def += strlen(def);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   265
    } while(1);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   266
}