summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util/CMakeLists.txt40
-rw-r--r--src/util/README9
-rw-r--r--src/util/const_char_ptr.h51
-rw-r--r--src/util/copy.h49
-rw-r--r--src/util/ege-appear-time-tracker.cpp163
-rw-r--r--src/util/ege-appear-time-tracker.h90
-rw-r--r--src/util/ege-tags.cpp171
-rw-r--r--src/util/ege-tags.h122
-rw-r--r--src/util/enums.h134
-rw-r--r--src/util/expression-evaluator.cpp396
-rw-r--r--src/util/expression-evaluator.h196
-rw-r--r--src/util/find-if-before.h52
-rw-r--r--src/util/find-last-if.h52
-rw-r--r--src/util/fixed_point.h112
-rw-r--r--src/util/format.h57
-rw-r--r--src/util/forward-pointer-iterator.h122
-rw-r--r--src/util/list-container-test.h258
-rw-r--r--src/util/list-container.h353
-rw-r--r--src/util/list-copy.h46
-rw-r--r--src/util/list.h413
-rw-r--r--src/util/longest-common-suffix.h115
-rw-r--r--src/util/reference.h50
-rw-r--r--src/util/reverse-list.h69
-rw-r--r--src/util/share.cpp45
-rw-r--r--src/util/share.h112
-rw-r--r--src/util/signal-blocker.h71
-rw-r--r--src/util/ucompose.hpp452
-rw-r--r--src/util/units.cpp578
-rw-r--r--src/util/units.h231
-rw-r--r--src/util/ziptool.cpp3039
-rw-r--r--src/util/ziptool.h575
31 files changed, 8223 insertions, 0 deletions
diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt
new file mode 100644
index 0000000..4815590
--- /dev/null
+++ b/src/util/CMakeLists.txt
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+set(util_SRC
+ ege-appear-time-tracker.cpp
+ ege-tags.cpp
+ expression-evaluator.cpp
+ share.cpp
+ units.cpp
+ ziptool.cpp
+
+
+ # -------
+ # Headers
+ const_char_ptr.h
+ copy.h
+ ege-appear-time-tracker.h
+ ege-tags.h
+ enums.h
+ expression-evaluator.h
+ find-if-before.h
+ find-last-if.h
+ fixed_point.h
+ format.h
+ forward-pointer-iterator.h
+ list-container-test.h
+ list-container.h
+ list-copy.h
+ list.h
+ longest-common-suffix.h
+ reference.h
+ reverse-list.h
+ share.h
+ signal-blocker.h
+ ucompose.hpp
+ units.h
+ ziptool.h
+)
+
+add_inkscape_lib(util_LIB "${util_SRC}")
+# add_inkscape_source("${util_SRC}")
diff --git a/src/util/README b/src/util/README
new file mode 100644
index 0000000..254e3fa
--- /dev/null
+++ b/src/util/README
@@ -0,0 +1,9 @@
+
+
+This directory contains a variety of utility code.
+
+To do:
+
+* Merge with 'helper' into this directory.
+* Move individual files to more appropriate directories.
+* Split into three sub-directories: numeric, color, svg.
diff --git a/src/util/const_char_ptr.h b/src/util/const_char_ptr.h
new file mode 100644
index 0000000..872ca67
--- /dev/null
+++ b/src/util/const_char_ptr.h
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/** @file
+ * Provides `const_char_ptr`
+ */
+/*
+ * Authors:
+ * Sergei Izmailov <sergei.a.izmailov@gmail.com>
+ *
+ * Copyright (C) 2020 Sergei Izmailov
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+#ifndef SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H
+#define SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H
+#include <glibmm/ustring.h>
+#include <string>
+#include "share.h"
+
+namespace Inkscape {
+namespace Util {
+
+/**
+ * Non-owning reference to 'const char*'
+ * Main-purpose: avoid overloads of type `f(char*, str&)`, `f(str&, char*)`, `f(char*, char*)`, ...
+ */
+class const_char_ptr{
+public:
+ const_char_ptr() noexcept: m_data(nullptr){};
+ const_char_ptr(std::nullptr_t): const_char_ptr() {};
+ const_char_ptr(const char* const data) noexcept: m_data(data) {};
+ const_char_ptr(const Glib::ustring& str) noexcept: const_char_ptr(str.c_str()) {};
+ const_char_ptr(const std::string& str) noexcept: const_char_ptr(str.c_str()) {};
+ const_char_ptr(const ptr_shared& shared) : const_char_ptr(static_cast<const char* const>(shared)) {};
+
+ const char * data() const noexcept { return m_data; }
+private:
+ const char * const m_data = nullptr;
+};
+}
+}
+#endif // SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace .0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99: \ No newline at end of file
diff --git a/src/util/copy.h b/src/util/copy.h
new file mode 100644
index 0000000..6068132
--- /dev/null
+++ b/src/util/copy.h
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Traits::Copy - traits class to determine types to use when copying
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_TRAITS_COPY_H
+#define SEEN_INKSCAPE_TRAITS_COPY_H
+
+namespace Inkscape {
+
+namespace Traits {
+
+template <typename T>
+struct Copy {
+ typedef T Type;
+};
+
+template <typename T>
+struct Copy<T const> {
+ typedef T Type;
+};
+
+template <typename T>
+struct Copy<T &> {
+ typedef T &Type;
+};
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/ege-appear-time-tracker.cpp b/src/util/ege-appear-time-tracker.cpp
new file mode 100644
index 0000000..d3c0899
--- /dev/null
+++ b/src/util/ege-appear-time-tracker.cpp
@@ -0,0 +1,163 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MPL-1.1 OR LGPL-2.1-or-later
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Appear Time Tracker.
+ *
+ * The Initial Developer of the Original Code is
+ * Jon A. Cruz.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+
+#include "ege-appear-time-tracker.h"
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+
+namespace ege
+{
+
+namespace {
+
+void unhookHandler( gulong &id, GtkWidget *obj )
+{
+ if ( id ) {
+ if ( obj ) {
+ g_signal_handler_disconnect( G_OBJECT(obj), id );
+ }
+ id = 0;
+ }
+}
+
+} // namespace
+
+
+AppearTimeTracker::AppearTimeTracker(GTimer *timer, GtkWidget *widget, gchar const* name) :
+ _name(name ? name : ""),
+ _timer(timer),
+ _widget(widget),
+ _topMost(widget),
+ _autodelete(false),
+ _mapId(0),
+ _realizeId(0),
+ _hierarchyId(0)
+
+{
+ while (gtk_widget_get_parent(_topMost)) {
+ _topMost = gtk_widget_get_parent(_topMost);
+ }
+ _mapId = g_signal_connect( G_OBJECT(_topMost), "map-event", G_CALLBACK(mapCB), this );
+ _realizeId = g_signal_connect( G_OBJECT(_topMost), "realize", G_CALLBACK(realizeCB), this );
+ _hierarchyId = g_signal_connect( G_OBJECT(_widget), "hierarchy-changed", G_CALLBACK(hierarchyCB), this );
+}
+
+AppearTimeTracker::~AppearTimeTracker()
+{
+ if ( _timer ) {
+ g_timer_destroy(_timer);
+ _timer = nullptr;
+ }
+
+ unhookHandler( _mapId, _topMost );
+ unhookHandler( _realizeId, _topMost );
+ unhookHandler( _hierarchyId, _widget );
+}
+
+void AppearTimeTracker::stop() {
+ if (_timer) {
+ g_timer_stop(_timer);
+ }
+}
+
+void AppearTimeTracker::setAutodelete(bool autodelete)
+{
+ if ( autodelete != _autodelete ) {
+ _autodelete = autodelete;
+ }
+}
+
+void AppearTimeTracker::report(gchar const* msg)
+{
+ gulong msCount = 0;
+ gdouble secs = g_timer_elapsed( _timer, &msCount );
+ g_message("Time ended at %2.3f with [%s] on [%s]", secs, msg, _name.c_str());
+}
+
+void AppearTimeTracker::handleHierarchyChange( GtkWidget * /*prevTop*/ )
+{
+ GtkWidget *newTop = _widget;
+ while (gtk_widget_get_parent(newTop)) {
+ newTop = gtk_widget_get_parent(newTop);
+ }
+
+ if ( newTop != _topMost ) {
+ unhookHandler( _mapId, _topMost );
+ unhookHandler( _realizeId, _topMost );
+
+ _topMost = newTop;
+ _mapId = g_signal_connect( G_OBJECT(_topMost), "map-event", G_CALLBACK(mapCB), this );
+ _realizeId = g_signal_connect( G_OBJECT(_topMost), "realize", G_CALLBACK(realizeCB), this );
+ }
+}
+
+gboolean AppearTimeTracker::mapCB(GtkWidget * /*widget*/, GdkEvent * /*event*/, gpointer userData)
+{
+ AppearTimeTracker *tracker = reinterpret_cast<AppearTimeTracker*>(userData);
+ tracker->report("MAP");
+ if ( tracker->_autodelete ) {
+ delete tracker;
+ }
+ return FALSE;
+}
+
+void AppearTimeTracker::realizeCB(GtkWidget * /*widget*/, gpointer userData)
+{
+ AppearTimeTracker *tracker = reinterpret_cast<AppearTimeTracker*>(userData);
+ tracker->report("REALIZE");
+}
+
+void AppearTimeTracker::hierarchyCB(GtkWidget * /*widget*/, GtkWidget *prevTop, gpointer userData)
+{
+ AppearTimeTracker *tracker = reinterpret_cast<AppearTimeTracker*>(userData);
+ tracker->handleHierarchyChange( prevTop );
+}
+
+} // namespace ege
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/ege-appear-time-tracker.h b/src/util/ege-appear-time-tracker.h
new file mode 100644
index 0000000..4318e62
--- /dev/null
+++ b/src/util/ege-appear-time-tracker.h
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MPL-1.1 OR LGPL-2.1-or-later
+#ifndef SEEN_APPEAR_TIME_TRACKER_H
+#define SEEN_APPEAR_TIME_TRACKER_H
+
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Appear Time Tracker.
+ *
+ * The Initial Developer of the Original Code is
+ * Jon A. Cruz.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include <glib.h>
+#include <glibmm/ustring.h>
+
+typedef union _GdkEvent GdkEvent;
+typedef struct _GtkWidget GtkWidget;
+
+namespace ege
+{
+
+class AppearTimeTracker {
+public:
+ AppearTimeTracker(GTimer *timer, GtkWidget *widget, gchar const* name);
+ ~AppearTimeTracker();
+
+ void stop();
+
+ bool isAutodelete() const { return _autodelete; }
+ void setAutodelete(bool autodelete);
+
+private:
+ Glib::ustring _name;
+ GTimer *_timer;
+ GtkWidget *_widget;
+ GtkWidget *_topMost;
+ bool _autodelete;
+ gulong _mapId;
+ gulong _realizeId;
+ gulong _hierarchyId;
+
+ static gboolean mapCB(GtkWidget *widget, GdkEvent *event, gpointer userData);
+ static void realizeCB(GtkWidget *widget, gpointer userData);
+ static void hierarchyCB(GtkWidget *widget, GtkWidget *prevTop, gpointer userData);
+
+ void report(gchar const* msg);
+ void handleHierarchyChange( GtkWidget *prevTop );
+};
+
+} // namespace ege
+
+#endif // SEEN_APPEAR_TIME_TRACKER_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/ege-tags.cpp b/src/util/ege-tags.cpp
new file mode 100644
index 0000000..f8acab8
--- /dev/null
+++ b/src/util/ege-tags.cpp
@@ -0,0 +1,171 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MPL-1.1 OR LGPL-2.1-or-later
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is EGE Tagging Support.
+ *
+ * The Initial Developer of the Original Code is
+ * Jon A. Cruz.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include <libintl.h>
+
+#if !defined(_)
+#define _(s) gettext(s)
+#endif // !defined(_)
+
+#include <set>
+#include <algorithm>
+#include <functional>
+#include <utility>
+
+#include "ege-tags.h"
+
+#include <glib.h>
+
+namespace ege
+{
+
+Label::Label(std::string lang, std::string value) :
+ lang(std::move(lang)),
+ value(std::move(value))
+{
+}
+
+Label::~Label()
+= default;
+
+// =========================================================================
+
+Tag::~Tag()
+= default;
+
+Tag::Tag(std::string key) :
+ key(std::move(key))
+{
+}
+
+// =========================================================================
+
+TagSet::TagSet() :
+ lang(),
+ tags(),
+ counts()
+{
+}
+
+TagSet::~TagSet()
+= default;
+
+void TagSet::setLang(std::string const& lang)
+{
+ if (lang != this->lang) {
+ this->lang = lang;
+ }
+}
+
+
+struct sameLang : public std::binary_function<Label, Label, bool> {
+ bool operator()(Label const& x, Label const& y) const { return (x.lang == y.lang); }
+};
+
+
+bool TagSet::addTag(Tag const& tag)
+{
+ bool present = false;
+
+ for ( std::vector<Tag>::iterator it = tags.begin(); (it != tags.end()) && !present; ++it ) {
+ if (tag.key == it->key) {
+ present = true;
+
+ for (const auto & label : tag.labels) {
+ std::vector<Label>::iterator itOld = std::find_if( it->labels.begin(), it->labels.end(), std::bind2nd(sameLang(), label) );
+ if (itOld != it->labels.end()) {
+ itOld->value = label.value;
+ } else {
+ it->labels.push_back(label);
+ }
+ }
+ }
+ }
+
+ if (!present) {
+ tags.push_back(tag);
+ counts[tag.key] = 0;
+ }
+
+ return present;
+}
+
+
+std::vector<Tag> const& TagSet::getTags()
+{
+ return tags;
+}
+
+int TagSet::getCount( std::string const& key )
+{
+ int count = 0;
+ if ( counts.find(key) != counts.end() ) {
+ count = counts[key];
+ }
+ return count;
+}
+
+void TagSet::increment( std::string const& key )
+{
+ if ( counts.find(key) != counts.end() ) {
+ counts[key]++;
+ } else {
+ Tag tag(key);
+ tags.push_back(tag);
+ counts[key] = 1;
+ }
+}
+
+void TagSet::decrement( std::string const& key )
+{
+ if ( counts.find(key) != counts.end() ) {
+ counts[key]--;
+ }
+}
+
+} // namespace ege
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/ege-tags.h b/src/util/ege-tags.h
new file mode 100644
index 0000000..dfa584d
--- /dev/null
+++ b/src/util/ege-tags.h
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MPL-1.1 OR LGPL-2.1-or-later
+#ifndef SEEN_EGE_TAGS_H
+#define SEEN_EGE_TAGS_H
+
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is EGE Tagging Support.
+ *
+ * The Initial Developer of the Original Code is
+ * Jon A. Cruz.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include <string>
+#include <vector>
+#include <map>
+
+/*
+ * Implements base tagging of http://create.freedesktop.org/wiki/ResourceTagging .
+ */
+
+// Note that this API is preliminary and subject to frequent change:
+
+namespace ege
+{
+
+class Label
+{
+public:
+ Label();
+ Label(std::string lang, std::string value);
+ ~Label();
+
+ std::string lang;
+ std::string value;
+};
+
+class Tag
+{
+public:
+ Tag();
+ Tag(std::string key);
+ ~Tag();
+
+ std::string key;
+ std::vector<Label> labels;
+};
+
+
+/**
+ * Contains a set of tags with unique keys, and with locale support.
+ *
+ */
+class TagSet
+{
+public:
+ TagSet();
+ ~TagSet();
+
+ std::string const & getLang() const;
+ void setLang(std::string const& lang);
+
+ /**
+ * Adds or updates a tag.
+ *
+ * @return true if a tag was updated, false if it was added.
+ */
+ bool addTag(Tag const& tag);
+ std::vector<Tag> const& getTags();
+
+ int getCount( std::string const& key );
+ void increment( std::string const& key );
+ void decrement( std::string const& key );
+
+private:
+
+ std::string lang;
+ std::vector<Tag> tags;
+ std::map<std::string, int> counts;
+};
+
+} // namespace ege
+
+
+#endif // SEEN_EGE_TAGS_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/enums.h b/src/util/enums.h
new file mode 100644
index 0000000..46c4d5e
--- /dev/null
+++ b/src/util/enums.h
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Authors:
+ * Nicholas Bishop <nicholasbishop@gmail.com>
+ * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
+ *
+ * Copyright (C) 2007 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+#ifndef INKSCAPE_UTIL_ENUMS_H
+#define INKSCAPE_UTIL_ENUMS_H
+
+#include <glibmm/ustring.h>
+
+namespace Inkscape {
+namespace Util {
+
+/**
+ * Simplified management of enumerations of svg items with UI labels.
+ * IMPORTANT:
+ * When initializing the EnumData struct, you cannot use _(...) to translate strings.
+ * Instead, one must use N_(...) and do the translation every time the string is retrieved.
+ */
+template<typename E>
+struct EnumData
+{
+ E id;
+ const Glib::ustring label;
+ const Glib::ustring key;
+};
+
+const Glib::ustring empty_string("");
+
+/**
+ * Simplified management of enumerations of svg items with UI labels.
+ *
+ * @note that get_id_from_key and get_id_from_label return 0 if it cannot find an entry for that key string.
+ * @note that get_label and get_key return an empty string when the requested id is not in the list.
+ */
+template<typename E> class EnumDataConverter
+{
+public:
+ typedef EnumData<E> Data;
+
+ EnumDataConverter(const EnumData<E>* cd, const unsigned int length)
+ : _length(length), _data(cd)
+ {}
+
+ E get_id_from_label(const Glib::ustring& label) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].label == label)
+ return _data[i].id;
+ }
+
+ return (E)0;
+ }
+
+ E get_id_from_key(const Glib::ustring& key) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].key == key)
+ return _data[i].id;
+ }
+
+ return (E)0;
+ }
+
+ bool is_valid_key(const Glib::ustring& key) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].key == key)
+ return true;
+ }
+
+ return false;
+ }
+
+ bool is_valid_id(const E id) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].id == id)
+ return true;
+ }
+ return false;
+ }
+
+ const Glib::ustring& get_label(const E id) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].id == id)
+ return _data[i].label;
+ }
+
+ return empty_string;
+ }
+
+ const Glib::ustring& get_key(const E id) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].id == id)
+ return _data[i].key;
+ }
+
+ return empty_string;
+ }
+
+ const EnumData<E>& data(const unsigned int i) const
+ {
+ return _data[i];
+ }
+
+ const unsigned int _length;
+private:
+ const EnumData<E>* _data;
+};
+
+
+}
+}
+
+#endif
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/expression-evaluator.cpp b/src/util/expression-evaluator.cpp
new file mode 100644
index 0000000..24a56b7
--- /dev/null
+++ b/src/util/expression-evaluator.cpp
@@ -0,0 +1,396 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+/** @file
+ * TODO: insert short description here
+ */
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * Original file from libgimpwidgets: gimpeevl.c
+ * Copyright (C) 2008 Fredrik Alstromer <roe@excu.se>
+ * Copyright (C) 2008 Martin Nordholts <martinn@svn.gnome.org>
+ * Modified for Inkscape by Johan Engelen
+ * Copyright (C) 2011 Johan Engelen
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * This library is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "util/expression-evaluator.h"
+#include "util/units.h"
+
+#include <glib/gconvert.h>
+
+#include <cmath>
+#include <cstring>
+
+using Inkscape::Util::unit_table;
+
+namespace Inkscape {
+namespace Util {
+
+EvaluatorQuantity::EvaluatorQuantity(double value, unsigned int dimension) :
+ value(value),
+ dimension(dimension)
+{
+}
+
+EvaluatorToken::EvaluatorToken()
+{
+ type = 0;
+ value.fl = 0;
+}
+
+ExpressionEvaluator::ExpressionEvaluator(const char *string, Unit const *unit) :
+ string(g_locale_to_utf8(string,-1,nullptr,nullptr,nullptr)),
+ unit(unit)
+{
+ current_token.type = TOKEN_END;
+
+ // Preload symbol
+ parseNextToken();
+}
+
+/**
+ * Evaluates the given arithmetic expression, along with an optional dimension
+ * analysis, and basic unit conversions.
+ *
+ * All units conversions factors are relative to some implicit
+ * base-unit. This is also the unit of the returned value.
+ *
+ * Returns: An EvaluatorQuantity with a value given in the base unit along with
+ * the order of the dimension (e.g. if the base unit is inches, a dimension
+ * order of two means in^2).
+ *
+ * @return Result of evaluation.
+ * @throws Inkscape::Util::EvaluatorException There was a parse error.
+ **/
+EvaluatorQuantity ExpressionEvaluator::evaluate()
+{
+ if (!g_utf8_validate(string, -1, nullptr)) {
+ throw EvaluatorException("Invalid UTF8 string", nullptr);
+ }
+
+ EvaluatorQuantity result = EvaluatorQuantity();
+ EvaluatorQuantity default_unit_factor;
+
+ // Empty expression evaluates to 0
+ if (acceptToken(TOKEN_END, nullptr)) {
+ return result;
+ }
+
+ result = evaluateExpression();
+
+ // There should be nothing left to parse by now
+ isExpected(TOKEN_END, nullptr);
+
+ resolveUnit(nullptr, &default_unit_factor, unit);
+
+ // Entire expression is dimensionless, apply default unit if applicable
+ if ( result.dimension == 0 && default_unit_factor.dimension != 0 ) {
+ result.value /= default_unit_factor.value;
+ result.dimension = default_unit_factor.dimension;
+ }
+ return result;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateExpression()
+{
+ bool subtract;
+ EvaluatorQuantity evaluated_terms;
+
+ evaluated_terms = evaluateTerm();
+
+ // Continue evaluating terms, chained with + or -.
+ for (subtract = FALSE;
+ acceptToken('+', nullptr) || (subtract = acceptToken('-', nullptr));
+ subtract = FALSE)
+ {
+ EvaluatorQuantity new_term = evaluateTerm();
+
+ // If dimensions mismatch, attempt default unit assignment
+ if ( new_term.dimension != evaluated_terms.dimension ) {
+ EvaluatorQuantity default_unit_factor;
+
+ resolveUnit(nullptr, &default_unit_factor, unit);
+
+ if ( new_term.dimension == 0
+ && evaluated_terms.dimension == default_unit_factor.dimension )
+ {
+ new_term.value /= default_unit_factor.value;
+ new_term.dimension = default_unit_factor.dimension;
+ } else if ( evaluated_terms.dimension == 0
+ && new_term.dimension == default_unit_factor.dimension )
+ {
+ evaluated_terms.value /= default_unit_factor.value;
+ evaluated_terms.dimension = default_unit_factor.dimension;
+ } else {
+ throwError("Dimension mismatch during addition");
+ }
+ }
+
+ evaluated_terms.value += (subtract ? -new_term.value : new_term.value);
+ }
+
+ return evaluated_terms;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateTerm()
+{
+ bool division;
+ EvaluatorQuantity evaluated_exp_terms = evaluateExpTerm();
+
+ for ( division = false;
+ acceptToken('*', nullptr) || (division = acceptToken('/', nullptr));
+ division = false )
+ {
+ EvaluatorQuantity new_exp_term = evaluateExpTerm();
+
+ if (division) {
+ evaluated_exp_terms.value /= new_exp_term.value;
+ evaluated_exp_terms.dimension -= new_exp_term.dimension;
+ } else {
+ evaluated_exp_terms.value *= new_exp_term.value;
+ evaluated_exp_terms.dimension += new_exp_term.dimension;
+ }
+ }
+
+ return evaluated_exp_terms;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateExpTerm()
+{
+ EvaluatorQuantity evaluated_signed_factors = evaluateSignedFactor();
+
+ while(acceptToken('^', nullptr)) {
+ EvaluatorQuantity new_signed_factor = evaluateSignedFactor();
+
+ if (new_signed_factor.dimension == 0) {
+ evaluated_signed_factors.value = pow(evaluated_signed_factors.value,
+ new_signed_factor.value);
+ evaluated_signed_factors.dimension *= new_signed_factor.value;
+ } else {
+ throwError("Unit in exponent");
+ }
+ }
+
+ return evaluated_signed_factors;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateSignedFactor()
+{
+ EvaluatorQuantity result;
+ bool negate = FALSE;
+
+ if (!acceptToken('+', nullptr)) {
+ negate = acceptToken ('-', nullptr);
+ }
+
+ result = evaluateFactor();
+
+ if (negate) {
+ result.value = -result.value;
+ }
+
+ return result;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateFactor()
+{
+ EvaluatorQuantity evaluated_factor = EvaluatorQuantity();
+ EvaluatorToken consumed_token = EvaluatorToken();
+
+ if (acceptToken(TOKEN_END, &consumed_token)) {
+ return evaluated_factor;
+ }
+ else if (acceptToken(TOKEN_NUM, &consumed_token)) {
+ evaluated_factor.value = consumed_token.value.fl;
+ } else if (acceptToken('(', nullptr)) {
+ evaluated_factor = evaluateExpression();
+ isExpected(')', nullptr);
+ } else {
+ throwError("Expected number or '('");
+ }
+
+ if ( current_token.type == TOKEN_IDENTIFIER ) {
+ char *identifier;
+ EvaluatorQuantity result;
+
+ acceptToken(TOKEN_ANY, &consumed_token);
+
+ identifier = g_newa(char, consumed_token.value.size + 1);
+
+ strncpy(identifier, consumed_token.value.c, consumed_token.value.size);
+ identifier[consumed_token.value.size] = '\0';
+
+ if (resolveUnit(identifier, &result, unit)) {
+ evaluated_factor.value /= result.value;
+ evaluated_factor.dimension += result.dimension;
+ } else {
+ throwError("Unit was not resolved");
+ }
+ }
+
+ return evaluated_factor;
+}
+
+bool ExpressionEvaluator::acceptToken(TokenType token_type,
+ EvaluatorToken *consumed_token)
+{
+ bool existed = FALSE;
+
+ if ( token_type == current_token.type || token_type == TOKEN_ANY ) {
+ existed = TRUE;
+
+ if (consumed_token) {
+ *consumed_token = current_token;
+ }
+
+ // Parse next token
+ parseNextToken();
+ }
+
+ return existed;
+}
+
+void ExpressionEvaluator::parseNextToken()
+{
+ const char *s;
+
+ movePastWhiteSpace();
+ s = string;
+ start_of_current_token = s;
+
+ if ( !s || s[0] == '\0' ) {
+ // We're all done
+ current_token.type = TOKEN_END;
+ } else if ( s[0] == '+' || s[0] == '-' ) {
+ // Snatch these before the g_strtod() does, otherwise they might
+ // be used in a numeric conversion.
+ acceptTokenCount(1, s[0]);
+ } else {
+ // Attempt to parse a numeric value
+ char *endptr = nullptr;
+ gdouble value = g_strtod(s, &endptr);
+
+ if ( endptr && endptr != s ) {
+ // A numeric could be parsed, use it
+ current_token.value.fl = value;
+
+ current_token.type = TOKEN_NUM;
+ string = endptr;
+ } else if (isUnitIdentifierStart(s[0])) {
+ // Unit identifier
+ current_token.value.c = s;
+ current_token.value.size = getIdentifierSize(s, 0);
+
+ acceptTokenCount(current_token.value.size, TOKEN_IDENTIFIER);
+ } else {
+ // Everything else is a single character token
+ acceptTokenCount(1, s[0]);
+ }
+ }
+}
+
+void ExpressionEvaluator::acceptTokenCount (int count, TokenType token_type)
+{
+ current_token.type = token_type;
+ string += count;
+}
+
+void ExpressionEvaluator::isExpected(TokenType token_type,
+ EvaluatorToken *value)
+{
+ if (!acceptToken(token_type, value)) {
+ throwError("Unexpected token");
+ }
+}
+
+void ExpressionEvaluator::movePastWhiteSpace()
+{
+ if (!string) {
+ return;
+ }
+
+ while (g_ascii_isspace(*string)) {
+ string++;
+ }
+}
+
+bool ExpressionEvaluator::isUnitIdentifierStart(gunichar c)
+{
+ return (g_unichar_isalpha (c)
+ || c == (gunichar) '%'
+ || c == (gunichar) '\'');
+}
+
+/**
+ * getIdentifierSize:
+ * @s:
+ * @start:
+ *
+ * Returns: Size of identifier in bytes (not including NULL
+ * terminator).
+ **/
+int ExpressionEvaluator::getIdentifierSize(const char *string, int start_offset)
+{
+ const char *start = g_utf8_offset_to_pointer(string, start_offset);
+ const char *s = start;
+ gunichar c = g_utf8_get_char(s);
+ int length = 0;
+
+ if (isUnitIdentifierStart(c)) {
+ s = g_utf8_next_char (s);
+ c = g_utf8_get_char (s);
+ length++;
+
+ while ( isUnitIdentifierStart (c) || g_unichar_isdigit (c) ) {
+ s = g_utf8_next_char(s);
+ c = g_utf8_get_char(s);
+ length++;
+ }
+ }
+
+ return g_utf8_offset_to_pointer(start, length) - start;
+}
+
+bool ExpressionEvaluator::resolveUnit (const char* identifier,
+ EvaluatorQuantity *result,
+ Unit const* unit)
+{
+ if (!unit) {
+ result->value = 1;
+ result->dimension = 1;
+ return true;
+ }else if (!identifier) {
+ result->value = 1;
+ result->dimension = unit->isAbsolute() ? 1 : 0;
+ return true;
+ } else if (unit_table.hasUnit(identifier)) {
+ Unit const *identifier_unit = unit_table.getUnit(identifier);
+ result->value = Quantity::convert(1, unit, identifier_unit);
+ result->dimension = identifier_unit->isAbsolute() ? 1 : 0;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void ExpressionEvaluator::throwError(const char *msg)
+{
+ throw EvaluatorException(msg, start_of_current_token);
+}
+
+} // namespace Util
+} // namespace Inkscape
diff --git a/src/util/expression-evaluator.h b/src/util/expression-evaluator.h
new file mode 100644
index 0000000..a45ad5a
--- /dev/null
+++ b/src/util/expression-evaluator.h
@@ -0,0 +1,196 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+/** @file
+ * TODO: insert short description here
+ */
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * Original file from libgimpwidgets: gimpeevl.h
+ * Copyright (C) 2008-2009 Fredrik Alstromer <roe@excu.se>
+ * Copyright (C) 2008-2009 Martin Nordholts <martinn@svn.gnome.org>
+ * Modified for Inkscape by Johan Engelen
+ * Copyright (C) 2011 Johan Engelen
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * This library is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
+#define INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
+
+#include "util/units.h"
+
+#include <exception>
+#include <sstream>
+#include <string>
+
+/**
+ * @file
+ * Expression evaluator: A straightforward recursive
+ * descent parser, no fuss, no new dependencies. The lexer is hand
+ * coded, tedious, not extremely fast but works. It evaluates the
+ * expression as it goes along, and does not create a parse tree or
+ * anything, and will not optimize anything. It uses doubles for
+ * precision, with the given use case, that's enough to combat any
+ * rounding errors (as opposed to optimizing the evaluation).
+ *
+ * It relies on external unit resolving through a callback and does
+ * elementary dimensionality constraint check (e.g. "2 mm + 3 px * 4
+ * in" is an error, as L + L^2 is a mismatch). It uses g_strtod() for numeric
+ * conversions and it's non-destructive in terms of the parameters, and
+ * it's reentrant.
+ *
+ * EBNF:
+ *
+ * expression ::= term { ('+' | '-') term }* |
+ * <empty string> ;
+ *
+ * term ::= exponent { ( '*' | '/' ) exponent }* ;
+ *
+ * exponent ::= signed factor { '^' signed factor }* ;
+ *
+ * signed factor ::= ( '+' | '-' )? factor ;
+ *
+ * unit factor ::= factor unit? ;
+ *
+ * factor ::= number | '(' expression ')' ;
+ *
+ * number ::= ? what g_strtod() consumes ? ;
+ *
+ * unit ::= ? what not g_strtod() consumes and not whitespace ? ;
+ *
+ * The code should match the EBNF rather closely (except for the
+ * non-terminal unit factor, which is inlined into factor) for
+ * maintainability reasons.
+ *
+ * It will allow 1++1 and 1+-1 (resulting in 2 and 0, respectively),
+ * but I figured one might want that, and I don't think it's going to
+ * throw anyone off.
+ */
+
+namespace Inkscape {
+namespace Util {
+
+class Unit;
+
+/**
+ * EvaluatorQuantity:
+ * @param value In reference units.
+ * @param dimension mm has a dimension of 1, mm^2 has a dimension of 2, etc.
+ */
+class EvaluatorQuantity
+{
+public:
+ EvaluatorQuantity(double value = 0, unsigned int dimension = 0);
+
+ double value;
+ unsigned int dimension;
+};
+
+/**
+ * TokenType
+ */
+enum {
+ TOKEN_NUM = 30000,
+ TOKEN_IDENTIFIER = 30001,
+ TOKEN_ANY = 40000,
+ TOKEN_END = 50000
+};
+typedef int TokenType;
+
+/**
+ * EvaluatorToken
+ */
+class EvaluatorToken
+{
+public:
+ EvaluatorToken();
+
+ TokenType type;
+
+ union {
+ double fl;
+ struct {
+ const char *c;
+ int size;
+ };
+ } value;
+};
+
+/**
+ * ExpressionEvaluator
+ * @param string NULL terminated input string to evaluate
+ * @param unit Unit output should be in
+ */
+class ExpressionEvaluator
+{
+public:
+ ExpressionEvaluator(const char *string, Unit const *unit = nullptr);
+
+ EvaluatorQuantity evaluate();
+
+private:
+ const char *string;
+ Unit const *unit;
+
+ EvaluatorToken current_token;
+ const char *start_of_current_token;
+
+ EvaluatorQuantity evaluateExpression();
+ EvaluatorQuantity evaluateTerm();
+ EvaluatorQuantity evaluateExpTerm();
+ EvaluatorQuantity evaluateSignedFactor();
+ EvaluatorQuantity evaluateFactor();
+
+ bool acceptToken(TokenType token_type, EvaluatorToken *consumed_token);
+ void parseNextToken();
+ void acceptTokenCount(int count, TokenType token_type);
+ void isExpected(TokenType token_type, EvaluatorToken *value);
+
+ void movePastWhiteSpace();
+
+ static bool isUnitIdentifierStart(gunichar c);
+ static int getIdentifierSize(const char *s, int start);
+
+ static bool resolveUnit(const char *identifier, EvaluatorQuantity *result, Unit const *unit);
+
+ void throwError(const char *msg);
+};
+
+/**
+ * Special exception class for the expression evaluator.
+ */
+class EvaluatorException : public std::exception {
+public:
+ EvaluatorException(const char *message, const char *at_position) {
+ std::ostringstream os;
+ const char *token = at_position ? at_position : "<End of input>";
+ os << "Expression evaluator error: " << message << " at '" << token << "'";
+ msgstr = os.str();
+ }
+
+ ~EvaluatorException() noexcept override = default; // necessary to destroy the string object!!!
+
+ const char *what() const noexcept override {
+ return msgstr.c_str();
+ }
+protected:
+ std::string msgstr;
+};
+
+}
+}
+
+#endif // INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
diff --git a/src/util/find-if-before.h b/src/util/find-if-before.h
new file mode 100644
index 0000000..6d15fdf
--- /dev/null
+++ b/src/util/find-if-before.h
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Algorithms::find_if_before - finds the position before
+ * the first value that satisfies
+ * the predicate
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2005 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_ALGORITHMS_FIND_IF_BEFORE_H
+#define SEEN_INKSCAPE_ALGORITHMS_FIND_IF_BEFORE_H
+
+#include <algorithm>
+
+namespace Inkscape {
+
+namespace Algorithms {
+
+template <typename ForwardIterator, typename UnaryPredicate>
+inline ForwardIterator find_if_before(ForwardIterator start,
+ ForwardIterator end,
+ UnaryPredicate pred)
+{
+ ForwardIterator before=end;
+ while ( start != end && !pred(*start) ) {
+ before = start;
+ ++start;
+ }
+ return ( start != end ) ? before : end;
+}
+
+}
+
+}
+
+#endif
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
diff --git a/src/util/find-last-if.h b/src/util/find-last-if.h
new file mode 100644
index 0000000..1402de7
--- /dev/null
+++ b/src/util/find-last-if.h
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Algorithms::find_last_if
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_ALGORITHMS_FIND_LAST_IF_H
+#define SEEN_INKSCAPE_ALGORITHMS_FIND_LAST_IF_H
+
+#include <algorithm>
+
+namespace Inkscape {
+
+namespace Algorithms {
+
+template <typename ForwardIterator, typename UnaryPredicate>
+inline ForwardIterator find_last_if(ForwardIterator start, ForwardIterator end,
+ UnaryPredicate pred)
+{
+ ForwardIterator last_found(end);
+ while ( start != end ) {
+ start = std::find_if(start, end, pred);
+ if ( start != end ) {
+ last_found = start;
+ ++start;
+ }
+ }
+ return last_found;
+}
+
+}
+
+}
+
+#endif
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
diff --git a/src/util/fixed_point.h b/src/util/fixed_point.h
new file mode 100644
index 0000000..5bb0dde
--- /dev/null
+++ b/src/util/fixed_point.h
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::FixedPoint - fixed point type
+ *
+ * Authors:
+ * Jasper van de Gronde <th.v.d.gronde@hccnet.net>
+ *
+ * Copyright (C) 2006 Jasper van de Gronde
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H
+#define SEEN_INKSCAPE_UTIL_FIXED_POINT_H
+
+#include "util/reference.h"
+#include <cmath>
+#include <algorithm>
+#include <limits>
+
+namespace Inkscape {
+
+namespace Util {
+
+template <typename T, unsigned int precision>
+class FixedPoint {
+public:
+ FixedPoint() = default;
+ FixedPoint(const FixedPoint& value) : v(value.v) {}
+ FixedPoint(char value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(unsigned char value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(short value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(unsigned short value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(int value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(unsigned int value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(double value) : v(static_cast<T>(floor(value*(1<<precision)))) {}
+
+ FixedPoint& operator+=(FixedPoint val) { v += val.v; return *this; }
+ FixedPoint& operator-=(FixedPoint val) { v -= val.v; return *this; }
+ FixedPoint& operator*=(FixedPoint val) {
+ const unsigned int half_size = 8*sizeof(T)/2;
+ const T al = v&((1<<half_size)-1), bl = val.v&((1<<half_size)-1);
+ const T ah = v>>half_size, bh = val.v>>half_size;
+ v = static_cast<unsigned int>(al*bl)>>precision;
+ if ( half_size >= precision ) {
+ v += ((al*bh)+(ah*bl)+((ah*bh)<<half_size))<<(half_size-precision);
+ } else {
+ v += ((al*bh)+(ah*bl))>>(precision-half_size);
+ v += (ah*bh)<<(2*half_size-precision);
+ }
+ return *this;
+ }
+
+ FixedPoint& operator*=(char val) { v *= val; return *this; }
+ FixedPoint& operator*=(unsigned char val) { v *= val; return *this; }
+ FixedPoint& operator*=(short val) { v *= val; return *this; }
+ FixedPoint& operator*=(unsigned short val) { v *= val; return *this; }
+ FixedPoint& operator*=(int val) { v *= val; return *this; }
+ FixedPoint& operator*=(unsigned int val) { v *= val; return *this; }
+
+ FixedPoint operator+(FixedPoint val) const { FixedPoint r(*this); return r+=val; }
+ FixedPoint operator-(FixedPoint val) const { FixedPoint r(*this); return r-=val; }
+ FixedPoint operator*(FixedPoint val) const { FixedPoint r(*this); return r*=val; }
+
+ FixedPoint operator*(char val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(unsigned char val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(short val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(unsigned short val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(int val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(unsigned int val) const { FixedPoint r(*this); return r*=val; }
+
+ float operator*(float val) const { return static_cast<float>(*this)*val; }
+ double operator*(double val) const { return static_cast<double>(*this)*val; }
+
+ operator char() const { return v>>precision; }
+ operator unsigned char() const { return v>>precision; }
+ operator short() const { return v>>precision; }
+ operator unsigned short() const { return v>>precision; }
+ operator int() const { return v>>precision; }
+ operator unsigned int() const { return v>>precision; }
+
+ operator float() const { return ldexpf(v,-precision); }
+ operator double() const { return ldexp(v,-precision); }
+private:
+ T v;
+};
+
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(char a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned char a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(short a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned short a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(int a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned int a, FixedPoint<T,precision> b) { return b*=a; }
+
+template<typename T, unsigned int precision> float operator *(float a, FixedPoint<T,precision> b) { return b*a; }
+template<typename T, unsigned int precision> double operator *(double a, FixedPoint<T,precision> b) { return b*a; }
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/format.h b/src/util/format.h
new file mode 100644
index 0000000..b38f3a6
--- /dev/null
+++ b/src/util/format.h
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::format - g_strdup_printf wrapper producing shared strings
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2006 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_FORMAT_H
+#define SEEN_INKSCAPE_UTIL_FORMAT_H
+
+#include <cstdarg>
+#include <glib.h>
+#include "util/share.h"
+
+namespace Inkscape {
+
+namespace Util {
+
+inline ptr_shared vformat(char const *format, va_list args) {
+ char *temp=g_strdup_vprintf(format, args);
+ ptr_shared result=share_string(temp);
+ g_free(temp);
+ return result;
+}
+
+ // needed since G_GNUC_PRINTF can only be used on a declaration
+ ptr_shared format(char const *format, ...) G_GNUC_PRINTF(1,2);
+inline ptr_shared format(char const *format, ...) {
+ va_list args;
+
+ va_start(args, format);
+ ptr_shared result=vformat(format, args);
+ va_end(args);
+
+ return result;
+}
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/forward-pointer-iterator.h b/src/util/forward-pointer-iterator.h
new file mode 100644
index 0000000..9fe3bb8
--- /dev/null
+++ b/src/util/forward-pointer-iterator.h
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::ForwardPointerIterator - wraps a simple pointer
+ * with various strategies
+ * to determine sequence
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H
+#define SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H
+
+#include <iterator>
+#include <cstddef>
+#include "util/reference.h"
+
+namespace Inkscape {
+
+namespace Util {
+
+template <typename BaseType, typename Strategy>
+class ForwardPointerIterator;
+
+template <typename BaseType, typename Strategy>
+class ForwardPointerIterator<BaseType const, Strategy> {
+public:
+ typedef std::forward_iterator_tag iterator_category;
+ typedef typename Traits::Reference<BaseType const>::LValue value_type;
+ typedef std::ptrdiff_t difference_type;
+ typedef typename Traits::Reference<BaseType const>::LValue reference;
+ typedef typename Traits::Reference<BaseType const>::RValue const_reference;
+ typedef typename Traits::Reference<BaseType const>::Pointer pointer;
+
+ typedef ForwardPointerIterator<BaseType const, Strategy> Self;
+
+ ForwardPointerIterator() = default;
+ ForwardPointerIterator(pointer p) : _p(p) {}
+
+ operator pointer() const { return _p; }
+ reference operator*() const { return *_p; }
+ pointer operator->() const { return _p; }
+
+ bool operator==(Self const &other) const {
+ return _p == other._p;
+ }
+ bool operator!=(Self const &other) const {
+ return _p != other._p;
+ }
+
+ Self &operator++() {
+ _p = Strategy::next(_p);
+ return *this;
+ }
+ Self operator++(int) {
+ Self old(*this);
+ operator++();
+ return old;
+ }
+
+ operator bool() const { return _p != nullptr; }
+
+private:
+ pointer _p;
+};
+
+template <typename BaseType, typename Strategy>
+class ForwardPointerIterator
+: public ForwardPointerIterator<BaseType const, Strategy>
+{
+public:
+ typedef typename Traits::Reference<BaseType>::LValue value_type;
+ typedef typename Traits::Reference<BaseType>::LValue reference;
+ typedef typename Traits::Reference<BaseType>::RValue const_reference;
+ typedef typename Traits::Reference<BaseType>::Pointer pointer;
+
+ typedef ForwardPointerIterator<BaseType const, Strategy> Ancestor;
+ typedef ForwardPointerIterator<BaseType, Strategy> Self;
+
+ ForwardPointerIterator() : Ancestor() {}
+ ForwardPointerIterator(pointer p) : Ancestor(p) {}
+
+ operator pointer() const {
+ return const_cast<pointer>(Ancestor::operator->());
+ }
+ reference operator*() const {
+ return const_cast<reference>(Ancestor::operator*());
+ }
+ pointer operator->() const {
+ return const_cast<pointer>(Ancestor::operator->());
+ }
+
+ Self &operator++() {
+ Ancestor::operator++();
+ return *this;
+ }
+ Self operator++(int) {
+ Self old(*this);
+ operator++();
+ return old;
+ }
+};
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/list-container-test.h b/src/util/list-container-test.h
new file mode 100644
index 0000000..b46ed7b
--- /dev/null
+++ b/src/util/list-container-test.h
@@ -0,0 +1,258 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/** @file
+ * TODO: insert short description here
+ *//*
+ * Authors: see git history
+ *
+ * Copyright (C) 2010 Authors
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+#include <cxxtest/TestSuite.h>
+
+#include <stdarg.h>
+#include "util/list-container.h"
+
+#define ARRAY_RANGE(array) (array), (array)+sizeof((array))/sizeof((array)[0])
+
+static bool check_values(Inkscape::Util::ListContainer<int> const &c, unsigned n_values, ...) {
+ bool ret = true;
+ va_list args;
+ va_start(args, n_values);
+ Inkscape::Util::ListContainer<int>::const_iterator iter(c.begin());
+ while ( n_values && iter != c.end() ) {
+ int const value = va_arg(args, int);
+ if ( value != *iter ) {
+ ret = false;
+ }
+ if ( n_values == 1 && &c.back() != &*iter ) {
+ ret = false;
+ }
+ n_values--;
+ ++iter;
+ }
+ va_end(args);
+ return ret && n_values == 0 && iter == c.end();
+}
+
+class ListContainerTest : public CxxTest::TestSuite {
+public:
+ ListContainerTest()
+ {
+ Inkscape::GC::init();
+ }
+ virtual ~ListContainerTest() {}
+
+// createSuite and destroySuite get us per-suite setup and teardown
+// without us having to worry about static initialization order, etc.
+ static ListContainerTest *createSuite() { return new ListContainerTest(); }
+ static void destroySuite( ListContainerTest *suite ) { delete suite; }
+
+ void testRangeConstructor()
+ {
+ int const values[]={1,2,3,4};
+ int const * const values_end=values+4;
+ Inkscape::Util::ListContainer<int> container(values, values_end);
+
+ Inkscape::Util::ListContainer<int>::iterator container_iter=container.begin();
+ int const * values_iter=values;
+
+ while ( values_iter != values_end && container_iter != container.end() ) {
+ TS_ASSERT_EQUALS(*values_iter , *container_iter);
+ ++values_iter;
+ ++container_iter;
+ }
+
+ TS_ASSERT_EQUALS(values_iter , values_end);
+ TS_ASSERT_EQUALS(container_iter , container.end());
+ }
+
+ void testEqualityTests()
+ {
+ int const a[] = { 1, 2, 3, 4 };
+ int const b[] = { 1, 2, 3, 4 };
+ int const c[] = { 1, 2, 3 };
+ int const d[] = { 1, 2, 3, 5 };
+ Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
+ Inkscape::Util::ListContainer<int> c_b(ARRAY_RANGE(b));
+ Inkscape::Util::ListContainer<int> c_c(ARRAY_RANGE(c));
+ Inkscape::Util::ListContainer<int> c_d(ARRAY_RANGE(d));
+
+ TS_ASSERT(c_a == c_b);
+ TS_ASSERT(!( c_a != c_b ));
+ TS_ASSERT(!( c_a == c_c ));
+ TS_ASSERT(c_a != c_c);
+ TS_ASSERT(!( c_a == c_d ));
+ TS_ASSERT(c_a != c_d);
+ }
+
+ void testLessThan()
+ {
+ int const a[] = { 1, 2, 3, 4 };
+ int const b[] = { 1, 2, 2, 4 };
+ int const c[] = { 1, 2, 4, 4 };
+ int const d[] = { 1, 2, 3 };
+ Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
+ Inkscape::Util::ListContainer<int> c_b(ARRAY_RANGE(b));
+ Inkscape::Util::ListContainer<int> c_c(ARRAY_RANGE(c));
+ Inkscape::Util::ListContainer<int> c_d(ARRAY_RANGE(d));
+ TS_ASSERT(c_a >= c_b);
+ TS_ASSERT(!( c_a < c_b ));
+ TS_ASSERT(!( c_a >= c_c ));
+ TS_ASSERT(c_a < c_c);
+ TS_ASSERT(!( c_a < c_d ));
+ TS_ASSERT(c_a >= c_d);
+ TS_ASSERT(c_d < c_a);
+ }
+
+ void testAssignmentOperator()
+ {
+ int const a[] = { 1, 2, 3, 4 };
+ Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
+ Inkscape::Util::ListContainer<int> c_c;
+ TS_ASSERT(c_a != c_c);
+ c_c = c_a;
+ TS_ASSERT(c_a == c_c);
+ c_c = c_a;
+ TS_ASSERT(c_a == c_c);
+ }
+
+ void testFillConstructor()
+ {
+ Inkscape::Util::ListContainer<int> filled((std::size_t)3, 2);
+ TS_ASSERT(check_values(filled, 3, 2, 2, 2));
+ }
+
+ void testContainerSize()
+ {
+ // max_size() and size() return ListContainer<>::size_type which is unsigned int
+ Inkscape::Util::ListContainer<int> empty;
+ TS_ASSERT(empty.empty());
+ TS_ASSERT_EQUALS(empty.size(), 0u);
+ int const a[] = { 1, 2, 3 };
+ Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
+ TS_ASSERT(!c_a.empty());
+ TS_ASSERT_EQUALS(c_a.size(), 3u);
+
+ TS_ASSERT_LESS_THAN(0u, empty.max_size());
+ }
+
+ void testAppending()
+ {
+ Inkscape::Util::ListContainer<int> c;
+ c.push_back(1);
+ TS_ASSERT(check_values(c, 1, 1));
+ c.push_back(2);
+ TS_ASSERT(check_values(c, 2, 1, 2));
+ c.push_back(3);
+ TS_ASSERT(check_values(c, 3, 1, 2, 3));
+ }
+
+ void testBulkAppending()
+ {
+ int const a[] = { 1, 2, 3, 4 };
+ int const b[] = { 5, 6, 7 };
+ Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
+ Inkscape::Util::ListContainer<int> c_b(ARRAY_RANGE(b));
+ c_a.insert(c_a.end(), c_b.begin(), c_b.end());
+ TS_ASSERT(check_values(c_a, 7, 1, 2, 3, 4, 5, 6, 7));
+ }
+
+ void testPrepending()
+ {
+ Inkscape::Util::ListContainer<int> c;
+ c.push_front(1);
+ TS_ASSERT(check_values(c, 1, 1));
+ c.push_front(2);
+ TS_ASSERT(check_values(c, 2, 2, 1));
+ c.push_front(3);
+ TS_ASSERT(check_values(c, 3, 3, 2, 1));
+ }
+
+ void testSingleValueInsertion()
+ {
+ Inkscape::Util::ListContainer<int> c;
+
+ c.insert(c.begin(), 1);
+ TS_ASSERT(check_values(c, 1, 1));
+
+ c.insert(c.end(), 2);
+ TS_ASSERT(check_values(c, 2, 1, 2));
+
+ c.insert(c.begin(), 3);
+ TS_ASSERT(check_values(c, 3, 3, 1, 2));
+
+ Inkscape::Util::ListContainer<int>::iterator pos=c.begin();
+ ++pos;
+ c.insert(pos, 4);
+ TS_ASSERT(check_values(c, 4, 3, 4, 1, 2));
+ }
+
+ void testSingleValueErasure()
+ {
+ int const values[] = { 1, 2, 3, 4 };
+ Inkscape::Util::ListContainer<int> c(ARRAY_RANGE(values));
+
+ c.erase(c.begin());
+ TS_ASSERT(check_values(c, 3, 2, 3, 4));
+
+ Inkscape::Util::ListContainer<int>::iterator pos=c.begin();
+ ++pos;
+ c.erase(pos);
+ TS_ASSERT(check_values(c, 2, 2, 4));
+
+ pos=c.begin();
+ ++pos;
+ c.erase(pos);
+ TS_ASSERT(check_values(c, 1, 2));
+
+ c.erase(c.begin());
+ TS_ASSERT(check_values(c, 0));
+ }
+
+ void testPopFront()
+ {
+ int const full_ary[] = { 1, 2, 3 };
+ Inkscape::Util::ListContainer<int> t(ARRAY_RANGE(full_ary));
+ TS_ASSERT(check_values(t, 3, 1, 2, 3));
+ TS_ASSERT_EQUALS(t.back() , 3);
+ t.pop_front();
+ TS_ASSERT(check_values(t, 2, 2, 3));
+ TS_ASSERT_EQUALS(t.back() , 3);
+ t.push_back(23);
+ TS_ASSERT(check_values(t, 3, 2, 3, 23));
+ TS_ASSERT_EQUALS(t.back() , 23);
+ t.pop_front();
+ TS_ASSERT(check_values(t, 2, 3, 23));
+ TS_ASSERT_EQUALS(t.back() , 23);
+ t.pop_front();
+ TS_ASSERT(check_values(t, 1, 23));
+ TS_ASSERT_EQUALS(t.back() , 23);
+ t.pop_front();
+ TS_ASSERT(check_values(t, 0));
+ t.push_back(42);
+ TS_ASSERT(check_values(t, 1, 42));
+ TS_ASSERT_EQUALS(t.back() , 42);
+ }
+
+ void testEraseAfter()
+ {
+ int const full_ary[] = { 1, 2, 3, 4 };
+ int const exp_ary[] = { 1, 3, 4 };
+ Inkscape::Util::ListContainer<int> full_list(ARRAY_RANGE(full_ary));
+ Inkscape::Util::ListContainer<int> exp_list(ARRAY_RANGE(exp_ary));
+ TS_ASSERT(full_list != exp_list);
+ full_list.erase_after(full_list.begin());
+ TS_ASSERT(full_list == exp_list);
+ }
+};
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/list-container.h b/src/util/list-container.h
new file mode 100644
index 0000000..7002387
--- /dev/null
+++ b/src/util/list-container.h
@@ -0,0 +1,353 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::ListContainer - encapsulates lists as STL containers,
+ * providing fast appending
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2005 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_LIST_CONTAINER_H
+#define SEEN_INKSCAPE_UTIL_LIST_CONTAINER_H
+
+#include <limits>
+#include "util/list.h"
+
+namespace Inkscape {
+
+namespace Util {
+
+template <typename T>
+class ListContainer {
+public:
+ /* default constructible */
+ ListContainer() = default;
+
+ /* assignable */
+ ListContainer(ListContainer const &other) {
+ *this = other;
+ }
+ ListContainer &operator=(ListContainer const &other) {
+ clear();
+ for ( const_iterator iter = other.begin() ; iter ; ++iter ) {
+ push_back(*iter);
+ }
+ return *this;
+ }
+ void swap(ListContainer<T> &other) {
+ std::swap(other._head, _head);
+ std::swap(other._tail, _tail);
+ }
+
+ /* equality comparable */
+ bool operator==(ListContainer const &other) const {
+ const_iterator iter = _head;
+ const_iterator other_iter = other._head;
+ while ( iter && other_iter ) {
+ if (!( *iter == *other_iter )) {
+ return false;
+ }
+ ++iter;
+ ++other_iter;
+ }
+ return !iter && !other_iter;
+ }
+ bool operator!=(ListContainer const &other) const {
+ return !operator==(other);
+ }
+
+ /* lessthan comparable */
+ bool operator<(ListContainer const &other) const {
+ const_iterator iter = _head;
+ const_iterator other_iter = other._head;
+ while ( iter && other_iter ) {
+ if ( *iter < *other_iter ) {
+ return true;
+ } else if ( *other_iter < *iter ) {
+ return false;
+ }
+ ++iter;
+ ++other_iter;
+ }
+ return bool(other_iter);
+ }
+ bool operator>=(ListContainer const &other) const {
+ return !operator<(other);
+ }
+
+ /* container */
+ typedef typename List<T>::value_type value_type;
+ typedef List<T> iterator;
+ typedef List<T const> const_iterator;
+ typedef typename List<T>::reference reference;
+ typedef typename List<T>::const_reference const_reference;
+ typedef typename List<T>::pointer pointer;
+ typedef typename List<T>::difference_type difference_type;
+ typedef std::size_t size_type;
+
+ iterator begin() { return _head; }
+ const_iterator begin() const { return _head; }
+ iterator end() { return iterator(); }
+ const_iterator end() const { return const_iterator(); }
+ size_type size() const {
+ size_type size=0;
+ for ( iterator iter = _head ; iter ; ++iter ) {
+ size++;
+ }
+ return size;
+ }
+ size_type max_size() const {
+ return std::numeric_limits<std::size_t>::max();
+ }
+ bool empty() const { return !_head; }
+
+ /* sequence */
+ ListContainer(size_type count, const_reference value) {
+ for ( ; count ; --count ) {
+ push_back(value);
+ }
+ }
+ ListContainer(size_type count) {
+ value_type default_value;
+ for ( ; count ; --count ) {
+ push_back(default_value);
+ }
+ }
+ template <typename ForwardIterator>
+ ListContainer(ForwardIterator i, ForwardIterator j) {
+ for ( ; i != j ; ++i ) {
+ push_back(*i);
+ }
+ }
+
+ reference front() { return *_head; }
+ const_reference front() const { return *_head; }
+
+ iterator insert(const_iterator position, const_reference value) {
+ if (position) {
+ if ( position != _head ) {
+ MutableList<T> added(value);
+ MutableList<T> before=_before(position);
+ set_rest(added, rest(before));
+ set_rest(before, added);
+ return added;
+ } else {
+ push_front(value);
+ return _head;
+ }
+ } else {
+ push_back(value);
+ return _tail;
+ }
+ }
+ void insert(const_iterator position, size_type count, const_reference value)
+ {
+ _insert_from_temp(position, ListContainer(count, value));
+ }
+ template <typename ForwardIterator>
+ void insert(const_iterator position, ForwardIterator i, ForwardIterator j) {
+ _insert_from_temp(position, ListContainer(i, j));
+ }
+ void erase(const_iterator position) {
+ erase(position, rest(position));
+ }
+ void erase(const_iterator i, const_iterator j) {
+ if ( i == _head ) {
+ _head = static_cast<MutableList<T> &>(j);
+ if ( !j || !rest(j) ) {
+ _tail = _head;
+ }
+ } else {
+ MutableList<T> before=_before(i);
+ if (j) {
+ set_rest(before, static_cast<MutableList<T> &>(j));
+ } else {
+ set_rest(before, MutableList<T>());
+ _tail = before;
+ }
+ }
+ }
+ void clear() {
+ _head = _tail = MutableList<T>();
+ }
+ void resize(size_type size, const_reference fill) {
+ MutableList<T> before;
+ MutableList<T> iter;
+ for ( iter = _head ; iter && size ; ++iter ) {
+ before = iter;
+ size--;
+ }
+ if (size) {
+ ListContainer temp(size, fill);
+ if (empty()) {
+ _head = temp._head;
+ _tail = temp._tail;
+ } else {
+ set_rest(_tail, temp._head);
+ _tail = temp._tail;
+ }
+ } else if (iter) {
+ if (before) {
+ set_rest(before, MutableList<T>());
+ _tail = before;
+ } else {
+ _head = _tail = MutableList<T>();
+ }
+ }
+ }
+ void resize(size_type size) {
+ resize(size, value_type());
+ }
+
+ /* front insertion sequence */
+ void push_front(const_reference value) {
+ if (_head) {
+ _head = cons(value, _head);
+ } else {
+ _head = _tail = MutableList<T>(value);
+ }
+ }
+ void pop_front() {
+ if (_head) {
+ _head = rest(_head);
+ if (!_head) {
+ _tail = _head;
+ }
+ }
+ }
+
+ /* back insertion sequence */
+ reference back() { return *_tail; }
+ const_reference back() const { return *_tail; }
+ void push_back(const_reference value) {
+ if (_tail) {
+ MutableList<T> added(value);
+ set_rest(_tail, added);
+ _tail = added;
+ } else {
+ _head = _tail = MutableList<T>(value);
+ }
+ }
+ // we're not required to provide pop_back if we can't
+ // implement it efficiently
+
+ /* additional */
+ MutableList<T> detatchList() {
+ MutableList<T> list=_head;
+ _head = _tail = MutableList<T>();
+ return list;
+ }
+ iterator insert_after(const_iterator pos, const_reference value) {
+ MutableList<T> added(value);
+ if (pos) {
+ MutableList<T> before=static_cast<MutableList<T> &>(pos);
+ set_rest(added, rest(before));
+ set_rest(before, added);
+ if ( _tail == before ) {
+ _tail = added;
+ }
+ } else {
+ push_front(value);
+ }
+ }
+ void insert_after(const_iterator position, size_type count,
+ const_reference value)
+ {
+ _insert_after_from_temp(position, ListContainer(count, value));
+ }
+ template <typename ForwardIterator>
+ void insert_after(const_iterator position,
+ ForwardIterator i, ForwardIterator j)
+ {
+ _insert_after_from_temp(position, ListContainer(i, j));
+ }
+ void erase_after(const_iterator position) {
+ if (!position) {
+ pop_front();
+ } else {
+ MutableList<T> before=static_cast<MutableList<T> &>(position);
+ MutableList<T> removed=rest(before);
+ set_rest(before, rest(removed));
+ if ( removed == _tail ) {
+ _tail = before;
+ }
+ }
+ }
+
+private:
+ MutableList<T> _head;
+ MutableList<T> _tail;
+
+ MutableList<T> _before(const_iterator position) {
+ for ( MutableList<T> iter = _head ; iter ; ++iter ) {
+ if ( rest(iter) == position ) {
+ return iter;
+ }
+ }
+ return MutableList<T>();
+ }
+ void _insert_from_temp(const_iterator pos, ListContainer const &temp) {
+ if (temp.empty()) {
+ return;
+ }
+ if (empty()) { /* if empty, just take the whole thing */
+ _head = temp._head;
+ _tail = temp._tail;
+ } else if (pos) {
+ if ( pos == _head ) { /* prepend */
+ set_rest(temp._tail, _head);
+ _head = temp._head;
+ } else { /* insert somewhere in the middle */
+ MutableList<T> before=_before(pos);
+ set_rest(temp._tail, static_cast<MutableList<T> &>(pos));
+ set_rest(before, temp._head);
+ }
+ } else { /* append */
+ set_rest(_tail, temp._head);
+ _tail = temp._tail;
+ }
+ }
+ void _insert_after_from_temp(const_iterator pos,
+ ListContainer const &temp)
+ {
+ if (temp.empty()) {
+ return;
+ }
+ if (empty()) { /* if empty, just take the whole thing */
+ _head = temp._head;
+ _tail = temp._tail;
+ } else if (pos) {
+ if ( pos == _tail ) { /* append */
+ set_rest(_tail, temp._head);
+ _tail = temp._tail;
+ } else { /* insert somewhere in the middle */
+ MutableList<T> before=static_cast<MutableList<T> &>(pos);
+ set_rest(temp._tail, rest(before));
+ set_rest(before, temp._head);
+ }
+ } else { /* prepend */
+ set_rest(temp._tail, _head);
+ _head = temp._head;
+ }
+ }
+};
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/list-copy.h b/src/util/list-copy.h
new file mode 100644
index 0000000..51e6af4
--- /dev/null
+++ b/src/util/list-copy.h
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Traits::ListCopy - helper traits class for copying lists
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_TRAITS_LIST_COPY_H
+#define SEEN_INKSCAPE_TRAITS_LIST_COPY_H
+
+#include <iterator>
+#include "util/copy.h"
+#include "util/list.h"
+
+namespace Inkscape {
+
+namespace Traits {
+
+template <typename InputIterator>
+struct ListCopy {
+ typedef typename Copy<
+ typename std::iterator_traits<InputIterator>::value_type
+ >::Type ResultValue;
+ typedef typename Util::MutableList<ResultValue> ResultList;
+};
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/list.h b/src/util/list.h
new file mode 100644
index 0000000..68f410b
--- /dev/null
+++ b/src/util/list.h
@@ -0,0 +1,413 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_LIST_H
+#define SEEN_INKSCAPE_UTIL_LIST_H
+
+#include <cstddef>
+#include <iterator>
+#include "inkgc/gc-managed.h"
+#include "util/reference.h"
+
+namespace Inkscape {
+
+namespace Util {
+
+/// Generic ListCell for Inkscape::Util::List.
+template <typename T>
+struct ListCell : public GC::Managed<> {
+ ListCell() = default;
+ ListCell(typename Traits::Reference<T>::RValue v, ListCell *n)
+ : value(v), next(n) {}
+
+ T value;
+ ListCell *next;
+};
+
+template <typename T> class List;
+template <typename T> class MutableList;
+
+template <typename T>
+bool is_empty(List<T> const &list);
+
+template <typename T>
+typename List<T>::reference first(List<T> const &list);
+
+template <typename T>
+List<T> const &rest(List<T> const &list);
+
+template <typename T>
+MutableList<T> &rest(MutableList<T> const &list);
+
+template <typename T>
+MutableList<T> const &set_rest(MutableList<T> const &list,
+ MutableList<T> const &rest);
+
+/// Helper template.
+template <typename T>
+class List<T const> {
+public:
+ typedef std::forward_iterator_tag iterator_category;
+ typedef T const value_type;
+ typedef std::ptrdiff_t difference_type;
+ typedef typename Traits::Reference<value_type>::LValue reference;
+ typedef typename Traits::Reference<value_type>::RValue const_reference;
+ typedef typename Traits::Reference<value_type>::Pointer pointer;
+
+ List() : _cell(nullptr) {}
+ explicit List(const_reference value, List const &next=List())
+ : _cell(new ListCell<T>(value, next._cell)) {}
+
+ operator bool() const { return this->_cell; }
+
+ reference operator*() const { return this->_cell->value; }
+ pointer operator->() const { return &this->_cell->value; }
+
+ bool operator==(List const &other) const {
+ return this->_cell == other._cell;
+ }
+ bool operator!=(List const &other) const {
+ return this->_cell != other._cell;
+ }
+
+ List &operator++() {
+ this->_cell = this->_cell->next;
+ return *this;
+ }
+ List operator++(int) {
+ List old(*this);
+ this->_cell = this->_cell->next;
+ return old;
+ }
+
+ friend reference first<>(List const &);
+ friend List const &rest<>(List const &);
+ friend bool is_empty<>(List const &);
+
+protected:
+ ListCell<T> *_cell;
+};
+
+/**
+ * Generic linked list.
+ *
+ * These lists are designed to store simple values like pointers,
+ * references, and scalar values. While they can be used to directly
+ * store more complex objects, destructors for those objects will not
+ * be called unless those objects derive from Inkscape::GC::Finalized.
+ *
+ * In general it's better to use lists to store pointers or references
+ * to objects requiring finalization and manage object lifetimes separately.
+ *
+ * @see Inkscape::GC::Finalized
+ *
+ * cons() is synonymous with List<T>(first, rest), except that the
+ * compiler will usually be able to infer T from the type of \a rest.
+ *
+ * If you need to create an empty list (which can, for example, be used
+ * as an 'end' value with STL algorithms), call the List<> constructor
+ * with no arguments, like so:
+ *
+ * <code> List<int>() </code>
+ */
+template <typename T>
+class List : public List<T const> {
+public:
+ typedef T value_type;
+ typedef typename Traits::Reference<value_type>::LValue reference;
+ typedef typename Traits::Reference<value_type>::RValue const_reference;
+ typedef typename Traits::Reference<value_type>::Pointer pointer;
+
+ List() : List<T const>() {}
+ explicit List(const_reference value, List const &next=List())
+ : List<T const>(value, next) {}
+
+ reference operator*() const { return this->_cell->value; }
+ pointer operator->() const { return &this->_cell->value; }
+
+ List &operator++() {
+ this->_cell = this->_cell->next;
+ return *this;
+ }
+ List operator++(int) {
+ List old(*this);
+ this->_cell = this->_cell->next;
+ return old;
+ }
+
+ friend reference first<>(List const &);
+ friend List const &rest<>(List const &);
+ friend bool is_empty<>(List const &);
+};
+
+/// Helper template.
+template <typename T>
+class List<T &> {
+public:
+ typedef std::forward_iterator_tag iterator_category;
+ typedef T &value_type;
+ typedef std::ptrdiff_t difference_type;
+ typedef typename Traits::Reference<value_type>::LValue reference;
+ typedef typename Traits::Reference<value_type>::RValue const_reference;
+ typedef typename Traits::Reference<value_type>::Pointer pointer;
+
+ List() : _cell(nullptr) {}
+ List(const_reference value, List const &next=List())
+ : _cell(new ListCell<T &>(value, next._cell)) {}
+
+ operator bool() const { return this->_cell; }
+
+ reference operator*() const { return this->_cell->value; }
+ pointer operator->() const { return &this->_cell->value; }
+
+ bool operator==(List const &other) const {
+ return this->_cell == other._cell;
+ }
+ bool operator!=(List const &other) const {
+ return this->_cell != other._cell;
+ }
+
+ List &operator++() {
+ this->_cell = this->_cell->next;
+ return *this;
+ }
+ List operator++(int) {
+ List old(*this);
+ this->_cell = this->_cell->next;
+ return old;
+ }
+
+ friend reference first<>(List const &);
+ friend List const &rest<>(List const &);
+ friend bool is_empty<>(List const &);
+
+protected:
+ ListCell<T &> *_cell;
+};
+
+/**
+ * Generic MutableList.
+ *
+ * Like a linked list, but one whose tail can be exchanged for
+ * another later by using set_rest() or assignment through rest()
+ * as an lvalue. It's otherwise identical to the "non-mutable" form.
+ *
+ * As with List, you can create an empty list like so:
+ *
+ * <code> MutableList<int>() </code>
+ */
+template <typename T>
+class MutableList : public List<T> {
+public:
+ MutableList() = default;
+ explicit MutableList(typename List<T>::const_reference value,
+ MutableList const &next=MutableList())
+ : List<T>(value, next) {}
+
+ MutableList &operator++() {
+ this->_cell = this->_cell->next;
+ return *this;
+ }
+ MutableList operator++(int) {
+ MutableList old(*this);
+ this->_cell = this->_cell->next;
+ return old;
+ }
+
+ friend MutableList &rest<>(MutableList const &);
+ friend MutableList const &set_rest<>(MutableList const &,
+ MutableList const &);
+};
+
+/**
+ * Creates a (non-empty) linked list.
+ *
+ * Creates a new linked list with a copy of the given value (\a first)
+ * in its first element; the remainder of the list will be the list
+ * provided as \a rest.
+ *
+ * The remainder of the list -- the "tail" -- is incorporated by
+ * reference rather than being copied.
+ *
+ * The returned value can also be treated as an STL forward iterator.
+ *
+ * @param first the value for the first element of the list
+ * @param rest the rest of the list; may be an empty list
+ *
+ * @return a new list
+ *
+ * @see List<>
+ * @see is_empty<>
+ *
+ */
+template <typename T>
+inline List<T> cons(typename Traits::Reference<T>::RValue first,
+ List<T> const &rest)
+{
+ return List<T>(first, rest);
+}
+
+/**
+ * Creates a (non-empty) linked list whose tail can be exchanged
+ * for another.
+ *
+ * Creates a new linked list, but one whose tail can be exchanged for
+ * another later by using set_rest() or assignment through rest()
+ * as an lvalue. It's otherwise identical to the "non-mutable" form.
+ *
+ * This form of cons() is synonymous with MutableList<T>(first, rest),
+ * except that the compiler can usually infer T from the type of \a rest.
+ *
+ * As with List<>, you can create an empty list like so:
+ *
+ * MutableList<int>()
+ *
+ * @see MutableList<>
+ * @see is_empty<>
+ *
+ * @param first the value for the first element of the list
+ * @param rest the rest of the list; may be an empty list
+ *
+ * @return a new list
+ */
+template <typename T>
+inline MutableList<T> cons(typename Traits::Reference<T>::RValue first,
+ MutableList<T> const &rest)
+{
+ return MutableList<T>(first, rest);
+}
+
+/**
+ * Returns true if the given list is empty.
+ *
+ * Returns true if the given list is empty. This is equivalent
+ * to !list.
+ *
+ * @param list the list
+ *
+ * @return true if the list is empty, false otherwise.
+ */
+template <typename T>
+inline bool is_empty(List<T> const &list) { return !list._cell; }
+
+/**
+ * Returns the first value in a linked list.
+ *
+ * Returns a reference to the first value in the list. This
+ * corresponds to the value of the first argument passed to cons().
+ *
+ * If the list holds mutable values (or references to them), first()
+ * can be used as an lvalue.
+ *
+ * For example:
+ *
+ * first(list) = value;
+ *
+ * The results of calling this on an empty list are undefined.
+ *
+ * @see cons<>
+ * @see is_empty<>
+ *
+ * @param list the list; cannot be empty
+ *
+ * @return a reference to the first value in the list
+ */
+template <typename T>
+inline typename List<T>::reference first(List<T> const &list) {
+ return list._cell->value;
+}
+
+/**
+ * Returns the remainder of a linked list after the first element.
+ *
+ * Returns the remainder of the list after the first element (its "tail").
+ *
+ * This will be the same as the second argument passed to cons().
+ *
+ * The results of calling this on an empty list are undefined.
+ *
+ * @see cons<>
+ * @see is_empty<>
+ *
+ * @param list the list; cannot be empty
+ *
+ * @return the remainder of the list
+ */
+template <typename T>
+inline List<T> const &rest(List<T> const &list) {
+ return reinterpret_cast<List<T> const &>(list._cell->next);
+}
+
+/**
+ * Returns a reference to the remainder of a linked list after
+ * the first element.
+ *
+ * Returns a reference to the remainder of the list after the first
+ * element (its "tail"). For MutableList<>, rest() can be used as
+ * an lvalue, to set a new tail.
+ *
+ * For example:
+ *
+ * rest(list) = other;
+ *
+ * Results of calling this on an empty list are undefined.
+ *
+ * @see cons<>
+ * @see is_empty<>
+ *
+ * @param list the list; cannot be empty
+ *
+ * @return a reference to the remainder of the list
+ */
+template <typename T>
+inline MutableList<T> &rest(MutableList<T> const &list) {
+ return reinterpret_cast<MutableList<T> &>(list._cell->next);
+}
+
+/**
+ * Sets a new tail for an existing linked list.
+ *
+ * Sets the tail of the given MutableList<>, corresponding to the
+ * second argument of cons().
+ *
+ * Results of calling this on an empty list are undefined.
+ *
+ * @see rest<>
+ * @see cons<>
+ * @see is_empty<>
+ *
+ * @param list the list; cannot be empty
+ * @param rest the new tail; corresponds to the second argument of cons()
+ *
+ * @return the new tail
+ */
+template <typename T>
+inline MutableList<T> const &set_rest(MutableList<T> const &list,
+ MutableList<T> const &rest)
+{
+ list._cell->next = rest._cell;
+ return reinterpret_cast<MutableList<T> &>(list._cell->next);
+}
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/longest-common-suffix.h b/src/util/longest-common-suffix.h
new file mode 100644
index 0000000..bc72e6f
--- /dev/null
+++ b/src/util/longest-common-suffix.h
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Algorithms::longest_common_suffix
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_ALGORITHMS_LONGEST_COMMON_SUFFIX_H
+#define SEEN_INKSCAPE_ALGORITHMS_LONGEST_COMMON_SUFFIX_H
+
+#include <iterator>
+#include <functional>
+#include "util/list.h"
+
+namespace Inkscape {
+
+namespace Algorithms {
+
+/**
+ * Time costs:
+ *
+ * The case of sharing a common successor is handled in O(1) time.
+ *
+ * If \a a is the longest common suffix, then runs in O(len(rest of b)) time.
+ *
+ * Otherwise, runs in O(len(a) + len(b)) time.
+ */
+
+template <typename ForwardIterator>
+ForwardIterator longest_common_suffix(ForwardIterator a, ForwardIterator b,
+ ForwardIterator end)
+{
+ typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
+ return longest_common_suffix(a, b, end, std::equal_to<value_type>());
+}
+
+template <typename ForwardIterator, typename BinaryPredicate>
+ForwardIterator longest_common_suffix(ForwardIterator a, ForwardIterator b,
+ ForwardIterator end, BinaryPredicate pred)
+{
+ if ( a == end || b == end ) {
+ return end;
+ }
+
+ /* Handle in O(1) time the common cases of identical lists or tails. */
+ {
+ /* identical lists? */
+ if ( a == b ) {
+ return a;
+ }
+
+ /* identical tails? */
+ ForwardIterator tail_a(a);
+ ForwardIterator tail_b(b);
+ if ( ++tail_a == ++tail_b ) {
+ return tail_a;
+ }
+ }
+
+ /* Build parallel lists of suffixes, ordered by increasing length. */
+
+ using Inkscape::Util::List;
+ using Inkscape::Util::cons;
+ ForwardIterator lists[2] = { a, b };
+ List<ForwardIterator> suffixes[2];
+
+ for ( int i=0 ; i < 2 ; i++ ) {
+ for ( ForwardIterator iter(lists[i]) ; iter != end ; ++iter ) {
+ if ( iter == lists[1-i] ) {
+ // the other list is a suffix of this one
+ return lists[1-i];
+ }
+
+ suffixes[i] = cons(iter, suffixes[i]);
+ }
+ }
+
+ /* Iterate in parallel through the lists of suffix lists from shortest to
+ * longest, stopping before the first pair of suffixes that differs
+ */
+
+ ForwardIterator longest_common(end);
+
+ while ( suffixes[0] && suffixes[1] &&
+ pred(**suffixes[0], **suffixes[1]) )
+ {
+ longest_common = *suffixes[0];
+ ++suffixes[0];
+ ++suffixes[1];
+ }
+
+ return longest_common;
+}
+
+}
+
+}
+
+#endif /* !SEEN_INKSCAPE_ALGORITHMS_LONGEST_COMMON_SUFFIX_H */
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
diff --git a/src/util/reference.h b/src/util/reference.h
new file mode 100644
index 0000000..b4a123b
--- /dev/null
+++ b/src/util/reference.h
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Traits::Reference - traits class for dealing with reference types
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_TRAITS_REFERENCE_H
+#define SEEN_INKSCAPE_TRAITS_REFERENCE_H
+
+namespace Inkscape {
+
+namespace Traits {
+
+template <typename T>
+struct Reference {
+ typedef T const &RValue;
+ typedef T &LValue;
+ typedef T *Pointer;
+ typedef T const *ConstPointer;
+};
+
+template <typename T>
+struct Reference<T &> {
+ typedef T &RValue;
+ typedef T &LValue;
+ typedef T *Pointer;
+ typedef T const *ConstPointer;
+};
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/reverse-list.h b/src/util/reverse-list.h
new file mode 100644
index 0000000..c016408
--- /dev/null
+++ b/src/util/reverse-list.h
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::reverse_list - generate a reversed list from iterator range
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_REVERSE_LIST_H
+#define SEEN_INKSCAPE_UTIL_REVERSE_LIST_H
+
+#include "util/list.h"
+#include "util/list-copy.h"
+
+namespace Inkscape {
+
+namespace Util {
+
+template <typename InputIterator>
+inline typename Traits::ListCopy<InputIterator>::ResultList
+reverse_list(InputIterator start, InputIterator end) {
+ typename Traits::ListCopy<InputIterator>::ResultList head;
+ while ( start != end ) {
+ head = cons(*start, head);
+ ++start;
+ }
+ return head;
+}
+
+template <typename T>
+inline typename Traits::ListCopy<List<T> >::ResultList
+reverse_list(List<T> const &list) {
+ return reverse_list(list, List<T>());
+}
+
+template <typename T>
+inline MutableList<T>
+reverse_list_in_place(MutableList<T> start,
+ MutableList<T> end=MutableList<T>())
+{
+ MutableList<T> reversed(end);
+ while ( start != end ) {
+ MutableList<T> temp(start);
+ ++start;
+ set_rest(temp, reversed);
+ reversed = temp;
+ }
+ return reversed;
+}
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/share.cpp b/src/util/share.cpp
new file mode 100644
index 0000000..6c436dc
--- /dev/null
+++ b/src/util/share.cpp
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::ptr_shared<T> - like T const *, but stronger.
+ * Used to hold c-style strings for objects that are managed by the gc.
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2006 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include "util/share.h"
+#include <glib.h>
+
+namespace Inkscape {
+namespace Util {
+
+ptr_shared share_string(char const *string) {
+ g_return_val_if_fail(string != nullptr, share_unsafe(nullptr));
+ return share_string(string, std::strlen(string));
+}
+
+ptr_shared share_string(char const *string, std::size_t length) {
+ g_return_val_if_fail(string != nullptr, share_unsafe(nullptr));
+ char *new_string=new (GC::ATOMIC) char[length+1];
+ std::memcpy(new_string, string, length);
+ new_string[length] = 0;
+ return share_unsafe(new_string);
+}
+
+}
+}
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/share.h b/src/util/share.h
new file mode 100644
index 0000000..e8b9fb1
--- /dev/null
+++ b/src/util/share.h
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::ptr_shared<T> - like T const *, but stronger.
+ * Used to hold c-style strings for objects that are managed by the gc.
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2006 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_SHARE_H
+#define SEEN_INKSCAPE_UTIL_SHARE_H
+
+#include "inkgc/gc-core.h"
+#include <cstring>
+#include <cstddef>
+
+namespace Inkscape {
+namespace Util {
+
+class ptr_shared {
+public:
+
+ ptr_shared() : _string(nullptr) {}
+ ptr_shared(ptr_shared const &other) = default;
+
+ operator char const *() const { return _string; }
+ operator bool() const { return _string; }
+
+ char const *pointer() const { return _string; }
+ char const &operator[](int i) const { return _string[i]; }
+
+ ptr_shared operator+(int i) const {
+ return share_unsafe(_string+i);
+ }
+ ptr_shared operator-(int i) const {
+ return share_unsafe(_string-i);
+ }
+ //WARNING: No bounds checking in += and -= functions. Moving the pointer
+ //past the end of the string and then back could probably cause the garbage
+ //collector to deallocate the string inbetween, as there's temporary no
+ //valid reference pointing into the allocated space.
+ ptr_shared &operator+=(int i) {
+ _string += i;
+ return *this;
+ }
+ ptr_shared &operator-=(int i) {
+ _string -= i;
+ return *this;
+ }
+ std::ptrdiff_t operator-(ptr_shared const &other) {
+ return _string - other._string;
+ }
+
+ ptr_shared &operator=(ptr_shared const &other) = default;
+
+ bool operator==(ptr_shared const &other) const {
+ return _string == other._string;
+ }
+ bool operator!=(ptr_shared const &other) const {
+ return _string != other._string;
+ }
+ bool operator>(ptr_shared const &other) const {
+ return _string > other._string;
+ }
+ bool operator<(ptr_shared const &other) const {
+ return _string < other._string;
+ }
+
+ friend ptr_shared share_unsafe(char const *string);
+
+private:
+ ptr_shared(char const *string) : _string(string) {}
+ static ptr_shared share_unsafe(char const *string) {
+ return ptr_shared(string);
+ }
+
+ //This class (and code using it) assumes that it never has to free this
+ //pointer, and that the memory it points to will not be freed as long as a
+ //ptr_shared pointing to it exists.
+ char const *_string;
+};
+
+ptr_shared share_string(char const *string);
+ptr_shared share_string(char const *string, std::size_t length);
+
+inline ptr_shared share_unsafe(char const *string) {
+ return ptr_shared::share_unsafe(string);
+}
+
+//TODO: Do we need this function?
+inline ptr_shared share_static_string(char const *string) {
+ return share_unsafe(string);
+}
+
+}
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/signal-blocker.h b/src/util/signal-blocker.h
new file mode 100644
index 0000000..8fb6256
--- /dev/null
+++ b/src/util/signal-blocker.h
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Base RAII blocker for sgic++ signals.
+ *
+ * Authors:
+ * Jon A. Cruz <jon@joncruz.org>
+ *
+ * Copyright (C) 2014 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H
+#define SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H
+
+#include <string>
+#include <sigc++/connection.h>
+
+/**
+ * Base RAII blocker for sgic++ signals.
+ */
+class SignalBlocker
+{
+public:
+ /**
+ * Creates a new instance that if the signal is currently unblocked will block
+ * it until this instance is destructed and then will unblock it.
+ */
+ SignalBlocker( sigc::connection *connection ) :
+ _connection(connection),
+ _wasBlocked(_connection->blocked())
+ {
+ if (!_wasBlocked)
+ {
+ _connection->block();
+ }
+ }
+
+ /**
+ * Destructor that will unblock the signal if it was blocked initially by this
+ * instance.
+ */
+ ~SignalBlocker()
+ {
+ if (!_wasBlocked)
+ {
+ _connection->block(false);
+ }
+ }
+
+private:
+ // noncopyable, nonassignable
+ SignalBlocker(SignalBlocker const &other) = delete;
+ SignalBlocker& operator=(SignalBlocker const &other) = delete;
+
+ sigc::connection *_connection;
+ bool _wasBlocked;
+};
+
+#endif // SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/ucompose.hpp b/src/util/ucompose.hpp
new file mode 100644
index 0000000..e0d2fc2
--- /dev/null
+++ b/src/util/ucompose.hpp
@@ -0,0 +1,452 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/** @file
+ * TODO: insert short description here
+ *//*
+ * Authors: see git history
+ *
+ * Copyright (C) 2006 Authors
+ * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information.
+ */
+/* Defines String::ucompose(fmt, arg...) for easy, i18n-friendly
+ * composition of strings with Gtkmm >= 1.3.* (see www.gtkmm.org).
+ * Uses Glib::ustring instead of std::string which doesn't work with
+ * Gtkmm due to character encoding troubles with stringstreams.
+ *
+ * Version 1.0.4.
+ *
+ * Copyright (c) 2002, 03, 04 Ole Laursen <olau@hardworking.dk>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA.
+ */
+
+//
+// Basic usage is like
+//
+// String::ucompose("This is a %1x%2 matrix.", rows, cols);
+//
+// See http://www.cs.aau.dk/~olau/compose/ or the included
+// README.compose for more details.
+//
+
+#ifndef STRING_UCOMPOSE_HPP
+#define STRING_UCOMPOSE_HPP
+
+#include <glibmm/ustring.h>
+#include <glibmm/convert.h>
+
+#include <stdexcept>
+#include <sstream>
+#include <string>
+#include <list>
+#include <map> // for multimap
+
+namespace UStringPrivate
+{
+ // the actual composition class - using String::ucompose is cleaner, so we
+ // hide it here
+ class Composition
+ {
+ public:
+ // initialize and prepare format string on the form "text %1 text %2 etc."
+ explicit Composition(std::string fmt);
+
+ // supply an replacement argument starting from %1
+ template <typename T>
+ Composition &arg(const T &obj);
+
+ // compose and return string
+ Glib::ustring str() const;
+
+ private:
+
+ //This is standard, not GCC-specific like wostringstream
+ std::basic_ostringstream<wchar_t> os;
+
+ int arg_no;
+
+ // we store the output as a list - when the output string is requested, the
+ // list is concatenated to a string; this way we can keep iterators into
+ // the list instead of into a string where they're possibly invalidated
+ // when inserting a specification string
+ typedef std::list<std::string> output_list;
+ output_list output;
+
+ // the initial parse of the format string fills in the specification map
+ // with positions for each of the various %?s
+ typedef std::multimap<int, output_list::iterator> specification_map;
+ specification_map specs;
+
+ template <typename T>
+ std::string stringify(T obj);
+ };
+
+ // helper for converting spec string numbers
+ inline int char_to_int(char c)
+ {
+ switch (c) {
+ case '0': return 0;
+ case '1': return 1;
+ case '2': return 2;
+ case '3': return 3;
+ case '4': return 4;
+ case '5': return 5;
+ case '6': return 6;
+ case '7': return 7;
+ case '8': return 8;
+ case '9': return 9;
+ default: return -1000;
+ }
+ }
+
+ inline bool is_number(int n)
+ {
+ switch (n) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ return true;
+
+ default:
+ return false;
+ }
+ }
+
+ template <typename T>
+ inline std::string Composition::stringify(T obj)
+ {
+ os << obj;
+
+ std::wstring str = os.str();
+
+ return Glib::convert(std::string(reinterpret_cast<const char *>(str.data()),
+ str.size() * sizeof(wchar_t)),
+ "UTF-8", "WCHAR_T");
+ }
+
+ // specialisations for the common string types
+ template <>
+ inline std::string
+ Composition::stringify<std::string>(std::string obj)
+ {
+ return obj;
+ }
+
+ template <>
+ inline std::string
+ Composition::stringify<Glib::ustring>(Glib::ustring obj)
+ {
+ return obj;
+ }
+
+ template <>
+ inline std::string
+ Composition::stringify<const char *>(const char *obj)
+ {
+ return obj;
+ }
+
+ // implementation of class Composition
+ template <typename T>
+ inline Composition &Composition::arg(const T &obj)
+ {
+ Glib::ustring rep = stringify(obj);
+
+ if (!rep.empty()) { // manipulators don't produce output
+ for (specification_map::const_iterator i = specs.lower_bound(arg_no),
+ end = specs.upper_bound(arg_no); i != end; ++i) {
+ output_list::iterator pos = i->second;
+ ++pos;
+
+ output.insert(pos, rep);
+ }
+
+ os.str(std::wstring());
+ //os.clear();
+ ++arg_no;
+ }
+
+ return *this;
+ }
+
+ inline Composition::Composition(std::string fmt)
+ : arg_no(1)
+ {
+#if __GNUC__ >= 3
+ try {
+ os.imbue(std::locale("")); // use the user's locale for the stream
+ }
+ catch (std::runtime_error& e) { // fallback to classic if it failed
+ os.imbue(std::locale::classic());
+ }
+#endif
+ std::string::size_type b = 0, i = 0;
+
+ // fill in output with the strings between the %1 %2 %3 etc. and
+ // fill in specs with the positions
+ while (i < fmt.length()) {
+ if (fmt[i] == '%' && i + 1 < fmt.length()) {
+ if (fmt[i + 1] == '%') { // catch %%
+ fmt.replace(i, 2, "%");
+ ++i;
+ }
+ else if (is_number(fmt[i + 1])) { // aha! a spec!
+ // save string
+ output.push_back(fmt.substr(b, i - b));
+
+ int n = 1; // number of digits
+ int spec_no = 0;
+
+ do {
+ spec_no += char_to_int(fmt[i + n]);
+ spec_no *= 10;
+ ++n;
+ } while (i + n < fmt.length() && is_number(fmt[i + n]));
+
+ spec_no /= 10;
+ output_list::iterator pos = output.end();
+ --pos; // safe since we have just inserted a string
+
+ specs.insert(specification_map::value_type(spec_no, pos));
+
+ // jump over spec string
+ i += n;
+ b = i;
+ }
+ else
+ ++i;
+ }
+ else
+ ++i;
+ }
+
+ if (i - b > 0) // add the rest of the string
+ output.push_back(fmt.substr(b, i - b));
+ }
+
+ inline Glib::ustring Composition::str() const
+ {
+ // assemble string
+ std::string str;
+
+ for (const auto & i : output)
+ str += i;
+
+ return str;
+ }
+}
+
+
+namespace String
+{
+ // a series of functions which accept a format string on the form "text %1
+ // more %2 less %3" and a number of templated parameters and spits out the
+ // composited string
+ template <typename T1>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt, const T1 &o1)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1);
+ return c.str();
+ }
+
+ template <typename T1, typename T2>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8, typename T9>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8, const T9 &o9)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8).arg(o9);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8, typename T9, typename T10>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8, const T9 &o9,
+ const T10 &o10)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8).arg(o9)
+ .arg(o10);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8, typename T9, typename T10,
+ typename T11>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8, const T9 &o9,
+ const T10 &o10, const T11 &o11)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8).arg(o9)
+ .arg(o10).arg(o11);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8, typename T9, typename T10,
+ typename T11, typename T12>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8, const T9 &o9,
+ const T10 &o10, const T11 &o11, const T12 &o12)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8).arg(o9)
+ .arg(o10).arg(o11).arg(o12);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8, typename T9, typename T10,
+ typename T11, typename T12, typename T13>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8, const T9 &o9,
+ const T10 &o10, const T11 &o11, const T12 &o12,
+ const T13 &o13)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8).arg(o9)
+ .arg(o10).arg(o11).arg(o12).arg(o13);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8, typename T9, typename T10,
+ typename T11, typename T12, typename T13, typename T14>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8, const T9 &o9,
+ const T10 &o10, const T11 &o11, const T12 &o12,
+ const T13 &o13, const T14 &o14)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8).arg(o9)
+ .arg(o10).arg(o11).arg(o12).arg(o13).arg(o14);
+ return c.str();
+ }
+
+ template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7, typename T8, typename T9, typename T10,
+ typename T11, typename T12, typename T13, typename T14,
+ typename T15>
+ inline Glib::ustring ucompose(const Glib::ustring &fmt,
+ const T1 &o1, const T2 &o2, const T3 &o3,
+ const T4 &o4, const T5 &o5, const T6 &o6,
+ const T7 &o7, const T8 &o8, const T9 &o9,
+ const T10 &o10, const T11 &o11, const T12 &o12,
+ const T13 &o13, const T14 &o14, const T15 &o15)
+ {
+ UStringPrivate::Composition c(fmt);
+ c.arg(o1).arg(o2).arg(o3).arg(o4).arg(o5).arg(o6).arg(o7).arg(o8).arg(o9)
+ .arg(o10).arg(o11).arg(o12).arg(o13).arg(o14).arg(o15);
+ return c.str();
+ }
+}
+
+
+#endif // STRING_UCOMPOSE_HPP
diff --git a/src/util/units.cpp b/src/util/units.cpp
new file mode 100644
index 0000000..d24ec3c
--- /dev/null
+++ b/src/util/units.cpp
@@ -0,0 +1,578 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape Units
+ *
+ * Authors:
+ * Matthew Petroff <matthew@mpetroff.net>
+ *
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include <cmath>
+#include <cerrno>
+#include <iomanip>
+#include <iostream>
+#include <utility>
+#include <unordered_map>
+#include <glib.h>
+#include <glibmm/regex.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/markup.h>
+
+#include <2geom/coord.h>
+
+#include "util/units.h"
+#include "path-prefix.h"
+#include "streq.h"
+
+using Inkscape::Util::UNIT_TYPE_DIMENSIONLESS;
+using Inkscape::Util::UNIT_TYPE_LINEAR;
+using Inkscape::Util::UNIT_TYPE_RADIAL;
+using Inkscape::Util::UNIT_TYPE_FONT_HEIGHT;
+
+namespace
+{
+
+#define MAKE_UNIT_CODE(a, b) \
+ ((((unsigned)(a) & 0xdf) << 8) | ((unsigned)(b) & 0xdf))
+
+enum UnitCode {
+ UNIT_CODE_PX = MAKE_UNIT_CODE('p','x'),
+ UNIT_CODE_PT = MAKE_UNIT_CODE('p','t'),
+ UNIT_CODE_PC = MAKE_UNIT_CODE('p','c'),
+ UNIT_CODE_MM = MAKE_UNIT_CODE('m','m'),
+ UNIT_CODE_CM = MAKE_UNIT_CODE('c','m'),
+ UNIT_CODE_IN = MAKE_UNIT_CODE('i','n'),
+ UNIT_CODE_EM = MAKE_UNIT_CODE('e','m'),
+ UNIT_CODE_EX = MAKE_UNIT_CODE('e','x'),
+ UNIT_CODE_PERCENT = MAKE_UNIT_CODE('%',0)
+};
+
+// TODO: convert to constexpr in C++11, so that the above constants can be eliminated
+inline unsigned make_unit_code(char a, char b) {
+ // this should work without the casts, but let's be 100% sure
+ // also ensure that the codes are in lowercase
+ return MAKE_UNIT_CODE(a,b);
+}
+inline unsigned make_unit_code(char const *str) {
+ if (!str || str[0] == 0) return 0;
+ return MAKE_UNIT_CODE(str[0], str[1]);
+}
+
+
+// This must match SVGLength::Unit
+unsigned const svg_length_lookup[] = {
+ 0,
+ UNIT_CODE_PX,
+ UNIT_CODE_PT,
+ UNIT_CODE_PC,
+ UNIT_CODE_MM,
+ UNIT_CODE_CM,
+ UNIT_CODE_IN,
+ UNIT_CODE_EM,
+ UNIT_CODE_EX,
+ UNIT_CODE_PERCENT
+};
+
+
+
+// maps unit codes obtained from their abbreviations to their SVGLength unit indexes
+typedef std::unordered_map<unsigned, SVGLength::Unit> UnitCodeLookup;
+
+UnitCodeLookup make_unit_code_lookup()
+{
+ UnitCodeLookup umap;
+ for (unsigned i = 1; i < G_N_ELEMENTS(svg_length_lookup); ++i) {
+ umap[svg_length_lookup[i]] = static_cast<SVGLength::Unit>(i);
+ }
+ return umap;
+}
+
+UnitCodeLookup const unit_code_lookup = make_unit_code_lookup();
+
+
+
+typedef std::unordered_map<Glib::ustring, Inkscape::Util::UnitType> TypeMap;
+
+/** A std::map that gives the data type value for the string version.
+ * @todo consider hiding map behind hasFoo() and getFoo() type functions. */
+TypeMap make_type_map()
+{
+ TypeMap tmap;
+ tmap["DIMENSIONLESS"] = UNIT_TYPE_DIMENSIONLESS;
+ tmap["LINEAR"] = UNIT_TYPE_LINEAR;
+ tmap["RADIAL"] = UNIT_TYPE_RADIAL;
+ tmap["FONT_HEIGHT"] = UNIT_TYPE_FONT_HEIGHT;
+ // Note that code was not yet handling LINEAR_SCALED, TIME, QTY and NONE
+
+ return tmap;
+}
+
+TypeMap const type_map = make_type_map();
+
+} // namespace
+
+namespace Inkscape {
+namespace Util {
+
+class UnitParser : public Glib::Markup::Parser
+{
+public:
+ typedef Glib::Markup::Parser::AttributeMap AttrMap;
+ typedef Glib::Markup::ParseContext Ctx;
+
+ UnitParser(UnitTable *table);
+ ~UnitParser() override = default;
+
+protected:
+ void on_start_element(Ctx &ctx, Glib::ustring const &name, AttrMap const &attrs) override;
+ void on_end_element(Ctx &ctx, Glib::ustring const &name) override;
+ void on_text(Ctx &ctx, Glib::ustring const &text) override;
+
+public:
+ UnitTable *tbl;
+ bool primary;
+ bool skip;
+ Unit unit;
+};
+
+UnitParser::UnitParser(UnitTable *table) :
+ tbl(table),
+ primary(false),
+ skip(false)
+{
+}
+
+#define BUFSIZE (255)
+
+Unit::Unit() :
+ type(UNIT_TYPE_DIMENSIONLESS), // should this or NONE be the default?
+ factor(1.0),
+ name(),
+ name_plural(),
+ abbr(),
+ description()
+{
+}
+
+Unit::Unit(UnitType type,
+ double factor,
+ Glib::ustring name,
+ Glib::ustring name_plural,
+ Glib::ustring abbr,
+ Glib::ustring description)
+ : type(type)
+ , factor(factor)
+ , name(std::move(name))
+ , name_plural(std::move(name_plural))
+ , abbr(std::move(abbr))
+ , description(std::move(description))
+{
+ g_return_if_fail(factor <= 0);
+}
+
+void Unit::clear()
+{
+ *this = Unit();
+}
+
+int Unit::defaultDigits() const
+{
+ int factor_digits = int(log10(factor));
+ if (factor_digits < 0) {
+ g_warning("factor = %f, factor_digits = %d", factor, factor_digits);
+ g_warning("factor_digits < 0 - returning 0");
+ factor_digits = 0;
+ }
+ return factor_digits;
+}
+
+bool Unit::compatibleWith(Unit const *u) const
+{
+ // Percentages
+ if (type == UNIT_TYPE_DIMENSIONLESS || u->type == UNIT_TYPE_DIMENSIONLESS) {
+ return true;
+ }
+
+ // Other units with same type
+ if (type == u->type) {
+ return true;
+ }
+
+ // Different, incompatible types
+ return false;
+}
+bool Unit::compatibleWith(Glib::ustring const &u) const
+{
+ return compatibleWith(unit_table.getUnit(u));
+}
+
+bool Unit::operator==(Unit const &other) const
+{
+ return (type == other.type && name.compare(other.name) == 0);
+}
+
+int Unit::svgUnit() const
+{
+ char const *astr = abbr.c_str();
+ unsigned code = make_unit_code(astr);
+
+ UnitCodeLookup::const_iterator u = unit_code_lookup.find(code);
+ if (u != unit_code_lookup.end()) {
+ return u->second;
+ }
+ return 0;
+}
+
+double Unit::convert(double from_dist, Unit const *to) const
+{
+ // Percentage
+ if (to->type == UNIT_TYPE_DIMENSIONLESS) {
+ return from_dist * to->factor;
+ }
+
+ // Incompatible units
+ if (type != to->type) {
+ return -1;
+ }
+
+ // Compatible units
+ return from_dist * factor / to->factor;
+}
+double Unit::convert(double from_dist, Glib::ustring const &to) const
+{
+ return convert(from_dist, unit_table.getUnit(to));
+}
+double Unit::convert(double from_dist, char const *to) const
+{
+ return convert(from_dist, unit_table.getUnit(to));
+}
+
+
+
+Unit UnitTable::_empty_unit;
+
+UnitTable::UnitTable()
+{
+ gchar *filename = g_build_filename(INKSCAPE_UIDIR, "units.xml", NULL);
+ load(filename);
+ g_free(filename);
+}
+
+UnitTable::~UnitTable()
+{
+ for (auto & iter : _unit_map)
+ {
+ delete iter.second;
+ }
+}
+
+void UnitTable::addUnit(Unit const &u, bool primary)
+{
+ _unit_map[make_unit_code(u.abbr.c_str())] = new Unit(u);
+ if (primary) {
+ _primary_unit[u.type] = u.abbr;
+ }
+}
+
+Unit const *UnitTable::getUnit(char const *abbr) const
+{
+ UnitCodeMap::const_iterator f = _unit_map.find(make_unit_code(abbr));
+ if (f != _unit_map.end()) {
+ return &(*f->second);
+ }
+ return &_empty_unit;
+}
+
+Unit const *UnitTable::getUnit(Glib::ustring const &unit_abbr) const
+{
+ return getUnit(unit_abbr.c_str());
+}
+Unit const *UnitTable::getUnit(SVGLength::Unit u) const
+{
+ if (u == 0 || u > SVGLength::LAST_UNIT) {
+ return &_empty_unit;
+ }
+
+ UnitCodeMap::const_iterator f = _unit_map.find(svg_length_lookup[u]);
+ if (f != _unit_map.end()) {
+ return &(*f->second);
+ }
+ return &_empty_unit;
+}
+
+Unit const *UnitTable::findUnit(double factor, UnitType type) const
+{
+ const double eps = factor * 0.01; // allow for 1% deviation
+
+ UnitCodeMap::const_iterator cit = _unit_map.begin();
+ while (cit != _unit_map.end()) {
+ if (cit->second->type == type) {
+ if (Geom::are_near(cit->second->factor, factor, eps)) {
+ // unit found!
+ break;
+ }
+ }
+ ++cit;
+ }
+
+ if (cit != _unit_map.end()) {
+ return cit->second;
+ } else {
+ return getUnit(_primary_unit[type]);
+ }
+}
+
+Quantity UnitTable::parseQuantity(Glib::ustring const &q) const
+{
+ Glib::MatchInfo match_info;
+
+ // Extract value
+ double value = 0;
+ Glib::RefPtr<Glib::Regex> value_regex = Glib::Regex::create("[-+]*[\\d+]*[\\.,]*[\\d+]*[eE]*[-+]*\\d+");
+ if (value_regex->match(q, match_info)) {
+ std::istringstream tmp_v(match_info.fetch(0));
+ tmp_v >> value;
+ }
+ int start_pos, end_pos;
+ match_info.fetch_pos(0, end_pos, start_pos);
+ end_pos = q.size() - start_pos;
+ Glib::ustring u = q.substr(start_pos, end_pos);
+
+ // Extract unit abbreviation
+ Glib::ustring abbr;
+ Glib::RefPtr<Glib::Regex> unit_regex = Glib::Regex::create("[A-z%]+");
+ if (unit_regex->match(u, match_info)) {
+ abbr = match_info.fetch(0);
+ }
+
+ Quantity qty(value, abbr);
+ return qty;
+}
+
+/* UNSAFE while passing around pointers to the Unit objects in this table
+bool UnitTable::deleteUnit(Unit const &u)
+{
+ bool deleted = false;
+ // Cannot delete the primary unit type since it's
+ // used for conversions
+ if (u.abbr != _primary_unit[u.type]) {
+ UnitCodeMap::iterator iter = _unit_map.find(make_unit_code(u.abbr.c_str()));
+ if (iter != _unit_map.end()) {
+ delete (*iter).second;
+ _unit_map.erase(iter);
+ deleted = true;
+ }
+ }
+ return deleted;
+}
+*/
+
+bool UnitTable::hasUnit(Glib::ustring const &unit) const
+{
+ UnitCodeMap::const_iterator iter = _unit_map.find(make_unit_code(unit.c_str()));
+ return (iter != _unit_map.end());
+}
+
+UnitTable::UnitMap UnitTable::units(UnitType type) const
+{
+ UnitMap submap;
+ for (auto iter : _unit_map) {
+ if (iter.second->type == type) {
+ submap.insert(UnitMap::value_type(iter.second->abbr, *iter.second));
+ }
+ }
+
+ return submap;
+}
+
+Glib::ustring UnitTable::primary(UnitType type) const
+{
+ return _primary_unit[type];
+}
+
+bool UnitTable::load(std::string const &filename) {
+ UnitParser uparser(this);
+ Glib::Markup::ParseContext ctx(uparser);
+
+ try {
+ Glib::ustring unitfile = Glib::file_get_contents(filename);
+ ctx.parse(unitfile);
+ ctx.end_parse();
+ } catch (Glib::FileError const &e) {
+ g_warning("Units file %s is missing: %s\n", filename.c_str(), e.what().c_str());
+ return false;
+ } catch (Glib::MarkupError const &e) {
+ g_warning("Problem loading units file '%s': %s\n", filename.c_str(), e.what().c_str());
+ return false;
+ }
+ return true;
+}
+
+/*
+bool UnitTable::save(std::string const &filename) {
+ g_warning("UnitTable::save(): not implemented");
+
+ return false;
+}
+*/
+
+Inkscape::Util::UnitTable unit_table;
+
+void UnitParser::on_start_element(Ctx &/*ctx*/, Glib::ustring const &name, AttrMap const &attrs)
+{
+ if (name == "unit") {
+ // reset for next use
+ unit.clear();
+ primary = false;
+ skip = false;
+
+ AttrMap::const_iterator f;
+ if ((f = attrs.find("type")) != attrs.end()) {
+ Glib::ustring type = f->second;
+ TypeMap::const_iterator tf = type_map.find(type);
+ if (tf != type_map.end()) {
+ unit.type = tf->second;
+ } else {
+ g_warning("Skipping unknown unit type '%s'.\n", type.c_str());
+ skip = true;
+ }
+ }
+ if ((f = attrs.find("pri")) != attrs.end()) {
+ primary = (f->second[0] == 'y' || f->second[0] == 'Y');
+ }
+ }
+}
+
+void UnitParser::on_text(Ctx &ctx, Glib::ustring const &text)
+{
+ Glib::ustring element = ctx.get_element();
+ if (element == "name") {
+ unit.name = text;
+ } else if (element == "plural") {
+ unit.name_plural = text;
+ } else if (element == "abbr") {
+ unit.abbr = text;
+ } else if (element == "factor") {
+ // TODO make sure we use the right conversion
+ unit.factor = g_ascii_strtod(text.c_str(), nullptr);
+ } else if (element == "description") {
+ unit.description = text;
+ }
+}
+
+void UnitParser::on_end_element(Ctx &/*ctx*/, Glib::ustring const &name)
+{
+ if (name == "unit" && !skip) {
+ tbl->addUnit(unit, primary);
+ }
+}
+
+Quantity::Quantity(double q, Unit const *u)
+ : unit(u)
+ , quantity(q)
+{
+}
+Quantity::Quantity(double q, Glib::ustring const &u)
+ : unit(unit_table.getUnit(u.c_str()))
+ , quantity(q)
+{
+}
+Quantity::Quantity(double q, char const *u)
+ : unit(unit_table.getUnit(u))
+ , quantity(q)
+{
+}
+
+bool Quantity::compatibleWith(Unit const *u) const
+{
+ return unit->compatibleWith(u);
+}
+bool Quantity::compatibleWith(Glib::ustring const &u) const
+{
+ return compatibleWith(u.c_str());
+}
+bool Quantity::compatibleWith(char const *u) const
+{
+ return compatibleWith(unit_table.getUnit(u));
+}
+
+double Quantity::value(Unit const *u) const
+{
+ return convert(quantity, unit, u);
+}
+double Quantity::value(Glib::ustring const &u) const
+{
+ return value(u.c_str());
+}
+double Quantity::value(char const *u) const
+{
+ return value(unit_table.getUnit(u));
+}
+
+Glib::ustring Quantity::string(Unit const *u) const {
+ return Glib::ustring::format(std::fixed, std::setprecision(2), value(u)) + " " + u->abbr;
+}
+Glib::ustring Quantity::string(Glib::ustring const &u) const {
+ return string(unit_table.getUnit(u.c_str()));
+}
+Glib::ustring Quantity::string() const {
+ return string(unit);
+}
+
+double Quantity::convert(double from_dist, Unit const *from, Unit const *to)
+{
+ return from->convert(from_dist, to);
+}
+double Quantity::convert(double from_dist, Glib::ustring const &from, Unit const *to)
+{
+ return convert(from_dist, unit_table.getUnit(from.c_str()), to);
+}
+double Quantity::convert(double from_dist, Unit const *from, Glib::ustring const &to)
+{
+ return convert(from_dist, from, unit_table.getUnit(to.c_str()));
+}
+double Quantity::convert(double from_dist, Glib::ustring const &from, Glib::ustring const &to)
+{
+ return convert(from_dist, unit_table.getUnit(from.c_str()), unit_table.getUnit(to.c_str()));
+}
+double Quantity::convert(double from_dist, char const *from, char const *to)
+{
+ return convert(from_dist, unit_table.getUnit(from), unit_table.getUnit(to));
+}
+
+bool Quantity::operator<(Quantity const &rhs) const
+{
+ if (unit->type != rhs.unit->type) {
+ g_warning("Incompatible units");
+ return false;
+ }
+ return quantity < rhs.value(unit);
+}
+bool Quantity::operator==(Quantity const &other) const
+{
+ /** \fixme This is overly strict. I think we should change this to:
+ if (unit->type != other.unit->type) {
+ g_warning("Incompatible units");
+ return false;
+ }
+ return are_near(quantity, other.value(unit));
+ */
+ return (*unit == *other.unit) && (quantity == other.quantity);
+}
+
+} // namespace Util
+} // namespace Inkscape
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/units.h b/src/util/units.h
new file mode 100644
index 0000000..48478c7
--- /dev/null
+++ b/src/util/units.h
@@ -0,0 +1,231 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape Units
+ * These classes are used for defining different unit systems.
+ *
+ * Authors:
+ * Matthew Petroff <matthew@mpetroff.net>
+ *
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef INKSCAPE_UTIL_UNITS_H
+#define INKSCAPE_UTIL_UNITS_H
+
+#include <unordered_map>
+#include <boost/operators.hpp>
+#include <glibmm/ustring.h>
+#include <2geom/coord.h>
+#include "svg/svg-length.h"
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+
+#define DEFAULT_UNIT_NAME "mm";
+
+namespace std {
+template <>
+struct hash<Glib::ustring> : public std::unary_function<Glib::ustring, std::size_t> {
+ std::size_t operator()(Glib::ustring const &s) const {
+ return hash<std::string>()(s.raw());
+ }
+};
+} // namespace std
+
+#endif
+
+namespace Inkscape {
+namespace Util {
+
+enum UnitType {
+ UNIT_TYPE_DIMENSIONLESS, /* Percentage */
+ UNIT_TYPE_LINEAR,
+ UNIT_TYPE_LINEAR_SCALED,
+ UNIT_TYPE_RADIAL,
+ UNIT_TYPE_TIME,
+ UNIT_TYPE_FONT_HEIGHT,
+ UNIT_TYPE_QTY,
+ UNIT_TYPE_NONE = -1
+};
+
+const char DEG[] = "°";
+
+class Unit
+ : boost::equality_comparable<Unit>
+{
+public:
+ Unit();
+ Unit(UnitType type,
+ double factor,
+ Glib::ustring name,
+ Glib::ustring name_plural,
+ Glib::ustring abbr,
+ Glib::ustring description);
+
+ void clear();
+
+ bool isAbsolute() const { return type != UNIT_TYPE_DIMENSIONLESS; }
+
+ /**
+ * Returns the suggested precision to use for displaying numbers
+ * of this unit.
+ */
+ int defaultDigits() const;
+
+ /** Checks if a unit is compatible with the specified unit. */
+ bool compatibleWith(Unit const *u) const;
+ bool compatibleWith(Glib::ustring const &) const;
+ bool compatibleWith(char const *) const;
+
+ UnitType type;
+ double factor;
+ Glib::ustring name;
+ Glib::ustring name_plural;
+ Glib::ustring abbr;
+ Glib::ustring description;
+
+ /** Check if units are equal. */
+ bool operator==(Unit const &other) const;
+
+ /** Get SVG unit code. */
+ int svgUnit() const;
+
+ /** Convert value from this unit **/
+ double convert(double from_dist, Unit const *to) const;
+ double convert(double from_dist, Glib::ustring const &to) const;
+ double convert(double from_dist, char const *to) const;
+};
+
+class Quantity
+ : boost::totally_ordered<Quantity>
+{
+public:
+ Unit const *unit;
+ double quantity;
+
+ /** Initialize a quantity. */
+ Quantity(double q, Unit const *u);
+ Quantity(double q, Glib::ustring const &u);
+ Quantity(double q, char const *u);
+
+ /** Checks if a quantity is compatible with the specified unit. */
+ bool compatibleWith(Unit const *u) const;
+ bool compatibleWith(Glib::ustring const &u) const;
+ bool compatibleWith(char const *u) const;
+
+ /** Return the quantity's value in the specified unit. */
+ double value(Unit const *u) const;
+ double value(Glib::ustring const &u) const;
+ double value(char const *u) const;
+
+ /** Return a printable string of the value in the specified unit. */
+ Glib::ustring string(Unit const *u) const;
+ Glib::ustring string(Glib::ustring const &u) const;
+ Glib::ustring string() const;
+
+ /** Convert distances.
+ no NULL check is performed on the passed pointers to Unit objects! */
+ static double convert(double from_dist, Unit const *from, Unit const *to);
+ static double convert(double from_dist, Glib::ustring const &from, Unit const *to);
+ static double convert(double from_dist, Unit const *from, Glib::ustring const &to);
+ static double convert(double from_dist, Glib::ustring const &from, Glib::ustring const &to);
+ static double convert(double from_dist, char const *from, char const *to);
+
+ /** Comparison operators. */
+ bool operator<(Quantity const &rhs) const;
+ bool operator==(Quantity const &other) const;
+};
+
+inline bool are_near(Quantity const &a, Quantity const &b, double eps=Geom::EPSILON)
+{
+ return Geom::are_near(a.quantity, b.value(a.unit), eps);
+}
+
+class UnitTable {
+public:
+ /**
+ * Initializes the unit tables and identifies the primary unit types.
+ *
+ * The primary unit's conversion factor is required to be 1.00
+ */
+ UnitTable();
+ virtual ~UnitTable();
+
+ typedef std::unordered_map<Glib::ustring, Unit> UnitMap;
+ typedef std::unordered_map<unsigned, Unit*> UnitCodeMap;
+
+ /** Add a new unit to the table */
+ void addUnit(Unit const &u, bool primary);
+
+ /** Retrieve a given unit based on its string identifier */
+ Unit const *getUnit(Glib::ustring const &name) const;
+ Unit const *getUnit(char const *name) const;
+
+ /** Try to find a unit based on its conversion factor to the primary */
+ Unit const *findUnit(double factor, UnitType type) const;
+
+ /** Retrieve a given unit based on its SVGLength unit */
+ Unit const *getUnit(SVGLength::Unit u) const;
+
+ /** Retrieve a quantity based on its string identifier */
+ Quantity parseQuantity(Glib::ustring const &q) const;
+
+ /** Remove a unit definition from the given unit type table * /
+ * DISABLED, unsafe with the current passing around pointers to Unit objects in this table */
+ //bool deleteUnit(Unit const &u);
+
+ /** Returns true if the given string 'name' is a valid unit in the table */
+ bool hasUnit(Glib::ustring const &name) const;
+
+ /** Provides an iteratable list of items in the given unit table */
+ UnitMap units(UnitType type) const;
+
+ /** Returns the default unit abbr for the given type */
+ Glib::ustring primary(UnitType type) const;
+
+ double getScale() const;
+
+ void setScale();
+
+ /** Load units from an XML file.
+ *
+ * Loads and merges the contents of the given file into the UnitTable,
+ * possibly overwriting existing unit definitions.
+ *
+ * @param filename file to be loaded
+ */
+ bool load(std::string const &filename);
+
+ /* * Saves the current UnitTable to the given file. */
+ //bool save(std::string const &filename);
+
+protected:
+ UnitCodeMap _unit_map;
+ Glib::ustring _primary_unit[UNIT_TYPE_QTY];
+
+ double _linear_scale;
+ static Unit _empty_unit;
+
+private:
+ UnitTable(UnitTable const &t);
+ UnitTable operator=(UnitTable const &t);
+
+};
+
+extern UnitTable unit_table;
+
+} // namespace Util
+} // namespace Inkscape
+
+#endif // define INKSCAPE_UTIL_UNITS_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/ziptool.cpp b/src/util/ziptool.cpp
new file mode 100644
index 0000000..8eb5254
--- /dev/null
+++ b/src/util/ziptool.cpp
@@ -0,0 +1,3039 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/** @file
+ * TODO: insert short description here
+ *//*
+ * Authors:
+ * see git history
+ * Bob Jamison
+ *
+ * Copyright (C) 2018 Authors
+ * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information.
+ */
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+/*
+ * This is intended to be a standalone, reduced capability
+ * implementation of Gzip and Zip functionality. Its
+ * targeted use case is for archiving and retrieving single files
+ * which use these encoding types. Being memory based and
+ * non-optimized, it is not useful in cases where very large
+ * archives are needed or where high performance is desired.
+ * However, it should hopefully work very well for smaller,
+ * one-at-a-time tasks. What you get in return is the ability
+ * to drop these files into your project and remove the dependencies
+ * on ZLib and Info-Zip. Enjoy.
+ */
+
+
+#include <cstdio>
+#include <cstdarg>
+#include <ctime>
+
+#include <string>
+#include <utility>
+
+#include "ziptool.h"
+
+
+
+
+
+
+//########################################################################
+//# A D L E R 3 2
+//########################################################################
+
+/**
+ * Constructor
+ */
+Adler32::Adler32()
+{
+ reset();
+}
+
+/**
+ * Destructor
+ */
+Adler32::~Adler32()
+= default;
+
+/**
+ * Reset Adler-32 checksum to initial value.
+ */
+void Adler32::reset()
+{
+ value = 1;
+}
+
+// ADLER32_BASE is the largest prime number smaller than 65536
+#define ADLER32_BASE 65521
+
+void Adler32::update(unsigned char b)
+{
+ unsigned long s1 = value & 0xffff;
+ unsigned long s2 = (value >> 16) & 0xffff;
+ s1 += b & 0xff;
+ s2 += s1;
+ value = ((s2 % ADLER32_BASE) << 16) | (s1 % ADLER32_BASE);
+}
+
+void Adler32::update(char *str)
+{
+ if (str)
+ while (*str)
+ update((unsigned char)*str++);
+}
+
+
+/**
+ * Returns current checksum value.
+ */
+unsigned long Adler32::getValue()
+{
+ return value & 0xffffffffL;
+}
+
+
+
+//########################################################################
+//# C R C 3 2
+//########################################################################
+
+/**
+ * Constructor
+ */
+Crc32::Crc32()
+{
+ reset();
+}
+
+/**
+ * Destructor
+ */
+Crc32::~Crc32()
+= default;
+
+static bool crc_table_ready = false;
+static unsigned long crc_table[256];
+
+/**
+ * make the table for a fast CRC.
+ */
+static void makeCrcTable()
+{
+ if (crc_table_ready)
+ return;
+ for (int n = 0; n < 256; n++)
+ {
+ unsigned long c = n;
+ for (int k = 8; --k >= 0; )
+ {
+ if ((c & 1) != 0)
+ c = 0xedb88320 ^ (c >> 1);
+ else
+ c >>= 1;
+ }
+ crc_table[n] = c;
+ }
+ crc_table_ready = true;
+}
+
+
+/**
+ * Reset CRC-32 checksum to initial value.
+ */
+void Crc32::reset()
+{
+ value = 0;
+ makeCrcTable();
+}
+
+void Crc32::update(unsigned char b)
+{
+ unsigned long c = ~value;
+
+ c &= 0xffffffff;
+ c = crc_table[(c ^ b) & 0xff] ^ (c >> 8);
+ value = ~c;
+}
+
+
+void Crc32::update(char *str)
+{
+ if (str)
+ while (*str)
+ update((unsigned char)*str++);
+}
+
+void Crc32::update(const std::vector<unsigned char> &buf)
+{
+ std::vector<unsigned char>::const_iterator iter;
+ for (iter=buf.begin() ; iter!=buf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ update(ch);
+ }
+}
+
+
+/**
+ * Returns current checksum value.
+ */
+unsigned long Crc32::getValue()
+{
+ return value & 0xffffffffL;
+}
+
+//########################################################################
+//# I N F L A T E R
+//########################################################################
+
+
+/**
+ *
+ */
+struct Huffman
+{
+ int *count; // number of symbols of each length
+ int *symbol; // canonically ordered symbols
+};
+
+/**
+ *
+ */
+class Inflater
+{
+public:
+
+ Inflater();
+
+ virtual ~Inflater();
+
+ static const int MAXBITS = 15; // max bits in a code
+ static const int MAXLCODES = 286; // max number of literal/length codes
+ static const int MAXDCODES = 30; // max number of distance codes
+ static const int MAXCODES = 316; // max codes lengths to read
+ static const int FIXLCODES = 288; // number of fixed literal/length codes
+
+ /**
+ *
+ */
+ bool inflate(std::vector<unsigned char> &destination,
+ std::vector<unsigned char> &source);
+
+private:
+
+ /**
+ *
+ */
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ /**
+ *
+ */
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ /**
+ *
+ */
+ void dump();
+
+ /**
+ *
+ */
+ int buildHuffman(Huffman *h, int *length, int n);
+
+ /**
+ *
+ */
+ bool getBits(int need, int *oval);
+
+ /**
+ *
+ */
+ int doDecode(Huffman *h);
+
+ /**
+ *
+ */
+ bool doCodes(Huffman *lencode, Huffman *distcode);
+
+ /**
+ *
+ */
+ bool doStored();
+
+ /**
+ *
+ */
+ bool doFixed();
+
+ /**
+ *
+ */
+ bool doDynamic();
+
+
+ std::vector<unsigned char>dest;
+
+ std::vector<unsigned char>src;
+ unsigned long srcPos; //current read position
+ int bitBuf;
+ int bitCnt;
+
+};
+
+
+/**
+ *
+ */
+Inflater::Inflater() :
+ dest(),
+ src(),
+ srcPos(0),
+ bitBuf(0),
+ bitCnt(0)
+{
+}
+
+/**
+ *
+ */
+Inflater::~Inflater()
+= default;
+
+/**
+ *
+ */
+void Inflater::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Inflater error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ *
+ */
+void Inflater::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Inflater:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+
+/**
+ *
+ */
+void Inflater::dump()
+{
+ for (unsigned char i : dest)
+ {
+ fputc(i, stdout);
+ }
+}
+
+/**
+ *
+ */
+int Inflater::buildHuffman(Huffman *h, int *length, int n)
+{
+ // count number of codes of each length
+ for (int len = 0; len <= MAXBITS; len++)
+ h->count[len] = 0;
+ for (int symbol = 0; symbol < n; symbol++)
+ (h->count[length[symbol]])++; // assumes lengths are within bounds
+ if (h->count[0] == n) // no codes!
+ {
+ error("huffman tree will result in failed decode");
+ return -1;
+ }
+
+ // check for an over-subscribed or incomplete set of lengths
+ int left = 1; // number of possible codes left of current length
+ for (int len = 1; len <= MAXBITS; len++)
+ {
+ left <<= 1; // one more bit, double codes left
+ left -= h->count[len]; // deduct count from possible codes
+ if (left < 0)
+ {
+ error("huffman over subscribed");
+ return -1;
+ }
+ }
+
+ // generate offsets into symbol table for each length for sorting
+ int offs[MAXBITS+1]; //offsets in symbol table for each length
+ offs[1] = 0;
+ for (int len = 1; len < MAXBITS; len++)
+ offs[len + 1] = offs[len] + h->count[len];
+
+ /*
+ * put symbols in table sorted by length, by symbol order within each
+ * length
+ */
+ for (int symbol = 0; symbol < n; symbol++)
+ if (length[symbol] != 0)
+ h->symbol[offs[length[symbol]]++] = symbol;
+
+ // return zero for complete set, positive for incomplete set
+ return left;
+}
+
+
+/**
+ *
+ */
+bool Inflater::getBits(int requiredBits, int *oval)
+{
+ long val = bitBuf;
+
+ //add more bytes if needed
+ while (bitCnt < requiredBits)
+ {
+ if (srcPos >= src.size())
+ {
+ error("premature end of input");
+ return false;
+ }
+ val |= ((long)(src[srcPos++])) << bitCnt;
+ bitCnt += 8;
+ }
+
+ //update the buffer and return the data
+ bitBuf = (int)(val >> requiredBits);
+ bitCnt -= requiredBits;
+ *oval = (int)(val & ((1L << requiredBits) - 1));
+
+ return true;
+}
+
+
+/**
+ *
+ */
+int Inflater::doDecode(Huffman *h)
+{
+ int bitTmp = bitBuf;
+ int left = bitCnt;
+ int code = 0;
+ int first = 0;
+ int index = 0;
+ int len = 1;
+ int *next = h->count + 1;
+ while (true)
+ {
+ while (left--)
+ {
+ code |= bitTmp & 1;
+ bitTmp >>= 1;
+ int count = *next++;
+ if (code < first + count)
+ { /* if length len, return symbol */
+ bitBuf = bitTmp;
+ bitCnt = (bitCnt - len) & 7;
+ return h->symbol[index + (code - first)];
+ }
+ index += count;
+ first += count;
+ first <<= 1;
+ code <<= 1;
+ len++;
+ }
+ left = (MAXBITS+1) - len;
+ if (left == 0)
+ break;
+ if (srcPos >= src.size())
+ {
+ error("premature end of input");
+ dump();
+ return -1;
+ }
+ bitTmp = src[srcPos++];
+ if (left > 8)
+ left = 8;
+ }
+
+ error("no end of block found");
+ return -1;
+}
+
+/**
+ *
+ */
+bool Inflater::doCodes(Huffman *lencode, Huffman *distcode)
+{
+ static const int lens[29] = { // Size base for length codes 257..285
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
+ static const int lext[29] = { // Extra bits for length codes 257..285
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
+ 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
+ static const int dists[30] = { // Offset base for distance codes 0..29
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+ 8193, 12289, 16385, 24577};
+ static const int dext[30] = { // Extra bits for distance codes 0..29
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
+ 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
+ 12, 12, 13, 13};
+
+ //decode literals and length/distance pairs
+ while (true)
+ {
+ int symbol = doDecode(lencode);
+ if (symbol == 256)
+ break;
+ if (symbol < 0)
+ {
+ return false;
+ }
+ if (symbol < 256) //literal
+ {
+ dest.push_back(symbol);
+ }
+ else if (symbol > 256)//length
+ {
+ symbol -= 257;
+ if (symbol >= 29)
+ {
+ error("invalid fixed code");
+ return false;
+ }
+ int ret;
+ if (!getBits(lext[symbol], &ret))
+ return false;
+ int len = lens[symbol] + ret;
+
+ symbol = doDecode(distcode);//distance
+ if (symbol < 0)
+ {
+ return false;
+ }
+
+ if (!getBits(dext[symbol], &ret))
+ return false;
+ unsigned int dist = dists[symbol] + ret;
+ if (dist > dest.size())
+ {
+ error("distance too far back %d/%d", dist, dest.size());
+ dump();
+ //printf("pos:%d\n", srcPos);
+ return false;
+ }
+
+ // copy length bytes from distance bytes back
+ //dest.push_back('{');
+ while (len--)
+ {
+ dest.push_back(dest[dest.size() - dist]);
+ }
+ //dest.push_back('}');
+
+ }
+ }
+
+ return true;
+}
+
+/**
+ */
+bool Inflater::doStored()
+{
+ //trace("### stored ###");
+
+ // clear bits from current byte
+ bitBuf = 0;
+ bitCnt = 0;
+
+ // length
+ if (srcPos + 4 > src.size())
+ {
+ error("not enough input");
+ return false;
+ }
+
+ int len = src[srcPos++];
+ len |= src[srcPos++] << 8;
+ //trace("### len:%d", len);
+ // check complement
+ if (src[srcPos++] != (~len & 0xff) ||
+ src[srcPos++] != ((~len >> 8) & 0xff))
+ {
+ error("twos complement for storage size do not match");
+ return false;
+ }
+
+ // copy data
+ if (srcPos + len > src.size())
+ {
+ error("Not enough input for stored block");
+ return false;
+ }
+ while (len--)
+ dest.push_back(src[srcPos++]);
+
+ return true;
+}
+
+/**
+ */
+bool Inflater::doFixed()
+{
+ //trace("### fixed ###");
+
+ static bool firstTime = true;
+ static int lencnt[MAXBITS+1], lensym[FIXLCODES];
+ static int distcnt[MAXBITS+1], distsym[MAXDCODES];
+ static Huffman lencode = {lencnt, lensym};
+ static Huffman distcode = {distcnt, distsym};
+
+ if (firstTime)
+ {
+ firstTime = false;
+
+ int lengths[FIXLCODES];
+
+ // literal/length table
+ int symbol = 0;
+ for ( ; symbol < 144; symbol++)
+ lengths[symbol] = 8;
+ for ( ; symbol < 256; symbol++)
+ lengths[symbol] = 9;
+ for ( ; symbol < 280; symbol++)
+ lengths[symbol] = 7;
+ for ( ; symbol < FIXLCODES; symbol++)
+ lengths[symbol] = 8;
+ buildHuffman(&lencode, lengths, FIXLCODES);
+
+ // distance table
+ for (int symbol = 0; symbol < MAXDCODES; symbol++)
+ lengths[symbol] = 5;
+ buildHuffman(&distcode, lengths, MAXDCODES);
+ }
+
+ // decode data until end-of-block code
+ bool ret = doCodes(&lencode, &distcode);
+ return ret;
+}
+
+/**
+ */
+bool Inflater::doDynamic()
+{
+ //trace("### dynamic ###");
+ int lengths[MAXCODES]; // descriptor code lengths
+ int lencnt[MAXBITS+1], lensym[MAXLCODES]; // lencode memory
+ int distcnt[MAXBITS+1], distsym[MAXDCODES]; // distcode memory
+ Huffman lencode = {lencnt, lensym}; // length code
+ Huffman distcode = {distcnt, distsym}; // distance code
+ static const int order[19] = // permutation of code length codes
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+ // get number of lengths in each table, check lengths
+ int ret;
+ if (!getBits(5, &ret))
+ return false;
+ int nlen = ret + 257;
+ if (!getBits(5, &ret))
+ return false;
+ int ndist = ret + 1;
+ if (!getBits(4, &ret))
+ return false;
+ int ncode = ret + 4;
+ if (nlen > MAXLCODES || ndist > MAXDCODES)
+ {
+ error("Bad codes");
+ return false;
+ }
+
+ // get code length code lengths
+ int index = 0;
+ for ( ; index < ncode; index++)
+ {
+ if (!getBits(3, &ret))
+ return false;
+ lengths[order[index]] = ret;
+ }
+ for ( ; index < 19; index++)
+ lengths[order[index]] = 0;
+
+ // build huffman table for code lengths codes
+ if (buildHuffman(&lencode, lengths, 19) != 0)
+ return false;
+
+ // read length/literal and distance code length tables
+ index = 0;
+ while (index < nlen + ndist)
+ {
+ int symbol = doDecode(&lencode);
+ if (symbol < 16) // length in 0..15
+ lengths[index++] = symbol;
+ else
+ { // repeat instruction
+ int len = 0; // assume repeating zeros
+ if (symbol == 16)
+ { // repeat last length 3..6 times
+ if (index == 0)
+ {
+ error("no last length");
+ return false;
+ }
+ len = lengths[index - 1];// last length
+ if (!getBits(2, &ret))
+ return false;
+ symbol = 3 + ret;
+ }
+ else if (symbol == 17) // repeat zero 3..10 times
+ {
+ if (!getBits(3, &ret))
+ return false;
+ symbol = 3 + ret;
+ }
+ else // == 18, repeat zero 11..138 times
+ {
+ if (!getBits(7, &ret))
+ return false;
+ symbol = 11 + ret;
+ }
+ if (index + symbol > nlen + ndist)
+ {
+ error("too many lengths");
+ return false;
+ }
+ while (symbol--) // repeat last or zero symbol times
+ lengths[index++] = len;
+ }
+ }
+
+ // build huffman table for literal/length codes
+ int err = buildHuffman(&lencode, lengths, nlen);
+ if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
+ {
+ error("incomplete length codes");
+ //return false;
+ }
+ // build huffman table for distance codes
+ err = buildHuffman(&distcode, lengths + nlen, ndist);
+ if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
+ {
+ error("incomplete dist codes");
+ return false;
+ }
+
+ // decode data until end-of-block code
+ bool retn = doCodes(&lencode, &distcode);
+ return retn;
+}
+
+/**
+ */
+bool Inflater::inflate(std::vector<unsigned char> &destination,
+ std::vector<unsigned char> &source)
+{
+ dest.clear();
+ src = source;
+ srcPos = 0;
+ bitBuf = 0;
+ bitCnt = 0;
+
+ while (true)
+ {
+ int last; // one if last block
+ if (!getBits(1, &last))
+ return false;
+ int type; // block type 0..3
+ if (!getBits(2, &type))
+ return false;
+ switch (type)
+ {
+ case 0:
+ if (!doStored())
+ return false;
+ break;
+ case 1:
+ if (!doFixed())
+ return false;
+ break;
+ case 2:
+ if (!doDynamic())
+ return false;
+ break;
+ default:
+ error("Unknown block type %d", type);
+ return false;
+ }
+ if (last)
+ break;
+ }
+
+ destination = dest;
+
+ return true;
+}
+
+
+
+
+
+
+//########################################################################
+//# D E F L A T E R
+//########################################################################
+
+
+#define DEFLATER_BUF_SIZE 32768
+class Deflater
+{
+public:
+
+ /**
+ *
+ */
+ Deflater();
+
+ /**
+ *
+ */
+ virtual ~Deflater();
+
+ /**
+ *
+ */
+ virtual void reset();
+
+ /**
+ *
+ */
+ virtual bool update(int ch);
+
+ /**
+ *
+ */
+ virtual bool finish();
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getCompressed();
+
+ /**
+ *
+ */
+ bool deflate(std::vector<unsigned char> &dest,
+ const std::vector<unsigned char> &src);
+
+ void encodeDistStatic(unsigned int len, unsigned int dist);
+
+private:
+
+ //debug messages
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ bool compressWindow();
+
+ bool compress();
+
+ std::vector<unsigned char> compressed;
+
+ std::vector<unsigned char> uncompressed;
+
+ std::vector<unsigned char> window;
+
+ unsigned int windowPos;
+
+ //#### Output
+ unsigned int outputBitBuf;
+ unsigned int outputNrBits;
+
+ void put(int ch);
+
+ void putWord(int ch);
+
+ void putFlush();
+
+ void putBits(unsigned int ch, unsigned int bitsWanted);
+
+ void putBitsR(unsigned int ch, unsigned int bitsWanted);
+
+ //#### Huffman Encode
+ void encodeLiteralStatic(unsigned int ch);
+
+ unsigned char windowBuf[DEFLATER_BUF_SIZE];
+ //assume 32-bit ints
+ unsigned int windowHashBuf[DEFLATER_BUF_SIZE];
+};
+
+
+//########################################################################
+//# A P I
+//########################################################################
+
+
+/**
+ *
+ */
+Deflater::Deflater()
+{
+ reset();
+}
+
+/**
+ *
+ */
+Deflater::~Deflater()
+= default;
+
+/**
+ *
+ */
+void Deflater::reset()
+{
+ compressed.clear();
+ uncompressed.clear();
+ window.clear();
+ windowPos = 0;
+ outputBitBuf = 0;
+ outputNrBits = 0;
+ for (int k=0; k<DEFLATER_BUF_SIZE; k++)
+ {
+ windowBuf[k]=0;
+ windowHashBuf[k]=0;
+ }
+}
+
+/**
+ *
+ */
+bool Deflater::update(int ch)
+{
+ uncompressed.push_back((unsigned char)(ch & 0xff));
+ return true;
+}
+
+/**
+ *
+ */
+bool Deflater::finish()
+{
+ return compress();
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &Deflater::getCompressed()
+{
+ return compressed;
+}
+
+
+/**
+ *
+ */
+bool Deflater::deflate(std::vector<unsigned char> &dest,
+ const std::vector<unsigned char> &src)
+{
+ reset();
+ uncompressed = src;
+ if (!compress())
+ return false;
+ dest = compressed;
+ return true;
+}
+
+
+
+
+
+
+
+//########################################################################
+//# W O R K I N G C O D E
+//########################################################################
+
+
+//#############################
+//# M E S S A G E S
+//#############################
+
+/**
+ * Print error messages
+ */
+void Deflater::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Deflater error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ * Print trace messages
+ */
+void Deflater::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Deflater:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+
+
+
+//#############################
+//# O U T P U T
+//#############################
+
+/**
+ *
+ */
+void Deflater::put(int ch)
+{
+ compressed.push_back(ch);
+ outputBitBuf = 0;
+ outputNrBits = 0;
+}
+
+/**
+ *
+ */
+void Deflater::putWord(int ch)
+{
+ int lo = (ch ) & 0xff;
+ int hi = (ch>>8) & 0xff;
+ put(lo);
+ put(hi);
+}
+
+/**
+ *
+ */
+void Deflater::putFlush()
+{
+ if (outputNrBits > 0)
+ {
+ put(outputBitBuf & 0xff);
+ }
+ outputBitBuf = 0;
+ outputNrBits = 0;
+}
+
+/**
+ *
+ */
+void Deflater::putBits(unsigned int ch, unsigned int bitsWanted)
+{
+ //trace("n:%4u, %d\n", ch, bitsWanted);
+
+ while (bitsWanted--)
+ {
+ //add bits to position 7. shift right
+ outputBitBuf = (outputBitBuf>>1) + (ch<<7 & 0x80);
+ ch >>= 1;
+ outputNrBits++;
+ if (outputNrBits >= 8)
+ {
+ unsigned char b = outputBitBuf & 0xff;
+ //printf("b:%02x\n", b);
+ put(b);
+ }
+ }
+}
+
+static unsigned int bitReverse(unsigned int code, unsigned int nrBits)
+{
+ unsigned int outb = 0;
+ while (nrBits--)
+ {
+ outb = (outb << 1) | (code & 0x01);
+ code >>= 1;
+ }
+ return outb;
+}
+
+
+/**
+ *
+ */
+void Deflater::putBitsR(unsigned int ch, unsigned int bitsWanted)
+{
+ //trace("r:%4u, %d", ch, bitsWanted);
+
+ unsigned int rcode = bitReverse(ch, bitsWanted);
+
+ putBits(rcode, bitsWanted);
+
+}
+
+
+//#############################
+//# E N C O D E
+//#############################
+
+
+
+void Deflater::encodeLiteralStatic(unsigned int ch)
+{
+ //trace("c: %d", ch);
+
+ if (ch < 144)
+ {
+ putBitsR(ch + 0x0030 , 8); // 00110000
+ }
+ else if (ch < 256)
+ {
+ putBitsR(ch - 144 + 0x0190 , 9); // 110010000
+ }
+ else if (ch < 280)
+ {
+ putBitsR(ch - 256 + 0x0000 , 7); // 0000000
+ }
+ else if (ch < 288)
+ {
+ putBitsR(ch - 280 + 0x00c0 , 8); // 11000000
+ }
+ else //out of range
+ {
+ error("Literal out of range: %d", ch);
+ }
+
+}
+
+
+struct LenBase
+{
+ unsigned int base;
+ unsigned int range;
+ unsigned int bits;
+};
+
+LenBase lenBases[] =
+{
+ { 3, 1, 0 },
+ { 4, 1, 0 },
+ { 5, 1, 0 },
+ { 6, 1, 0 },
+ { 7, 1, 0 },
+ { 8, 1, 0 },
+ { 9, 1, 0 },
+ { 10, 1, 0 },
+ { 11, 2, 1 },
+ { 13, 2, 1 },
+ { 15, 2, 1 },
+ { 17, 2, 1 },
+ { 19, 4, 2 },
+ { 23, 4, 2 },
+ { 27, 4, 2 },
+ { 31, 4, 2 },
+ { 35, 8, 3 },
+ { 43, 8, 3 },
+ { 51, 8, 3 },
+ { 59, 8, 3 },
+ { 67, 16, 4 },
+ { 83, 16, 4 },
+ { 99, 16, 4 },
+ { 115, 16, 4 },
+ { 131, 32, 5 },
+ { 163, 32, 5 },
+ { 195, 32, 5 },
+ { 227, 32, 5 },
+ { 258, 1, 0 }
+};
+
+struct DistBase
+{
+ unsigned int base;
+ unsigned int range;
+ unsigned int bits;
+};
+
+DistBase distBases[] =
+{
+ { 1, 1, 0 },
+ { 2, 1, 0 },
+ { 3, 1, 0 },
+ { 4, 1, 0 },
+ { 5, 2, 1 },
+ { 7, 2, 1 },
+ { 9, 4, 2 },
+ { 13, 4, 2 },
+ { 17, 8, 3 },
+ { 25, 8, 3 },
+ { 33, 16, 4 },
+ { 49, 16, 4 },
+ { 65, 32, 5 },
+ { 97, 32, 5 },
+ { 129, 64, 6 },
+ { 193, 64, 6 },
+ { 257, 128, 7 },
+ { 385, 128, 7 },
+ { 513, 256, 8 },
+ { 769, 256, 8 },
+ { 1025, 512, 9 },
+ { 1537, 512, 9 },
+ { 2049, 1024, 10 },
+ { 3073, 1024, 10 },
+ { 4097, 2048, 11 },
+ { 6145, 2048, 11 },
+ { 8193, 4096, 12 },
+ { 12289, 4096, 12 },
+ { 16385, 8192, 13 },
+ { 24577, 8192, 13 }
+};
+
+void Deflater::encodeDistStatic(unsigned int len, unsigned int dist)
+{
+
+ //## Output length
+
+ if (len < 3 || len > 258)
+ {
+ error("Length out of range:%d", len);
+ return;
+ }
+
+ bool found = false;
+ for (int i=0 ; i<29 ; i++)
+ {
+ unsigned int base = lenBases[i].base;
+ unsigned int range = lenBases[i].range;
+ if (base + range > len)
+ {
+ unsigned int lenCode = 257 + i;
+ unsigned int length = len - base;
+ //trace("--- %d %d %d %d", len, base, range, length);
+ encodeLiteralStatic(lenCode);
+ putBits(length, lenBases[i].bits);
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ error("Length not found in table:%d", len);
+ return;
+ }
+
+ //## Output distance
+
+ if (dist < 4 || dist > 32768)
+ {
+ error("Distance out of range:%d", dist);
+ return;
+ }
+
+ found = false;
+ for (int i=0 ; i<30 ; i++)
+ {
+ unsigned int base = distBases[i].base;
+ unsigned int range = distBases[i].range;
+ if (base + range > dist)
+ {
+ unsigned int distCode = i;
+ unsigned int distance = dist - base;
+ //error("--- %d %d %d %d", dist, base, range, distance);
+ putBitsR(distCode, 5);
+ putBits(distance, distBases[i].bits);
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ error("Distance not found in table:%d", dist);
+ return;
+ }
+}
+
+
+//#############################
+//# C O M P R E S S
+//#############################
+
+
+/**
+ * This method does the dirty work of dictionary
+ * compression. Basically it looks for redundant
+ * strings and has the current duplicate refer back
+ * to the previous one.
+ */
+bool Deflater::compressWindow()
+{
+ windowPos = 0;
+ unsigned int windowSize = window.size();
+ //### Compress as much of the window as possible
+
+ unsigned int hash = 0;
+ //Have each value be a long with the byte at this position,
+ //plus the 3 bytes after it in the window
+ for (int i=windowSize-1 ; i>=0 ; i--)
+ {
+ unsigned char ch = window[i];
+ windowBuf[i] = ch;
+ hash = ((hash<<8) & 0xffffff00) | ch;
+ windowHashBuf[i] = hash;
+ }
+
+ while (windowPos < windowSize - 3)
+ {
+ //### Find best match, if any
+ unsigned int bestMatchLen = 0;
+ unsigned int bestMatchDist = 0;
+ if (windowPos >= 4)
+ {
+ for (unsigned int lookBack=0 ; lookBack<windowPos-4 ; lookBack++)
+ {
+ //Check 4-char hashes first, before continuing with string
+ if (windowHashBuf[lookBack] == windowHashBuf[windowPos])
+ {
+ unsigned int lookAhead=4;
+ unsigned int lookAheadMax = windowSize - 4 - windowPos;
+ if (lookBack + lookAheadMax >= windowPos -4 )
+ lookAheadMax = windowPos - 4 - lookBack;
+ if (lookAheadMax > 258)
+ lookAheadMax = 258;
+ unsigned char *wp = &(windowBuf[windowPos+4]);
+ unsigned char *lb = &(windowBuf[lookBack+4]);
+ while (lookAhead<lookAheadMax)
+ {
+ if (*lb++ != *wp++)
+ break;
+ lookAhead++;
+ }
+ if (lookAhead > bestMatchLen)
+ {
+ bestMatchLen = lookAhead;
+ bestMatchDist = windowPos - lookBack;
+ }
+ }
+ }
+ }
+ if (bestMatchLen > 3)
+ {
+ //Distance encode
+ //trace("### distance");
+ /*
+ printf("### 1 '");
+ for (int i=0 ; i < bestMatchLen ; i++)
+ fputc(window[windowPos+i], stdout);
+ printf("'\n### 2 '");
+ for (int i=0 ; i < bestMatchLen ; i++)
+ fputc(window[windowPos-bestMatchDist+i], stdout);
+ printf("'\n");
+ */
+ encodeDistStatic(bestMatchLen, bestMatchDist);
+ windowPos += bestMatchLen;
+ }
+ else
+ {
+ //Literal encode
+ //trace("### literal");
+ encodeLiteralStatic(windowBuf[windowPos]);
+ windowPos++;
+ }
+ }
+
+ while (windowPos < windowSize)
+ encodeLiteralStatic(windowBuf[windowPos++]);
+
+ encodeLiteralStatic(256);
+ return true;
+}
+
+
+/**
+ *
+ */
+bool Deflater::compress()
+{
+ //trace("compress");
+ unsigned long total = 0L;
+ windowPos = 0;
+ std::vector<unsigned char>::iterator iter;
+ for (iter = uncompressed.begin(); iter != uncompressed.end() ; )
+ {
+ total += windowPos;
+ trace("total:%ld", total);
+ if (windowPos > window.size())
+ windowPos = window.size();
+ window.erase(window.begin() , window.begin()+windowPos);
+ while (window.size() < 32768 && iter != uncompressed.end())
+ {
+ window.push_back(*iter);
+ ++iter;
+ }
+ if (window.size() >= 32768)
+ putBits(0x00, 1); //0 -- more blocks
+ else
+ putBits(0x01, 1); //1 -- last block
+ putBits(0x01, 2); //01 -- static trees
+ if (!compressWindow())
+ return false;
+ }
+ putFlush();
+ return true;
+}
+
+
+
+
+
+//########################################################################
+//# G Z I P F I L E
+//########################################################################
+
+/**
+ * Constructor
+ */
+GzipFile::GzipFile() :
+ data(),
+ fileName(),
+ fileBuf(),
+ fileBufPos(0),
+ compressionMethod(0)
+{
+}
+
+/**
+ * Destructor
+ */
+GzipFile::~GzipFile()
+= default;
+
+/**
+ * Print error messages
+ */
+void GzipFile::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "GzipFile error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ * Print trace messages
+ */
+void GzipFile::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "GzipFile:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ *
+ */
+void GzipFile::put(unsigned char ch)
+{
+ data.push_back(ch);
+}
+
+/**
+ *
+ */
+void GzipFile::setData(const std::vector<unsigned char> &str)
+{
+ data = str;
+}
+
+/**
+ *
+ */
+void GzipFile::clearData()
+{
+ data.clear();
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &GzipFile::getData()
+{
+ return data;
+}
+
+/**
+ *
+ */
+std::string &GzipFile::getFileName()
+{
+ return fileName;
+}
+
+/**
+ *
+ */
+void GzipFile::setFileName(const std::string &val)
+{
+ fileName = val;
+}
+
+
+
+//#####################################
+//# U T I L I T Y
+//#####################################
+
+/**
+ * Loads a new file into an existing GzipFile
+ */
+bool GzipFile::loadFile(const std::string &fName)
+{
+ FILE *f = fopen(fName.c_str() , "rb");
+ if (!f)
+ {
+ error("Cannot open file %s", fName.c_str());
+ return false;
+ }
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ data.push_back(ch);
+ }
+ fclose(f);
+ setFileName(fName);
+ return true;
+}
+
+
+
+//#####################################
+//# W R I T E
+//#####################################
+
+/**
+ *
+ */
+bool GzipFile::putByte(unsigned char ch)
+{
+ fileBuf.push_back(ch);
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool GzipFile::putLong(unsigned long val)
+{
+ fileBuf.push_back( (unsigned char)((val ) & 0xff));
+ fileBuf.push_back( (unsigned char)((val>> 8) & 0xff));
+ fileBuf.push_back( (unsigned char)((val>>16) & 0xff));
+ fileBuf.push_back( (unsigned char)((val>>24) & 0xff));
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool GzipFile::write()
+{
+ fileBuf.clear();
+
+ putByte(0x1f); //magic
+ putByte(0x8b); //magic
+ putByte( 8); //compression method
+ putByte(0x08); //flags. say we have a crc and file name
+
+ unsigned long ltime = (unsigned long) time(nullptr);
+ putLong(ltime);
+
+ //xfl
+ putByte(0);
+ //OS
+ putByte(0);
+
+ //file name
+ for (char i : fileName)
+ putByte(i);
+ putByte(0);
+
+
+ //compress
+ std::vector<unsigned char> compBuf;
+ Deflater deflater;
+ if (!deflater.deflate(compBuf, data))
+ {
+ return false;
+ }
+
+ std::vector<unsigned char>::iterator iter;
+ for (iter=compBuf.begin() ; iter!=compBuf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ putByte(ch);
+ }
+
+ Crc32 crcEngine;
+ crcEngine.update(data);
+ unsigned long crc = crcEngine.getValue();
+ putLong(crc);
+
+ putLong(data.size());
+
+ return true;
+}
+
+
+/**
+ *
+ */
+bool GzipFile::writeBuffer(std::vector<unsigned char> &outBuf)
+{
+ if (!write())
+ return false;
+ outBuf.clear();
+ outBuf = fileBuf;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool GzipFile::writeFile(const std::string &fileName)
+{
+ if (!write())
+ return false;
+ FILE *f = fopen(fileName.c_str(), "wb");
+ if (!f)
+ return false;
+ std::vector<unsigned char>::iterator iter;
+ for (iter=fileBuf.begin() ; iter!=fileBuf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ fputc(ch, f);
+ }
+ fclose(f);
+ return true;
+}
+
+
+//#####################################
+//# R E A D
+//#####################################
+
+bool GzipFile::getByte(unsigned char *ch)
+{
+ if (fileBufPos >= fileBuf.size())
+ {
+ error("unexpected end of data");
+ return false;
+ }
+ *ch = fileBuf[fileBufPos++];
+ return true;
+}
+
+/**
+ *
+ */
+bool GzipFile::getLong(unsigned long *val)
+{
+ if (fileBuf.size() - fileBufPos < 4)
+ return false;
+ int ch1 = fileBuf[fileBufPos++];
+ int ch2 = fileBuf[fileBufPos++];
+ int ch3 = fileBuf[fileBufPos++];
+ int ch4 = fileBuf[fileBufPos++];
+ *val = ((ch4<<24) & 0xff000000L) |
+ ((ch3<<16) & 0x00ff0000L) |
+ ((ch2<< 8) & 0x0000ff00L) |
+ ((ch1 ) & 0x000000ffL);
+ return true;
+}
+
+bool GzipFile::read()
+{
+ fileBufPos = 0;
+
+ unsigned char ch;
+
+ //magic cookie
+ if (!getByte(&ch))
+ return false;
+ if (ch != 0x1f)
+ {
+ error("bad gzip header");
+ return false;
+ }
+ if (!getByte(&ch))
+ return false;
+ if (ch != 0x8b)
+ {
+ error("bad gzip header");
+ return false;
+ }
+
+ //## compression method
+ if (!getByte(&ch))
+ return false;
+ compressionMethod = ch & 0xff;
+
+ //## flags
+ if (!getByte(&ch))
+ return false;
+ //bool ftext = ch & 0x01;
+ bool fhcrc = ch & 0x02;
+ bool fextra = ch & 0x04;
+ bool fname = ch & 0x08;
+ bool fcomment = ch & 0x10;
+
+ //trace("cm:%d ftext:%d fhcrc:%d fextra:%d fname:%d fcomment:%d",
+ // cm, ftext, fhcrc, fextra, fname, fcomment);
+
+ //## file time
+ unsigned long ltime;
+ if (!getLong(&ltime))
+ return false;
+ //time_t mtime = (time_t)ltime;
+
+ //## XFL
+ if (!getByte(&ch))
+ return false;
+ //int xfl = ch;
+
+ //## OS
+ if (!getByte(&ch))
+ return false;
+ //int os = ch;
+
+ //std::string timestr = ctime(&mtime);
+ //trace("xfl:%d os:%d mtime:%s", xfl, os, timestr.c_str());
+
+ if (fextra)
+ {
+ if (!getByte(&ch))
+ return false;
+ long xlen = ch;
+ if (!getByte(&ch))
+ return false;
+ xlen = (xlen << 8) + ch;
+ for (long l=0 ; l<xlen ; l++)
+ {
+ if (!getByte(&ch))
+ return false;
+ }
+ }
+
+ if (fname)
+ {
+ fileName = "";
+ while (true)
+ {
+ if (!getByte(&ch))
+ return false;
+ if (ch==0)
+ break;
+ fileName.push_back(ch);
+ }
+ }
+
+ if (fcomment)
+ {
+ while (true)
+ {
+ if (!getByte(&ch))
+ return false;
+ if (ch==0)
+ break;
+ }
+ }
+
+ if (fhcrc)
+ {
+ if (!getByte(&ch))
+ return false;
+ if (!getByte(&ch))
+ return false;
+ }
+
+ //read remainder of stream
+ //compressed data runs up until 8 bytes before end of buffer
+ std::vector<unsigned char> compBuf;
+ while (fileBufPos < fileBuf.size() - 8)
+ {
+ if (!getByte(&ch))
+ return false;
+ compBuf.push_back(ch);
+ }
+ //uncompress
+ data.clear();
+ Inflater inflater;
+ if (!inflater.inflate(data, compBuf))
+ {
+ return false;
+ }
+
+ //Get the CRC and compare
+ Crc32 crcEngine;
+ crcEngine.update(data);
+ unsigned long calcCrc = crcEngine.getValue();
+ unsigned long givenCrc;
+ if (!getLong(&givenCrc))
+ return false;
+ if (givenCrc != calcCrc)
+ {
+ error("Specified crc, %ud, not what received: %ud",
+ givenCrc, calcCrc);
+ return false;
+ }
+
+ //Get the file size and compare
+ unsigned long givenFileSize;
+ if (!getLong(&givenFileSize))
+ return false;
+ if (givenFileSize != data.size())
+ {
+ error("Specified data size, %ld, not what received: %ld",
+ givenFileSize, data.size());
+ return false;
+ }
+
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool GzipFile::readBuffer(const std::vector<unsigned char> &inbuf)
+{
+ fileBuf = inbuf;
+ if (!read())
+ return false;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool GzipFile::readFile(const std::string &fileName)
+{
+ fileBuf.clear();
+ FILE *f = fopen(fileName.c_str(), "rb");
+ if (!f)
+ return false;
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ fileBuf.push_back(ch);
+ }
+ fclose(f);
+ if (!read())
+ return false;
+ return true;
+}
+
+
+
+
+
+
+
+
+//########################################################################
+//# Z I P F I L E
+//########################################################################
+
+/**
+ * Constructor
+ */
+ZipEntry::ZipEntry() :
+ crc (0L),
+ fileName (),
+ comment (),
+ compressionMethod (8),
+ compressedData (),
+ uncompressedData (),
+ position (0)
+{
+}
+
+/**
+ *
+ */
+ZipEntry::ZipEntry(std::string fileNameArg,
+ std::string commentArg) :
+ crc (0L),
+ fileName (std::move(fileNameArg)),
+ comment (std::move(commentArg)),
+ compressionMethod (8),
+ compressedData (),
+ uncompressedData (),
+ position (0)
+{
+}
+
+/**
+ * Destructor
+ */
+ZipEntry::~ZipEntry()
+= default;
+
+
+/**
+ *
+ */
+std::string ZipEntry::getFileName()
+{
+ return fileName;
+}
+
+/**
+ *
+ */
+void ZipEntry::setFileName(const std::string &val)
+{
+ fileName = val;
+}
+
+/**
+ *
+ */
+std::string ZipEntry::getComment()
+{
+ return comment;
+}
+
+/**
+ *
+ */
+void ZipEntry::setComment(const std::string &val)
+{
+ comment = val;
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getCompressedSize()
+{
+ return (unsigned long)compressedData.size();
+}
+
+/**
+ *
+ */
+int ZipEntry::getCompressionMethod()
+{
+ return compressionMethod;
+}
+
+/**
+ *
+ */
+void ZipEntry::setCompressionMethod(int val)
+{
+ compressionMethod = val;
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &ZipEntry::getCompressedData()
+{
+ return compressedData;
+}
+
+/**
+ *
+ */
+void ZipEntry::setCompressedData(const std::vector<unsigned char> &val)
+{
+ compressedData = val;
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getUncompressedSize()
+{
+ return (unsigned long)uncompressedData.size();
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &ZipEntry::getUncompressedData()
+{
+ return uncompressedData;
+}
+
+/**
+ *
+ */
+void ZipEntry::setUncompressedData(const std::vector<unsigned char> &val)
+{
+ uncompressedData = val;
+}
+
+void ZipEntry::setUncompressedData(const std::string &s)
+{
+ uncompressedData.clear();
+ uncompressedData.reserve(s.size());
+ uncompressedData.insert(uncompressedData.begin(), s.begin(), s.end());
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getCrc()
+{
+ return crc;
+}
+
+/**
+ *
+ */
+void ZipEntry::setCrc(unsigned long val)
+{
+ crc = val;
+}
+
+/**
+ *
+ */
+void ZipEntry::write(unsigned char ch)
+{
+ uncompressedData.push_back(ch);
+}
+
+/**
+ *
+ */
+void ZipEntry::finish()
+{
+ Crc32 c32;
+ std::vector<unsigned char>::iterator iter;
+ for (iter = uncompressedData.begin() ;
+ iter!= uncompressedData.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ c32.update(ch);
+ }
+ crc = c32.getValue();
+ switch (compressionMethod)
+ {
+ case 0: //none
+ {
+ for (iter = uncompressedData.begin() ;
+ iter!= uncompressedData.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ compressedData.push_back(ch);
+ }
+ break;
+ }
+ case 8: //deflate
+ {
+ Deflater deflater;
+ if (!deflater.deflate(compressedData, uncompressedData))
+ {
+ //some error
+ }
+ break;
+ }
+ default:
+ {
+ printf("error: unknown compression method %d\n",
+ compressionMethod);
+ }
+ }
+}
+
+
+
+
+/**
+ *
+ */
+bool ZipEntry::readFile(const std::string &fileNameArg,
+ const std::string &commentArg)
+{
+ crc = 0L;
+ uncompressedData.clear();
+ fileName = fileNameArg;
+ comment = commentArg;
+ FILE *f = fopen(fileName.c_str(), "rb");
+ if (!f)
+ {
+ return false;
+ }
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ uncompressedData.push_back((unsigned char)ch);
+ }
+ fclose(f);
+ finish();
+ return true;
+}
+
+
+/**
+ *
+ */
+void ZipEntry::setPosition(unsigned long val)
+{
+ position = val;
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getPosition()
+{
+ return position;
+}
+
+
+
+
+
+
+
+/**
+ * Constructor
+ */
+ZipFile::ZipFile() :
+ entries(),
+ fileBuf(),
+ fileBufPos(0),
+ comment()
+{
+}
+
+/**
+ * Destructor
+ */
+ZipFile::~ZipFile()
+{
+ std::vector<ZipEntry *>::iterator iter;
+ for (iter=entries.begin() ; iter!=entries.end() ; ++iter)
+ {
+ ZipEntry *entry = *iter;
+ delete entry;
+ }
+ entries.clear();
+}
+
+/**
+ *
+ */
+void ZipFile::setComment(const std::string &val)
+{
+ comment = val;
+}
+
+/**
+ *
+ */
+std::string ZipFile::getComment()
+{
+ return comment;
+}
+
+
+/**
+ *
+ */
+std::vector<ZipEntry *> &ZipFile::getEntries()
+{
+ return entries;
+}
+
+
+
+//#####################################
+//# M E S S A G E S
+//#####################################
+
+void ZipFile::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "ZipFile error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+void ZipFile::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "ZipFile:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+//#####################################
+//# U T I L I T Y
+//#####################################
+
+/**
+ *
+ */
+ZipEntry *ZipFile::addFile(const std::string &fileName,
+ const std::string &comment)
+{
+ ZipEntry *ze = new ZipEntry();
+ if (!ze->readFile(fileName, comment))
+ {
+ delete ze;
+ return nullptr;
+ }
+ entries.push_back(ze);
+ return ze;
+}
+
+
+/**
+ *
+ */
+ZipEntry *ZipFile::newEntry(const std::string &fileName,
+ const std::string &comment)
+{
+ ZipEntry *ze = new ZipEntry(fileName, comment);
+ entries.push_back(ze);
+ return ze;
+}
+
+
+//#####################################
+//# W R I T E
+//#####################################
+
+/**
+ *
+ */
+bool ZipFile::putLong(unsigned long val)
+{
+ fileBuf.push_back( ((int)(val )) & 0xff);
+ fileBuf.push_back( ((int)(val>> 8)) & 0xff);
+ fileBuf.push_back( ((int)(val>>16)) & 0xff);
+ fileBuf.push_back( ((int)(val>>24)) & 0xff);
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::putInt(unsigned int val)
+{
+ fileBuf.push_back( (val ) & 0xff);
+ fileBuf.push_back( (val>> 8) & 0xff);
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::putByte(unsigned char val)
+{
+ fileBuf.push_back(val);
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::writeFileData()
+{
+ std::vector<ZipEntry *>::iterator iter;
+ for (iter = entries.begin() ; iter != entries.end() ; ++iter)
+ {
+ ZipEntry *entry = *iter;
+ entry->setPosition(fileBuf.size());
+ //##### HEADER
+ std::string fname = entry->getFileName();
+ putLong(0x04034b50L);
+ putInt(20); //versionNeeded
+ putInt(0); //gpBitFlag
+ //putInt(0); //compression method
+ putInt(entry->getCompressionMethod()); //compression method
+ putInt(0); //mod time
+ putInt(0); //mod date
+ putLong(entry->getCrc()); //crc32
+ putLong(entry->getCompressedSize());
+ putLong(entry->getUncompressedSize());
+ putInt(fname.size());//fileName length
+ putInt(8);//extra field length
+ //file name
+ for (char i : fname)
+ putByte((unsigned char)i);
+ //extra field
+ putInt(0x7855);
+ putInt(4);
+ putInt(100);
+ putInt(100);
+
+ //##### DATA
+ std::vector<unsigned char> &buf = entry->getCompressedData();
+ std::vector<unsigned char>::iterator iter;
+ for (iter = buf.begin() ; iter != buf.end() ; ++iter)
+ {
+ unsigned char ch = (unsigned char) *iter;
+ putByte(ch);
+ }
+ }
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::writeCentralDirectory()
+{
+ unsigned long cdPosition = fileBuf.size();
+ std::vector<ZipEntry *>::iterator iter;
+ for (iter = entries.begin() ; iter != entries.end() ; ++iter)
+ {
+ ZipEntry *entry = *iter;
+ std::string fname = entry->getFileName();
+ std::string ecomment = entry->getComment();
+ putLong(0x02014b50L); //magic cookie
+ putInt(2386); //versionMadeBy
+ putInt(20); //versionNeeded
+ putInt(0); //gpBitFlag
+ putInt(entry->getCompressionMethod()); //compression method
+ putInt(0); //mod time
+ putInt(0); //mod date
+ putLong(entry->getCrc()); //crc32
+ putLong(entry->getCompressedSize());
+ putLong(entry->getUncompressedSize());
+ putInt(fname.size());//fileName length
+ putInt(4);//extra field length
+ putInt(ecomment.size());//comment length
+ putInt(0); //disk number start
+ putInt(0); //internal attributes
+ putLong(0); //external attributes
+ putLong(entry->getPosition());
+
+ //file name
+ for (char i : fname)
+ putByte((unsigned char)i);
+ //extra field
+ putInt(0x7855);
+ putInt(0);
+ //comment
+ for (char i : ecomment)
+ putByte((unsigned char)i);
+ }
+ unsigned long cdSize = fileBuf.size() - cdPosition;
+
+ putLong(0x06054b50L);
+ putInt(0);//number of this disk
+ putInt(0);//nr of disk with central dir
+ putInt(entries.size()); //number of entries on this disk
+ putInt(entries.size()); //number of entries total
+ putLong(cdSize); //size of central dir
+ putLong(cdPosition); //position of central dir
+ putInt(comment.size());//comment size
+ for (char i : comment)
+ putByte(i);
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool ZipFile::write()
+{
+ fileBuf.clear();
+ if (!writeFileData())
+ return false;
+ if (!writeCentralDirectory())
+ return false;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::writeBuffer(std::vector<unsigned char> &outBuf)
+{
+ if (!write())
+ return false;
+ outBuf.clear();
+ outBuf = fileBuf;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::writeFile(const std::string &fileName)
+{
+ if (!write())
+ return false;
+ FILE *f = fopen(fileName.c_str(), "wb");
+ if (!f)
+ return false;
+ std::vector<unsigned char>::iterator iter;
+ for (iter=fileBuf.begin() ; iter!=fileBuf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ fputc(ch, f);
+ }
+ fclose(f);
+ return true;
+}
+
+//#####################################
+//# R E A D
+//#####################################
+
+/**
+ *
+ */
+bool ZipFile::getLong(unsigned long *val)
+{
+ if (fileBuf.size() - fileBufPos < 4)
+ return false;
+ int ch1 = fileBuf[fileBufPos++];
+ int ch2 = fileBuf[fileBufPos++];
+ int ch3 = fileBuf[fileBufPos++];
+ int ch4 = fileBuf[fileBufPos++];
+ *val = ((ch4<<24) & 0xff000000L) |
+ ((ch3<<16) & 0x00ff0000L) |
+ ((ch2<< 8) & 0x0000ff00L) |
+ ((ch1 ) & 0x000000ffL);
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::getInt(unsigned int *val)
+{
+ if (fileBuf.size() - fileBufPos < 2)
+ return false;
+ int ch1 = fileBuf[fileBufPos++];
+ int ch2 = fileBuf[fileBufPos++];
+ *val = ((ch2<< 8) & 0xff00) |
+ ((ch1 ) & 0x00ff);
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::getByte(unsigned char *val)
+{
+ if (fileBuf.size() <= fileBufPos)
+ return false;
+ *val = fileBuf[fileBufPos++];
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::readFileData()
+{
+ //printf("#################################################\n");
+ //printf("###D A T A\n");
+ //printf("#################################################\n");
+ while (true)
+ {
+ unsigned long magicCookie;
+ if (!getLong(&magicCookie))
+ {
+ error("magic cookie not found");
+ break;
+ }
+ trace("###Cookie:%lx", magicCookie);
+ if (magicCookie == 0x02014b50L) //central directory
+ break;
+ if (magicCookie != 0x04034b50L)
+ {
+ error("file header not found");
+ return false;
+ }
+ unsigned int versionNeeded;
+ if (!getInt(&versionNeeded))
+ {
+ error("bad version needed found");
+ return false;
+ }
+ unsigned int gpBitFlag;
+ if (!getInt(&gpBitFlag))
+ {
+ error("bad bit flag found");
+ return false;
+ }
+ unsigned int compressionMethod;
+ if (!getInt(&compressionMethod))
+ {
+ error("bad compressionMethod found");
+ return false;
+ }
+ unsigned int modTime;
+ if (!getInt(&modTime))
+ {
+ error("bad modTime found");
+ return false;
+ }
+ unsigned int modDate;
+ if (!getInt(&modDate))
+ {
+ error("bad modDate found");
+ return false;
+ }
+ unsigned long crc32;
+ if (!getLong(&crc32))
+ {
+ error("bad crc32 found");
+ return false;
+ }
+ unsigned long compressedSize;
+ if (!getLong(&compressedSize))
+ {
+ error("bad compressedSize found");
+ return false;
+ }
+ unsigned long uncompressedSize;
+ if (!getLong(&uncompressedSize))
+ {
+ error("bad uncompressedSize found");
+ return false;
+ }
+ unsigned int fileNameLength;
+ if (!getInt(&fileNameLength))
+ {
+ error("bad fileNameLength found");
+ return false;
+ }
+ unsigned int extraFieldLength;
+ if (!getInt(&extraFieldLength))
+ {
+ error("bad extraFieldLength found");
+ return false;
+ }
+ std::string fileName;
+ for (unsigned int i=0 ; i<fileNameLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ fileName.push_back(ch);
+ }
+ std::string extraField;
+ for (unsigned int i=0 ; i<extraFieldLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ extraField.push_back(ch);
+ }
+ trace("######################### DATA");
+ trace("FileName :%d:%s" , fileName.size(), fileName.c_str());
+ trace("Extra field :%d:%s" , extraField.size(), extraField.c_str());
+ trace("Version needed :%d" , versionNeeded);
+ trace("Bitflag :%d" , gpBitFlag);
+ trace("Compression Method :%d" , compressionMethod);
+ trace("Mod time :%d" , modTime);
+ trace("Mod date :%d" , modDate);
+ trace("CRC :%lx", crc32);
+ trace("Compressed size :%ld", compressedSize);
+ trace("Uncompressed size :%ld", uncompressedSize);
+
+ //#### Uncompress the data
+ std::vector<unsigned char> compBuf;
+ if (gpBitFlag & 0x8)//bit 3 was set. means we don't know compressed size
+ {
+ unsigned char c1, c2, c3, c4;
+ c2 = c3 = c4 = 0;
+ while (true)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ {
+ error("premature end of data");
+ break;
+ }
+ compBuf.push_back(ch);
+ c1 = c2; c2 = c3; c3 = c4; c4 = ch;
+ if (c1 == 0x50 && c2 == 0x4b && c3 == 0x07 && c4 == 0x08)
+ {
+ trace("found end of compressed data");
+ //remove the cookie
+ compBuf.erase(compBuf.end() -4, compBuf.end());
+ break;
+ }
+ }
+ }
+ else
+ {
+ for (unsigned long bnr = 0 ; bnr < compressedSize ; bnr++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ {
+ error("premature end of data");
+ break;
+ }
+ compBuf.push_back(ch);
+ }
+ }
+
+ printf("### data: ");
+ for (int i=0 ; i<10 ; i++)
+ printf("%02x ", compBuf[i] & 0xff);
+ printf("\n");
+
+ if (gpBitFlag & 0x8)//only if bit 3 set
+ {
+ /* this cookie was read in the loop above
+ unsigned long dataDescriptorSignature ;
+ if (!getLong(&dataDescriptorSignature))
+ break;
+ if (dataDescriptorSignature != 0x08074b50L)
+ {
+ error("bad dataDescriptorSignature found");
+ return false;
+ }
+ */
+ unsigned long crc32;
+ if (!getLong(&crc32))
+ {
+ error("bad crc32 found");
+ return false;
+ }
+ unsigned long compressedSize;
+ if (!getLong(&compressedSize))
+ {
+ error("bad compressedSize found");
+ return false;
+ }
+ unsigned long uncompressedSize;
+ if (!getLong(&uncompressedSize))
+ {
+ error("bad uncompressedSize found");
+ return false;
+ }
+ }//bit 3 was set
+ //break;
+
+ std::vector<unsigned char> uncompBuf;
+ switch (compressionMethod)
+ {
+ case 8: //deflate
+ {
+ Inflater inflater;
+ if (!inflater.inflate(uncompBuf, compBuf))
+ {
+ return false;
+ }
+ break;
+ }
+ default:
+ {
+ error("Unimplemented compression method %d", compressionMethod);
+ return false;
+ }
+ }
+
+ if (uncompressedSize != uncompBuf.size())
+ {
+ error("Size mismatch. Expected %ld, received %ld",
+ uncompressedSize, uncompBuf.size());
+ return false;
+ }
+
+ Crc32 crcEngine;
+ crcEngine.update(uncompBuf);
+ unsigned long crc = crcEngine.getValue();
+ if (crc != crc32)
+ {
+ error("Crc mismatch. Calculated %08ux, received %08ux", crc, crc32);
+ return false;
+ }
+
+ ZipEntry *ze = new ZipEntry(fileName, comment);
+ ze->setCompressionMethod(compressionMethod);
+ ze->setCompressedData(compBuf);
+ ze->setUncompressedData(uncompBuf);
+ ze->setCrc(crc);
+ entries.push_back(ze);
+
+
+ }
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::readCentralDirectory()
+{
+ //printf("#################################################\n");
+ //printf("###D I R E C T O R Y\n");
+ //printf("#################################################\n");
+ while (true)
+ {
+ //We start with a central directory cookie already
+ //Check at the bottom of the loop.
+ unsigned int version;
+ if (!getInt(&version))
+ {
+ error("bad version found");
+ return false;
+ }
+ unsigned int versionNeeded;
+ if (!getInt(&versionNeeded))
+ {
+ error("bad version found");
+ return false;
+ }
+ unsigned int gpBitFlag;
+ if (!getInt(&gpBitFlag))
+ {
+ error("bad bit flag found");
+ return false;
+ }
+ unsigned int compressionMethod;
+ if (!getInt(&compressionMethod))
+ {
+ error("bad compressionMethod found");
+ return false;
+ }
+ unsigned int modTime;
+ if (!getInt(&modTime))
+ {
+ error("bad modTime found");
+ return false;
+ }
+ unsigned int modDate;
+ if (!getInt(&modDate))
+ {
+ error("bad modDate found");
+ return false;
+ }
+ unsigned long crc32;
+ if (!getLong(&crc32))
+ {
+ error("bad crc32 found");
+ return false;
+ }
+ unsigned long compressedSize;
+ if (!getLong(&compressedSize))
+ {
+ error("bad compressedSize found");
+ return false;
+ }
+ unsigned long uncompressedSize;
+ if (!getLong(&uncompressedSize))
+ {
+ error("bad uncompressedSize found");
+ return false;
+ }
+ unsigned int fileNameLength;
+ if (!getInt(&fileNameLength))
+ {
+ error("bad fileNameLength found");
+ return false;
+ }
+ unsigned int extraFieldLength;
+ if (!getInt(&extraFieldLength))
+ {
+ error("bad extraFieldLength found");
+ return false;
+ }
+ unsigned int fileCommentLength;
+ if (!getInt(&fileCommentLength))
+ {
+ error("bad fileCommentLength found");
+ return false;
+ }
+ unsigned int diskNumberStart;
+ if (!getInt(&diskNumberStart))
+ {
+ error("bad diskNumberStart found");
+ return false;
+ }
+ unsigned int internalFileAttributes;
+ if (!getInt(&internalFileAttributes))
+ {
+ error("bad internalFileAttributes found");
+ return false;
+ }
+ unsigned long externalFileAttributes;
+ if (!getLong(&externalFileAttributes))
+ {
+ error("bad externalFileAttributes found");
+ return false;
+ }
+ unsigned long localHeaderOffset;
+ if (!getLong(&localHeaderOffset))
+ {
+ error("bad localHeaderOffset found");
+ return false;
+ }
+ std::string fileName;
+ for (unsigned int i=0 ; i<fileNameLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ fileName.push_back(ch);
+ }
+ std::string extraField;
+ for (unsigned int i=0 ; i<extraFieldLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ extraField.push_back(ch);
+ }
+ std::string fileComment;
+ for (unsigned int i=0 ; i<fileCommentLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ fileComment.push_back(ch);
+ }
+ trace("######################### ENTRY");
+ trace("FileName :%s" , fileName.c_str());
+ trace("Extra field :%s" , extraField.c_str());
+ trace("File comment :%s" , fileComment.c_str());
+ trace("Version :%d" , version);
+ trace("Version needed :%d" , versionNeeded);
+ trace("Bitflag :%d" , gpBitFlag);
+ trace("Compression Method :%d" , compressionMethod);
+ trace("Mod time :%d" , modTime);
+ trace("Mod date :%d" , modDate);
+ trace("CRC :%lx", crc32);
+ trace("Compressed size :%ld", compressedSize);
+ trace("Uncompressed size :%ld", uncompressedSize);
+ trace("Disk nr start :%ld", diskNumberStart);
+ trace("Header offset :%ld", localHeaderOffset);
+
+
+ unsigned long magicCookie;
+ if (!getLong(&magicCookie))
+ {
+ error("magic cookie not found");
+ return false;
+ }
+ trace("###Cookie:%lx", magicCookie);
+ if (magicCookie == 0x06054b50L) //end of central directory
+ break;
+ else if (magicCookie == 0x05054b50L) //signature
+ {
+ //## Digital Signature
+ unsigned int signatureSize;
+ if (!getInt(&signatureSize))
+ {
+ error("bad signatureSize found");
+ return false;
+ }
+ std::string digitalSignature;
+ for (unsigned int i=0 ; i<signatureSize ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ digitalSignature.push_back(ch);
+ }
+ trace("######## SIGNATURE :'%s'" , digitalSignature.c_str());
+ }
+ else if (magicCookie != 0x02014b50L) //central directory
+ {
+ error("directory file header not found");
+ return false;
+ }
+ }
+
+ unsigned int diskNr;
+ if (!getInt(&diskNr))
+ {
+ error("bad diskNr found");
+ return false;
+ }
+ unsigned int diskWithCd;
+ if (!getInt(&diskWithCd))
+ {
+ error("bad diskWithCd found");
+ return false;
+ }
+ unsigned int nrEntriesDisk;
+ if (!getInt(&nrEntriesDisk))
+ {
+ error("bad nrEntriesDisk found");
+ return false;
+ }
+ unsigned int nrEntriesTotal;
+ if (!getInt(&nrEntriesTotal))
+ {
+ error("bad nrEntriesTotal found");
+ return false;
+ }
+ unsigned long cdSize;
+ if (!getLong(&cdSize))
+ {
+ error("bad cdSize found");
+ return false;
+ }
+ unsigned long cdPos;
+ if (!getLong(&cdPos))
+ {
+ error("bad cdPos found");
+ return false;
+ }
+ unsigned int commentSize;
+ if (!getInt(&commentSize))
+ {
+ error("bad commentSize found");
+ return false;
+ }
+ comment = "";
+ for (unsigned int i=0 ; i<commentSize ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ comment.push_back(ch);
+ }
+ trace("######## Zip Comment :'%s'" , comment.c_str());
+
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::read()
+{
+ fileBufPos = 0;
+ if (!readFileData())
+ {
+ return false;
+ }
+ if (!readCentralDirectory())
+ {
+ return false;
+ }
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::readBuffer(const std::vector<unsigned char> &inbuf)
+{
+ fileBuf = inbuf;
+ if (!read())
+ return false;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::readFile(const std::string &fileName)
+{
+ fileBuf.clear();
+ FILE *f = fopen(fileName.c_str(), "rb");
+ if (!f)
+ return false;
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ fileBuf.push_back(ch);
+ }
+ fclose(f);
+ if (!read())
+ return false;
+ return true;
+}
+
+
+
+
+
+
+
+
+
+//########################################################################
+//# E N D O F F I L E
+//########################################################################
+
+
diff --git a/src/util/ziptool.h b/src/util/ziptool.h
new file mode 100644
index 0000000..26aaac8
--- /dev/null
+++ b/src/util/ziptool.h
@@ -0,0 +1,575 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/** @file
+ * TODO: insert short description here
+ *//*
+ * Authors:
+ * see git history
+ * Bob Jamison
+ *
+ * Copyright (C) 2018 Authors
+ * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information.
+ */
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#ifndef SEEN_ZIPTOOL_H
+#define SEEN_ZIPTOOL_H
+/**
+ * This is intended to be a standalone, reduced capability
+ * implementation of Gzip and Zip functionality. Its
+ * targeted use case is for archiving and retrieving single files
+ * which use these encoding types. Being memory based and
+ * non-optimized, it is not useful in cases where very large
+ * archives are needed or where high performance is desired.
+ * However, it should hopefully work well for smaller,
+ * one-at-a-time tasks. What you get in return is the ability
+ * to drop these files into your project and remove the dependencies
+ * on ZLib and Info-Zip. Enjoy.
+ */
+
+
+
+#include <vector>
+#include <string>
+
+
+//########################################################################
+//# A D L E R 3 2
+//########################################################################
+
+class Adler32
+{
+public:
+
+ Adler32();
+
+ virtual ~Adler32();
+
+ void reset();
+
+ void update(unsigned char b);
+
+ void update(char *str);
+
+ unsigned long getValue();
+
+private:
+
+ unsigned long value;
+
+};
+
+
+//########################################################################
+//# C R C 3 2
+//########################################################################
+
+class Crc32
+{
+public:
+
+ Crc32();
+
+ virtual ~Crc32();
+
+ void reset();
+
+ void update(unsigned char b);
+
+ void update(char *str);
+
+ void update(const std::vector<unsigned char> &buf);
+
+ unsigned long getValue();
+
+private:
+
+ unsigned long value;
+
+};
+
+
+
+
+
+
+//########################################################################
+//# G Z I P S T R E A M S
+//########################################################################
+
+class GzipFile
+{
+public:
+
+ /**
+ *
+ */
+ GzipFile();
+
+ /**
+ *
+ */
+ virtual ~GzipFile();
+
+ /**
+ *
+ */
+ virtual void put(unsigned char ch);
+
+ /**
+ *
+ */
+ virtual void setData(const std::vector<unsigned char> &str);
+
+ /**
+ *
+ */
+ virtual void clearData();
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getData();
+
+ /**
+ *
+ */
+ virtual std::string &getFileName();
+
+ /**
+ *
+ */
+ virtual void setFileName(const std::string &val);
+
+
+ //######################
+ //# U T I L I T Y
+ //######################
+
+ /**
+ *
+ */
+ virtual bool readFile(const std::string &fName);
+
+ //######################
+ //# W R I T E
+ //######################
+
+ /**
+ *
+ */
+ virtual bool write();
+
+ /**
+ *
+ */
+ virtual bool writeBuffer(std::vector<unsigned char> &outbuf);
+
+ /**
+ *
+ */
+ virtual bool writeFile(const std::string &fileName);
+
+
+ //######################
+ //# R E A D
+ //######################
+
+
+ /**
+ *
+ */
+ virtual bool read();
+
+ /**
+ *
+ */
+ virtual bool readBuffer(const std::vector<unsigned char> &inbuf);
+
+ /**
+ *
+ */
+ virtual bool loadFile(const std::string &fileName);
+
+
+
+private:
+
+ std::vector<unsigned char> data;
+ std::string fileName;
+
+ //debug messages
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ std::vector<unsigned char> fileBuf;
+ unsigned long fileBufPos;
+
+ bool getByte(unsigned char *ch);
+ bool getLong(unsigned long *val);
+
+ bool putByte(unsigned char ch);
+ bool putLong(unsigned long val);
+
+ int compressionMethod;
+};
+
+
+
+
+//########################################################################
+//# Z I P F I L E
+//########################################################################
+
+
+/**
+ *
+ */
+class ZipEntry
+{
+public:
+
+ /**
+ *
+ */
+ ZipEntry();
+
+ /**
+ *
+ */
+ ZipEntry(std::string fileName,
+ std::string comment);
+
+ /**
+ *
+ */
+ virtual ~ZipEntry();
+
+ /**
+ *
+ */
+ virtual std::string getFileName();
+
+ /**
+ *
+ */
+ virtual void setFileName(const std::string &val);
+
+ /**
+ *
+ */
+ virtual std::string getComment();
+
+ /**
+ *
+ */
+ virtual void setComment(const std::string &val);
+
+ /**
+ *
+ */
+ virtual unsigned long getCompressedSize();
+
+ /**
+ *
+ */
+ virtual int getCompressionMethod();
+
+ /**
+ *
+ */
+ virtual void setCompressionMethod(int val);
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getCompressedData();
+
+ /**
+ *
+ */
+ virtual void setCompressedData(const std::vector<unsigned char> &val);
+
+ /**
+ *
+ */
+ virtual unsigned long getUncompressedSize();
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getUncompressedData();
+
+ /**
+ *
+ */
+ virtual void setUncompressedData(const std::vector<unsigned char> &val);
+ virtual void setUncompressedData(const std::string &val);
+
+ /**
+ *
+ */
+ virtual void write(unsigned char ch);
+
+ /**
+ *
+ */
+ virtual void finish();
+
+ /**
+ *
+ */
+ virtual unsigned long getCrc();
+
+ /**
+ *
+ */
+ virtual void setCrc(unsigned long crc);
+
+ /**
+ *
+ */
+ virtual bool readFile(const std::string &fileNameArg,
+ const std::string &commentArg);
+
+ /**
+ *
+ */
+ virtual void setPosition(unsigned long val);
+
+ /**
+ *
+ */
+ virtual unsigned long getPosition();
+
+private:
+
+ unsigned long crc;
+
+ std::string fileName;
+ std::string comment;
+
+ int compressionMethod;
+
+ std::vector<unsigned char> compressedData;
+ std::vector<unsigned char> uncompressedData;
+
+ unsigned long position;
+};
+
+
+
+
+
+
+
+
+
+/**
+ * This class sits over the zlib and gzip code to
+ * implement a PKWare or Info-Zip .zip file reader and
+ * writer
+ */
+class ZipFile
+{
+public:
+
+ /**
+ *
+ */
+ ZipFile();
+
+ /**
+ *
+ */
+ virtual ~ZipFile();
+
+ //######################
+ //# V A R I A B L E S
+ //######################
+
+ /**
+ *
+ */
+ virtual void setComment(const std::string &val);
+
+ /**
+ *
+ */
+ virtual std::string getComment();
+
+ /**
+ * Return the list of entries currently in this file
+ */
+ std::vector<ZipEntry *> &getEntries();
+
+
+ //######################
+ //# U T I L I T Y
+ //######################
+
+ /**
+ *
+ */
+ virtual ZipEntry *addFile(const std::string &fileNameArg,
+ const std::string &commentArg);
+
+ /**
+ *
+ */
+ virtual ZipEntry *newEntry(const std::string &fileNameArg,
+ const std::string &commentArg);
+
+ //######################
+ //# W R I T E
+ //######################
+
+ /**
+ *
+ */
+ virtual bool write();
+
+ /**
+ *
+ */
+ virtual bool writeBuffer(std::vector<unsigned char> &outbuf);
+
+ /**
+ *
+ */
+ virtual bool writeFile(const std::string &fileName);
+
+
+ //######################
+ //# R E A D
+ //######################
+
+
+ /**
+ *
+ */
+ virtual bool read();
+
+ /**
+ *
+ */
+ virtual bool readBuffer(const std::vector<unsigned char> &inbuf);
+
+ /**
+ *
+ */
+ virtual bool readFile(const std::string &fileName);
+
+
+private:
+
+ //debug messages
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ //# Private writing methods
+
+ /**
+ *
+ */
+ bool putLong(unsigned long val);
+
+ /**
+ *
+ */
+ bool putInt(unsigned int val);
+
+
+ /**
+ *
+ */
+ bool putByte(unsigned char val);
+
+ /**
+ *
+ */
+ bool writeFileData();
+
+ /**
+ *
+ */
+ bool writeCentralDirectory();
+
+
+ //# Private reading methods
+
+ /**
+ *
+ */
+ bool getLong(unsigned long *val);
+
+ /**
+ *
+ */
+ bool getInt(unsigned int *val);
+
+ /**
+ *
+ */
+ bool getByte(unsigned char *val);
+
+ /**
+ *
+ */
+ bool readFileData();
+
+ /**
+ *
+ */
+ bool readCentralDirectory();
+
+
+ std::vector<ZipEntry *> entries;
+
+ std::vector<unsigned char> fileBuf;
+ unsigned long fileBufPos;
+
+ std::string comment;
+};
+
+
+
+
+
+
+#endif // SEEN_ZIPTOOL_H
+
+
+//########################################################################
+//# E N D O F F I L E
+//########################################################################
+