From b3e3b41de535e94cf28a7d6714b7b8b7a187bf65 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 7 Apr 2020 17:10:31 +0200 Subject: [PATCH 1/4] debug/demangle: Replace command parsing with Glib::spawn_sync(). --- src/debug/demangle.cpp | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/debug/demangle.cpp b/src/debug/demangle.cpp index 60e1f176d6..fa0a58cbe3 100644 --- a/src/debug/demangle.cpp +++ b/src/debug/demangle.cpp @@ -10,13 +10,11 @@ * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ -#include -#include #include #include #include +#include #include "debug/demangle.h" -#include "util/format.h" namespace Inkscape { @@ -24,21 +22,12 @@ namespace Debug { namespace { -char const *demangle_helper(char const *name) { - char buffer[1024]; - char const *result; - FILE *stream=popen(Util::format("c++filt %s", name), "r"); - if (fgets(buffer, sizeof(buffer), stream)) { - size_t len=strlen(buffer); - if ( buffer[len-1] == '\n' ) { - buffer[len-1] = '\000'; - } - result = strdup(buffer); - } else { - result = name; - } - pclose(stream); - return result; +static std::string demangle_helper(char const *name) { + std::vector argv = {"c++filt", name}; + std::string output; + Glib::spawn_sync("", argv, Glib::SPAWN_DEFAULT, Glib::SlotSpawnChildSetup(), &output); + output.pop_back(); + return output; } struct string_less_than { @@ -58,7 +47,7 @@ std::shared_ptr demangle(char const *name) { return (*found).second; } - char const *result = demangle_helper(name); + std::string result = demangle_helper(name); return mangle_cache[name] = std::make_shared(result); } -- GitLab From e1566d19511c449e2007fbed68545cf8815d1601 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 7 Apr 2020 17:11:10 +0200 Subject: [PATCH 2/4] debug/simple-event: Replace g_strdup_vprintf() with Glib::ustring::sprintf(). --- src/debug/simple-event.h | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/debug/simple-event.h b/src/debug/simple-event.h index c753ae1fee..0c6e4f3225 100644 --- a/src/debug/simple-event.h +++ b/src/debug/simple-event.h @@ -17,7 +17,7 @@ #include #include #include -#include // g_assert() +#include #include "debug/event.h" @@ -47,6 +47,9 @@ protected: void _addProperty(char const *name, std::shared_ptr&& value) { _properties.push_back(PropertyPair(name, std::move(value))); } + void _addProperty(char const *name, std::string&& value) { + _properties.push_back(PropertyPair(name, std::make_shared(std::move(value)))); + } void _addProperty(char const *name, char const *value) { _addProperty(name, std::make_shared(value)); } @@ -58,15 +61,10 @@ private: char const *_name; std::vector _properties; - void _addFormattedProperty(char const *name, char const *format, ...) + template + void _addFormattedProperty(char const *name, char const *format, const Ts&... args) { - va_list args; - va_start(args, format); - gchar *value=g_strdup_vprintf(format, args); - g_assert(value != nullptr); - va_end(args); - _addProperty(name, value); - g_free(value); + _addProperty(name, Glib::ustring::sprintf(format, args...)); } }; -- GitLab From 7529defbb0e24bb024aa80726370de1a28f19565 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 7 Apr 2020 17:12:10 +0200 Subject: [PATCH 3/4] Replace Util::format() with Glib::ustring::sprintf(). --- src/gc-anchored.cpp | 6 ++--- src/gc-finalized.cpp | 6 ++--- src/object/sp-object.cpp | 6 ++--- src/util/CMakeLists.txt | 1 - src/util/format.h | 57 ---------------------------------------- src/xml/simple-node.cpp | 2 +- 6 files changed, 10 insertions(+), 68 deletions(-) delete mode 100644 src/util/format.h diff --git a/src/gc-anchored.cpp b/src/gc-anchored.cpp index 12eaa83193..f29212d17b 100644 --- a/src/gc-anchored.cpp +++ b/src/gc-anchored.cpp @@ -11,11 +11,11 @@ */ #include +#include #include "gc-anchored.h" #include "debug/event-tracker.h" #include "debug/simple-event.h" #include "debug/demangle.h" -#include "util/format.h" namespace Inkscape { @@ -31,8 +31,8 @@ public: char const *name) : RefCountEvent(name) { - _addProperty("base", Util::format("%p", Core::base(const_cast(object))).pointer()); - _addProperty("pointer", Util::format("%p", object).pointer()); + _addProperty("base", Glib::ustring::sprintf("%p", Core::base(const_cast(object)))); + _addProperty("pointer", Glib::ustring::sprintf("%p", object)); _addProperty("class", Debug::demangle(typeid(*object).name())); _addProperty("new-refcount", object->_anchored_refcount() + bias); } diff --git a/src/gc-finalized.cpp b/src/gc-finalized.cpp index 8bba510d29..d50c63a436 100644 --- a/src/gc-finalized.cpp +++ b/src/gc-finalized.cpp @@ -23,9 +23,9 @@ */ #include +#include #include "debug/simple-event.h" #include "debug/event-tracker.h" -#include "util/format.h" #include "gc-finalized.h" namespace Inkscape { @@ -42,8 +42,8 @@ public: FinalizerEvent(Finalized *object) : BaseEvent("gc-finalizer") { - _addProperty("base", Util::format("%p", Core::base(object)).pointer()); - _addProperty("pointer", Util::format("%p", object).pointer()); + _addProperty("base", Glib::ustring::sprintf("%p", Core::base(object))); + _addProperty("pointer", Glib::ustring::sprintf("%p", object)); _addProperty("class", typeid(*object).name()); } }; diff --git a/src/object/sp-object.cpp b/src/object/sp-object.cpp index 4030219295..f310102eb6 100644 --- a/src/object/sp-object.cpp +++ b/src/object/sp-object.cpp @@ -21,6 +21,7 @@ #include #include +#include #include "helper/sp-marshal.h" #include "xml/node-event-vector.h" @@ -42,7 +43,6 @@ #include "debug/event-tracker.h" #include "debug/simple-event.h" #include "debug/demangle.h" -#include "util/format.h" #include "util/longest-common-suffix.h" #define noSP_OBJECT_DEBUG_CASCADE @@ -200,9 +200,9 @@ public: RefCountEvent(SPObject *object, int bias, char const *name) : BaseRefCountEvent(name) { - _addProperty("object", Util::format("%p", object).pointer()); + _addProperty("object", Glib::ustring::sprintf("%p", object)); _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object)))); - _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias).pointer()); + _addProperty("new-refcount", Glib::ustring::sprintf("%d", G_OBJECT(object)->ref_count + bias)); } }; diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 4831c3b42a..4f73813656 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -16,7 +16,6 @@ set(util_SRC find-if-before.h find-last-if.h fixed_point.h - format.h forward-pointer-iterator.h list-container-test.h list-container.h diff --git a/src/util/format.h b/src/util/format.h deleted file mode 100644 index b38f3a64ab..0000000000 --- a/src/util/format.h +++ /dev/null @@ -1,57 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/* - * Inkscape::Util::format - g_strdup_printf wrapper producing shared strings - * - * Authors: - * MenTaLguY - * - * 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 -#include -#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/xml/simple-node.cpp b/src/xml/simple-node.cpp index e3e13ac9f3..32deed48c4 100644 --- a/src/xml/simple-node.cpp +++ b/src/xml/simple-node.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "preferences.h" @@ -26,7 +27,6 @@ #include "xml/node-fns.h" #include "debug/event-tracker.h" #include "debug/simple-event.h" -#include "util/format.h" #include "attribute-rel-util.h" -- GitLab From e687c57019cc640bc29a3e241e47dba9c4f6c4ef Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 7 Apr 2020 17:56:06 +0200 Subject: [PATCH 4/4] Add a util/ustring.h defining Glib::ustring::sprintf() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These template methods have only been added to glibmm 2.62, so until we require this version let’s import them as Inkscape::ustring::sprintf(). --- src/debug/simple-event.h | 4 +- src/gc-anchored.cpp | 6 +-- src/gc-finalized.cpp | 6 +-- src/object/sp-object.cpp | 6 +-- src/util/CMakeLists.txt | 1 + src/util/ustring.h | 95 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 src/util/ustring.h diff --git a/src/debug/simple-event.h b/src/debug/simple-event.h index 0c6e4f3225..b6542063c7 100644 --- a/src/debug/simple-event.h +++ b/src/debug/simple-event.h @@ -17,9 +17,9 @@ #include #include #include -#include #include "debug/event.h" +#include "util/ustring.h" namespace Inkscape { @@ -64,7 +64,7 @@ private: template void _addFormattedProperty(char const *name, char const *format, const Ts&... args) { - _addProperty(name, Glib::ustring::sprintf(format, args...)); + _addProperty(name, Inkscape::ustring::sprintf(format, args...)); } }; diff --git a/src/gc-anchored.cpp b/src/gc-anchored.cpp index f29212d17b..b24588c733 100644 --- a/src/gc-anchored.cpp +++ b/src/gc-anchored.cpp @@ -11,11 +11,11 @@ */ #include -#include #include "gc-anchored.h" #include "debug/event-tracker.h" #include "debug/simple-event.h" #include "debug/demangle.h" +#include "util/ustring.h" namespace Inkscape { @@ -31,8 +31,8 @@ public: char const *name) : RefCountEvent(name) { - _addProperty("base", Glib::ustring::sprintf("%p", Core::base(const_cast(object)))); - _addProperty("pointer", Glib::ustring::sprintf("%p", object)); + _addProperty("base", Inkscape::ustring::sprintf("%p", Core::base(const_cast(object)))); + _addProperty("pointer", Inkscape::ustring::sprintf("%p", object)); _addProperty("class", Debug::demangle(typeid(*object).name())); _addProperty("new-refcount", object->_anchored_refcount() + bias); } diff --git a/src/gc-finalized.cpp b/src/gc-finalized.cpp index d50c63a436..efd9436e22 100644 --- a/src/gc-finalized.cpp +++ b/src/gc-finalized.cpp @@ -23,10 +23,10 @@ */ #include -#include #include "debug/simple-event.h" #include "debug/event-tracker.h" #include "gc-finalized.h" +#include "util/ustring.h" namespace Inkscape { @@ -42,8 +42,8 @@ public: FinalizerEvent(Finalized *object) : BaseEvent("gc-finalizer") { - _addProperty("base", Glib::ustring::sprintf("%p", Core::base(object))); - _addProperty("pointer", Glib::ustring::sprintf("%p", object)); + _addProperty("base", Inkscape::ustring::sprintf("%p", Core::base(object))); + _addProperty("pointer", Inkscape::ustring::sprintf("%p", object)); _addProperty("class", typeid(*object).name()); } }; diff --git a/src/object/sp-object.cpp b/src/object/sp-object.cpp index f310102eb6..b8570dc0da 100644 --- a/src/object/sp-object.cpp +++ b/src/object/sp-object.cpp @@ -21,7 +21,6 @@ #include #include -#include #include "helper/sp-marshal.h" #include "xml/node-event-vector.h" @@ -44,6 +43,7 @@ #include "debug/simple-event.h" #include "debug/demangle.h" #include "util/longest-common-suffix.h" +#include "util/ustring.h" #define noSP_OBJECT_DEBUG_CASCADE @@ -200,9 +200,9 @@ public: RefCountEvent(SPObject *object, int bias, char const *name) : BaseRefCountEvent(name) { - _addProperty("object", Glib::ustring::sprintf("%p", object)); + _addProperty("object", Inkscape::ustring::sprintf("%p", object)); _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object)))); - _addProperty("new-refcount", Glib::ustring::sprintf("%d", G_OBJECT(object)->ref_count + bias)); + _addProperty("new-refcount", Inkscape::ustring::sprintf("%d", G_OBJECT(object)->ref_count + bias)); } }; diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 4f73813656..fb724438ad 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -28,6 +28,7 @@ set(util_SRC signal-blocker.h ucompose.hpp units.h + ustring.h ziptool.h ) diff --git a/src/util/ustring.h b/src/util/ustring.h new file mode 100644 index 0000000000..ef89585b8e --- /dev/null +++ b/src/util/ustring.h @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * Glib::ustring::sprintf - Implement this function for glibmm < 2.62 + * + * Copyright (C) 2002 The gtkmm Development Team + * + * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_USTRING_H +#define SEEN_INKSCAPE_USTRING_H + +#include + +namespace Inkscape { + +namespace ustring { + +// All of this is a direct copy from , because they are only available from +// glibmm 2.62 onwards. + +/* These helper functions used by ustring::sprintf() let users pass C++ strings + * to match %s placeholders, without the hassle of writing .c_str() in user code + */ +template +inline // static + const T& + sprintify(const T& arg) +{ + return arg; +} + +inline // static + const char* + sprintify(const Glib::ustring& arg) +{ + return arg.c_str(); +} + +inline // static + const char* + sprintify(const std::string& arg) +{ + return arg.c_str(); +} + +template +inline // static + Glib::ustring + sprintf(const Glib::ustring& fmt, const Ts&... args) +{ + return sprintf(fmt.c_str(), args...); +} + +template +inline // static + Glib::ustring + sprintf(const char* fmt, const Ts&... args) +{ + auto c_str = g_strdup_printf(fmt, sprintify(args)...); + Glib::ustring ustr(c_str); + g_free(c_str); + + return ustr; +} + +inline // static + Glib::ustring + sprintf(const Glib::ustring& fmt) +{ + return fmt; +} + +inline // static + Glib::ustring + sprintf(const char* fmt) +{ + return Glib::ustring(fmt); +} + +} + +} + +#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 : -- GitLab