eliot-dev
[Top][All Lists]
Advanced

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

[Eliot-dev] eliot configure.in dic/compdic.cpp dic/dic.cpp ... [cppdic]


From: eliot-dev
Subject: [Eliot-dev] eliot configure.in dic/compdic.cpp dic/dic.cpp ... [cppdic]
Date: Mon, 03 Dec 2007 17:27:34 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Branch:         cppdic
Changes by:     Olivier Teulière <ipkiss>      07/12/03 17:27:34

Modified files:
        .              : configure.in 
        dic            : compdic.cpp dic.cpp dic.h dic_internals.h 
                         dic_search.cpp header.h listdic.cpp regexp.h 
        utils          : Makefile.am ncurses.cpp ncurses.h 

Log message:
         - Link the ncurses interface with libncursesw, not libncurses
         - Fixed several wide-characters-related problems in the ncurses 
interface
         - It should now be possible to use the dictionary on big endian 
machines
         - Quick and dirty support for 63 letters in the dictionary instead of 
31. To make it less dirty, a better encapsulation of the Dawg_edge struct is 
needed.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/configure.in?cvsroot=eliot&only_with_tag=cppdic&r1=1.19.2.6&r2=1.19.2.7
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/compdic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.10&r2=1.1.2.11
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.8&r2=1.1.2.9
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.13.2.5&r2=1.13.2.6
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic_internals.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.7.2.5&r2=1.7.2.6
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic_search.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.3&r2=1.1.2.4
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/header.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.6&r2=1.1.2.7
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/listdic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.6&r2=1.1.2.7
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/regexp.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.12.2.1&r2=1.12.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/utils/Makefile.am?cvsroot=eliot&only_with_tag=cppdic&r1=1.9.4.3&r2=1.9.4.4
http://cvs.savannah.gnu.org/viewcvs/eliot/utils/ncurses.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.22.2.1&r2=1.22.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/utils/ncurses.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.6&r2=1.6.4.1

Patches:
Index: configure.in
===================================================================
RCS file: /cvsroot/eliot/eliot/configure.in,v
retrieving revision 1.19.2.6
retrieving revision 1.19.2.7
diff -u -b -r1.19.2.6 -r1.19.2.7
--- configure.in        30 Nov 2007 17:53:46 -0000      1.19.2.6
+++ configure.in        3 Dec 2007 17:27:32 -0000       1.19.2.7
@@ -139,8 +139,8 @@
 AC_ARG_ENABLE([ncurses],AC_HELP_STRING([--enable-ncurses],[ncurses interface 
support (default disabled)]))
 if test "${enable_ncurses}" = "yes"
 then
-  AC_CHECK_HEADERS(ncurses.h, want_ncurses=1,
-    [AC_MSG_ERROR([Could not find the ncurses library on your system])])
+  AC_CHECK_HEADERS(ncursesw/curses.h, want_ncurses=1,
+    [AC_MSG_ERROR([Could not find the ncursesw library on your system])])
 fi
 AM_CONDITIONAL([BUILD_NCURSES], [test "${want_ncurses}" = "1"])
 

Index: dic/compdic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/compdic.cpp,v
retrieving revision 1.1.2.10
retrieving revision 1.1.2.11
diff -u -b -r1.1.2.10 -r1.1.2.11
--- dic/compdic.cpp     29 Nov 2007 19:09:56 -0000      1.1.2.10
+++ dic/compdic.cpp     3 Dec 2007 17:27:33 -0000       1.1.2.11
@@ -24,6 +24,8 @@
  *  \date   1999
  */
 
+#include "config.h"
+
 #include <fstream>
 #include <sstream>
 #include <iostream>
@@ -40,7 +42,18 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#include "config.h"
+// For ntohl & Co.
+#ifdef WIN32
+#   include <winsock2.h>
+#else
+#    if HAVE_NETINET_IN_H
+#       include <netinet/in.h>
+#    endif
+#    if HAVE_ARPA_INET_H
+#       include <arpa/inet.h>
+#    endif
+#endif
+
 #include "hashtable.h"
 #include "encoding.h"
 #include "header.h"
@@ -202,21 +215,28 @@
 }
 
 
-void write_node(const Dawg_edge *edges, int num, ostream &outfile)
+// Change endianness of the pointes edges, and write them to the given ostream
+void write_node(uint32_t *ioEdges, unsigned int num, ostream &outfile)
 {
+    // Handle endianness
+    for (unsigned int i = 0; i < num; ++i)
+    {
+        ioEdges[i] = htonl(ioEdges[i]);
+    }
+
 #ifdef DEBUG_OUTPUT
     printf("writing %d edges\n", num);
     for (int i = 0; i < num; i++)
     {
 #ifdef DEBUG_OUTPUT_L2
-        printf("ptr=%2d t=%d l=%d f=%d chr=%2d (%c)\n",
-               edges[i].ptr, edges[i].term, edges[i].last,
-               edges[i].fill, edges[i].chr, edges[i].chr -1 +'a');
+        printf("ptr=%2d t=%d l=%d chr=%2d (%c)\n",
+               ioEdges[i].ptr, ioEdges[i].term, ioEdges[i].last,
+               ioEdges[i].chr, ioEdges[i].chr -1 +'a');
 #endif
-        outfile.write((char*)(edges + i), sizeof(Dawg_edge));
+        outfile.write((char*)(ioEdges + i), sizeof(Dawg_edge));
     }
 #else
-    outfile.write((char*)edges, num * sizeof(Dawg_edge));
+    outfile.write((char*)ioEdges, num * sizeof(Dawg_edge));
 #endif
 }
 
