diff --git a/src/nfd_gtk.cpp b/src/nfd_gtk.cpp index 9c1d9b9..dd9d311 100644 --- a/src/nfd_gtk.cpp +++ b/src/nfd_gtk.cpp @@ -54,22 +54,6 @@ filters to be case-insensitive. namespace { -template -struct Free_Guard { - T* data; - Free_Guard(T* freeable) noexcept : data(freeable) {} - ~Free_Guard() { NFDi_Free(data); } -}; - -template -struct FreeCheck_Guard { - T* data; - FreeCheck_Guard(T* freeable = nullptr) noexcept : data(freeable) {} - ~FreeCheck_Guard() { - if (data) NFDi_Free(data); - } -}; - /* current error */ const char* g_errorstr = nullptr; @@ -77,49 +61,6 @@ void NFDi_SetError(const char* msg) { g_errorstr = msg; } -template -T* NFDi_Malloc(size_t bytes) { - void* ptr = malloc(bytes); - if (!ptr) NFDi_SetError("NFDi_Malloc failed."); - - return static_cast(ptr); -} - -template -void NFDi_Free(T* ptr) { - assert(ptr); - free(static_cast(ptr)); -} - -template -T* copy(const T* begin, const T* end, T* out) { - for (; begin != end; ++begin) { - *out++ = *begin; - } - return out; -} - -#ifndef NFD_CASE_SENSITIVE_FILTER -nfdnchar_t* emit_case_insensitive_glob(const nfdnchar_t* begin, - const nfdnchar_t* end, - nfdnchar_t* out) { - // this code will only make regular Latin characters case-insensitive; other - // characters remain case sensitive - for (; begin != end; ++begin) { - if ((*begin >= 'A' && *begin <= 'Z') || (*begin >= 'a' && *begin <= 'z')) { - *out++ = '['; - *out++ = *begin; - // invert the case of the original character - *out++ = *begin ^ static_cast(0x20); - *out++ = ']'; - } else { - *out++ = *begin; - } - } - return out; -} -#endif - // Does not own the filter and extension. struct Pair_GtkFileFilter_FileExtension { GtkFileFilter* filter; diff --git a/src/nfd_linux_shared.hpp b/src/nfd_linux_shared.hpp index 52a0a1a..1413bac 100644 --- a/src/nfd_linux_shared.hpp +++ b/src/nfd_linux_shared.hpp @@ -7,6 +7,11 @@ These are shared functions for Linux (GTK and Portal). */ +#include +#include + +#include "nfd.h" + #ifdef NFD_WAYLAND #include #include "xdg-foreign-unstable-v1.h" @@ -14,6 +19,65 @@ namespace { +template +T* NFDi_Malloc(size_t bytes) { + void* ptr = malloc(bytes); + assert(ptr); // Linux malloc never fails + + return static_cast(ptr); +} + +template +void NFDi_Free(T* ptr) { + assert(ptr); + free(static_cast(ptr)); +} + +template +struct Free_Guard { + T* data; + Free_Guard(T* freeable) noexcept : data(freeable) {} + ~Free_Guard() { NFDi_Free(data); } +}; + +template +struct FreeCheck_Guard { + T* data; + FreeCheck_Guard(T* freeable = nullptr) noexcept : data(freeable) {} + ~FreeCheck_Guard() { + if (data) NFDi_Free(data); + } +}; + +template +T* copy(const T* begin, const T* end, T* out) { + for (; begin != end; ++begin) { + *out++ = *begin; + } + return out; +} + +#ifndef NFD_CASE_SENSITIVE_FILTER +nfdnchar_t* emit_case_insensitive_glob(const nfdnchar_t* begin, + const nfdnchar_t* end, + nfdnchar_t* out) { + // this code will only make regular Latin characters case-insensitive; other + // characters remain case sensitive + for (; begin != end; ++begin) { + if ((*begin >= 'A' && *begin <= 'Z') || (*begin >= 'a' && *begin <= 'z')) { + *out++ = '['; + *out++ = *begin; + // invert the case of the original character + *out++ = *begin ^ static_cast(0x20); + *out++ = ']'; + } else { + *out++ = *begin; + } + } + return out; +} +#endif + #ifdef NFD_WAYLAND struct wl_display* wayland_display; struct wl_registry* wayland_registry; diff --git a/src/nfd_portal.cpp b/src/nfd_portal.cpp index de22da0..ffb2aa2 100644 --- a/src/nfd_portal.cpp +++ b/src/nfd_portal.cpp @@ -47,36 +47,6 @@ filters to be case-insensitive. namespace { -template -T* NFDi_Malloc(size_t bytes) { - void* ptr = malloc(bytes); - assert(ptr); // Linux malloc never fails - - return static_cast(ptr); -} - -template -void NFDi_Free(T* ptr) { - assert(ptr); - free(static_cast(ptr)); -} - -template -struct Free_Guard { - T* data; - Free_Guard(T* freeable) noexcept : data(freeable) {} - ~Free_Guard() { NFDi_Free(data); } -}; - -template -struct FreeCheck_Guard { - T* data; - FreeCheck_Guard(T* freeable = nullptr) noexcept : data(freeable) {} - ~FreeCheck_Guard() { - if (data) NFDi_Free(data); - } -}; - struct DBusMessage_Guard { DBusMessage* data; DBusMessage_Guard(DBusMessage* freeable) noexcept : data(freeable) {} @@ -109,14 +79,6 @@ void NFDi_SetFormattedError(const char* format, ...) { err_ptr = owned_err; } -template -T* copy(const T* begin, const T* end, T* out) { - for (; begin != end; ++begin) { - *out++ = *begin; - } - return out; -} - template T* transform(const T* begin, const T* end, T* out, Callback callback) { for (; begin != end; ++begin) { @@ -133,27 +95,6 @@ T* reverse_copy(const T* begin, const T* end, T* out) { return out; } -#ifndef NFD_CASE_SENSITIVE_FILTER -nfdnchar_t* emit_case_insensitive_glob(const nfdnchar_t* begin, - const nfdnchar_t* end, - nfdnchar_t* out) { - // this code will only make regular Latin characters case-insensitive; other - // characters remain case sensitive - for (; begin != end; ++begin) { - if ((*begin >= 'A' && *begin <= 'Z') || (*begin >= 'a' && *begin <= 'z')) { - *out++ = '['; - *out++ = *begin; - // invert the case of the original character - *out++ = *begin ^ static_cast(0x20); - *out++ = ']'; - } else { - *out++ = *begin; - } - } - return out; -} -#endif - // Returns true if ch is in [0-9A-Za-z], false otherwise. bool IsHex(char ch) { return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F') || ('a' <= ch && ch <= 'f');