]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxsum.C
fix crossref label list, some debug messages + various
[lyx.git] / src / support / lyxsum.C
index 602da11bbe6d501152e8ab9d0831add7073ed8e2..aa0c71ac225fb1d4f4530a9177d86ceef19fddf5 100644 (file)
@@ -3,18 +3,26 @@
  * 
  *           LyX, The Document Processor        
  *
- *    The function lyxsum is taken from GNU textutill-1.22
+ *    The function lyx::sum is taken from GNU textutill-1.22
  *    and is there part of the program chsum. The chsum program
  *    is written by Q. Frank Xia, qx@math.columbia.edu.
  *
  *    Modified for use in LyX by Lars G. Bjønnes.
  *
- *======================================================
+ * ======================================================
  */
 
 
 #include <config.h>
-#include <stdio.h>
+#ifdef MODERN_STL_STREAMS
+#include <fstream>
+using std::ifstream;
+#else
+#include <cstdio>
+#endif
+
+#include "support/lyxlib.h"
+
 
 /* Number of bytes to read at once.  */
 #define BUFLEN (1 << 16)
@@ -80,15 +88,38 @@ static unsigned long const crctab[256] =
    Return crc if successful, 0 if an error occurs. */
  
 unsigned long
-lyxsum (char const*file)
+lyx::sum (char const * file)
 {
-       unsigned char buf[BUFLEN];
        unsigned long crc = 0;
        long length = 0;
        long bytes_read;
-       register FILE *fp;
+#ifdef MODERN_STL_STREAMS
+       char buf[BUFLEN];
+       ifstream ifs(file);
+       if (!ifs) {
+               return 0;
+       }
  
-       fp = fopen (file, "r");
+       while ((bytes_read = ifs.readsome(buf, BUFLEN)) > 0) {
+               unsigned char * cp = reinterpret_cast<unsigned char*>(buf);
+               length += bytes_read;
+               while (bytes_read--)
+                       crc = (crc << 8) ^ crctab[((crc >> 24) ^ *(cp++)) & 0xFF];
+       }
+       if (ifs.fail()) {
+               ifs.close();
+               return 0;
+       }
+
+       ifs.close();
+       if (ifs.fail()) {
+               return 0;
+       }
+#else
+       unsigned char buf[BUFLEN];
+       register FILE * fp = fopen (file, "r");
        if (fp == 0) {
                return 0;
        }
@@ -109,7 +140,7 @@ lyxsum (char const*file)
        if (fclose (fp) == EOF) {
                return 0;
        }
+#endif 
        bytes_read = length;
        while (bytes_read > 0) {
                crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];