eliot-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Eliot-dev] eliot/dic listdic.c er.y er.l dic_internals.h d...


From: eliot-dev
Subject: [Eliot-dev] eliot/dic listdic.c er.y er.l dic_internals.h d...
Date: Sun, 16 Apr 2006 11:27:19 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Branch:         
Changes by:     Antoine Fraboulet <address@hidden>      06/04/16 11:27:19

Modified files:
        dic            : listdic.c er.y er.l dic_internals.h dic.h dic.c 
                         compdic.c 

Log message:
        - add load dictionnary functions for bigendian arch
        regression test is ok on linux/ppc

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/eliot/dic/listdic.c.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/eliot/eliot/dic/er.y.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/eliot/eliot/dic/er.l.diff?tr1=1.9&tr2=1.10&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/eliot/eliot/dic/dic_internals.h.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/eliot/eliot/dic/dic.h.diff?tr1=1.12&tr2=1.13&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/eliot/eliot/dic/dic.c.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/eliot/eliot/dic/compdic.c.diff?tr1=1.8&tr2=1.9&r1=text&r2=text

Patches:
Index: eliot/dic/compdic.c
diff -u eliot/dic/compdic.c:1.8 eliot/dic/compdic.c:1.9
--- eliot/dic/compdic.c:1.8     Sun Jan 22 12:23:53 2006
+++ eliot/dic/compdic.c Sun Apr 16 11:27:19 2006
@@ -33,8 +33,10 @@
 #include <string.h>
 #include <ctype.h>
 #include <assert.h>
+
 #include "hashtable.h"
 #include "dic_internals.h"
+#include "dic.h"
 
 //#define DEBUG_LIST
 //#define DEBUG_OUTPUT
@@ -103,7 +105,13 @@
   strcpy(header->ident,_COMPIL_KEYWORD_);
   header->root = header->edgesused;
   rewind (outfile);
+#if defined(WORDS_BIGENDIAN)
+  #warning "**********************************************"
+  #warning "compdic does not run yet on bigendian machines"
+  #warning "**********************************************"
+#else
   fwrite (header, sizeof(Dict_header), 1, outfile);
+#endif
 }
 
 
