summaryrefslogtreecommitdiff
path: root/tests/src/Interop/common/xplatform.h
blob: 36c4e56f0268ac30aff4b14d723726c7e231c87e (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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#ifndef __XPLAT_H__
#define __XPLAT_H__

#ifdef _MSC_VER
// Our tests don't care about secure CRT
#define _CRT_SECURE_NO_WARNINGS 1
#endif

// common headers
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

// This macro is used to standardize the wide character string literals between UNIX and Windows.
// Unix L"" is UTF32, and on windows it's UTF16.  Because of built-in assumptions on the size
// of string literals, it's important to match behaviour between Unix and Windows.  Unix will be defined
// as u"" (char16_t)
#ifdef _WIN32
#define W(str)  L##str
#else // !_WIN32
#define W(str)  u##str
#endif //_WIN32


//  include 
#ifdef _WIN32
	#include <windows.h>
	#include <tchar.h>
#else
	#include "types.h"
#endif
#include <wchar.h>


// dllexport
#if defined _WIN32
#define DLL_EXPORT __declspec(dllexport)

#ifndef snprintf
#define snprintf _snprintf
#endif //snprintf

#else //!_Win32
#if __GNUC__ >= 4    
#define DLL_EXPORT __attribute__ ((visibility ("default")))
#else
#define DLL_EXPORT
#endif

#endif //_WIN32

// The default P/Invoke calling convetion is STDCALL on Window, but CDECL on Unix.
#ifdef _WIN32
#define CALLBACK    __stdcall
#define NATIVEAPI   __stdcall
#else // _WIN32
#define CALLBACK
#define NATIVEAPI
#endif // !_WIN32

#ifndef _MSC_VER
#if __i386__
#define __stdcall __attribute__((stdcall))
#define _cdecl __attribute__((cdecl))
#define __cdecl __attribute__((cdecl))
#else
#define __stdcall
#define _cdecl
#define __cdecl
#endif
#endif





// Ensure that both UNICODE and _UNICODE are set.
#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE
#endif
#else
#ifdef _UNICODE
#define UNICODE
#endif
#endif


// redirected functions
#ifdef UNICODE
#define _tcslen	wcslen
#define _tcsncmp wcsncmp
#else
#define _tcslen strlen
#define _tcsncmp strncmp
#endif // UNICODE



// redirected types not-windows only
#ifndef  _WIN32

typedef union tagCY {
	struct {
		unsigned long Lo;
		long          Hi;
	};
	long int64;
} CY, CURRENCY;


class IUnknown
{
public:
  virtual int  QueryInterface(void* riid,void** ppvObject);
  virtual unsigned long  AddRef();
  virtual unsigned long  Release();
};

#define CoTaskMemAlloc(p) malloc(p)
#define CoTaskMemFree(p) free(p)

// function implementation
size_t strncpy_s(char* strDest, size_t numberOfElements, const char *strSource, size_t count)
{
    // NOTE: Need to pass count + 1 since strncpy_s does not count null,
    // while snprintf does. 
	return snprintf(strDest, count + 1, "%s", strSource);
}

size_t strcpy_s(char *dest, size_t n, char const *src)
{
	return snprintf(dest, n, "%s", src);
}

void SysFreeString(char* str)
{
	free(str);
}


char* SysAllocString( const char* str)
{
	size_t nz = strlen(str);
	char *cArr = (char*) malloc(nz);
	memcpy(cArr, str, nz);
	return cArr;
}


size_t wcslen(const WCHAR *str)
{
	int len;
	if (!str) return 0;
	len = 0;
	while ('\0' != *(str + len)) len++;
	return len;
}

WCHAR* SysAllocString(const WCHAR* str)
{
	size_t nz = wcslen(str);
	nz *= 2;
	WCHAR *cArr = (WCHAR*)malloc(nz);
	memcpy(cArr, str, nz);
	return cArr;
}



int wcsncpy_s(LPWSTR strDestination, size_t size1, LPCWSTR strSource, size_t size2)
{
	int cnt;
	// copy sizeInBytes bytes of strSource into strDestination
	if (NULL == strDestination || NULL == strSource) return 1;

	cnt = 0;
	while (cnt < size1 && '\0' != strSource[cnt])
	{
		strDestination[cnt] = strSource[cnt];
		cnt++;
	}
	strDestination[cnt] = '\0';
	return 0;
}

int wcsncpy_s(LPWSTR strDestination, size_t size1, LPCWSTR strSource)
{
	return wcsncpy_s(strDestination, size1, strSource, 0);

}

int wmemcmp(LPWSTR str1, LPWSTR str2,size_t len)
{
	// < 0 str1 less than str2
	// 0  str1 identical to str2
	// > 0 str1 greater than str2

	if (NULL == str1 && NULL != str2) return -1;
	if (NULL != str1 && NULL == str2) return 1;
	if (NULL == str1 && NULL == str2) return 0;

	while (*str1 == *str2 && '\0' != *str1 && '\0' != *str2 && len--!= 0)
	{
		str1++;
		str2++;
	}

	if ('\0' == *str1 && '\0' == *str2) return 0;

	if ('\0' != *str1) return -1;
	if ('\0' != *str2) return 1;

	return (*str1 > *str2) ? 1 : -1;
}


#endif //!_Win32

#endif // __XPLAT_H__