]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxsum.C
remove commented HAVE_SSTREAM code
[lyx.git] / src / support / lyxsum.C
index 602da11bbe6d501152e8ab9d0831add7073ed8e2..e018073f2bea25095d021baeb0b56195d42559f4 100644 (file)
@@ -3,24 +3,30 @@
  * 
  *           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>
 
-/* Number of bytes to read at once.  */
-#define BUFLEN (1 << 16)
+#include <fstream>
+
+#include "Lsstream.h"
+
+#include "support/lyxlib.h"
+
+using std::ifstream;
+using std::ios;
 
 // DO _NOT_ CHANGE _ANYTHING_ IN THIS TABLE
-static unsigned long const crctab[256] =
+static
+unsigned long const crctab[256] =
 {
        0x0,
        0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
@@ -75,49 +81,40 @@ static unsigned long const crctab[256] =
        0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF,
        0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
 };
+
 /* Calculate the checksum of file FILE.
    Return crc if successful, 0 if an error occurs. */
  
-unsigned long
-lyxsum (char const*file)
+template<typename InputIterator>
+static inline
+unsigned long do_crc(InputIterator first, InputIterator last)
 {
-       unsigned char buf[BUFLEN];
        unsigned long crc = 0;
-       long length = 0;
-       long bytes_read;
-       register FILE *fp;
-       fp = fopen (file, "r");
-       if (fp == 0) {
-               return 0;
-       }
-       while ((bytes_read = fread (buf, 1, BUFLEN, fp)) > 0) {
-               unsigned char *cp = buf;
-               
-               length += bytes_read;
-               while (bytes_read--)
-                       crc = (crc << 8) ^ crctab[((crc >> 24) ^ *(cp++)) & 0xFF];
-       }
-       if (ferror (fp)) {
-               fclose (fp);
-               return 0;
+       long bytes_read = 0;
+       while(first != last) {
+               ++bytes_read;
+               crc = (crc << 8)
+                       ^ crctab[((crc >> 24) ^ *first++) & 0xFF];
        }
-       if (fclose (fp) == EOF) {
-               return 0;
-       }
-       bytes_read = length;
        while (bytes_read > 0) {
-               crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];
+               crc = (crc << 8)
+                       ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];
                bytes_read >>= 8;
        }
-       crc = ~crc & 0xFFFFFFFF;
-       return crc;
+       return ~crc & 0xFFFFFFFF;
+}
+
+
+// And this would be the file interface.
+unsigned long lyx::sum(char const * file)
+{
+       ifstream ifs(file);
+       if (!ifs) return 0;
+       ifs.unsetf(ios::skipws);
+       ostringstream ostr;
+       ostr << ifs.rdbuf();
+       // The .c_str() is here in case we use our lyxstring class
+       // instead of standard string. 
+       string w = ostr.str().c_str();
+       return do_crc(w.begin(), w.end());
 }