@@ -312,7 +332,6 @@
         newEdge.ptr  = 0;
         newEdge.term = 0;
         newEdge.last = 0;
-        newEdge.fill = 0;
         newEdge.chr = iHeader.getCodeFromChar(*global_endstring++ = 
*global_input++);
         edges.push_back(newEdge);
 
@@ -365,7 +384,8 @@
         global_hashtable->add(edges, ioHeaderInfo.edgesused);
         ioHeaderInfo.edgesused += numedges;
         ioHeaderInfo.nodesused++;
-        write_node(&edges.front(), numedges, outfile);
+        write_node(reinterpret_cast<uint32_t*>(&edges.front()),
+                   numedges, outfile);
 
         return node_pos;
     }
@@ -492,20 +512,25 @@
         headerInfo.dawg = true;
         Header tempHeader = skip_init_header(outfile, headerInfo);
 
-        Dawg_edge specialnode = {0, 0, 0, 0, 0};
+        Dawg_edge specialnode = {0, 0, 0, 0};
         specialnode.last = 1;
-        write_node(&specialnode, 1, outfile);
+        // Temporary variable to avoid a warning when compiling with -O2
+        // (there is no warning with -O0... g++ bug?)
+        Dawg_edge *tmpPtr = &specialnode;
+        write_node(reinterpret_cast<uint32_t*>(tmpPtr), 1, outfile);
 
         /*
          * Call makenode with null (relative to stringbuf) prefix;
          * Initialize string to null; Put index of start node on output
          */
-        Dawg_edge rootnode = {0, 0, 0, 0, 0};
+        Dawg_edge rootnode = {0, 0, 0, 0};
         global_endstring = global_stringbuf;
         clock_t startBuildTime = clock();
         rootnode.ptr = makenode(global_endstring, outfile, headerInfo, 
tempHeader);
         clock_t endBuildTime = clock();
-        write_node(&rootnode, 1, outfile);
+        // Reuse the temporary variable
+        tmpPtr = &rootnode;
+        write_node(reinterpret_cast<uint32_t*>(tmpPtr), 1, outfile);
 
         fix_header(outfile, headerInfo);
 

Index: dic/dic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/dic.cpp,v
retrieving revision 1.1.2.8
retrieving revision 1.1.2.9
diff -u -b -r1.1.2.8 -r1.1.2.9
--- dic/dic.cpp 27 Nov 2007 18:38:29 -0000      1.1.2.8
+++ dic/dic.cpp 3 Dec 2007 17:27:33 -0000       1.1.2.9
@@ -24,13 +24,26 @@
  *  \date   2002
  */
 
+#include "config.h"
+
 #include <fstream>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
 
-#include "config.h"
+// For ntohl & Co.
+#ifdef WIN32
+#   include <winsock2.h>
+#else
+#    if HAVE_NETINET_IN_H
+#       include <netinet/in.h>
+#    endif
+#    if HAVE_ARPA_INET_H
+#       include <arpa/inet.h>
+#    endif
+#endif
+
 #include "dic.h"
 #include "header.h"
 #include "dic_exception.h"
@@ -38,7 +51,7 @@
 #include "tile.h"
 
 
