aboutsummaryrefslogtreecommitdiff
path: root/c/util.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-15 22:18:10 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-15 22:18:10 +0200
commit509c461498f0e68a62c004778df890d56ebb2a09 (patch)
treeae552acdcfc4711e399ca7e00f38ee4295cfbca8 /c/util.h
parent1eb7d7013902dc8bbb7b84dc05e0dda4c2243c4f (diff)
downloadEpisodeBrowser-509c461498f0e68a62c004778df890d56ebb2a09.tar.gz
Fix bug in Strcpy. Add Sprintf, Wcscpy.
Diffstat (limited to 'c/util.h')
-rw-r--r--c/util.h22
1 files changed, 20 insertions, 2 deletions
diff --git a/c/util.h b/c/util.h
index d58301b..d233ce1 100644
--- a/c/util.h
+++ b/c/util.h
@@ -12,12 +12,30 @@ 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)
{
- memcpy(dst, src, std::min(N, strlen(src)));
- dst[N-1] = 0;
+ const size_t len = std::min(N, strlen(src)+1);
+ memcpy(dst, src, len);
+ dst[len-1] = 0;
return dst;
}