2004-04-08 11:43:06 +02:00
|
|
|
/* Eliot */
|
2005-05-06 01:45:04 +02:00
|
|
|
/* Copyright (C) 1999 Antoine Fraboulet */
|
2004-04-08 11:43:06 +02:00
|
|
|
/* */
|
2005-05-06 01:45:04 +02:00
|
|
|
/* This file is part of Eliot. */
|
|
|
|
/* */
|
|
|
|
/* Eliot is free software; you can redistribute it and/or modify */
|
2004-04-08 11:43:06 +02:00
|
|
|
/* it under the terms of the GNU General Public License as published by */
|
|
|
|
/* the Free Software Foundation; either version 2 of the License, or */
|
|
|
|
/* (at your option) any later version. */
|
|
|
|
/* */
|
2005-05-06 01:45:04 +02:00
|
|
|
/* Elit is distributed in the hope that it will be useful, */
|
2004-04-08 11:43:06 +02:00
|
|
|
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
|
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
|
|
/* GNU General Public License for more details. */
|
|
|
|
/* */
|
|
|
|
/* You should have received a copy of the GNU General Public License */
|
|
|
|
/* along with this program; if not, write to the Free Software */
|
2005-10-23 16:53:42 +02:00
|
|
|
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "dic_internals.h"
|
|
|
|
#include "dic.h"
|
|
|
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
static int
|
2004-04-08 11:43:06 +02:00
|
|
|
check_header(FILE* file, Dict_header *header)
|
|
|
|
{
|
|
|
|
if (fread(header,sizeof(Dict_header),1,file) != 1)
|
|
|
|
return 1;
|
|
|
|
return strcmp(header->ident,_COMPIL_KEYWORD_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
Dic_load(Dictionary *dic, const char* path)
|
|
|
|
{
|
|
|
|
FILE* file;
|
|
|
|
Dict_header header;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
*dic = NULL;
|
|
|
|
if ((file = fopen(path,"rb")) == NULL)
|
|
|
|
return 1;
|
|
|
|
if (check_header(file,&header))
|
|
|
|
return 2;
|
|
|
|
if ((*dic = (Dictionary) malloc(sizeof(struct _Dictionary))) == NULL)
|
|
|
|
return 3;
|
|
|
|
if (((*dic)->dawg = (Dawg_edge*)malloc((header.edgesused + 1)*
|
|
|
|
sizeof(Dawg_edge))) == NULL)
|
|
|
|
{
|
|
|
|
free(*dic);
|
|
|
|
*dic = NULL;
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if (fread((*dic)->dawg,sizeof(Dawg_edge),header.edgesused + 1,file) !=
|
|
|
|
(header.edgesused + 1))
|
|
|
|
{
|
|
|
|
free((*dic)->dawg);
|
|
|
|
free(*dic);
|
|
|
|
*dic = NULL;
|
|
|
|
return 5;
|
|
|
|
}
|
2005-04-19 18:26:50 +02:00
|
|
|
(*dic)->root = header.root;
|
2004-04-08 11:43:06 +02:00
|
|
|
(*dic)->nwords = header.nwords;
|
|
|
|
(*dic)->nnodes = header.nodesused;
|
|
|
|
(*dic)->nedges = header.edgesused;
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
int
|
2004-04-08 11:43:06 +02:00
|
|
|
Dic_destroy(Dictionary dic)
|
|
|
|
{
|
|
|
|
if (dic != NULL)
|
|
|
|
{
|
|
|
|
if (dic->dawg != NULL)
|
|
|
|
free(dic->dawg);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(dic);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
free(dic);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-20 20:11:00 +02:00
|
|
|
dic_elt_t
|
|
|
|
Dic_next(Dictionary d, dic_elt_t e)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
if (! Dic_last(d,e))
|
|
|
|
return e+1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-20 20:11:00 +02:00
|
|
|
dic_elt_t
|
|
|
|
Dic_succ(Dictionary d, dic_elt_t e)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
return (d->dawg[e]).ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-20 20:11:00 +02:00
|
|
|
dic_elt_t
|
2004-04-08 11:43:06 +02:00
|
|
|
Dic_root(Dictionary d)
|
|
|
|
{
|
|
|
|
return d->root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char
|
2005-04-20 20:11:00 +02:00
|
|
|
Dic_chr(Dictionary d, dic_elt_t e)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
2005-12-24 19:42:44 +01:00
|
|
|
char c = (d->dawg[e]).chr;
|
|
|
|
if (c)
|
|
|
|
return c + 'A' - 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2004-04-08 11:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-04-20 20:11:00 +02:00
|
|
|
Dic_last(Dictionary d, dic_elt_t e)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
return (d->dawg[e]).last;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-04-20 20:11:00 +02:00
|
|
|
Dic_word(Dictionary d, dic_elt_t e)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
return (d->dawg[e]).term;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
unsigned int
|
2005-04-20 20:11:00 +02:00
|
|
|
Dic_lookup(Dictionary d, dic_elt_t root, char* s)
|
2004-08-07 20:10:42 +02:00
|
|
|
{
|
|
|
|
unsigned int p;
|
|
|
|
begin:
|
|
|
|
if (! *s)
|
2005-04-20 20:11:00 +02:00
|
|
|
return root;
|
|
|
|
if (! Dic_succ(d, root))
|
2004-08-07 20:10:42 +02:00
|
|
|
return 0;
|
2005-04-20 20:11:00 +02:00
|
|
|
p = Dic_succ(d, root);
|
2004-08-07 20:10:42 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (Dic_chr(d, p) == *s)
|
|
|
|
{
|
2005-04-20 20:11:00 +02:00
|
|
|
root = p;
|
2004-08-07 20:10:42 +02:00
|
|
|
s++;
|
|
|
|
goto begin;
|
|
|
|
}
|
|
|
|
else if (Dic_last(d, p))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p = Dic_next(d, p);
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|