-// FIXME: duplicated in header.cpp
+// Note: duplicated in header.cpp
 #if defined(WORDS_BIGENDIAN)
 static uint32_t swap4(uint32_t v)
 {
@@ -58,13 +71,22 @@
 
 void Dictionary::convertDataToArch()
 {
+    if (m_header->getVersion() == 0)
+    {
 #if defined(WORDS_BIGENDIAN)
-    uint32_t *p = (uint32_t*)m_dawg;
-    for (int i = 0; i < (m_header->getNbEdgesUsed() + 1); i++)
+        for (unsigned int i = 0; i < (m_header->getNbEdgesUsed() + 1); i++)
     {
-        p[i] = swap4(p[i]);
+            m_dawg[i] = swap4(m_dawg[i]);
     }
 #endif
+    }
+    else
+    {
+        for (unsigned int i = 0; i < (m_header->getNbEdgesUsed() + 1); i++)
+        {
+            m_dawg[i] = ntohl(m_dawg[i]);
+        }
+    }
 }
 
 
@@ -78,9 +100,9 @@
 
     // XXX: we should protect these allocations with auto_ptr
     m_header = new Header(file);
-    m_dawg = new Dawg_edge[m_header->getNbEdgesUsed() + 1];
+    m_dawg = new uint32_t[m_header->getNbEdgesUsed() + 1];
 
-    streamsize toRead = (m_header->getNbEdgesUsed() + 1) * sizeof(Dawg_edge);
+    streamsize toRead = (m_header->getNbEdgesUsed() + 1) * sizeof(uint32_t);
     file.read((char*)m_dawg, toRead);
     if (file.gcount() != toRead)
     {
@@ -89,6 +111,7 @@
         throw DicException("Problem reading dictionary arcs");
     }
 
+    // Handle endianness
     convertDataToArch();
 
     initializeTiles();
@@ -127,7 +150,10 @@
 
 const dic_elt_t Dictionary::getSucc(const dic_elt_t &e) const
 {
-    return (m_dawg[e]).ptr;
+    if (m_header->getVersion() == 0)
+        return reinterpret_cast<const Dawg_edge_old*>(m_dawg + e)->ptr;
+    else
+        return reinterpret_cast<const Dawg_edge*>(m_dawg + e)->ptr;
 }
 
 
@@ -139,25 +165,34 @@
 
 const dic_code_t Dictionary::getCode(const dic_elt_t &e) const
 {
-    return (dic_code_t)(m_dawg[e]).chr;
+    if (m_header->getVersion() == 0)
+        return reinterpret_cast<const Dawg_edge_old*>(m_dawg + e)->chr;
+    else
+        return reinterpret_cast<const Dawg_edge*>(m_dawg + e)->chr;
 }
 
 
 wchar_t Dictionary::getChar(const dic_elt_t &e) const
 {
-    return m_header->getCharFromCode(m_dawg[e].chr);
+    return m_header->getCharFromCode(getCode(e));
 }
 
 
 bool Dictionary::isLast(const dic_elt_t &e) const
 {
-    return (m_dawg[e]).last;
+    if (m_header->getVersion() == 0)
+        return reinterpret_cast<const Dawg_edge_old*>(m_dawg + e)->last;
+    else
+        return reinterpret_cast<const Dawg_edge*>(m_dawg + e)->last;
 }
 
 
 bool Dictionary::isEndOfWord(const dic_elt_t &e) const
 {
-    return (m_dawg[e]).term;
+    if (m_header->getVersion() == 0)
+        return reinterpret_cast<const Dawg_edge_old*>(m_dawg + e)->term;
+    else
+        return reinterpret_cast<const Dawg_edge*>(m_dawg + e)->term;
 }
 
 unsigned int Dictionary::lookup(const dic_elt_t &root, const dic_code_t *s) 
const

Index: dic/dic.h
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/dic.h,v
retrieving revision 1.13.2.5
retrieving revision 1.13.2.6
diff -u -b -r1.13.2.5 -r1.13.2.6
--- dic/dic.h   27 Nov 2007 18:01:04 -0000      1.13.2.5
+++ dic/dic.h   3 Dec 2007 17:27:33 -0000       1.13.2.6
@@ -36,17 +36,11 @@
 
 
 /**
- * different letters in the dictionary
- */
-#define DIC_LETTERS  27
-
-/**
  * max length of words (including last \0)
  */
 #define DIC_WORD_MAX 16
 
 class Header;
-class Dawg_edge;
 typedef unsigned int dic_elt_t;
 typedef unsigned char dic_code_t;
 
@@ -140,7 +134,7 @@
     unsigned int charLookup(const dic_elt_t &iRoot, const wchar_t *iPattern) 
const;
 
     /// Getter for the dawg
-    const Dawg_edge *getDawg() const { return m_dawg; }
+    const uint32_t *getDawg() const { return m_dawg; }
 
 private:
     // Prevent from copying the dictionary!
@@ -148,7 +142,7 @@
     Dictionary(const Dictionary&);
 
     Header *m_header;
-    Dawg_edge *m_dawg;
+    uint32_t *m_dawg;
 
     /// Vector of available tiles
     vector<Tile> m_tilesVect;

Index: dic/dic_internals.h
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/dic_internals.h,v
retrieving revision 1.7.2.5
retrieving revision 1.7.2.6
diff -u -b -r1.7.2.5 -r1.7.2.6
--- dic/dic_internals.h 29 Nov 2007 19:09:56 -0000      1.7.2.5
+++ dic/dic_internals.h 3 Dec 2007 17:27:33 -0000       1.7.2.6
@@ -44,7 +44,7 @@
  *  ----------------
  */
 
-struct __attribute__ ((packed)) Dawg_edge
+struct __attribute__ ((packed)) Dawg_edge_old
 {
     public:
       uint32_t
@@ -53,6 +53,21 @@
         last:  1,
         fill:  1,
         chr :  5;
+      bool operator==(const Dawg_edge_old &iOther) const
+      {
+          return memcmp(this, &iOther, sizeof(*this)) == 0;
+      }
+};
+
+
+struct __attribute__ ((packed)) Dawg_edge
+{
+    public:
+      uint32_t
+        ptr : 24,
+        term:  1,
+        last:  1,
+        chr :  6;
       bool operator==(const Dawg_edge &iOther) const
       {
           return memcmp(this, &iOther, sizeof(*this)) == 0;

Index: dic/dic_search.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/dic_search.cpp,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -b -r1.1.2.3 -r1.1.2.4
--- dic/dic_search.cpp  27 Nov 2007 18:01:04 -0000      1.1.2.3
+++ dic/dic_search.cpp  3 Dec 2007 17:27:33 -0000       1.1.2.4
@@ -55,17 +55,18 @@
  * @param s: current pointer to letters
  * @param eptr: current edge in the dawg
  */
-static const Dawg_edge* Dic_seek_edgeptr(const Dictionary &iDic, const 
wchar_t* s, const Dawg_edge *eptr)
+template <typename DAWG_EDGE>
+static const DAWG_EDGE* Dic_seek_edgeptr(const Dictionary &iDic, const 
wchar_t* s, const DAWG_EDGE *eptr)
 {
     if (*s)
     {
-        const Dawg_edge *p = iDic.getDawg() + eptr->ptr;
+        const DAWG_EDGE *p = reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg() 
+ eptr->ptr);
         do
         {
             if (p->chr == iDic.getHeader().getCodeFromChar(*s))
                 return Dic_seek_edgeptr(iDic, s + 1, p);
         } while (!(*p++).last);
-        return iDic.getDawg();
+        return reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg());
     }
     else
         return eptr;
@@ -78,14 +79,29 @@
  * @param iWord: word to lookup
  * @result 0 not a valid word, 1 ok
  */
-int DicSearch::searchWord(const Dictionary &iDic, const wstring &iWord)
+template <typename DAWG_EDGE>
+static int searchWordTempl(const Dictionary &iDic, const wstring &iWord)
 {
-    const Dawg_edge *e = Dic_seek_edgeptr(iDic, iWord.c_str(), iDic.getDawg() 
+ iDic.getRoot());
+    const DAWG_EDGE *e =
+        Dic_seek_edgeptr(iDic, iWord.c_str(), reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + iDic.getRoot()));
     return e->term;
 }
 
 
 /**
+ * XXX: Wrapper around the template method, until the dictionary
+ * completely hides Dawg_edge
+ */
+int DicSearch::searchWord(const Dictionary &iDic, const wstring &iWord)
+{
+    if (iDic.getHeader().getVersion() == 0)
+        return searchWordTempl<Dawg_edge_old>(iDic, iWord);
+    else
+        return searchWordTempl<Dawg_edge>(iDic, iWord);
+}
+
+
+/**
  * global variables for Dic_search_word_by_len:
  *
  * a pointer to the structure is passed as a parameter
@@ -103,9 +119,9 @@
     wchar_t search_letters[DIC_LETTERS];
 };
 
-static void
-Dic_search_word_by_len(const Dictionary &iDic, struct params_7plus1_t *params,
-                       int i, const Dawg_edge *edgeptr)
+template <typename DAWG_EDGE>
+static void Dic_search_word_by_len(const Dictionary &iDic, struct 
params_7plus1_t *params,
+                                   int i, const DAWG_EDGE *edgeptr)
 {
     /* depth first search in the dictionary */
     do
@@ -127,7 +143,8 @@
                 }
                 else
                 {
-                    Dic_search_word_by_len(iDic, params, i + 1, iDic.getDawg() 
+ edgeptr->ptr);
+                    Dic_search_word_by_len(iDic, params, i + 1,
+                                           reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + edgeptr->ptr));
                 }
                 params->search_letters[edgeptr->chr] ++;
                 params->search_wordtst[i] = L'\0';
