yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie HermaszewskaCorrectly distinguish between windows and MSVC (#5851)48ac6f25f

master
2.5 KiB109 linesraw
1#ifndef _WIN32
2#ifndef SLANG_CORE_SECURE_CRT_H
3#define SLANG_CORE_SECURE_CRT_H
4#include <assert.h>
5#include <stdarg.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <strings.h>
10#include <wchar.h>
11
12inline void memcpy_s(void* dest, [[maybe_unused]] size_t destSize, const void* src, size_t count)
13{
14    assert(destSize >= count);
15    memcpy(dest, src, count);
16}
17
18#define _TRUNCATE ((size_t)-1)
19#define _stricmp strcasecmp
20
21inline void fopen_s(FILE** f, const char* fileName, const char* mode)
22{
23    *f = fopen(fileName, mode);
24}
25
26inline size_t fread_s(
27    void* buffer,
28    [[maybe_unused]] size_t bufferSize,
29    size_t elementSize,
30    size_t count,
31    FILE* stream)
32{
33    assert(bufferSize >= elementSize * count);
34    return fread(buffer, elementSize, count, stream);
35}
36
37inline size_t wcsnlen_s(const wchar_t* str, size_t /*numberofElements*/)
38{
39    return wcslen(str);
40}
41
42inline size_t strnlen_s(const char* str, size_t numberOfElements)
43{
44#if defined(__CYGWIN__)
45    const char* cur = str;
46    if (str)
47    {
48        const char* const end = str + numberOfElements;
49        while (*cur && cur < end)
50            cur++;
51    }
52    return size_t(cur - str);
53#else
54    return strnlen(str, numberOfElements);
55#endif
56}
57
58__attribute__((format(printf, 3, 4))) inline int sprintf_s(
59    char* buffer,
60    size_t sizeOfBuffer,
61    const char* format,
62    ...)
63{
64    va_list argptr;
65    va_start(argptr, format);
66    int rs = vsnprintf(buffer, sizeOfBuffer, format, argptr);
67    va_end(argptr);
68    return rs;
69}
70
71// A patch was submitted to GCC wchar_t support in 2001, so I'm sure we can
72// enable this any day now...
73// __attribute__((format(wprintf, 3, 4)))
74inline int swprintf_s(wchar_t* buffer, size_t sizeOfBuffer, const wchar_t* format, ...)
75{
76    va_list argptr;
77    va_start(argptr, format);
78    int rs = vswprintf(buffer, sizeOfBuffer, format, argptr);
79    va_end(argptr);
80    return rs;
81}
82
83inline void wcscpy_s(wchar_t* strDestination, size_t /*numberOfElements*/, const wchar_t* strSource)
84{
85    wcscpy(strDestination, strSource);
86}
87inline void strcpy_s(char* strDestination, size_t /*numberOfElements*/, const char* strSource)
88{
89    strcpy(strDestination, strSource);
90}
91
92inline void wcsncpy_s(
93    wchar_t* strDestination,
94    size_t /*numberOfElements*/,
95    const wchar_t* strSource,
96    size_t count)
97{
98    wcsncpy(strDestination, strSource, count);
99}
100inline void strncpy_s(
101    char* strDestination,
102    size_t /*numberOfElements*/,
103    const char* strSource,
104    size_t count)
105{
106    strncpy(strDestination, strSource, count);
107}
108#endif
109#endif