find.c
author viric <viriketo@gmail.com>
Fri, 30 Mar 2012 18:56:20 +0200
changeset 33 ebbedaa090be
parent 24 026a2ba0ce16
permissions -rw-r--r--
Adding what I had in the web for zparsetext (akcentiga)
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
    {
24
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
    77
        /* prepare ltrimming when finding the first newline character */
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    78
        if (def[i] == '\n')
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    79
        {
24
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
    80
            /* Remove spaces after the first newline */
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
    81
            for(i+=1; def[i] == ' '; ++i);
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
    82
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    83
            /* Break */
24
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
    84
            memmove(def, def + i,
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    85
                    len - i - 1);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    86
            def[len-i-1] = 0;
24
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
    87
            return len-i-1+1/*\0*/;
21
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
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    90
    return len;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    91
}
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    92
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    93
static int trim_last_newlines(char *def, int len)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    94
{
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    95
    int new_line_pos;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
    96
    int i,j;
22
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    97
    if (len < 2)
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    98
        return len;
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
    99
0b923f95df16 Added 'signifoj' kreadon
viric@llimona
parents: 21
diff changeset
   100
    for(i=len-2; i >= 0; --i)
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   101
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   102
        if (def[i] != '\n' && def[i] != '\r')
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
            def[i+1] = '\0';
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   105
            return i + 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   106
        }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   107
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   108
    return len;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   109
}
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   110
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   111
static void fill_def(struct Dict *d, int offset, int length, char * def)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   112
{
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   113
    fseek(d->defs, offset, SEEK_SET);
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   114
    fread(def, 1, length, d->defs);
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   115
    def[length] = 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   116
    if (d->trim_first_line)
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_first_line(def, length + 1/*\0*/) - 1 /*\0*/;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   119
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   120
    if (d->trim_last_newlines)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   121
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   122
        length = trim_last_newlines(def, length+1) - 1; /* math as above*/
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   123
    }
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   124
}
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   125
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   126
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
   127
{
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   128
    if (ptr >= (d->index + d->indexsize))
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   129
        return 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   130
    return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   131
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   132
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   133
static const char * skip_until_newline(struct Dict *d, const char *from)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   134
{
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   135
    if (pointer_at_end(d, from))
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   136
        return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   137
    while(*from != '\n' && *from != 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   138
    {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   139
        ++from;
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   140
        if(pointer_at_end(d, from))
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   141
            return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   142
    }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   143
    return from;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   144
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   145
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   146
static int compare(const unsigned char *word, const unsigned char *test)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   147
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   148
    int i;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   149
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   150
    /*printf("Comparing %s to %.20s\n", word, test);*/
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   151
    for(i=0; word[i] != 0 && test[i] != 0; ++i)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   152
    {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   153
        if (word[i] != test[i])
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   154
        {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   155
            break;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   156
        }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   157
    }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   158
    if (word[i] == 0 && test[i] == '\t')
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   159
        return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   160
    else if (word[i] == 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   161
        return -1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   162
    else if (test[i] == '\t')
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   163
        return 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   164
    else if (word[i] > test[i])
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   165
        return 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   166
    else if (word[i] < test[i])
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   167
        return -1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   168
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   169
    /* It should never reach this. */
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   170
    return -1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   171
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   172
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   173
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
   174
{
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   175
    const char *ret;
24
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
   176
    ret = skip_until_newline(d, from) + 1;
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
   177
    if (ret == (char *) 1) /* pointer at end */
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
   178
        return 0;
026a2ba0ce16 Now the HTML result shows word meanings.
viric@llimona
parents: 22
diff changeset
   179
    if (compare(word, ret) == 0)
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   180
        return ret;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   181
    return 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   182
}
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   183
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   184
static const char * bin_search(struct Dict *d, const char *word)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   185
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   186
    int step, pivot;
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   187
    const char *ret;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   188
    const char *test;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   189
    int comparision;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   190
    int found_once = 0;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   191
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   192
    pivot = d->indexsize / 2;
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   193
    step = d->indexsize / 2;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   194
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   195
    do
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   196
    {
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   197
        test = d->index + pivot;
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   198
        test = skip_until_newline(d, test);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   199
        if (test == 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   200
            return 0;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   201
        test += 1; /* skip exactly the new line */
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   202
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   203
        comparision = compare(word, test);
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   204
        if (comparision <= 0)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   205
        {
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   206
            if (comparision == 0)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   207
                found_once = 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   208
            /* If == 0, we don't know that it's the FIRST
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   209
             * match possible in the dictionary.
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   210
             * We want all possible matches. */
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   211
            step = step / 2;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   212
            pivot = pivot - step;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   213
        } else if (comparision > 0)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   214
        {
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   215
            step = step / 2;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   216
            pivot = pivot + step;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   217
        }
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   218
    } while(step > 0);
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   219
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   220
    if (!found_once)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   221
        return 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   222
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   223
    if (comparision == 0) /* last comparision */
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   224
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   225
        ret = skip_until_newline(d, d->index + pivot) + 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   226
    } else
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   227
    {
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   228
        ret = skip_until_newline(d, test) + 1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   229
    }
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   230
    return ret;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   231
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   232
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   233
static int my_get_int(const char **pos)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   234
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   235
    int i;
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   236
    const char *start;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   237
    int val;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   238
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   239
    start = *pos;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   240
    for(i=0; start[i] != '\t' && start[i] != '\n'; ++i)
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   241
        ;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   242
    val = str2int_len(start, i);
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   243
    *pos += i + 1;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   244
    return val;
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   245
}
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   246
17
d95d9e7a2b81 General interface to dictionary search.
viric@llimona
parents: 14
diff changeset
   247
void find_def(struct Dict *d, const char *word, char * def)
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   248
{
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   249
    int offset, len;
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   250
    const char *found, *pos;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   251
    int wordlen;
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   252
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   253
    def[0] = 0;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   254
    /* we will get a pointer to the offset for the ints*/
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   255
    found = bin_search(d, word);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   256
    if (found == 0)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   257
        return;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   258
    wordlen = strlen(word);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   259
    do
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   260
    {
21
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   261
        found += wordlen+1;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   262
        pos = found;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   263
        offset = my_get_int(&pos); /* increments pos */
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   264
        len = my_get_int(&pos); /* increments pos */
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   265
        fill_def(d, offset, len, def);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   266
        found = search_next(d, word, found);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   267
        if (!found)
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   268
            break;
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   269
        strcat(def, ", ");
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   270
        def += strlen(def);
01fe372188ac Added capabilities to the dictionary finder.
viric@llimona
parents: 17
diff changeset
   271
    } while(1);
14
a961bb8806b9 first 'zparsetext'.
viric@llimona
parents:
diff changeset
   272
}