*
* 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)
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;
}
if (fclose (fp) == EOF) {
return 0;
}
-
+#endif
bytes_read = length;
while (bytes_read > 0) {
crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];