Skip to content

Commit c9cc7ba

Browse files
authored
Linux: Hoist identical code from GTK and Portal implementations into Linux shared header (#179)
1 parent 433edaa commit c9cc7ba

3 files changed

Lines changed: 64 additions & 118 deletions

File tree

src/nfd_gtk.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -54,72 +54,13 @@ filters to be case-insensitive.
5454

5555
namespace {
5656

57-
template <typename T>
58-
struct Free_Guard {
59-
T* data;
60-
Free_Guard(T* freeable) noexcept : data(freeable) {}
61-
~Free_Guard() { NFDi_Free(data); }
62-
};
63-
64-
template <typename T>
65-
struct FreeCheck_Guard {
66-
T* data;
67-
FreeCheck_Guard(T* freeable = nullptr) noexcept : data(freeable) {}
68-
~FreeCheck_Guard() {
69-
if (data) NFDi_Free(data);
70-
}
71-
};
72-
7357
/* current error */
7458
const char* g_errorstr = nullptr;
7559

7660
void NFDi_SetError(const char* msg) {
7761
g_errorstr = msg;
7862
}
7963

80-
template <typename T = void>
81-
T* NFDi_Malloc(size_t bytes) {
82-
void* ptr = malloc(bytes);
83-
if (!ptr) NFDi_SetError("NFDi_Malloc failed.");
84-
85-
return static_cast<T*>(ptr);
86-
}
87-
88-
template <typename T>
89-
void NFDi_Free(T* ptr) {
90-
assert(ptr);
91-
free(static_cast<void*>(ptr));
92-
}
93-
94-
template <typename T>
95-
T* copy(const T* begin, const T* end, T* out) {
96-
for (; begin != end; ++begin) {
97-
*out++ = *begin;
98-
}
99-
return out;
100-
}
101-
102-
#ifndef NFD_CASE_SENSITIVE_FILTER
103-
nfdnchar_t* emit_case_insensitive_glob(const nfdnchar_t* begin,
104-
const nfdnchar_t* end,
105-
nfdnchar_t* out) {
106-
// this code will only make regular Latin characters case-insensitive; other
107-
// characters remain case sensitive
108-
for (; begin != end; ++begin) {
109-
if ((*begin >= 'A' && *begin <= 'Z') || (*begin >= 'a' && *begin <= 'z')) {
110-
*out++ = '[';
111-
*out++ = *begin;
112-
// invert the case of the original character
113-
*out++ = *begin ^ static_cast<nfdnchar_t>(0x20);
114-
*out++ = ']';
115-
} else {
116-
*out++ = *begin;
117-
}
118-
}
119-
return out;
120-
}
121-
#endif
122-
12364
// Does not own the filter and extension.
12465
struct Pair_GtkFileFilter_FileExtension {
12566
GtkFileFilter* filter;

src/nfd_linux_shared.hpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,77 @@
77
These are shared functions for Linux (GTK and Portal).
88
*/
99

10+
#include <assert.h>
11+
#include <stdlib.h>
12+
13+
#include "nfd.h"
14+
1015
#ifdef NFD_WAYLAND
1116
#include <wayland-client.h>
1217
#include "xdg-foreign-unstable-v1.h"
1318
#endif
1419

1520
namespace {
1621

22+
template <typename T = void>
23+
T* NFDi_Malloc(size_t bytes) {
24+
void* ptr = malloc(bytes);
25+
assert(ptr); // Linux malloc never fails
26+
27+
return static_cast<T*>(ptr);
28+
}
29+
30+
template <typename T>
31+
void NFDi_Free(T* ptr) {
32+
assert(ptr);
33+
free(static_cast<void*>(ptr));
34+
}
35+
36+
template <typename T>
37+
struct Free_Guard {
38+
T* data;
39+
Free_Guard(T* freeable) noexcept : data(freeable) {}
40+
~Free_Guard() { NFDi_Free(data); }
41+
};
42+
43+
template <typename T>
44+
struct FreeCheck_Guard {
45+
T* data;
46+
FreeCheck_Guard(T* freeable = nullptr) noexcept : data(freeable) {}
47+
~FreeCheck_Guard() {
48+
if (data) NFDi_Free(data);
49+
}
50+
};
51+
52+
template <typename T>
53+
T* copy(const T* begin, const T* end, T* out) {
54+
for (; begin != end; ++begin) {
55+
*out++ = *begin;
56+
}
57+
return out;
58+
}
59+
60+
#ifndef NFD_CASE_SENSITIVE_FILTER
61+
nfdnchar_t* emit_case_insensitive_glob(const nfdnchar_t* begin,
62+
const nfdnchar_t* end,
63+
nfdnchar_t* out) {
64+
// this code will only make regular Latin characters case-insensitive; other
65+
// characters remain case sensitive
66+
for (; begin != end; ++begin) {
67+
if ((*begin >= 'A' && *begin <= 'Z') || (*begin >= 'a' && *begin <= 'z')) {
68+
*out++ = '[';
69+
*out++ = *begin;
70+
// invert the case of the original character
71+
*out++ = *begin ^ static_cast<nfdnchar_t>(0x20);
72+
*out++ = ']';
73+
} else {
74+
*out++ = *begin;
75+
}
76+
}
77+
return out;
78+
}
79+
#endif
80+
1781
#ifdef NFD_WAYLAND
1882
struct wl_display* wayland_display;
1983
struct wl_registry* wayland_registry;

src/nfd_portal.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,6 @@ filters to be case-insensitive.
4747

4848
namespace {
4949

50-
template <typename T = void>
51-
T* NFDi_Malloc(size_t bytes) {
52-
void* ptr = malloc(bytes);
53-
assert(ptr); // Linux malloc never fails
54-
55-
return static_cast<T*>(ptr);
56-
}
57-
58-
template <typename T>
59-
void NFDi_Free(T* ptr) {
60-
assert(ptr);
61-
free(static_cast<void*>(ptr));
62-
}
63-
64-
template <typename T>
65-
struct Free_Guard {
66-
T* data;
67-
Free_Guard(T* freeable) noexcept : data(freeable) {}
68-
~Free_Guard() { NFDi_Free(data); }
69-
};
70-
71-
template <typename T>
72-
struct FreeCheck_Guard {
73-
T* data;
74-
FreeCheck_Guard(T* freeable = nullptr) noexcept : data(freeable) {}
75-
~FreeCheck_Guard() {
76-
if (data) NFDi_Free(data);
77-
}
78-
};
79-
8050
struct DBusMessage_Guard {
8151
DBusMessage* data;
8252
DBusMessage_Guard(DBusMessage* freeable) noexcept : data(freeable) {}
@@ -109,14 +79,6 @@ void NFDi_SetFormattedError(const char* format, ...) {
10979
err_ptr = owned_err;
11080
}
11181

112-
template <typename T>
113-
T* copy(const T* begin, const T* end, T* out) {
114-
for (; begin != end; ++begin) {
115-
*out++ = *begin;
116-
}
117-
return out;
118-
}
119-
12082
template <typename T, typename Callback>
12183
T* transform(const T* begin, const T* end, T* out, Callback callback) {
12284
for (; begin != end; ++begin) {
@@ -133,27 +95,6 @@ T* reverse_copy(const T* begin, const T* end, T* out) {
13395
return out;
13496
}
13597

136-
#ifndef NFD_CASE_SENSITIVE_FILTER
137-
nfdnchar_t* emit_case_insensitive_glob(const nfdnchar_t* begin,
138-
const nfdnchar_t* end,
139-
nfdnchar_t* out) {
140-
// this code will only make regular Latin characters case-insensitive; other
141-
// characters remain case sensitive
142-
for (; begin != end; ++begin) {
143-
if ((*begin >= 'A' && *begin <= 'Z') || (*begin >= 'a' && *begin <= 'z')) {
144-
*out++ = '[';
145-
*out++ = *begin;
146-
// invert the case of the original character
147-
*out++ = *begin ^ static_cast<nfdnchar_t>(0x20);
148-
*out++ = ']';
149-
} else {
150-
*out++ = *begin;
151-
}
152-
}
153-
return out;
154-
}
155-
#endif
156-
15798
// Returns true if ch is in [0-9A-Za-z], false otherwise.
15899
bool IsHex(char ch) {
159100
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F') || ('a' <= ch && ch <= 'f');

0 commit comments

Comments
 (0)