dic.c

Go to the documentation of this file.
00001 /* Eliot                                                                     */
00002 /* Copyright (C) 1999  Antoine Fraboulet                                     */
00003 /*                                                                           */
00004 /* This file is part of Eliot.                                               */
00005 /*                                                                           */
00006 /* Eliot is free software; you can redistribute it and/or modify             */
00007 /* it under the terms of the GNU General Public License as published by      */
00008 /* the Free Software Foundation; either version 2 of the License, or         */
00009 /* (at your option) any later version.                                       */
00010 /*                                                                           */
00011 /* Eliot is distributed in the hope that it will be useful,                  */
00012 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
00013 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
00014 /* GNU General Public License for more details.                              */
00015 /*                                                                           */
00016 /* You should have received a copy of the GNU General Public License         */
00017 /* along with this program; if not, write to the Free Software               */
00018 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00019 
00020 /**
00021  *  \file   dic.c
00022  *  \brief  Dawg dictionary
00023  *  \author Antoine Fraboulet
00024  *  \date   2002
00025  */
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <errno.h>
00031 #include <ctype.h>
00032 #include "dic_internals.h"
00033 #include "dic.h"
00034 
00035 
00036 static int
00037 check_header(FILE* file, Dict_header *header)
00038 {
00039   if (fread(header,sizeof(Dict_header),1,file) != 1)
00040     return 1;
00041   return strcmp(header->ident,_COMPIL_KEYWORD_);
00042 }
00043 
00044 
00045 int
00046 Dic_load(Dictionary *dic, const char* path)
00047 {
00048   FILE* file;
00049   Dict_header header;
00050 
00051   *dic = NULL;
00052   if ((file = fopen(path,"rb")) == NULL)
00053     return 1;
00054   if (check_header(file,&header))
00055     return 2;
00056   if ((*dic = (Dictionary) malloc(sizeof(struct _Dictionary))) == NULL)
00057     return 3;
00058   if (((*dic)->dawg = (Dawg_edge*)malloc((header.edgesused + 1)*
00059                                   sizeof(Dawg_edge))) == NULL)
00060     {
00061       free(*dic);
00062       *dic = NULL;
00063       return 4;
00064     }
00065   if (fread((*dic)->dawg,sizeof(Dawg_edge),header.edgesused + 1,file) !=
00066       (header.edgesused + 1))
00067     {
00068       free((*dic)->dawg);
00069       free(*dic);
00070       *dic = NULL;
00071       return 5;
00072     }
00073   (*dic)->root   = header.root;
00074   (*dic)->nwords = header.nwords;
00075   (*dic)->nnodes = header.nodesused;
00076   (*dic)->nedges = header.edgesused;
00077 
00078   fclose(file);
00079   return 0;
00080 }
00081 
00082 
00083 int
00084 Dic_destroy(Dictionary dic)
00085 {
00086   if (dic != NULL)
00087     {
00088       if (dic->dawg != NULL)
00089         free(dic->dawg);
00090       else
00091         {
00092           free(dic);
00093           return 2;
00094         }
00095       free(dic);
00096     }
00097   else
00098     return 1;
00099 
00100   return 0;
00101 }
00102 
00103 
00104 dic_elt_t
00105 Dic_next(Dictionary d, dic_elt_t e)
00106 {
00107      if (! Dic_last(d,e))
00108           return e+1;
00109      return 0;
00110 }
00111 
00112 
00113 dic_elt_t
00114 Dic_succ(Dictionary d, dic_elt_t e)
00115 {
00116   return (d->dawg[e]).ptr;
00117 }
00118 
00119 
00120 dic_elt_t
00121 Dic_root(Dictionary d)
00122 {
00123   return d->root;
00124 }
00125 
00126 
00127 char
00128 Dic_chr(Dictionary d, dic_elt_t e)
00129 {
00130     char c = (d->dawg[e]).chr;
00131     if (c)
00132         return c + 'A' - 1;
00133     else
00134         return 0;
00135 }
00136 
00137 
00138 int
00139 Dic_last(Dictionary d, dic_elt_t e)
00140 {
00141   return (d->dawg[e]).last;
00142 }
00143 
00144 
00145 int
00146 Dic_word(Dictionary d, dic_elt_t e)
00147 {
00148   return (d->dawg[e]).term;
00149 }
00150 
00151 
00152 unsigned int
00153 Dic_lookup(Dictionary d, dic_elt_t root, char* s)
00154 {
00155     unsigned int p;
00156 begin:
00157     if (! *s)
00158         return root;
00159     if (! Dic_succ(d, root))
00160         return 0;
00161     p = Dic_succ(d, root);
00162     do
00163     {
00164         if (Dic_chr(d, p) == *s)
00165         {
00166             root = p;
00167             s++;
00168             goto begin;
00169         }
00170         else if (Dic_last(d, p))
00171         {
00172             return 0;
00173         }
00174         p = Dic_next(d, p);
00175     } while (1);
00176 
00177     return 0;
00178 }
00179 

Generated on Thu Dec 29 02:01:14 2005 for Eliot by  doxygen 1.4.5