diff --git a/src/debug/demangle.cpp b/src/debug/demangle.cpp index 60e1f176d6aebb083a9075ad1689597595af3675..fa0a58cbe35daee7c22e507f7866588ea0df56e7 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); } diff --git a/src/debug/simple-event.h b/src/debug/simple-event.h index c753ae1fee94e12ef98dc693e661d392a482a581..b6542063c7521f421654bb1ae40b21d04e2c9123 100644 --- a/src/debug/simple-event.h +++ b/src/debug/simple-event.h @@ -17,9 +17,9 @@ #include #include #include -#include // g_assert() #include "debug/event.h" +#include "util/ustring.h" namespace Inkscape { @@ -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, Inkscape::ustring::sprintf(format, args...)); } }; diff --git a/src/gc-anchored.cpp b/src/gc-anchored.cpp index 12eaa831938cbad9074f0b65dffe42c7c83c7cbe..b24588c73354d3fbeca2eb0863c35a2cfdc82ea4 100644 --- a/src/gc-anchored.cpp +++ b/src/gc-anchored.cpp @@ -15,7 +15,7 @@ #include "debug/event-tracker.h" #include "debug/simple-event.h" #include "debug/demangle.h" -#include "util/format.h" +#include "util/ustring.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", 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 8bba510d29767cae98b01a696313b1a3dbc5be0b..efd9436e22bb77494d57673aaea908badc8a52d8 100644 --- a/src/gc-finalized.cpp +++ b/src/gc-finalized.cpp @@ -25,8 +25,8 @@ #include #include "debug/simple-event.h" #include "debug/event-tracker.h" -#include "util/format.h" #include "gc-finalized.h" +#include "util/ustring.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", 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 403021929525b2b99d958d4628674ffd76937d7b..b8570dc0da5bb3259b6dc0703dcac32003ba5e87 100644 --- a/src/object/sp-object.cpp +++ b/src/object/sp-object.cpp @@ -42,8 +42,8 @@ #include "debug/event-tracker.h" #include "debug/simple-event.h" #include "debug/demangle.h" -#include "util/format.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", Util::format("%p", object).pointer()); + _addProperty("object", Inkscape::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", Inkscape::ustring::sprintf("%d", G_OBJECT(object)->ref_count + bias)); } }; diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 4831c3b42abc339a5186d23e3a1426dfd977d26b..fb724438ad70c014f2e30c3e4b4d879dd428b140 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 @@ -29,6 +28,7 @@ set(util_SRC signal-blocker.h ucompose.hpp units.h + ustring.h ziptool.h ) diff --git a/src/util/format.h b/src/util/format.h deleted file mode 100644 index b38f3a64aba3a1cf08473864f1341f9114a765bb..0000000000000000000000000000000000000000 --- 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/util/ustring.h b/src/util/ustring.h new file mode 100644 index 0000000000000000000000000000000000000000..ef89585b8ef1c573e5cd58e2213123faeb63398c --- /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 : diff --git a/src/xml/simple-node.cpp b/src/xml/simple-node.cpp index e3e13ac9f347864ae87127d0f822e963f1bbda94..32deed48c4a7ca971ba9ca4a5c142cb4e9ede0e9 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"