1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
#ifndef UTIL_H
#define UTIL_H
#include <cstring>
#include <memory>
#include <string>
#include <SWI-Prolog.h>
#include <windows.h>
#define CONCAT_IMPL(a, b) a##b
#define CONCAT(a, b) CONCAT_IMPL(a, b)
#define _ CONCAT(unused_, __LINE__)
inline size_t Min(size_t a, size_t b)
{
return a < b? a: b;
}
inline int Cmp(const int a, const int b)
{
if (a == b) return 0;
if (a > b) return 1;
return -1;
}
template <typename T, auto F, typename U, auto E = 0>
struct Managed
{
T obj;
Managed(T obj) : obj(obj)
{
if (obj == reinterpret_cast<T>(E))
throw U();
}
~Managed() { F(obj); }
operator T() { return obj; }
auto& operator *() { return *obj; }
auto& operator ->() { return obj; }
auto& operator [](size_t i) { return obj[i]; }
};
/* 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 wide string. */
template<typename... T>
inline int Swprintf(Buf<wchar_t> buf, const wchar_t* const fmt, T... xs)
{
return _snwprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...);
}
/* Format static narrow string. */
template<typename... T>
inline int Sprintf(Buf<char> buf, const char* const fmt, T... xs)
{
return _snprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...);
}
/* Copy to static wide string buffer. */
inline wchar_t* Wcscpy(Buf<wchar_t> dst, const wchar_t* const src)
{
const size_t len = 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. */
inline char* Strcpy(Buf<char> dst, const char* const src)
{
const size_t len = Min(dst.size, strlen(src)+1);
memcpy(dst, src, len);
dst[len-1] = 0;
return dst;
}
#endif
|