eliot-dev
[Top][All Lists]
Advanced

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

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


From: eliot-dev
Subject: [Eliot-dev] eliot/dic compdic.cpp dic.cpp header.cpp header... [cppdic]
Date: Mon, 25 Dec 2006 17:37:58 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Branch:         cppdic
Changes by:     Olivier Teulière <ipkiss>      06/12/25 17:37:58

Modified files:
        dic            : compdic.cpp dic.cpp header.cpp header.h 
                         listdic.cpp 

Log message:
        Use streams instead of FILE pointers.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/compdic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.2&r2=1.1.2.3
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/dic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.2&r2=1.1.2.3
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/header.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.h?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.1&r2=1.1.2.2
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/listdic.cpp?cvsroot=eliot&only_with_tag=cppdic&r1=1.1.2.2&r2=1.1.2.3

Patches:
Index: compdic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/compdic.cpp,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -b -r1.1.2.2 -r1.1.2.3
--- compdic.cpp 24 Dec 2006 20:49:42 -0000      1.1.2.2
+++ compdic.cpp 25 Dec 2006 17:37:58 -0000      1.1.2.3
@@ -24,6 +24,7 @@
  *  \date   1999
  */
 
+#include <fstream>
 #include <time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -85,7 +86,7 @@
 }
 
 
-void skip_init_header(FILE* outfile, Dict_header_info *headerInfo)
+void skip_init_header(ostream &outfile, Dict_header_info *headerInfo)
 {
     headerInfo->root       = 0;
     headerInfo->nwords     = 0;
@@ -99,10 +100,11 @@
 }
 
 
-void fix_header(FILE* outfile, Dict_header_info* headerInfo)
+void fix_header(ostream &outfile, Dict_header_info* headerInfo)
 {
     headerInfo->root = headerInfo->edgesused;
-    rewind(outfile);
+    // Go back to the beginning of the stream to overwrite the header
+    outfile.seekp(0, ios::beg);
 #if defined(WORDS_BIGENDIAN)
 #warning "**********************************************"
 #warning "compdic does not run yet on bigendian machines"
@@ -114,7 +116,7 @@
 }
 
 
-void write_node(Dawg_edge *edges, int size, int num, FILE* outfile)
+void write_node(const Dawg_edge *edges, int size, int num, ostream &outfile)
 {
 #ifdef DEBUG_OUTPUT
     printf("writing %d edges\n", num);
@@ -125,10 +127,10 @@
                edges[i].ptr, edges[i].term, edges[i].last,
                edges[i].fill, edges[i].chr, edges[i].chr -1 +'a');
 #endif
-        fwrite(edges+i, sizeof(Dawg_edge), 1, outfile);
+        outfile.write((char*)(edges + i), sizeof(Dawg_edge));
     }
 #else
-    fwrite(edges, size, num, outfile);
+    outfile.write((char*)edges, num * size);
 #endif
 }
 
@@ -140,7 +142,7 @@
 /* ods4: 1746 */
 
 /* global variables */
-FILE*       global_outfile;
+ofstream    global_outfile;
 Dict_header_info global_header_info;
 Hash_table  global_hashtable;
 
@@ -262,7 +264,9 @@
 
     const char *outfilename = (argc == 3) ? argv[2] : "dict.daw";
 
-    if ((global_outfile = fopen(outfilename, "wb")) == NULL)
+    global_outfile.open(outfilename,
+                        ios_base::out | ios_base::binary | ios_base::trunc);
+    if (!global_outfile.is_open())
     {
         fprintf(stderr, "Cannot open output file %s\n", outfilename);
         exit(1);
@@ -303,7 +307,7 @@
 
     hash_destroy(global_hashtable);
     free(uncompressed);
-    fclose(global_outfile);
+    global_outfile.close();
 
     printf(" Elapsed time is                : %f s\n", 1.0 * (endtime - 
starttime) / CLOCKS_PER_SEC);
 #ifdef CHECK_RECURSION

Index: dic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/dic.cpp,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -b -r1.1.2.2 -r1.1.2.3
--- dic.cpp     24 Dec 2006 20:49:42 -0000      1.1.2.2
+++ dic.cpp     25 Dec 2006 17:37:58 -0000      1.1.2.3
@@ -24,6 +24,7 @@
  *  \date   2002
  */
 
+#include <fstream>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
@@ -78,9 +79,9 @@
 
 int Dictionary::load(const string &iPath)
 {
-    FILE *file;
+    ifstream file(iPath.c_str(), ios_base::in | ios_base::binary);
 
-    if ((file = fopen(iPath.c_str(), "rb")) == NULL)
+    if (!file.is_open())
         return 1;
 
     Header aHeader;
@@ -90,16 +91,15 @@
     m_dawg = new Dawg_edge[aHeader.getNbEdgesUsed() + 1];
     if (m_dawg == NULL)
     {
-        fclose(file);
         return 4;
     }
 
-    if (fread(m_dawg, sizeof(Dawg_edge), aHeader.getNbEdgesUsed() + 1, file) !=
-        (aHeader.getNbEdgesUsed() + 1))
+    unsigned int toRead = (aHeader.getNbEdgesUsed() + 1) * sizeof(Dawg_edge);
+    file.read((char*)m_dawg, toRead);
+    if (file.gcount() != toRead)
     {
         delete[] m_dawg;
         m_dawg = NULL;
-        fclose(file);
         return 5;
     }
 
@@ -110,7 +110,6 @@
 
     convertDataToArch();
 
-    fclose(file);
     return 0;
 }
 

Index: header.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/header.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
--- header.cpp  24 Dec 2006 20:49:42 -0000      1.1.2.1
+++ header.cpp  25 Dec 2006 17:37:58 -0000      1.1.2.2
@@ -66,10 +66,11 @@
 }
 
 
