aboutsummaryrefslogtreecommitdiff
path: root/c/util.h
blob: d233ce188d1c2d841235bb6bb1d0c2d0297cf01f (plain)
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
#ifndef UTIL_H
#define UTIL_H

#include <algorithm>
#include <cstring>
#include <memory>

/* Format static wide string. */
template<size_t N, typename... T>
inline int Swprintf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs)
{
	return _snwprintf_s(buf, N, _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)
{
	return _snprintf_s(buf, N, _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)
{
	const size_t len = std::min(N, 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)
{
	const size_t len = std::min(N, strlen(src)+1);
	memcpy(dst, src, len);
	dst[len-1] = 0;
	return dst;
}

inline int Cmp(const int a, const int b)
{
	if (a == b) return 0;
	if (a > b) return 1;
	return -1;
}

#endif