@@ -147,7 +164,8 @@
                 }
                 else
                 {
-                    Dic_search_word_by_len(iDic, params, i + 1, iDic.getDawg() 
+ edgeptr->ptr);
+                    Dic_search_word_by_len(iDic, params, i + 1,
+                                           reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + edgeptr->ptr));
                 }
                 params->search_letters[0] ++;
                 params->search_wordtst[i] = L'\0';
@@ -157,11 +175,8 @@
 }
 
 
-/**
- * Wrapper around Dic_search_7pl1_inner, until we have multibyte support in
- * the dictionary
- */
-void DicSearch::search7pl1(const Dictionary &iDic, const wstring &iRack,
+template <typename DAWG_EDGE>
+static void search7pl1Templ(const Dictionary &iDic, const wstring &iRack,
                            map<wchar_t, list<wstring> > &oWordList,
                            bool joker)
 {
@@ -203,8 +218,9 @@
     if (wordlen < 1)
         return;
 
-    const Dawg_edge *root_edge =
-        iDic.getDawg() + (iDic.getDawg()[iDic.getRoot()].ptr);
+    const DAWG_EDGE *root_edge =
+        reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg() +
+                                           reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + iDic.getRoot())->ptr);
 
     params.results = &oWordList;
 
@@ -230,10 +246,26 @@
     }
 }
 
+
+/**
+ * XXX: Wrapper around the template method, until the dictionary
+ * completely hides Dawg_edge
+ */
+void DicSearch::search7pl1(const Dictionary &iDic, const wstring &iRack,
+                           map<wchar_t, list<wstring> > &oWordList,
+                           bool joker)
+{
+    if (iDic.getHeader().getVersion() == 0)
+        search7pl1Templ<Dawg_edge_old>(iDic, iRack, oWordList, joker);
+    else
+        search7pl1Templ<Dawg_edge>(iDic, iRack, oWordList, joker);
+}
+
 /****************************************/
 /****************************************/
 
-void DicSearch::searchRacc(const Dictionary &iDic, const wstring &iWord,
+template <typename DAWG_EDGE>
+static void searchRaccTempl(const Dictionary &iDic, const wstring &iWord,
                            list<wstring> &oWordList)
 {
     if (iWord == L"")
@@ -248,7 +280,7 @@
     for (unsigned int i = 0; i <= letters.size(); i++)
     {
         wordtst[0] = letters[i];
-        if (searchWord(iDic, wordtst))
+        if (searchWordTempl<DAWG_EDGE>(iDic, wordtst))
             oWordList.push_back(wordtst);
     }
 
@@ -260,12 +292,13 @@
     wordtst[i  ] = '\0';
     wordtst[i+1] = '\0';
 
-    const Dawg_edge *edge_seek = Dic_seek_edgeptr(iDic, iWord.c_str(), 
iDic.getDawg() + iDic.getRoot());
+    const DAWG_EDGE *edge_seek = Dic_seek_edgeptr(iDic, iWord.c_str(),
+                                                  reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + iDic.getRoot()));
 
     /* points to what the next letter can be */
-    const Dawg_edge *edge = iDic.getDawg() + edge_seek->ptr;
+    const DAWG_EDGE *edge = reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg() 
+ edge_seek->ptr);
 
-    if (edge != iDic.getDawg())
+    if (edge != reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg()))
     {
         do
         {
@@ -278,10 +311,25 @@
     }
 }
 
+
+/**
+ * XXX: Wrapper around the template method, until the dictionary
+ * completely hides Dawg_edge
+ */
+void DicSearch::searchRacc(const Dictionary &iDic, const wstring &iWord,
+                           list<wstring> &oWordList)
+{
+    if (iDic.getHeader().getVersion() == 0)
+        searchRaccTempl<Dawg_edge_old>(iDic, iWord, oWordList);
+    else
+        searchRaccTempl<Dawg_edge>(iDic, iWord, oWordList);
+}
+
 /****************************************/
 /****************************************/
 