@@ -111,14 +119,14 @@
 print_header_info(Dict_header *header)
 {
   printf("============================\n");
-  printf("keyword length %lu bytes\n", strlen(_COMPIL_KEYWORD_));
-  printf("keyword size   %lu bytes\n", sizeof(_COMPIL_KEYWORD_));
-  printf("header size    %lu bytes\n", sizeof(Dict_header));
+  printf("keyword length %u bytes\n", strlen(_COMPIL_KEYWORD_));
+  printf("keyword size   %u bytes\n", sizeof(_COMPIL_KEYWORD_));
+  printf("header size    %u bytes\n", sizeof(Dict_header));
   printf("\n");
   printf("%d words\n",header->nwords);
   printf("\n");
   printf("root : %7d (edge)\n",header->root);
-  printf("root : %7lu (byte)\n",header->root * sizeof(Dawg_edge));
+  printf("root : %7u (byte)\n",header->root * sizeof(Dawg_edge));
   printf("\n");
   printf("nodes : %d+%d\n",header->nodesused, header->nodessaved);
   printf("edges : %d+%d\n",header->edgesused, header->edgessaved);
Index: eliot/dic/dic.c
diff -u eliot/dic/dic.c:1.10 eliot/dic/dic.c:1.11
--- eliot/dic/dic.c:1.10        Sun Jan  1 19:51:00 2006
+++ eliot/dic/dic.c     Sun Apr 16 11:27:19 2006
@@ -29,18 +29,76 @@
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
+
+#include "config.h"
 #include "dic_internals.h"
 #include "dic.h"
 
+#if defined(WORDS_BIGENDIAN)
+static uint32_t swap4(uint32_t v)
+{
+  uint32_t   r;
+  uint8_t  *pv,*pr;
+
+  pv = (uint8_t*)&v;
+  pr = (uint8_t*)&r;
+  
+  pr[0] = pv[3];
+  pr[1] = pv[2];
+  pr[2] = pv[1];
+  pr[3] = pv[0];
+
+  return r;
+}
+#endif
 
 static int
-check_header(FILE* file, Dict_header *header)
+Dic_read_convert_header(Dict_header *header, FILE* file)
 {
+
   if (fread(header,sizeof(Dict_header),1,file) != 1)
     return 1;
-  return strcmp(header->ident,_COMPIL_KEYWORD_);
+
+#if defined(WORDS_BIGENDIAN)
+  header->root       = swap4(header->root);
+  header->nwords     = swap4(header->nwords);
+  header->nodesused  = swap4(header->nodesused);
+  header->edgesused  = swap4(header->edgesused);
+  header->nodessaved = swap4(header->nodessaved);
+  header->edgessaved = swap4(header->edgessaved);
+#else
+
+#endif
+  return 0;
 }
 
+int
+Dic_check_header(Dict_header *header, const char *path)
+{
+  int r;
+  FILE* file;
+  if ((file = fopen(path,"rb")) == NULL)
+    return 1;
+  
+  r = Dic_read_convert_header(header,file);
+  fclose(file);
+
+  return r || strcmp(header->ident,_COMPIL_KEYWORD_);
+}
+
+static void
+Dic_convert_data_to_arch(Dictionary dic)
+{
+#if defined(WORDS_BIGENDIAN)
+  int i;
+  uint32_t* p;
+  p = (uint32_t*)dic->dawg;
+  for(i=0; i < (dic->nedges + 1); i++)
+    {
+      p[i] = swap4(p[i]);
+    }
+#endif
+}
 
 int
 Dic_load(Dictionary *dic, const char* path)
@@ -48,20 +106,23 @@
   FILE* file;
   Dict_header header;
 
+
   *dic = NULL;
   if ((file = fopen(path,"rb")) == NULL)
     return 1;
-  if (check_header(file,&header))
-    return 2;
+
+  Dic_read_convert_header(&header,file);
+
   if ((*dic = (Dictionary) malloc(sizeof(struct _Dictionary))) == NULL)
     return 3;
-  if (((*dic)->dawg = (Dawg_edge*)malloc((header.edgesused + 1)*
-                                  sizeof(Dawg_edge))) == NULL)
+
+  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))
     {
@@ -70,11 +131,14 @@
       *dic = NULL;
       return 5;
     }
+
   (*dic)->root   = header.root;
   (*dic)->nwords = header.nwords;
   (*dic)->nnodes = header.nodesused;
   (*dic)->nedges = header.edgesused;
 
+  Dic_convert_data_to_arch(*dic);
+
   fclose(file);
   return 0;
 }
Index: eliot/dic/dic.h
diff -u eliot/dic/dic.h:1.12 eliot/dic/dic.h:1.13
--- eliot/dic/dic.h:1.12        Sun Jan  1 19:51:00 2006
+++ eliot/dic/dic.h     Sun Apr 16 11:27:19 2006
@@ -41,10 +41,20 @@
  */
 #define DIC_WORD_MAX 16
 
-typedef struct _Dictionary* Dictionary;
+typedef struct _Dict_header  Dict_header;
+typedef struct _Dictionary  *Dictionary;
 typedef unsigned int dic_elt_t;
 typedef unsigned char dic_code_t;
 
+
+    /**
+     * Dictionary header loading from a file
+     * @param dic : pointer to a header
+     * @param path : compressed dictionary path
+     * @return 0 ok, otherwise error
+     */
+int    Dic_check_header(Dict_header *header, const char* path);
+
     /**
      * Dictionary creation and loading from a file
      * @param dic : pointer to a dictionary
Index: eliot/dic/dic_internals.h
diff -u eliot/dic/dic_internals.h:1.6 eliot/dic/dic_internals.h:1.7
--- eliot/dic/dic_internals.h:1.6       Sun Jan  1 19:51:00 2006
+++ eliot/dic/dic_internals.h   Sun Apr 16 11:27:19 2006
@@ -31,6 +31,9 @@
   {
 #endif
 
+#include <stdint.h>
+#include "config.h"
+
 /**
  * bit masking for ascii characters \n
  * ('a' & CHAR) == ('A' & CHAR) == 1
@@ -57,15 +60,30 @@
  *  ----------------
  */
 
