[LyX/master] Refactor checksum calculation

Yuriy Skalko yuriy.skalko at gmail.com
Sat Nov 21 18:03:29 UTC 2020


commit cbad214cdda5f85a177148e38c138b9560ba0c17
Author: Yuriy Skalko <yuriy.skalko at gmail.com>
Date:   Sat Nov 21 15:40:31 2020 +0200

    Refactor checksum calculation
---
 src/ConverterCache.cpp              |   13 ++-------
 src/frontends/qt/GuiApplication.cpp |   11 ++------
 src/frontends/qt/GuiClipboard.cpp   |   15 ++++-------
 src/support/FileName.cpp            |   22 ++++------------
 src/support/Makefile.am             |    2 +
 src/support/checksum.cpp            |   46 +++++++++++++++++++++++++++++++++++
 src/support/checksum.h              |   30 ++++++++++++++++++++++
 7 files changed, 95 insertions(+), 44 deletions(-)

diff --git a/src/ConverterCache.cpp b/src/ConverterCache.cpp
index bee0253..0f4d03e 100644
--- a/src/ConverterCache.cpp
+++ b/src/ConverterCache.cpp
@@ -25,8 +25,8 @@
 #include "support/lyxtime.h"
 #include "support/Package.h"
 
+#include "support/checksum.h"
 #include "support/lassert.h"
-#include <boost/crc.hpp>
 
 #include <algorithm>
 #include <fstream>