-bool Header::read(FILE *iFile)
+bool Header::read(istream &iStream)
 {
     Dict_header_old aHeader;
-    if (fread(&aHeader, sizeof(Dict_header_old), 1, iFile) != 1)
+    iStream.read((char*)&aHeader, sizeof(Dict_header_old));
+    if (iStream.gcount() != sizeof(Dict_header_old))
         return false;
 
 #if defined(WORDS_BIGENDIAN)
@@ -91,7 +92,7 @@
 }
 
 
-bool Header::write(FILE *oFile) const
+bool Header::write(ostream &oStream) const
 {
     Dict_header_old aHeader;
     strcpy(aHeader.ident, _COMPIL_KEYWORD_);
@@ -104,7 +105,8 @@
     aHeader.nodessaved = m_nodesSaved;
     aHeader.edgessaved = m_edgesSaved;
 
-    return fwrite(&aHeader, sizeof(Dict_header_old), 1, oFile) == 1;
+    oStream.write((char*)&aHeader, sizeof(Dict_header_old));
+    return true;
 }
 
 

Index: header.h
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/header.h,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -b -r1.1.2.1 -r1.1.2.2
--- header.h    24 Dec 2006 20:49:42 -0000      1.1.2.1
+++ header.h    25 Dec 2006 17:37:58 -0000      1.1.2.2
@@ -20,7 +20,7 @@
 #ifndef _HEADER_H
 #define _HEADER_H
 
-#include <stdio.h>
+#include <iostream>
 
 using namespace std;
 
@@ -82,19 +82,17 @@
 
     /**
      * Load the header from a file
-     * @param iFile: File where to read the header. The file is supposed to be
-     *               already opened for reading
+     * @param iStream: Input stream where to read the header
      * @return true if the header was read correctly, false otherwise
      */
-    bool read(FILE *iFile);
+    bool read(istream &iStream);
 
     /**
      * 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
+     * @param oStream: Output stream where to write the header
      * @return true if the header was written correctly, false otherwise
      */
-    bool write(FILE *oFile) const;
+    bool write(ostream &oStream) const;
 
 private:
     unsigned int m_root;

Index: listdic.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/Attic/listdic.cpp,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -b -r1.1.2.2 -r1.1.2.3
--- listdic.cpp 24 Dec 2006 20:49:42 -0000      1.1.2.2
+++ listdic.cpp 25 Dec 2006 17:37:58 -0000      1.1.2.3
@@ -24,6 +24,7 @@
  *  \date   1999
  */
 
+#include <fstream>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -38,7 +39,7 @@
     if (i.term)  /* edge points at a complete word */
     {
         *s = '\0';
-        fprintf (out, "%s\n", buf);
+        fprintf(out, "%s\n", buf);
     }
     if (i.ptr)
     {           /* Compute index: is it non-zero ? */
@@ -96,8 +97,8 @@
 
 void print_header(const string &iFileName)
 {
-    FILE *file;
-    if ((file = fopen(iFileName.c_str(), "rb")) == NULL)
+    ifstream file(iFileName.c_str(), ios_base::in | ios_base::binary);
+    if (!file.is_open())
     {
         fprintf(stderr, "Couldn't open file: %s\n", iFileName.c_str());
         exit(1);
@@ -107,7 +108,6 @@
     if (!header.read(file))
     {
         fprintf(stderr, "Invalid header in '%s'\n", iFileName.c_str());
-        fclose(file);
         exit(1);
     }
     header.print();




reply via email to

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