-void DicSearch::searchBenj(const Dictionary &iDic, const wstring &iWord,
+template <typename DAWG_EDGE>
+static void searchBenjTempl(const Dictionary &iDic, const wstring &iWord,
                            list<wstring> &oWordList)
 {
     if (iWord == L"")
@@ -289,16 +337,17 @@
 
     wchar_t wordtst[DIC_WORD_MAX];
     wcscpy(wordtst + 3, iWord.c_str());
-    const Dawg_edge *edge0, *edge1, *edge2, *edgetst;
-    edge0 = iDic.getDawg() + (iDic.getDawg()[iDic.getRoot()].ptr);
+    const DAWG_EDGE *edge0, *edge1, *edge2, *edgetst;
+    edge0 = reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg() +
+                                               reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + iDic.getRoot())->ptr);
     do
     {
         wordtst[0] = iDic.getHeader().getCharFromCode(edge0->chr);
-        edge1 = iDic.getDawg() + edge0->ptr;
+        edge1 = reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg() + 
edge0->ptr);
         do
         {
             wordtst[1] = iDic.getHeader().getCharFromCode(edge1->chr);
-            edge2 = iDic.getDawg() + edge1->ptr;
+            edge2 = reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg() + 
edge1->ptr);
             do
             {
                 wordtst[2] = iDic.getHeader().getCharFromCode(edge2->chr);
@@ -311,6 +360,19 @@
 }
 
 
+/**
+ * XXX: Wrapper around the template method, until the dictionary
+ * completely hides Dawg_edge
+ */
+void DicSearch::searchBenj(const Dictionary &iDic, const wstring &iWord,
+                           list<wstring> &oWordList)
+{
+    if (iDic.getHeader().getVersion() == 0)
+        searchBenjTempl<Dawg_edge_old>(iDic, iWord, oWordList);
+    else
+        searchBenjTempl<Dawg_edge>(iDic, iWord, oWordList);
+}
+
 /****************************************/
 /****************************************/
 
@@ -321,12 +383,13 @@
 };
 
 
+template <typename DAWG_EDGE>
 static void Dic_search_cross_rec(const Dictionary &iDic,
                                  struct params_cross_t *params,
                                  list<wstring> &oWordList,
-                                 const Dawg_edge *edgeptr)
+                                 const DAWG_EDGE *edgeptr)
 {
-    const Dawg_edge *current = iDic.getDawg() + edgeptr->ptr;
+    const DAWG_EDGE *current = reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + edgeptr->ptr);
 
     if (params->mask[params->wordlen] == '\0' && edgeptr->term)
     {
@@ -380,7 +443,16 @@
     params.mask[i] = '\0';
 
     params.wordlen = 0;
-    Dic_search_cross_rec(iDic, &params, oWordList, iDic.getDawg() + 
iDic.getRoot());
+    if (iDic.getHeader().getVersion() == 0)
+    {
+        Dic_search_cross_rec(iDic, &params, oWordList,
+                             reinterpret_cast<const 
Dawg_edge_old*>(iDic.getDawg() + iDic.getRoot()));
+    }
+    else
+    {
+        Dic_search_cross_rec(iDic, &params, oWordList,
+                             reinterpret_cast<const Dawg_edge*>(iDic.getDawg() 
+ iDic.getRoot()));
+    }
 }
 
 /****************************************/
@@ -397,9 +469,11 @@
     int  wordlen;
 };
 
+
+template <typename DAWG_EDGE>
 static void Dic_search_regexp_rec(struct params_regexp_t *params,
                                   int state,
-                                  const Dawg_edge *edgeptr,
+                                  const DAWG_EDGE *edgeptr,
                                   list<string> &oWordList)
 {
     int next_state;
@@ -414,7 +488,7 @@
         }
     }
     /* we now drive the search by exploring the dictionary */
-    const Dawg_edge *current = params->dic->getDawg() + edgeptr->ptr;
+    const DAWG_EDGE *current = reinterpret_cast<const 
DAWG_EDGE*>(params->dic->getDawg() + edgeptr->ptr);
     do
     {
         /* the current letter is current->chr */
@@ -491,7 +565,16 @@
         params.charlist       = iList;
         memset(params.word, '\0', sizeof(params.word));
         params.wordlen        = 0;
-        Dic_search_regexp_rec(&params, a->getInitId(), iDic.getDawg() + 
iDic.getRoot(), oWordList);
+        if (iDic.getHeader().getVersion() == 0)
+        {
+            Dic_search_regexp_rec(&params, a->getInitId(),
+                                  reinterpret_cast<const 
Dawg_edge_old*>(iDic.getDawg() + iDic.getRoot()), oWordList);
+        }
+        else
+        {
+            Dic_search_regexp_rec(&params, a->getInitId(),
+                                  reinterpret_cast<const 
Dawg_edge*>(iDic.getDawg() + iDic.getRoot()), oWordList);
+        }
 
         delete a;
     }

Index: dic/header.h
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/header.h,v
retrieving revision 1.1.2.6
retrieving revision 1.1.2.7
diff -u -b -r1.1.2.6 -r1.1.2.7
--- dic/header.h        27 Nov 2007 18:01:05 -0000      1.1.2.6
+++ dic/header.h        3 Dec 2007 17:27:33 -0000       1.1.2.7
@@ -89,6 +89,9 @@
      */
     Header(const DictHeaderInfo &iInfo);
 
+    /// Return the version of the dictionary format
+    uint8_t getVersion() const { return m_version; }
+
     /// Getters
     //@{
     unsigned int getRoot()         const { return m_root; }

