listdic.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   listdic.c
00022  *  \brief  Program used to list a dictionary
00023  *  \author Antoine Fraboulet
00024  *  \date   1999
00025  */
00026 
00027 #include <string.h>
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include "dic_internals.h"
00031 #include "dic.h"
00032 
00033 
00034 static void
00035 print_dic_rec(FILE* out, Dictionary dic, char *buf, char* s, Dawg_edge i)
00036 {
00037   if (i.term)  /* edge points at a complete word */
00038     {
00039       *s = '\0';
00040       fprintf (out,"%s\n", buf);
00041     }
00042   if (i.ptr)
00043     {           /* Compute index: is it non-zero ? */
00044       Dawg_edge *p = dic->dawg + i.ptr;
00045       do {                         /* for each edge out of this node */
00046         *s = p->chr + 'a' - 1;
00047         print_dic_rec (out,dic,buf,s + 1, *p);
00048       }
00049       while (!(*p++).last);
00050     }
00051 }
00052 
00053 void
00054 dic_load(Dictionary* dic, char* filename)
00055 {
00056   int res;
00057   if ((res = Dic_load(dic, filename)) != 0)
00058     {
00059       switch (res) {
00060       case 1: printf("chargement: problème d'ouverture de %s\n",filename); break;
00061       case 2: printf("chargement: mauvais en-tete de dictionnaire\n"); break;
00062       case 3: printf("chargement: problème 3 d'allocation mémoire\n"); break;
00063       case 4: printf("chargement: problème 4 d'alocation mémoire\n"); break;
00064       case 5: printf("chargement: problème de lecture des arcs du dictionnaire\n"); break;
00065       default: printf("chargement: problème non-repertorié\n"); break;
00066       }
00067       exit(res);
00068     }
00069 }
00070 
00071 void
00072 print_dic_list(char* filename, char* out)
00073 {
00074   FILE* fout;
00075   Dictionary dic;
00076   static char buf[80];
00077 
00078   dic_load(&dic,filename);
00079 
00080   if (strcmp(out,"stdout") == 0)
00081     print_dic_rec(stdout,dic,buf,buf,dic->dawg[dic->root]);
00082   else if (strcmp(out,"stderr") == 0)
00083     print_dic_rec(stderr,dic,buf,buf,dic->dawg[dic->root]);
00084   else
00085     {
00086       if ((fout = fopen(out,"w")) == NULL)
00087         return;
00088       print_dic_rec(fout,dic,buf,buf,dic->dawg[dic->root]);
00089       fclose(fout);
00090     }
00091   Dic_destroy(dic);
00092 }
00093 
00094 char
00095 b2h(int i)
00096 {
00097   if (i < 10)
00098     return i+'0';
00099   return i-10+'a';
00100 }
00101 
00102 char*
00103 hexb(unsigned char h)
00104 {
00105   static char buf[3];
00106   buf[0] = b2h((h & 0xf0) >> 4);
00107   buf[1] = b2h((h & 0x0f));
00108   buf[2] = 0;
00109   return buf;
00110 }
00111 
00112 char*
00113 hexl(unsigned int h)
00114 {
00115   static char buf[9];
00116   int i;
00117   for(i=0; i<4; i++)
00118     {
00119       int l = h >> (24 - i*8);
00120       buf[i*2+0] = b2h((l & 0xf0) >> 4);
00121       buf[i*2+1] = b2h((l & 0x0f));
00122     }
00123   buf[8] = 0;
00124   return buf;
00125 }
00126 
00127 char*
00128 offset(void* base, void* off)
00129 {
00130   static char buf[20];
00131   int o = (char*)off - (char*)base;
00132   sprintf(buf,"%s",hexb(o));
00133   return buf;
00134 }
00135 
00136 void
00137 print_header(char* filename)
00138 {
00139   FILE* file;
00140   Dict_header header;
00141 
00142   if ((file = fopen(filename,"rb")) == NULL)
00143     return;
00144   if (fread(&header,sizeof(Dict_header),1,file) != 1)
00145     return;
00146   fclose(file);
00147 
00148   printf("Dictionary header information\n");
00149   printf("0x%s ident       : %s\n",offset(&header,&header.ident),header.ident);
00150   printf("0x%s unused 1    : %6d %s\n",offset(&header,&header.unused_1)  ,header.unused_1  ,hexl(header.unused_1));
00151   printf("0x%s unused 2    : %6d %s\n",offset(&header,&header.unused_2)  ,header.unused_2  ,hexl(header.unused_2));
00152   printf("0x%s root        : %6d %s\n",offset(&header,&header.root)      ,header.root      ,hexl(header.root));
00153   printf("0x%s words       : %6d %s\n",offset(&header,&header.nwords)    ,header.nwords    ,hexl(header.nwords));
00154   printf("0x%s edges used  : %6d %s\n",offset(&header,&header.edgesused) ,header.edgesused ,hexl(header.edgesused));
00155   printf("0x%s nodes used  : %6d %s\n",offset(&header,&header.nodesused) ,header.nodesused ,hexl(header.nodesused));
00156   printf("0x%s nodes saved : %6d %s\n",offset(&header,&header.nodessaved),header.nodessaved,hexl(header.nodessaved));
00157   printf("0x%s edges saved : %6d %s\n",offset(&header,&header.edgessaved),header.edgessaved,hexl(header.edgessaved));
00158   printf("\n");
00159   printf("sizeof(header) = 0x%s (%d)\n",hexb(sizeof(header)),sizeof(header));
00160 }
00161 
00162 void
00163 print_node_hex(int i, Dictionary dic)
00164 {
00165   unsigned int* pe;
00166   Dawg_edge e = dic->dawg[i];
00167   pe = (unsigned int*)&e;
00168   printf("0x%s %s |%2d ptr=%2d t=%d l=%d f=%d chr=%d (%c)\n",
00169          offset(&(dic->dawg[0]),&(dic->dawg[i])),hexl(*pe),i,
00170          e.ptr, e.term, e.last, e.fill, e.chr, e.chr +'a' -1);
00171 }
00172 
00173 void
00174 print_dic_hex(char* filename)
00175 {
00176   int i;
00177   Dictionary dic;
00178   dic_load(&dic,filename);
00179 
00180   printf("offs binary       structure         \n");
00181   printf("---- -------- |   ------------------\n");
00182   for(i=0; i < (dic->nedges + 1); i++)
00183     print_node_hex(i,dic);
00184   Dic_destroy(dic);
00185 }
00186 
00187 void
00188 usage(char* name)
00189 {
00190   printf("usage: %s [-a|-d|-h|-l] dictionnaire\n", name);
00191   printf("  -a : print all\n");
00192   printf("  -h : print header\n");
00193   printf("  -d : print dic in hex\n");
00194   printf("  -l : print dic word list\n");
00195 }
00196 
00197 
00198 int
00199 main(int argc, char *argv[])
00200 {
00201   int arg_count;
00202   int option_print_all      = 0;
00203   int option_print_header   = 0;
00204   int option_print_dic_hex  = 0;
00205   int option_print_dic_list = 0;
00206 
00207   if (argc < 3)
00208     {
00209       usage(argv[0]);
00210       exit(1);
00211     }
00212 
00213   arg_count = 1;
00214   while(argv[arg_count][0] == '-')
00215     {
00216       switch (argv[arg_count][1])
00217         {
00218         case 'a': option_print_all = 1; break;
00219         case 'h': option_print_header = 1; break;
00220         case 'd': option_print_dic_hex = 1; break;
00221         case 'l': option_print_dic_list = 1; break;
00222         default: usage(argv[0]); exit(2);
00223           break;
00224         }
00225       arg_count++;
00226     }
00227 
00228   if (option_print_header || option_print_all)
00229     {
00230       print_header(argv[arg_count]);
00231     }
00232   if (option_print_dic_hex || option_print_all)
00233     {
00234       print_dic_hex(argv[arg_count]);
00235     }
00236   if (option_print_dic_list || option_print_all)
00237     {
00238       print_dic_list(argv[arg_count],"stdout");
00239     }
00240   return 0;
00241 }

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