-typedef struct _Dawg_edge {
-   unsigned int ptr  : 24;
-   unsigned int term : 1;
-   unsigned int last : 1;
-   unsigned int fill : 1;
-   unsigned int chr  : 5;
-} Dawg_edge;
+#if defined(WORDS_BIGENDIAN)
+struct __attribute__ ((packed)) _Dawg_edge {
+  uint32_t
+    chr  :  5,
+    fill :  1,
+    last :  1,
+    term :  1,
+    ptr  : 24;
+};
+#else
+struct __attribute__ ((packed)) _Dawg_edge {
+  uint32_t
+    ptr  : 24,
+    term :  1,
+    last :  1,
+    fill :  1,
+    chr  :  5;
+};
+#endif
+
+typedef struct _Dawg_edge Dawg_edge;
+
 
-typedef struct _Dict_header {
+struct _Dict_header {
   char ident[sizeof(_COMPIL_KEYWORD_)];
   char unused_1;
   char unused_2;
@@ -75,7 +93,8 @@
   unsigned int nodesused;
   unsigned int nodessaved;
   unsigned int edgessaved;
-} Dict_header;
+};
+
 
 struct _Dictionary
 {
Index: eliot/dic/er.l
diff -u eliot/dic/er.l:1.9 eliot/dic/er.l:1.10
--- eliot/dic/er.l:1.9  Sun Oct 23 14:53:43 2005
+++ eliot/dic/er.l      Sun Apr 16 11:27:19 2006
@@ -18,6 +18,7 @@
 /* along with this program; if not, write to the Free Software               */
 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
*/
 
+#include "dic_internals.h"
 #include "dic.h"
 #include "regexp.h"
 #include "libdic_a-er.h"
Index: eliot/dic/er.y
diff -u eliot/dic/er.y:1.10 eliot/dic/er.y:1.11
--- eliot/dic/er.y:1.10 Sat Nov  5 17:56:22 2005
+++ eliot/dic/er.y      Sun Apr 16 11:27:19 2006
@@ -23,8 +23,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "dic.h"
 #include "dic_internals.h"
+#include "dic.h"
 
 #include "dic.h"
 #include "regexp.h"
Index: eliot/dic/listdic.c
diff -u eliot/dic/listdic.c:1.8 eliot/dic/listdic.c:1.9
--- eliot/dic/listdic.c:1.8     Sun Jan 22 12:23:53 2006
+++ eliot/dic/listdic.c Sun Apr 16 11:27:19 2006
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stddef.h>
 #include "dic_internals.h"
 #include "dic.h"
 
@@ -50,6 +51,7 @@
     }
 }
 
+
 void
 dic_load(Dictionary* dic, char* filename)
 {
@@ -68,6 +70,7 @@
     }
 }
 
+
 void
 print_dic_list(char* filename, char* out)
 {
@@ -91,85 +94,47 @@
   Dic_destroy(dic);
 }
 