Index: dic/listdic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/listdic.cpp,v
retrieving revision 1.1.2.6
retrieving revision 1.1.2.7
diff -u -b -r1.1.2.6 -r1.1.2.7
--- dic/listdic.cpp     21 Nov 2007 16:25:44 -0000      1.1.2.6
+++ dic/listdic.cpp     3 Dec 2007 17:27:33 -0000       1.1.2.7
@@ -39,7 +39,8 @@
 using namespace std;
 
 
-static void print_dic_rec(ostream &out, const Dictionary &iDic, wchar_t *buf, 
wchar_t *s, Dawg_edge i)
+template <typename DAWG_EDGE>
+static void print_dic_rec(ostream &out, const Dictionary &iDic, wchar_t *buf, 
wchar_t *s, DAWG_EDGE i)
 {
     if (i.term)  /* edge points at a complete word */
     {
@@ -48,7 +49,7 @@
     }
     if (i.ptr)
     {           /* Compute index: is it non-zero ? */
-        const Dawg_edge *p = iDic.getDawg() + i.ptr;
+        const DAWG_EDGE *p = reinterpret_cast<const DAWG_EDGE*>(iDic.getDawg() 
+ i.ptr);
         do
         {                         /* for each edge out of this node */
             *s = iDic.getHeader().getCharFromCode(p->chr);
@@ -59,58 +60,44 @@
 }
 
 
-void print_dic_list(const string &iFileName)
+template <typename DAWG_EDGE>
+void print_dic_list(const Dictionary &iDic)
 {
     static wchar_t buf[80];
-    Dictionary dic(iFileName);
-    print_dic_rec(cout, dic, buf, buf, dic.getDawg()[dic.getRoot()]);
-}
-
-
-void print_header(const string &iFileName)
-{
-    ifstream file(iFileName.c_str(), ios::in | ios::binary);
-    if (!file.is_open())
-    {
-        cerr << "Couldn't open file: " << iFileName << endl;
-        exit(1);
-    }
-
-    Header header(file);
-    header.print();
+    print_dic_rec(cout, iDic, buf, buf, *reinterpret_cast<const 
DAWG_EDGE*>(iDic.getDawg() + iDic.getRoot()));
 }
 
 
+template <typename DAWG_EDGE>
 static void print_node_hex(const Dictionary &dic, int i)
 {
     union edge_t
     {
-        Dawg_edge e;
+        DAWG_EDGE e;
         uint32_t  s;
     } ee;
 
-    ee.e = dic.getDawg()[i];
+    ee.e = *reinterpret_cast<const DAWG_EDGE*>(dic.getDawg() + i);
 
-    printf("0x%04lx %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 chr=%2d (%c)\n",
            (unsigned long)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);
+           i, ee.e.ptr, ee.e.term, ee.e.last, ee.e.chr, ee.e.chr +'a' -1);
 }
 
 
-void print_dic_hex(const string &iFileName)
+template <typename DAWG_EDGE>
+void print_dic_hex(const Dictionary &iDic)
 {
-    Dictionary dic(iFileName);
-
     printf("offs binary       structure\n");
     printf("---- -------- |   ------------------\n");
-    for (unsigned int i = 0; i < (dic.getHeader().getNbEdgesUsed() + 1); i++)
-        print_node_hex(dic, i);
+    for (unsigned int i = 0; i < (iDic.getHeader().getNbEdgesUsed() + 1); i++)
+        print_node_hex<DAWG_EDGE>(iDic, i);
 }
 
 
 void usage(const string &iName)
 {
-    printf("usage: %s [-a|-d|-h|-l] dictionnaire\n", iName.c_str());
+    printf("usage: %s [-a|-h|-d|-l] dictionary\n", iName.c_str());
     printf("  -a: print all\n");
     printf("  -h: print header\n");
     printf("  -d: print dic in hex\n");
@@ -143,25 +130,32 @@
             case 'h': option_print_header = 1; break;
             case 'd': option_print_dic_hex = 1; break;
             case 'l': option_print_dic_list = 1; break;
-            default: usage(argv[0]); exit(2);
-                     break;
+            default: usage(argv[0]); exit(2); break;
         }
         arg_count++;
     }
 
     try
     {
+        Dictionary dic(argv[arg_count]);
+
         if (option_print_header || option_print_all)
         {
-            print_header(argv[arg_count]);
+            dic.getHeader().print();
         }
         if (option_print_dic_hex || option_print_all)
         {
-            print_dic_hex(argv[arg_count]);
+            if (dic.getHeader().getVersion() == 0)
+                print_dic_hex<Dawg_edge_old>(dic);
+            else
+                print_dic_hex<Dawg_edge>(dic);
         }
         if (option_print_dic_list || option_print_all)
         {
-            print_dic_list(argv[arg_count]);
+            if (dic.getHeader().getVersion() == 0)
+                print_dic_list<Dawg_edge_old>(dic);
+            else
+                print_dic_list<Dawg_edge>(dic);
         }
         return 0;
     }

Index: dic/regexp.h
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/regexp.h,v
retrieving revision 1.12.2.1
retrieving revision 1.12.2.2
diff -u -b -r1.12.2.1 -r1.12.2.2
--- dic/regexp.h        15 Oct 2006 11:07:55 -0000      1.12.2.1
+++ dic/regexp.h        3 Dec 2007 17:27:33 -0000       1.12.2.2
@@ -49,6 +49,11 @@
 } NODE;
 
     /**
+     * different letters in the dictionary
+     */
+#define DIC_LETTERS  27
+
+    /**
      * maximum number of accepted terminals in regular expressions
      */
 #define REGEXP_MAX 32

