eliot-dev
[Top][All Lists]
Advanced

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

[Eliot-dev] eliot/dic Makefile.am compdic.cpp dic.cpp dic.h... [cppdic]


From: eliot-dev
Subject: [Eliot-dev] eliot/dic Makefile.am compdic.cpp dic.cpp dic.h... [cppdic]
Date: Sun, 24 Dec 2006 20:49:43 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Branch:         cppdic
Changes by:     Olivier Teulière <ipkiss>      06/12/24 20:49:43

Modified files:
        dic            : Makefile.am compdic.cpp dic.cpp dic.h 
                         dic_internals.h listdic.cpp 
Added files:
        dic            : header.cpp header.h 

Log message:
        Isolation of the dictionary header into a dedicated class. This will 
allow easier changes of its format (only one place where to handle backwards 
compatibility)

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/Makefile.am?cvsroot=eliot&only_with_tag=cppdic&r1=1.14.4.1&r2=1.14.4.2
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/compdic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.1&r2=1.1.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.1&r2=1.1.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.13.2.1&r2=1.13.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic_internals.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.7.2.1&r2=1.7.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/listdic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.1&r2=1.1.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/header.cpp?cvsroot=eliot&only_with_tag=cppdic&rev=1.1.2.1
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/header.h?cvsroot=eliot&only_with_tag=cppdic&rev=1.1.2.1

Patches:
Index: Makefile.am
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Makefile.am,v
retrieving revision 1.14.4.1
retrieving revision 1.14.4.2
diff -u -b -r1.14.4.1 -r1.14.4.2
--- Makefile.am 15 Oct 2006 11:07:54 -0000      1.14.4.1
+++ Makefile.am 24 Dec 2006 20:49:42 -0000      1.14.4.2
@@ -25,6 +25,8 @@
 libdic_a_SOURCES =               \
        er.ypp                    \
        er.lpp                    \
+       header.h \
+       header.cpp \
        dic_internals.h           \
        dic_search.cpp dic_search.h \
        dic.cpp dic.h             \
@@ -60,11 +62,13 @@
 compdic_SOURCES=               \
        dic_internals.h         \
        hashtable.cpp hashtble.h        \
+       header.cpp header.h \
        compdic.cpp
 
 listdic_SOURCES=               \
        dic_internals.h         \
        dic.cpp dic.h           \
+       header.cpp header.h \
        listdic.cpp
 
 regexp_SOURCES=        regexpmain.cpp

Index: compdic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/compdic.cpp,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -b -r1.1.2.1 -r1.1.2.2
--- compdic.cpp 15 Oct 2006 11:07:55 -0000      1.1.2.1
+++ compdic.cpp 24 Dec 2006 20:49:42 -0000      1.1.2.2
@@ -35,6 +35,7 @@
 #include <assert.h>
 
 #include "hashtable.h"
+#include "header.h"
 #include "dic_internals.h"
 #include "dic.h"
 
@@ -84,54 +85,35 @@
 }
 
 
-void skip_init_header(FILE* outfile, Dict_header *header)
+void skip_init_header(FILE* outfile, Dict_header_info *headerInfo)
 {
-    header->unused_1   = 0;
-    header->unused_2   = 0;
-    header->root       = 0;
-    header->nwords     = 0;
-    header->nodesused  = 1;
-    header->edgesused  = 1;
-    header->nodessaved = 0;
-    header->edgessaved = 0;
+    headerInfo->root       = 0;
+    headerInfo->nwords     = 0;
+    headerInfo->nodesused  = 1;
+    headerInfo->edgesused  = 1;
+    headerInfo->nodessaved = 0;
+    headerInfo->edgessaved = 0;
 
-    fwrite(header, sizeof(Dict_header), 1, outfile);
+    Header aHeader(*headerInfo);
+    aHeader.write(outfile);
 }
 
 
-void fix_header(FILE* outfile, Dict_header* header)
+void fix_header(FILE* outfile, Dict_header_info* headerInfo)
 {
-    strcpy(header->ident, _COMPIL_KEYWORD_);
-    header->root = header->edgesused;
+    headerInfo->root = headerInfo->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);
+    Header aHeader(*headerInfo);
+    aHeader.write(outfile);
 #endif
 }
 
 
