aboutsummaryrefslogtreecommitdiff
path: root/c/util.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-22 01:05:05 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-22 01:05:05 +0200
commitba0750ff48bcf97ef99602fe27322fa706a93b6b (patch)
tree0a6e0110e1940610266d280e2c857195819ffa1a /c/util.h
parent28b87e4d9c60b49d46b03a19b7b83e23e222087a (diff)
downloadEpisodeBrowser-ba0750ff48bcf97ef99602fe27322fa706a93b6b.tar.gz
Add Buf class.
Diffstat (limited to 'c/util.h')
-rw-r--r--c/util.h45
1 files changed, 29 insertions, 16 deletions
diff --git a/c/util.h b/c/util.h
index 1d8d222..698ce10 100644
--- a/c/util.h
+++ b/c/util.h
@@ -4,42 +4,51 @@
#include <algorithm>
#include <cstring>
#include <memory>
+#include <string>
#include <SWI-Prolog.h>
#include <windows.h>
-#define CAT(a, b) a##b
-#define APPLY(a, ...) a(__VA_ARGS__)
-#define UNUSED APPLY(CAT, unused_, __COUNTER__)
+/* Buf is a span-like structure of a buffer and its size. */
+template <typename T>
+struct Buf
+{
+ T* data;
+ size_t size;
+ Buf(T* data, size_t size) : data(data), size(size) {}
+ Buf(std::basic_string<T>& s) : data(s.data()), size(s.capacity()) {}
+ template <size_t N> Buf(T (&data)[N]) : data(data), size(N) {}
+ operator T*() { return data; }
+ T& operator *() { return *data; }
+ T& operator [](size_t i) { return data[i]; }
+};
-/* Format static wide string. */
-template<size_t N, typename... T>
-inline int Swprintf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs)
+/* Format wide string. */
+template<typename... T>
+inline int Swprintf(Buf<wchar_t> buf, const wchar_t* const fmt, T... xs)
{
- return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...);
+ return _snwprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...);
}
/* Format static narrow string. */
-template<size_t N, typename... T>
-inline int Sprintf(char (&buf)[N], const char* const fmt, T... xs)
+template<typename... T>
+inline int Sprintf(Buf<char> buf, const char* const fmt, T... xs)
{
- return _snprintf_s(buf, N, _TRUNCATE, fmt, xs...);
+ return _snprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...);
}
/* Copy to static wide string buffer. */
-template <size_t N>
-inline wchar_t* Wcscpy(wchar_t (&dst)[N], const wchar_t* const src)
+inline wchar_t* Wcscpy(Buf<wchar_t> dst, const wchar_t* const src)
{
- const size_t len = std::min(N, wcslen(src)+1);
+ const size_t len = std::min(dst.size, wcslen(src)+1);
memcpy(dst, src, len*sizeof(wchar_t));
dst[len-1] = 0;
return dst;
}
/* Copy to static narrow string buffer. */
-template <size_t N>
-inline char* Strcpy(char (&dst)[N], const char* const src)
+inline char* Strcpy(Buf<char> dst, const char* const src)
{
- const size_t len = std::min(N, strlen(src)+1);
+ const size_t len = std::min(dst.size, strlen(src)+1);
memcpy(dst, src, len);
dst[len-1] = 0;
return dst;
@@ -52,4 +61,8 @@ inline int Cmp(const int a, const int b)
return -1;
}
+#define CAT(a, b) a##b
+#define APPLY(a, ...) a(__VA_ARGS__)
+#define UNUSED APPLY(CAT, unused_, __COUNTER__)
+
#endif