Index: utils/Makefile.am
===================================================================
RCS file: /cvsroot/eliot/eliot/utils/Makefile.am,v
retrieving revision 1.9.4.3
retrieving revision 1.9.4.4
diff -u -b -r1.9.4.3 -r1.9.4.4
--- utils/Makefile.am   30 Nov 2007 17:53:47 -0000      1.9.4.3
+++ utils/Makefile.am   3 Dec 2007 17:27:33 -0000       1.9.4.4
@@ -34,7 +34,7 @@
 if BUILD_NCURSES
 noinst_PROGRAMS += eliotcurses
 eliotcurses_SOURCES = ncurses.cpp ncurses.h
-eliotcurses_LDADD = ../game/libgame.a ../dic/libdic.a -lncurses @LIBINTL@
+eliotcurses_LDADD = ../game/libgame.a ../dic/libdic.a -lncursesw @LIBINTL@
 endif
 
 datadir = @datadir@

Index: utils/ncurses.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/utils/ncurses.cpp,v
retrieving revision 1.22.2.1
retrieving revision 1.22.2.2
diff -u -b -r1.22.2.1 -r1.22.2.2
--- utils/ncurses.cpp   15 Oct 2006 11:07:55 -0000      1.22.2.1
+++ utils/ncurses.cpp   3 Dec 2007 17:27:33 -0000       1.22.2.2
@@ -161,14 +161,20 @@
             mvwprintw(win, y + row + 1, x + 3 * col + 1, "   ");
 
             // Now add the letter