-void print_header_info(Dict_header *header)
-{
-    printf("============================\n");
-    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 : %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);
-    printf("============================\n");
-}
-
-
 void write_node(Dawg_edge *edges, int size, int num, FILE* outfile)
 {
 #ifdef DEBUG_OUTPUT
@@ -159,7 +141,7 @@
 
 /* global variables */
 FILE*       global_outfile;
-Dict_header global_header;
+Dict_header_info global_header_info;
 Hash_table  global_hashtable;
 
 char        global_stringbuf[MAX_STRING_LENGTH]; /* Space for current string */
@@ -202,7 +184,7 @@
         (*(edgeptr++)).chr = (*global_endstring++ = *global_input++) & 
DIC_CHAR_MASK;
         if (*global_input == '\n')                 /* End of a word */
         {
-            global_header.nwords++;
+            global_header_info.nwords++;
             edgeptr[-1].term = 1;                  /* Mark edge as word */
             *global_endstring++ = *global_input++; /* Skip \n */
             if (global_input == global_endofinput) /* At end of input? */
@@ -235,8 +217,8 @@
                                                numedges*sizeof(Dawg_edge));
     if (saved_position)
     {
-        global_header.edgessaved += numedges;
-        global_header.nodessaved++;
+        global_header_info.edgessaved += numedges;
+        global_header_info.nodessaved++;
 
 #ifdef CHECK_RECURSION
         current_rec --;
@@ -245,12 +227,12 @@
     }
     else
     {
-        unsigned int node_pos = global_header.edgesused;
+        unsigned int node_pos = global_header_info.edgesused;
         hash_add(global_hashtable,
                  (void*)edges, numedges*sizeof(Dawg_edge),
-                 (void*)(&global_header.edgesused), 
sizeof(global_header.edgesused));
-        global_header.edgesused += numedges;
-        global_header.nodesused++;
+                 (void*)(&global_header_info.edgesused), 
sizeof(global_header_info.edgesused));
+        global_header_info.edgesused += numedges;
+        global_header_info.nodesused++;
         write_node(edges, sizeof(Dawg_edge), numedges, global_outfile);
 
 #ifdef CHECK_RECURSION
@@ -263,12 +245,7 @@
 
 int main(int argc, char* argv[])
 {
-    unsigned int dicsize;
     char *uncompressed;
-    Dawg_edge rootnode = {0, 0, 0, 0, 0};
-    Dawg_edge specialnode = {0, 0, 0, 0, 0};
-
-    char outfilenamedefault[] = "dict.daw";
 
     if (argc < 2)
     {
@@ -276,14 +253,14 @@
         exit(1);
     }
 
-    dicsize = file_length(argv[1]);
+    unsigned int dicsize = file_length(argv[1]);
     if (dicsize < 0)
     {
         fprintf(stderr, "Cannot stat uncompressed dictionary %s\n", argv[1]);
         exit(1);
     }
 
-    char* outfilename = (argc == 3) ? argv[2] : outfilenamedefault;
+    const char *outfilename = (argc == 3) ? argv[2] : "dict.daw";
 
     if ((global_outfile = fopen(outfilename, "wb")) == NULL)
     {
@@ -304,8 +281,9 @@
     global_hashtable = hash_init((unsigned int)(dicsize * SCALE));
 #undef SCALE
 
-    skip_init_header(global_outfile, &global_header);
+    skip_init_header(global_outfile, &global_header_info);
 
+    Dawg_edge specialnode = {0, 0, 0, 0, 0};
     specialnode.last = 1;
     write_node(&specialnode, sizeof(specialnode), 1, global_outfile);
     /*
@@ -313,20 +291,23 @@
      * Initialize string to null; Put index of start node on output
      */
     clock_t starttime = clock();
+    Dawg_edge rootnode = {0, 0, 0, 0, 0};
     rootnode.ptr = makenode(global_endstring = global_stringbuf);
     clock_t endtime = clock();
     write_node(&rootnode, sizeof(rootnode), 1, global_outfile);
 
-    fix_header(global_outfile, &global_header);
+    fix_header(global_outfile, &global_header_info);
+
+    Header aHeader(global_header_info);
+    aHeader.print();
 
-    print_header_info(&global_header);
     hash_destroy(global_hashtable);
     free(uncompressed);
     fclose(global_outfile);
 
-    printf(" Elapsed time is                 : %f s\n", 
1.0*(endtime-starttime) / CLOCKS_PER_SEC);
+    printf(" Elapsed time is                : %f s\n", 1.0 * (endtime - 
starttime) / CLOCKS_PER_SEC);
 #ifdef CHECK_RECURSION
-    printf(" Maximum recursion level reached : %d\n", max_rec);
+    printf(" Maximum recursion level reached: %d\n", max_rec);
 #endif
     return 0;
 }

Index: dic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/dic.cpp,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -b -r1.1.2.1 -r1.1.2.2
--- dic.cpp     15 Oct 2006 11:07:55 -0000      1.1.2.1
+++ dic.cpp     24 Dec 2006 20:49:42 -0000      1.1.2.2
@@ -24,15 +24,15 @@
  *  \date   2002
  */
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
 
 #include "config.h"
-#include "dic_internals.h"
 #include "dic.h"
+#include "header.h"
+#include "dic_internals.h"
 
 #if defined(WORDS_BIGENDIAN)
 static uint32_t swap4(uint32_t v)
@@ -50,42 +50,12 @@
 }
 #endif
 
-static int Dic_read_convert_header(Dict_header *oHeader, FILE *iFile)
-{
-
-    if (fread(oHeader, sizeof(Dict_header), 1, iFile) != 1)
-        return 1;
-
-#if defined(WORDS_BIGENDIAN)
-    oHeader->root       = swap4(oHeader->root);
-    oHeader->nwords     = swap4(oHeader->nwords);
-    oHeader->nodesused  = swap4(oHeader->nodesused);
-    oHeader->edgesused  = swap4(oHeader->edgesused);
-    oHeader->nodessaved = swap4(oHeader->nodessaved);
-    oHeader->edgessaved = swap4(oHeader->edgessaved);
-#else
-
-#endif
-    return 0;
-}
-
-int Dictionary::checkHeader(Dict_header *oHeader, const string &iPath)
-{
-    FILE *file;
-    if ((file = fopen(iPath.c_str(), "rb")) == NULL)
-        return 1;
-
-    int r = Dic_read_convert_header(oHeader, file);
-    fclose(file);
-
-    return r || strcmp(oHeader->ident, _COMPIL_KEYWORD_);
-}
 
 void Dictionary::convertDataToArch()
 {
 #if defined(WORDS_BIGENDIAN)
     uint32_t *p = (uint32_t*)m_dawg;
-    for (int i = 0; i < (dic->nedges + 1); i++)
+    for (int i = 0; i < (m_nbEdges + 1); i++)
     {
         p[i] = swap4(p[i]);
     }
@@ -113,18 +83,19 @@
     if ((file = fopen(iPath.c_str(), "rb")) == NULL)
         return 1;
 
-    Dict_header header;
-    Dic_read_convert_header(&header, file);
+    Header aHeader;
+    if (!aHeader.read(file))
+        return 2;
 
-    m_dawg = new Dawg_edge[header.edgesused + 1];
+    m_dawg = new Dawg_edge[aHeader.getNbEdgesUsed() + 1];
     if (m_dawg == NULL)
     {
         fclose(file);
         return 4;
     }
 
-    if (fread(m_dawg, sizeof(Dawg_edge), header.edgesused + 1, file) !=
-        (header.edgesused + 1))
+    if (fread(m_dawg, sizeof(Dawg_edge), aHeader.getNbEdgesUsed() + 1, file) !=
+        (aHeader.getNbEdgesUsed() + 1))
     {
         delete[] m_dawg;
         m_dawg = NULL;
@@ -132,10 +103,10 @@
         return 5;
     }
 
-    m_root = header.root;
-    m_nbWords = header.nwords;
-    m_nbNodes = header.nodesused;
-    m_nbEdges = header.edgesused;
+    m_root = aHeader.getRoot();
+    m_nbWords = aHeader.getNbWords();
+    m_nbNodes = aHeader.getNbNodesUsed();
+    m_nbEdges = aHeader.getNbEdgesUsed();
 
     convertDataToArch();
 
@@ -146,7 +117,7 @@
 
 const dic_elt_t Dictionary::getNext(const dic_elt_t &e) const
 {
-     if (! isLast(e))
+     if (!isLast(e))
          return e + 1;
      return 0;
 }

Index: dic.h
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/dic.h,v
retrieving revision 1.13.2.1
retrieving revision 1.13.2.2
diff -u -b -r1.13.2.1 -r1.13.2.2
--- dic.h       15 Oct 2006 11:07:55 -0000      1.13.2.1
+++ dic.h       24 Dec 2006 20:49:42 -0000      1.13.2.2
@@ -37,7 +37,6 @@
  */
 #define DIC_WORD_MAX 16
 
-typedef struct _Dict_header  Dict_header;
 typedef struct _Dawg_edge Dawg_edge;
 typedef unsigned int dic_elt_t;
 typedef unsigned char dic_code_t;
@@ -66,22 +65,14 @@
     int getNbEdges() const { return m_nbEdges; }
 
     /**
-     * Dictionary header loading from a file
-     * @param dic : pointer to a header
-     * @param path : compressed dictionary path
-     * @return 0 ok, otherwise error
-     */
-    static int checkHeader(Dict_header *header, const string &iPath);
-
-    /**
-     * Dic_chr returns the character code associated with an element,
+     * Returns the character code associated with an element,
      * codes may range from 0 to 31. 0 is the null character.
      * @returns code for the encoded character
      */
     const dic_code_t getCode(const dic_elt_t &elt) const;
 
     /**
-     * Dic_char returns the character associated with an element
+     * Returns the character associated with an element
      * (in the range ['A'-'Z']), or the null character ('\0').
      * @returns ASCII code for the character
      */
@@ -96,7 +87,7 @@
 
     /**
      * Returns a boolean to show if we are at the end of a word
-     * (see Dic_next)
+     * (see getNext)
      * @returns 0 or 1 (true)
      */
     bool isEndOfWord(const dic_elt_t &elt) const;
@@ -108,7 +99,7 @@
     const dic_elt_t getRoot() const;
 
     /**
-     * Returns the next available neighbor (see Dic_last)
+     * Returns the next available neighbor (see getLast)
      * @returns next dictionary element at the same depth
      */
     const dic_elt_t getNext(const dic_elt_t &elt) const;
@@ -116,7 +107,6 @@
     /**
      * Returns the first element available at the next depth
      * in the dictionary
-     * @params dic : dictionary
      * @params elt : current dictionary element
      * @returns next element (successor)
      */
@@ -125,7 +115,6 @@
     /**
      * Find the dictionary element matching the pattern starting
      * from the given root node by walking the dictionary tree
-     * @params dic : valid dictionary
      * @params root : starting dictionary node for the search
      * @params pattern : string encoded according to the dictionary codes,
      * the pattern must be null ('\0') terminated
@@ -138,7 +127,6 @@
     /**
      * Find the dictionary element matching the pattern starting
      * from the given root node by walking the dictionary tree
-     * @params dic : valid dictionary
      * @params root : starting dictionary node for the search
      * @params pattern : string made of uppercase characters in the range
      * ['A'-'Z']. The pattern must be null ('\0') terminated

Index: dic_internals.h
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/dic_internals.h,v
retrieving revision 1.7.2.1
retrieving revision 1.7.2.2
diff -u -b -r1.7.2.1 -r1.7.2.2
--- dic_internals.h     15 Oct 2006 11:07:55 -0000      1.7.2.1
+++ dic_internals.h     24 Dec 2006 20:49:42 -0000      1.7.2.2
@@ -37,12 +37,6 @@
 #define DIC_CHAR_MASK    0x1F
 
 /**
- * keyword included in dictionary headers
- * implies little endian storage on words
- */
-#define _COMPIL_KEYWORD_ "_COMPILED_DICTIONARY_"
-
-/**
  *  structure of a compressed dictionary \n
  *  \n
  *  ----------------    \n
@@ -79,19 +73,6 @@
 typedef struct _Dawg_edge Dawg_edge;
 
 
-struct _Dict_header {
-  char ident[sizeof(_COMPIL_KEYWORD_)];
-  char unused_1;
-  char unused_2;
-  int root;
-  int nwords;
-  unsigned int edgesused;
-  unsigned int nodesused;
-  unsigned int nodessaved;
-  unsigned int edgessaved;
-};
-
-
 struct _Dictionary
 {
   Dawg_edge *dawg;

Index: listdic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/listdic.cpp,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -b -r1.1.2.1 -r1.1.2.2
--- listdic.cpp 15 Oct 2006 11:07:55 -0000      1.1.2.1
+++ listdic.cpp 24 Dec 2006 20:49:42 -0000      1.1.2.2
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stddef.h>
+#include "header.h"
 #include "dic_internals.h"
 #include "dic.h"
 
@@ -62,7 +63,7 @@
             case 1: printf("chargement: problème d'ouverture de %s\n", 
iFileName.c_str()); break;
             case 2: printf("chargement: mauvais en-tete de dictionnaire\n"); 
break;
             case 3: printf("chargement: problème 3 d'allocation mémoire\n"); 
break;
-            case 4: printf("chargement: problème 4 d'alocation mémoire\n"); 
break;
+            case 4: printf("chargement: problème 4 d'allocation mémoire\n"); 
break;
             case 5: printf("chargement: problème de lecture des arcs du 
dictionnaire\n"); break;
             default: printf("chargement: problème non-repertorié\n"); break;
         }
@@ -95,24 +96,21 @@
 
 void print_header(const string &iFileName)
 {
-    Dict_header header;
-
-    Dictionary::checkHeader(&header, iFileName);
-
-#define OO(IDENT) offsetof(Dict_header, IDENT)
+    FILE *file;
+    if ((file = fopen(iFileName.c_str(), "rb")) == NULL)
+    {
+        fprintf(stderr, "Couldn't open file: %s\n", iFileName.c_str());
+        exit(1);
+    }
 
-    printf("Dictionary header information\n");
-    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%x (%u)\n", sizeof(header), sizeof(header));
+    Header header;
+    if (!header.read(file))
+    {
+        fprintf(stderr, "Invalid header in '%s'\n", iFileName.c_str());
+        fclose(file);
+        exit(1);
+    }
+    header.print();
 }
 
 
@@ -126,7 +124,7 @@
 
     ee.e = dic.getDawg()[i];
 
-    printf("0x%04x %08x |%4d ptr=%8d t=%d l=%d f=%d chr=%2d (%c)\n",
+    printf("0x%04lx %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);
 }
@@ -137,20 +135,20 @@
     Dictionary dic;
     dic_load(dic, iFileName);
 
-    printf("offs binary       structure         \n");
+    printf("offs binary       structure\n");
     printf("---- -------- |   ------------------\n");
     for (int i = 0; i < (dic.getNbEdges() + 1); i++)
         print_node_hex(dic, i);
 }
 
 
-void usage(const char *iName)
+void usage(const string &iName)
 {
-    printf("usage: %s [-a|-d|-h|-l] dictionnaire\n", iName);
-    printf("  -a : print all\n");
-    printf("  -h : print header\n");
-    printf("  -d : print dic in hex\n");
-    printf("  -l : print dic word list\n");
+    printf("usage: %s [-a|-d|-h|-l] dictionnaire\n", iName.c_str());
+    printf("  -a: print all\n");
+    printf("  -h: print header\n");
+    printf("  -d: print dic in hex\n");
+    printf("  -l: print dic word list\n");
 }
 
 

Index: header.cpp
===================================================================
RCS file: header.cpp
diff -N header.cpp
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ header.cpp  24 Dec 2006 20:49:42 -0000      1.1.2.1
@@ -0,0 +1,144 @@
+/* Eliot                                                                     */
+/* Copyright (C) 2006  Olivier Teuliere                                      */
+/*                                                                           */
+/* This file is part of Eliot.                                               */
+/*                                                                           */
+/* Eliot is free software; you can redistribute it and/or modify             */
+/* 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.                                       */
+/*                                                                           */
+/* Eliot is distributed in the hope that it will be useful,                  */
+/* 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               */
+/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
*/
+
+#include <string>
+
+#include "header.h"
+
+
+/**
+ * Keyword included in dictionary headers
+ * Implies little endian storage on words
+ */
+#define _COMPIL_KEYWORD_ "_COMPILED_DICTIONARY_"
+
+/**
+ * Old format of the header
+ */
+typedef struct _Dict_header_old
+{
+    char ident[sizeof(_COMPIL_KEYWORD_)];
+    char unused_1;
+    char unused_2;
+    int root;
+    int nwords;
+    unsigned int edgesused;
+    unsigned int nodesused;
+    unsigned int nodessaved;
+    unsigned int edgessaved;
+} Dict_header_old;
+
+
+Header::Header()
+{
+}
+
+
+Header::Header(const Dict_header_info &iInfo)
+{
+    m_root = iInfo.root;
+    m_nbWords = iInfo.nwords;
+    m_nodesUsed = iInfo.nodesused;
+    m_nodesSaved = iInfo.nodessaved;
+    m_edgesUsed = iInfo.edgesused;
+    m_edgesSaved = iInfo.edgessaved;
+}
+
+
+Header::~Header()
+{
+}
+
+
+bool Header::read(FILE *iFile)
+{
+    Dict_header_old aHeader;
+    if (fread(&aHeader, sizeof(Dict_header_old), 1, iFile) != 1)
+        return false;
+
+#if defined(WORDS_BIGENDIAN)
+    aHeader.root       = swap4(aHeader.root);
+    aHeader.nwords     = swap4(aHeader.nwords);
+    aHeader.nodesused  = swap4(aHeader.nodesused);
+    aHeader.edgesused  = swap4(aHeader.edgesused);
+    aHeader.nodessaved = swap4(aHeader.nodessaved);
+    aHeader.edgessaved = swap4(aHeader.edgessaved);
+#endif
+
+    m_root = aHeader.root;
+    m_nbWords = aHeader.nwords;
+    m_nodesUsed = aHeader.nodesused;
+    m_edgesUsed = aHeader.edgesused;
+    m_nodesSaved = aHeader.nodessaved;
+    m_edgesSaved = aHeader.edgessaved;
+    return true;
+}
+
+
+bool Header::write(FILE *oFile) const
+{
+    Dict_header_old aHeader;
+    strcpy(aHeader.ident, _COMPIL_KEYWORD_);
+    aHeader.unused_1 = 0;
+    aHeader.unused_2 = 0;
+    aHeader.root = m_root;
+    aHeader.nwords = m_nbWords;
+    aHeader.nodesused = m_nodesUsed;
+    aHeader.edgesused = m_edgesUsed;
+    aHeader.nodessaved = m_nodesSaved;
+    aHeader.edgessaved = m_edgesSaved;
+
+    return fwrite(&aHeader, sizeof(Dict_header_old), 1, oFile) == 1;
+}
+
+
+void Header::print() const
+{
+    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_old));
+    printf("\n");
+    printf("%d words\n", m_nbWords);
+    printf("\n");
+    printf("root: %7d (edge)\n", m_root);
+//     printf("root: %7u (byte)\n", m_root * sizeof(Dawg_edge));
+    printf("\n");
+    printf("nodes: %d+%d\n", m_nodesUsed, m_nodesSaved);
+    printf("edges: %d+%d\n", m_edgesUsed, m_edgesSaved);
+    printf("============================\n");
+
+#if 0
+#define OO(IDENT) offsetof(Dict_header_old, IDENT)
+
+    printf("Dictionary header information\n");
+    printf("0x%02x ident      : %s\n",       OO(ident)     , _COMPIL_KEYWORD_);
+    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)      , m_root      , 
m_root);
+    printf("0x%02x words      : %6d %06x\n", OO(nwords)    , m_nbWords   , 
m_nbWords);
+    printf("0x%02x edges used : %6d %06x\n", OO(edgesused) , m_edgesUsed , 
m_edgesUsed);
+    printf("0x%02x nodes used : %6d %06x\n", OO(nodesused) , m_nodesUsed , 
m_nodesUsed);
+    printf("0x%02x edges saved: %6d %06x\n", OO(edgessaved), m_edgesSaved, 
m_edgesSaved);
+    printf("0x%02x nodes saved: %6d %06x\n", OO(nodessaved), m_nodesSaved, 
m_nodesSaved);
+    printf("\n");
+    printf("sizeof(header) = 0x%x (%u)\n", sizeof(header), sizeof(header));
+#endif
+}
+

