root/lang/cplusplus/i3/src/mol/include/mol/MscCrt.h @ 23022

Revision 23022, 5.6 kB (checked in by saturday06, 5 years ago)

foo

Line 
1#pragma once
2
3#include <cerrno>
4#include <cstdlib>
5#include <cstring>
6
7namespace mol
8{
9namespace msccrt
10{
11using namespace std;
12
13template <typename Char>
14inline static size_t basic_strnlen_s(const Char* str, size_t numberOfElements)
15{
16    if (str == NULL) {
17        return 0;
18    }
19    if (numberOfElements == 0) {
20        return 0;
21    }
22    size_t size = 0;
23    for (; size != numberOfElements && str[size]; size++) {}
24    return size;
25}
26
27template <typename Char>
28inline static int basic_strcpy_s(Char* strDestination,
29                                 size_t numberOfElements,
30                                 const Char* strSource)
31{
32    if (strDestination == 0)
33    {
34        return EINVAL;
35    }
36    if (strSource == 0)
37    {
38        return EINVAL;
39    }
40    if (numberOfElements == 0)
41    {
42        return ERANGE;
43    }
44    size_t strSourceElements = basic_strnlen_s<Char>(strSource, numberOfElements);
45    if (strSourceElements == numberOfElements)
46    {
47        return ERANGE;
48    }
49    memcpy((void*)strDestination,
50           (const void*)strSource,
51           sizeof(Char) * (strSourceElements + 1));
52    return 0;
53}
54
55template <typename Char>
56inline static int basic_strcat_s(Char* strDestination,
57                                 size_t numberOfElements,
58                                 const Char* strSource)
59{
60    if (strDestination == 0)
61    {
62        return EINVAL;
63    }
64    if (strSource == 0)
65    {
66        return EINVAL;
67    }
68    if (numberOfElements == 0)
69    {
70        return ERANGE;
71    }
72    size_t strDestinationElements = basic_strnlen_s<Char>(strDestination,
73                                    numberOfElements);
74    if (strDestinationElements == numberOfElements)
75    {
76        return EINVAL;
77    }
78    size_t strDestinationElementsLeft =
79        numberOfElements - strDestinationElements;
80    size_t strSourceElements =
81        basic_strnlen_s<Char>(strSource, strDestinationElementsLeft);
82    if (strSourceElements == strDestinationElementsLeft)
83    {
84        return ERANGE;
85    }
86    memcpy(strDestination + strDestinationElements,
87           strSource,
88           sizeof(Char) * (strSourceElements + 1));
89    return 0;
90}
91
92template <typename Char, size_t size>
93inline static int basic_strcpy_s(Char (&strDestination)[size],
94                                 const Char* strSource)
95{
96    return basic_strcpy_s(strDestination, size, strSource);
97}
98
99template <typename Char, size_t size>
100inline static int basic_strcat_s(Char (&strDestination)[size],
101                                 const Char* strSource)
102{
103    return basic_strcat_s(strDestination, size, strSource);
104}
105
106}
107}
108
109
110using mol::msccrt::basic_strnlen_s;
111using mol::msccrt::basic_strcpy_s;
112using mol::msccrt::basic_strcat_s;
113
114#if !defined(_MSC_VER)
115
116#define strnlen_s basic_strnlen_s<char>
117#define strcpy_s basic_strcpy_s<char>
118#define strcat_s basic_strcat_s<char>
119
120#define wcsnlen_s basic_strnlen_s<wchar_t>
121#define wcscpy_s basic_strcpy_s<wchar_t>
122#define wcscat_s basic_strcat_s<wchar_t>
123
124#define _tcsnlen basic_strnlen_s<TCHAR>
125#define _tcscpy_s basic_strcpy_s<TCHAR>
126#define _tcscat_s basic_strcat_s<TCHAR>
127
128#include <time.h>
129#include <string.h>
130#include <errno.h>
131
132#ifndef _countof
133#define _countof(array) (sizeof(array)/sizeof(array[0]))
134#endif
135
136namespace mol
137{
138namespace msccrt
139{
140
141
142#ifndef _T
143#define _T TEXT
144#endif
145
146#if defined(UNICODE) || defined(_UNICODE)
147
148#ifndef TEXT
149#define TEXT(x) L##x
150#endif
151
152#define _tprintf wprintf
153#define _stprintf_s swprintf_s
154
155#else
156
157#ifndef TEXT
158#define TEXT(x) x
159#endif
160
161#define _tprintf printf
162#define _stprintf_s sprintf_s
163
164#endif
165
166// XXX ???
167typedef int errno_t;
168
169#ifndef __GNUC__
170
171template <size_t size>
172inline static int swprintf_s(wchar_t (&buffer)[size],
173                             const wchar_t *format, ...)
174{
175    va_list ap;
176    va_start(ap, format);
177    int result = vswprintf(buffer,size,format,ap);
178    va_end(ap);
179    return result;
180}
181
182
183template <size_t size>
184inline static int sprintf_s(char (&buffer)[size],
185                            const char *format, ...)
186{
187    va_list ap;
188    va_start(ap, format);
189    int result = snprintf(buffer,size,format,ap);
190    va_end(ap);
191    return result;
192}
193
194#endif
195
196inline static errno_t localtime_s(struct tm* tm_, const time_t *time_)
197{
198    tm* m = localtime(time_); // XXX localtime is not thread safe !
199    *tm_ = *m;
200    return !EINVAL;
201}
202
203#ifdef __CYGWIN__
204#define wcsdup _wcsdup
205inline static wchar_t* _wcsdup(const wchar_t *str)
206{
207    size_t len = 0;
208    for (; len<str[len]; len++);
209    if (len == 0)
210    {
211        return NULL;
212    }
213    size_t memory_size = sizeof(wchar_t)*(len+1);
214    wchar_t* d = (wchar_t*)malloc(memory_size);
215    if (!d)
216    {
217        return NULL;
218    }
219    memcpy(d, str, memory_size);
220    return d;
221}
222#endif
223
224#if (defined(WIN32) || defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE)) && !defined(__CYGWIN__)
225#include <windows.h>
226#include <tchar.h>
227
228template <size_t drivesize, size_t dirsize, size_t fnamesize, size_t extsize>
229inline static errno_t _tsplitpath_s(
230    const TCHAR *path,
231    TCHAR (&drive)[drivesize],
232    TCHAR (&dir)[dirsize],
233    TCHAR (&fname)[fnamesize],
234    TCHAR (&ext)[extsize])
235{
236    _tsplitpath(path, drive, dir, fname, ext);
237    return !EINVAL;
238}
239
240template <size_t size>
241inline static errno_t _tmakepath_s(
242    TCHAR (&path)[size],
243    const TCHAR *drive,
244    const TCHAR *dir,
245    const TCHAR *fname,
246    const TCHAR *ext)
247{
248    _tmakepath(path, drive, dir, fname, ext);
249    return !EINVAL;
250}
251
252#endif
253
254}
255}
256
257#endif
Note: See TracBrowser for help on using the browser.