@@ -41,14 +41,6 @@ namespace lyx {
 
 namespace {
 
-unsigned long do_crc(string const & s)
-{
-	boost::crc_32_type crc;
-	crc = for_each(s.begin(), s.end(), crc);
-	return crc.checksum();
-}
-
-
 // FIXME THREAD
 // This should be OK because it is only assigned during init()
 static FileName cache_dir;
@@ -62,7 +54,8 @@ public:
 		: timestamp(t), checksum(c)
 	{
 		ostringstream os;
-		os << setw(10) << setfill('0') << do_crc(orig_from.absFileName())
+		os << setw(10) << setfill('0')
+		   << support::checksum(orig_from.absFileName())
 		   << '-' << to_format;
 		cache_name = FileName(addName(cache_dir.absFileName(), os.str()));
 		LYXERR(Debug::FILES, "Add file cache item " << orig_from
diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index b3cfd5e..8ab89b8 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -59,6 +59,7 @@
 
 #include "insets/InsetText.h"
 
+#include "support/checksum.h"
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/ExceptionMessage.h"
@@ -146,8 +147,6 @@
 #include <QMacPasteboardMime>
 #endif // Q_OS_MAC
 
-#include <boost/crc.hpp>
-
 #include <exception>
 #include <sstream>
 #include <vector>
@@ -1708,9 +1707,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 			&& !is_open)) {
 			// We want the ui session to be saved per document and not per
 			// window number. The filename crc is a good enough identifier.
-			boost::crc_32_type crc;
-			crc = for_each(fname.begin(), fname.end(), crc);
-			createView(crc.checksum());
+			createView(support::checksum(fname));
 			current_view_->openDocument(fname);
 			if (!current_view_->documentBufferView())
 				current_view_->close();
@@ -2670,10 +2667,8 @@ void GuiApplication::restoreGuiSession()
 		FileName const & file_name = last.file_name;
 		if (!current_view_ || (!lyxrc.open_buffers_in_tabs
 			  && current_view_->documentBufferView() != 0)) {
-			boost::crc_32_type crc;
 			string const & fname = file_name.absFileName();
-			crc = for_each(fname.begin(), fname.end(), crc);
-			createView(crc.checksum());
+			createView(support::checksum(fname));
 		}
 		current_view_->loadDocument(file_name, false);
 
diff --git a/src/frontends/qt/GuiClipboard.cpp b/src/frontends/qt/GuiClipboard.cpp
index 59e5900..f78ab1c 100644
--- a/src/frontends/qt/GuiClipboard.cpp
+++ b/src/frontends/qt/GuiClipboard.cpp
@@ -35,6 +35,8 @@
 
 #include "frontends/alert.h"
 
+#include "support/checksum.h"
+
 #include <QApplication>
 #include <QBuffer>
 #include <QClipboard>
@@ -47,8 +49,6 @@
 #include <QTextDocument>
 #include <QTimer>
 
-#include <boost/crc.hpp>
-
 #include <memory>
 #include <map>
 #include <iostream>
@@ -431,11 +431,8 @@ void GuiClipboard::put(string const & lyx, docstring const & html, docstring con
 		data->setData(lyxMimeType(), qlyx);
 		// If the OS has not the concept of clipboard ownership,
 		// we recognize internal data through its checksum.
-		if (!hasInternal()) {
-			boost::crc_32_type crc32;
-			crc32.process_bytes(lyx.c_str(), lyx.size());
-			checksum = crc32.checksum();
-		}
+		if (!hasInternal())
+			checksum = support::checksum(lyx);
 	}
 	// Don't test for text.empty() since we want to be able to clear the
 	// clipboard.
@@ -528,9 +525,7 @@ bool GuiClipboard::isInternal() const
 	// ourself by comparing its checksum with the stored one.
 	QByteArray const ar = cache_.data(lyxMimeType());
 	string const data(ar.data(), ar.count());
-	boost::crc_32_type crc32;
-	crc32.process_bytes(data.c_str(), data.size());
-	return checksum == crc32.checksum();
+	return checksum == static_cast<std::uint32_t>(support::checksum(data));
 }
 
 
diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp
index 4e075f2..3d31c7d 100644
--- a/src/support/FileName.cpp
+++ b/src/support/FileName.cpp
@@ -35,7 +35,7 @@
 #include <QThread>
 #endif
 
-#include <boost/crc.hpp>
+#include "support/checksum.h"
 
 #include <algorithm>
 #include <iterator>
@@ -537,20 +537,14 @@ bool FileName::link(FileName const & name) const
 
 unsigned long checksum_ifstream_fallback(char const * file)
 {
-	unsigned long result = 0;
 	//LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
 	ifstream ifs(file, ios_base::in | ios_base::binary);
 	if (!ifs)
-		return result;
-
-	istreambuf_iterator<char> beg(ifs);
-	istreambuf_iterator<char> end;
-	boost::crc_32_type crc;
-	crc = for_each(beg, end, crc);
-	result = crc.checksum();
-	return result;
+		return 0;
+	return support::checksum(ifs);
 }
 
+
 unsigned long FileName::checksum() const
 {
 	if (!exists()) {
@@ -583,11 +577,9 @@ unsigned long FileName::checksum() const
 	qint64 size = fi.size();
 	uchar * ubeg = qf.map(0, size);
 	uchar * uend = ubeg + size;
-	boost::crc_32_type ucrc;
-	ucrc.process_block(ubeg, uend);
+	result = support::checksum(ubeg, uend);
 	qf.unmap(ubeg);
 	qf.close();
-	result = ucrc.checksum();
 
 #else // QT_VERSION
 
@@ -619,9 +611,7 @@ unsigned long FileName::checksum() const
 	char * beg = static_cast<char*>(mm);
 	char * end = beg + info.st_size;
 
-	boost::crc_32_type crc;
-	crc.process_block(beg, end);
-	result = crc.checksum();
+	result = support::checksum(beg, end);
 
 	munmap(mm, info.st_size);
 	close(fd);
diff --git a/src/support/Makefile.am b/src/support/Makefile.am
index addeab8..45d30b8 100644
--- a/src/support/Makefile.am
+++ b/src/support/Makefile.am
@@ -38,6 +38,8 @@ liblyxsupport_a_SOURCES = \
 	bind.h \
 	Cache.h \
 	Changer.h \
+	checksum.cpp \
+	checksum.h \
 	ConsoleApplication.cpp \
 	ConsoleApplication.h \
 	ConsoleApplicationPrivate.h \
diff --git a/src/support/checksum.cpp b/src/support/checksum.cpp
new file mode 100644
index 0000000..151b533
--- /dev/null
+++ b/src/support/checksum.cpp
@@ -0,0 +1,46 @@
+// -*- C++ -*-
+/**
+ * \file checksum.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Yuriy Skalko
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include "support/checksum.h"
+#include "boost/crc.hpp"
+#include <iterator>
+
+namespace lyx {
+
+namespace support {
+
+unsigned long checksum(std::string const & s)
+{
+	boost::crc_32_type crc;
+	crc.process_bytes(s.c_str(), s.size());
+	return crc.checksum();
+}
+
+unsigned long checksum(std::ifstream & ifs)
+{
+	std::istreambuf_iterator<char> beg(ifs);
+	std::istreambuf_iterator<char> end;
+
+	boost::crc_32_type crc;
+	crc = for_each(beg, end, crc);
+	return crc.checksum();
+}
+
+unsigned long checksum(char const * beg, char const * end)
+{
+	boost::crc_32_type crc;
+	crc.process_block(beg, end);
+	return crc.checksum();
+}
+
+} // namespace support
+
+} // namespace lyx
diff --git a/src/support/checksum.h b/src/support/checksum.h
new file mode 100644
index 0000000..ab14339
--- /dev/null
+++ b/src/support/checksum.h
@@ -0,0 +1,30 @@
+// -*- C++ -*-
+/**
+ * \file checksum.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Yuriy Skalko
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef LYX_CHECKSUM_H
+#define LYX_CHECKSUM_H
+
+#include <fstream>
+#include <string>
+
+namespace lyx {
+
+namespace support {
+
+unsigned long checksum(std::string const & s);
+unsigned long checksum(std::ifstream & ifs);
+unsigned long checksum(char const * beg, char const * end);
+
+} // namespace support
+
+} // namespace lyx
+
+#endif // LYX_CHECKSUM_H


More information about the lyx-cvs mailing list