--- a/find.c Sat Sep 01 01:19:18 2007 +0200
+++ b/find.c Sat Sep 01 12:26:22 2007 +0200
@@ -5,12 +5,8 @@
#include <fcntl.h>
#include "dictre.h"
-static unsigned char *index;
-static int indexfd;
-static int indexsize;
-static FILE *defs;
-const static char indexname[] = "akcentiga.index";
-const static char dictname[] = "akcentiga.dict";
+const static char indexext[] = ".index";
+const static char dictext[] = ".dict";
int get_filesize(const char *fname)
{
@@ -27,56 +23,69 @@
return st.st_size;
}
-void init_dictionary()
+void init_dictionary(struct Dict *d, const char *base)
{
- indexsize = get_filesize(indexname);
- indexfd = open(indexname, O_RDONLY);
- if (indexfd == -1)
+ char *filename;
+
+ filename = (char *) malloc(strlen(base) + 10);
+
+ /* Prepare .index filename and open it*/
+ strcpy(filename, base);
+ strcat(filename, indexext);
+
+ d->indexsize = get_filesize(filename);
+ d->indexfd = open(filename, O_RDONLY);
+ if (d->indexfd == -1)
{
- fprintf(stderr, "Problem opening the file %s\n", indexname);
+ fprintf(stderr, "Problem opening the file %s\n", filename);
perror("Error:");
exit(-1);
}
- index = (unsigned char *) mmap(0, indexsize, PROT_READ, MAP_SHARED,
- indexfd, 0);
+ d->index = (unsigned char *) mmap(0, d->indexsize, PROT_READ, MAP_SHARED,
+ d->indexfd, 0);
- defs = fopen(dictname, "r");
- if (defs == 0)
+ /* Prepare .dict filename and open it*/
+ strcpy(filename, base);
+ strcat(filename, dictext);
+ d->defs = fopen(filename, "r");
+ if (d->defs == 0)
{
- fprintf(stderr, "Problem opening the file %s\n", dictname);
+ fprintf(stderr, "Problem opening the file %s\n", filename);
perror("Error:");
exit(-1);
}
-}
-void end_dictionary()
-{
- munmap(index, indexsize);
- close(indexfd);
- fclose(defs);
+ free(filename);
}
-static void fill_def(int offset, int length, char * def)
+void end_dictionary(struct Dict *d)
{
- fseek(defs, offset, SEEK_SET);
- fread(def, 1, length, defs);
+ munmap(d->index, d->indexsize);
+ close(d->indexfd);
+ fclose(d->defs);
}
-static int pointer_at_end(unsigned char *ptr)
+static void fill_def(struct Dict *d, int offset, int length, char * def)
{
- if (ptr >= (index + indexsize))
+ fseek(d->defs, offset, SEEK_SET);
+ fread(def, 1, length, d->defs);
+}
+
+static int pointer_at_end(struct Dict *d, unsigned char *ptr)
+{
+ if (ptr >= (d->index + d->indexsize))
return 1;
return 0;
}
-static char * skip_until_newline(char *from)
+static char * skip_until_newline(struct Dict *d, char *from)
{
- if (pointer_at_end(from))
+ if (pointer_at_end(d, from))
return 0;
while(*from != '\n' && *from != 0)
{
++from;
- if(pointer_at_end(from))
+ if(pointer_at_end(d, from))
return 0;
}
return from;
@@ -109,19 +118,19 @@
return -1;
}
-static char * bin_search(const char *word)
+static char * bin_search(struct Dict *d, const char *word)
{
int step, pivot;
- pivot = indexsize / 2;
- step = indexsize / 2;
+ pivot = d->indexsize / 2;
+ step = d->indexsize / 2;
do
{
char *test;
int comparision;
- test = index + pivot;
- test = skip_until_newline(test);
+ test = d->index + pivot;
+ test = skip_until_newline(d, test);
if (test == 0)
return 0;
test += 1; /* skip exactly the new line */
@@ -157,12 +166,12 @@
return val;
}
-void find_def(const char *word, char * def)
+void find_def(struct Dict *d, const char *word, char * def)
{
int offset, len;
char *pos;
- pos = bin_search(word); /* pos points to the offset already. */
+ pos = bin_search(d, word); /* pos points to the offset already. */
if (pos == 0)
{
def[0] = 0;
@@ -171,5 +180,5 @@
}
offset = my_get_int(&pos); /* increments pos */
len = my_get_int(&pos); /* increments pos */
- fill_def(offset, len, def);
+ fill_def(d, offset, len, def);
}