-char
-b2h(int i)
-{
-  if (i < 10)
-    return i+'0';
-  return i-10+'a';
-}
-
-char*
-hexb(unsigned char h)
-{
-  static char buf[3];
-  buf[0] = b2h((h & 0xf0) >> 4);
-  buf[1] = b2h((h & 0x0f));
-  buf[2] = 0;
-  return buf;
-}
-
-char*
-hexl(unsigned int h)
-{
-  static char buf[9];
-  int i;
-  for(i=0; i<4; i++)
-    {
-      int l = h >> (24 - i*8);
-      buf[i*2+0] = b2h((l & 0xf0) >> 4);
-      buf[i*2+1] = b2h((l & 0x0f));
-    }
-  buf[8] = 0;
-  return buf;
-}
-
-char*
-offset(void* base, void* off)
-{
-  static char buf[20];
-  int o = (char*)off - (char*)base;
-  sprintf(buf,"%s",hexb(o));
-  return buf;
-}
 
 void
 print_header(char* filename)
 {
-  FILE* file;
   Dict_header header;
 
-  if ((file = fopen(filename,"rb")) == NULL)
-    return;
-  if (fread(&header,sizeof(Dict_header),1,file) != 1)
-    return;
-  fclose(file);
+  Dic_check_header(&header,filename);
+
+#define OO(IDENT) offsetof(Dict_header,IDENT)
 
   printf("Dictionary header information\n");
-  printf("0x%s ident       : %s\n",offset(&header,&header.ident),header.ident);
-  printf("0x%s unused 1    : %6d %s\n",offset(&header,&header.unused_1)  
,header.unused_1  ,hexl(header.unused_1));
-  printf("0x%s unused 2    : %6d %s\n",offset(&header,&header.unused_2)  
,header.unused_2  ,hexl(header.unused_2));
-  printf("0x%s root        : %6d %s\n",offset(&header,&header.root)      
,header.root      ,hexl(header.root));
-  printf("0x%s words       : %6d %s\n",offset(&header,&header.nwords)    
,header.nwords    ,hexl(header.nwords));
-  printf("0x%s edges used  : %6d %s\n",offset(&header,&header.edgesused) 
,header.edgesused ,hexl(header.edgesused));
-  printf("0x%s nodes used  : %6d %s\n",offset(&header,&header.nodesused) 
,header.nodesused ,hexl(header.nodesused));
-  printf("0x%s nodes saved : %6d 
%s\n",offset(&header,&header.nodessaved),header.nodessaved,hexl(header.nodessaved));
-  printf("0x%s edges saved : %6d 
%s\n",offset(&header,&header.edgessaved),header.edgessaved,hexl(header.edgessaved));
+  printf("0x%02x ident       : %s\n",      OO(ident)     ,header.ident);
+  printf("0x%02x unused 1    : %6d %06x\n",OO(unused_1)  ,header.unused_1  
,header.unused_1);
+  printf("0x%02x unused 2    : %6d %06x\n",OO(unused_2)  ,header.unused_2  
,header.unused_2);
+  printf("0x%02x root        : %6d %06x\n",OO(root)      ,header.root      
,header.root);
+  printf("0x%02x words       : %6d %06x\n",OO(nwords)    ,header.nwords    
,header.nwords);
+  printf("0x%02x edges used  : %6d %06x\n",OO(edgesused) ,header.edgesused 
,header.edgesused);
+  printf("0x%02x nodes used  : %6d %06x\n",OO(nodesused) ,header.nodesused 
,header.nodesused);
+  printf("0x%02x nodes saved : %6d 
%06x\n",OO(nodessaved),header.nodessaved,header.nodessaved);
+  printf("0x%02x edges saved : %6d 
%06x\n",OO(edgessaved),header.edgessaved,header.edgessaved);
   printf("\n");
-  printf("sizeof(header) = 0x%s (%lu)\n", hexb(sizeof(header)), 
sizeof(header));
+  printf("sizeof(header) = 0x%x (%u)\n", sizeof(header), sizeof(header));
 }
 
-void
-print_node_hex(int i, Dictionary dic)
+
+static void
+print_node_hex(Dictionary dic, int i)
 {
-  unsigned int* pe;
-  Dawg_edge e = dic->dawg[i];
-  pe = (unsigned int*)&e;
-  printf("0x%s %s |%2d ptr=%2d t=%d l=%d f=%d chr=%d (%c)\n",
-        offset(&(dic->dawg[0]),&(dic->dawg[i])),hexl(*pe),i,
-        e.ptr, e.term, e.last, e.fill, e.chr, e.chr +'a' -1);
+  union edge_t {
+    Dawg_edge e;
+    uint32_t  s;
+  } ee;
+
+  ee.e = dic->dawg[i];
+
+  printf("0x%04x %08x |%4d ptr=%8d t=%d l=%d f=%d chr=%2d (%c)\n",
+        i*sizeof(ee), (unsigned int)(ee.s), 
+        i, ee.e.ptr, ee.e.term, ee.e.last, ee.e.fill, ee.e.chr, ee.e.chr +'a' 
-1);
 }
 
+
 void
 print_dic_hex(char* filename)
 {
@@ -180,10 +145,11 @@
   printf("offs binary       structure         \n");
   printf("---- -------- |   ------------------\n");
   for(i=0; i < (dic->nedges + 1); i++)
-    print_node_hex(i,dic);
+    print_node_hex(dic,i);
   Dic_destroy(dic);
 }
 
+
 void
 usage(char* name)
 {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]