Index: header.h
===================================================================
RCS file: header.h
diff -N header.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ header.h    24 Dec 2006 20:49:42 -0000      1.1.2.1
@@ -0,0 +1,116 @@
+/* Eliot                                                                     */
+/* Copyright (C) 2006  Olivier Teuliere                                      */
+/*                                                                           */
+/* This file is part of Eliot.                                               */
+/*                                                                           */
+/* Eliot is free software; you can redistribute it and/or modify             */
+/* 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.                                       */
+/*                                                                           */
+/* Eliot is distributed in the hope that it will be useful,                  */
+/* 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               */
+/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
*/
+
+#ifndef _HEADER_H
+#define _HEADER_H
+
+#include <stdio.h>
+
+using namespace std;
+
+
+/**
+ * Structure used to create a Header object.
+ * Note: this structure doesn't pretend to map the way the data is stored in a
+ * file. For (de)serialization, always use a Header object
+ */
+struct Dict_header_info
+{
+    int root;
+    int nwords;
+    unsigned int edgesused;
+    unsigned int nodesused;
+    unsigned int nodessaved;
+    unsigned int edgessaved;
+};
+
+
+/**
+ * Dictionary header.
+ * There are 2 ways to create a Header object:
+ *  - fill a Dict_header_info structure and give it in the constructor of the 
Header class
+ *  - use the default constructor, then call read() on a dictionary file
+ *
+ * Serialization:
+ * Several formats of headers will be handled by this class, even though only
+ * the first one is handled at the moment. You can use the write() method to
+ * write the latest version of the header into a given file. The read() method
+ * can be used to read any header format.
+ */
+class Header
+{
+public:
+    /// Default constructor (usually used to call read() afterwards)
+    Header();
+
+    /// Constructor from a filled structure
+    Header(const Dict_header_info &iInfo);
+
+    /// Destructor
+    ~Header();
+
+    /// Getters
+    //@{
+    unsigned int getRoot()         const { return m_root; }
+    unsigned int getNbWords()      const { return m_nbWords; }
+    unsigned int getNbNodesUsed()  const { return m_nodesUsed; }
+    unsigned int getNbEdgesUsed()  const { return m_edgesUsed; }
+    unsigned int getNbNodesSaved() const { return m_nodesSaved; }
+    unsigned int getNbEdgesSaved() const { return m_edgesSaved; }
+    //@}
+
+    /**
+     * Print a readable summary of the header on standard output
+     */
+    void print() const;
+
+    /**
+     * Load the header from a file
+     * @param iFile: File where to read the header. The file is supposed to be
+     *               already opened for reading
+     * @return true if the header was read correctly, false otherwise
+     */
+    bool read(FILE *iFile);
+
+    /**
+     * Write the header to a file, using the latest format
+     * @param oFile: File where to write the header. The file is supposed to be
+     *               already opened for writing
+     * @return true if the header was written correctly, false otherwise
+     */
+    bool write(FILE *oFile) const;
+
+private:
+    unsigned int m_root;
+    unsigned int m_nbWords;
+    unsigned int m_nodesUsed;
+    unsigned int m_edgesUsed;
+    unsigned int m_nodesSaved;
+    unsigned int m_edgesSaved;
+};
+
+
+#endif /* _HEADER_H */
+
+/// Local Variables:
+/// mode: c++
+/// mode: hs-minor
+/// c-basic-offset: 4
+/// indent-tabs-mode: nil
+/// End:




reply via email to

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