-            char c = m_game->getBoard().getChar(row, col);
+            wchar_t c = m_game->getBoard().getChar(row, col);
             if (c)
             {
-                if (islower(c))
-                    mvwaddch(win, y + row + 1, x + 3 * col + 2,
-                             c | A_BOLD | COLOR_PAIR(COLOR_GREEN));
+                cchar_t cc;
+                if (iswlower(c))
+                {
+                    setcchar(&cc, &c, A_BOLD, COLOR_GREEN, NULL);
+                    mvwadd_wch(win, y + row + 1, x + 3 * col + 2, &cc);
+                }
                 else
-                    mvwaddch(win, y + row + 1, x + 3 * col + 2, c);
+                {
+                    setcchar(&cc, &c, 0, 0, NULL);
+                    mvwadd_wch(win, y + row + 1, x + 3 * col + 2, &cc);
+                }
             }
             else
             {
@@ -203,9 +209,9 @@
     {
         if (m_game->getMode() != Game::kTRAINING && i == m_game->currPlayer())
             attron(A_BOLD);
-        string rack = 
convertToMb(m_game->getPlayer(i).getCurrentRack().toString(PlayedRack::RACK_SIMPLE));
+        wstring rack = 
m_game->getPlayer(i).getCurrentRack().toString(PlayedRack::RACK_SIMPLE);
         mvwprintw(win, y + yOff + i + 1, x + 2,
-                  _("Player %d: %s"), i, rack.c_str());
+                  _("Player %d: %ls"), i, rack.c_str());
         if (m_game->getMode() != Game::kTRAINING && i == m_game->currPlayer())
             attroff(A_BOLD);
         // Force to refresh the whole rack
@@ -277,12 +283,11 @@
     {
         const Turn& t = m_game->getHistory().getTurn(i);
         const Round& r = t.getRound();
-        string word = convertToMb(r.getWord());
         string coord = convertToMb(r.getCoord().toString());
         boxPrint(win, i + 2, x + 2,
-                 "%2d   %8s   %s%s   %3s   %3d   %1d   %c",
-                 i + 1, convertToMb(t.getPlayedRack().toString()).c_str(),
-                 word.c_str(), string(15 - word.size(), ' ').c_str(),
+                 "%2d   %8ls   %ls%s   %3s   %3d   %1d   %c",
+                 i + 1, t.getPlayedRack().toString().c_str(),
+                 r.getWord().c_str(), string(15 - r.getWordLen(), ' ').c_str(),
                  coord.c_str(), r.getPoints(),
                  t.getPlayer(), r.getBonus() ? '*' : ' ');
     }
@@ -360,11 +365,11 @@
     else
         xOff = l2 + 3;
 
-    string word, coord;
+    wstring word, coord;
     if (readString(win, y + 1, x + xOff, 15, word) &&
         readString(win, y + 2, x + xOff, 3, coord))
     {
-        int res = m_game->play(convertToWc(coord), convertToWc(word));
+        int res = m_game->play(coord, word);
         if (res)
         {
             drawStatus(win, LINES - 1, 0, _("Incorrect or misplaced word"));
@@ -381,15 +386,15 @@
     mvwprintw(win, y + 1, x + 2, _("Enter the word to check:"));
     wrefresh(win);
 
-    string word;
+    wstring word;
     if (readString(win, y + 2, x + 2, 15, word))
     {
-        int res = DicSearch::searchWord(m_game->getDic(), convertToWc(word));
+        int res = DicSearch::searchWord(m_game->getDic(), word);
         char s[100];
         if (res)
-            snprintf(s, 100, _("The word '%s' exists"), word.c_str());
+            snprintf(s, 100, _("The word '%ls' exists"), word.c_str());
         else
-            snprintf(s, 100, _("The word '%s' does not exist"), word.c_str());
+            snprintf(s, 100, _("The word '%ls' does not exist"), word.c_str());
         drawStatus(win, LINES - 1, 0, s);
     }
     m_state = DEFAULT;
@@ -403,21 +408,21 @@
     mvwprintw(win, y + 1, x + 2, _("Enter the file name:"));
     wrefresh(win);
 
-    string filename;
+    wstring filename;
     if (readString(win, y + 2, x + 2, 28, filename, kFILENAME))
     {
-        ofstream fout(filename.c_str());
+        ofstream fout(convertToMb(filename).c_str());
         char s[100];
         if (fout.rdstate() == ios::failbit)
         {
-            snprintf(s, 100, _("Cannot open file %s for writing"),
+            snprintf(s, 100, _("Cannot open file %ls for writing"),
                      filename.c_str());
         }
         else
         {
             m_game->save(fout);
             fout.close();
-            snprintf(s, 100, _("Game saved in %s"), filename.c_str());
+            snprintf(s, 100, _("Game saved in %ls"), filename.c_str());
         }
         drawStatus(win, LINES - 1, 0, s);
     }
@@ -432,14 +437,14 @@
     mvwprintw(win, y + 1, x + 2, _("Enter the file name:"));
     wrefresh(win);
 
-    string filename;
+    wstring filename;
     if (readString(win, y + 2, x + 2, 28, filename, kFILENAME))
     {
         char s[100];
         FILE *fin;
-        if ((fin = fopen(filename.c_str(), "r")) == NULL)
+        if ((fin = fopen(convertToMb(filename).c_str(), "r")) == NULL)
         {
-            snprintf(s, 100, _("Cannot open file %s for reading"),
+            snprintf(s, 100, _("Cannot open file %ls for reading"),
                      filename.c_str());
         }
         else
@@ -470,10 +475,10 @@
     mvwprintw(win, y + 1, x + 2, _("Enter the letters to change:"));
     wrefresh(win);
 
-    string letters;
+    wstring letters;
     if (readString(win, y + 2, x + 2, 7, letters))
     {
-        int res = iGame.pass(convertToWc(letters), m_game->currPlayer());
+        int res = iGame.pass(letters, m_game->currPlayer());
         if (res)
         {
             drawStatus(win, LINES - 1, 0, _("Cannot pass the turn"));
@@ -490,23 +495,23 @@
     mvwprintw(win, y + 1, x + 2, _("Enter the new letters:"));
     wrefresh(win);
 
-    string letters;
+    wstring letters;
     if (readString(win, y + 2, x + 2, 7, letters, kJOKER))
     {
-        iGame.setRackManual(false, convertToWc(letters));
+        iGame.setRackManual(false, letters);
     }
     m_state = DEFAULT;
     clearRect(win, y, x, 4, 32);
 }
 
 
-bool CursesIntf::readString(WINDOW *win, int y, int x, int n, string &oString,
+bool CursesIntf::readString(WINDOW *win, int y, int x, int n, wstring &oString,
                             unsigned int flag)
 {
-    int c;
+    wint_t c;
     wmove(win, y, x);
     curs_set(1);
-    while ((c = getch()) != 0)
+    while ((get_wch(&c)) == OK)
     {
         if (c == 0x1b )  // Esc
         {
@@ -531,32 +536,32 @@
             wmove(win, y, x);
             oString.erase(oString.size() - 1);
         }
-        else if (isalnum(c) && oString.size() < (unsigned int)n)
+        else if (iswalnum(c) && oString.size() < (unsigned int)n)
         {
-            mvwprintw(win, y, x, "%c", c);
+            mvwprintw(win, y, x, "%lc", c);
             x++;
-            oString += (char)c;
+            oString += c;
         }
         else
         {
-            if (flag & kJOKER && c == '?')
+            if (flag & kJOKER && c == L'?')
             {
-                mvwprintw(win, y, x, "%c", c);
+                mvwprintw(win, y, x, "%lc", c);
                 x++;
-                oString += (char)c;
+                oString += c;
             }
             if (flag & kFILENAME)
             {
-                if (c == '/' || c == '.' || c == '-' || c == '_' || c == ' ')
+                if (c == L'/' || c == L'.' || c == L'-' || c == L'_' || c == 
L' ')
                 {
-                    mvwprintw(win, y, x, "%c", c);
+                    mvwprintw(win, y, x, "%lc", c);
                     x++;
-                    oString += (char)c;
+                    oString += c;
                 }
             }
         }
 //         else
-//             mvwprintw(win, 0, 0, "%3d", c);
+//             mvwprintw(win, 0, 0, "%3lc", c);
     }
     curs_set(0);
     return 0;

Index: utils/ncurses.h
===================================================================
RCS file: /cvsroot/eliot/eliot/utils/ncurses.h,v
retrieving revision 1.6
retrieving revision 1.6.4.1
diff -u -b -r1.6 -r1.6.4.1
--- utils/ncurses.h     4 Nov 2005 20:00:06 -0000       1.6
+++ utils/ncurses.h     3 Dec 2007 17:27:34 -0000       1.6.4.1
@@ -20,7 +20,7 @@
 #ifndef _NCURSES_H_
 #define _NCURSES_H_
 
-#include <curses.h>
+#include <ncursesw/curses.h>
 #include <string>
 
 class Game;
@@ -29,6 +29,7 @@
 class FreeGame;
 
 using std::string;
+using std::wstring;
 
 
 /**
@@ -84,7 +85,7 @@
     // Get a string from the user, with a maximum length
     // The string is validated if the user presses Enter (return value: true)
     // and it is cancelled if the user presses Esc (return value: false)
-    bool readString(WINDOW *win, int y, int x, int n, string &oString,
+    bool readString(WINDOW *win, int y, int x, int n, wstring &oString,
                     unsigned int flag = 0);
     // Any combination of the following constants can be used as the "flag"
     // parameter of the readString() method.




reply via email to

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