1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 #include "platformdefines.h"
8 LPWSTR HackyConvertToWSTR(char* pszInput)
14 if (NULL == pszInput) return NULL;
19 while('\0' != *pStr) {cchInput++; pStr++;}
20 pwszOutput = new WCHAR[ cchInput + 1];
22 for(size_t i=0; i<=cchInput; i++)
24 pwszOutput[i] = (WCHAR)pszInput[i];
30 LPSTR HackyConvertToSTR(LPWSTR pwszInput)
36 if (NULL == pwszInput) return NULL;
38 cchInput = wcslen(pwszInput);
39 pszOutput = new char[ cchInput + 1];
41 for(size_t i=0; i<=cchInput; i++)
44 pszOutput[i] = (char)pwszInput[i];
50 error_t TP_scpy_s(LPWSTR strDestination, size_t sizeInWords, LPCWSTR strSource)
53 // copy sizeInBytes bytes of strSource into strDestination
55 if (NULL == strDestination || NULL == strSource) return 1;
58 while(cnt < sizeInWords && '\0' != strSource[cnt])
60 strDestination[cnt] = strSource[cnt];
63 strDestination[cnt] = '\0';
68 error_t TP_scat_s(LPWSTR strDestination, size_t sizeInWords, LPCWSTR strSource)
71 // locate the end (ie. '\0') and TP_scpy_s the string
73 if (NULL == strDestination || NULL == strSource) return 1;
75 strEnd = strDestination;
76 while('\0' != *strEnd) strEnd++;
78 return TP_scpy_s(strEnd, sizeInWords - ((strEnd - strDestination) / sizeof(WCHAR)), strSource);
81 int TP_slen(LPWSTR str)
85 if (NULL == str) return 0;
88 while('\0' != *(str+len)) len++;
93 int TP_scmp_s(LPSTR str1, LPSTR str2)
95 // < 0 str1 less than str2
96 // 0 str1 identical to str2
97 // > 0 str1 greater than str2
99 if (NULL == str1 && NULL != str2) return -1;
100 if (NULL != str1 && NULL == str2) return 1;
101 if (NULL == str1 && NULL == str2) return 0;
103 while (*str1 == *str2 && '\0' != *str1 && '\0' != *str2)
109 if ('\0' == *str1 && '\0' == *str2) return 0;
111 if ('\0' != *str1) return -1;
112 if ('\0' != *str2) return 1;
114 return (*str1 > *str2) ? 1 : -1;
117 int TP_wcmp_s(LPWSTR str1, LPWSTR str2)
119 // < 0 str1 less than str2
120 // 0 str1 identical to str2
121 // > 0 str1 greater than str2
123 if (NULL == str1 && NULL != str2) return -1;
124 if (NULL != str1 && NULL == str2) return 1;
125 if (NULL == str1 && NULL == str2) return 0;
127 while (*str1 == *str2 && '\0' != *str1 && '\0' != *str2)
133 if ('\0' == *str1 && '\0' == *str2) return 0;
135 if ('\0' != *str1) return -1;
136 if ('\0' != *str2) return 1;
138 return (*str1 > *str2) ? 1 : -1;
141 error_t TP_getenv_s(size_t* pReturnValue, LPWSTR buffer, size_t sizeInWords, LPCWSTR varname)
143 if (NULL == pReturnValue || NULL == varname) return 1;
149 if( 0 != _wgetenv_s(&returnValue, buf, 100, varname) || returnValue<=0 )
153 TP_scpy_s(buffer, sizeInWords, (LPWSTR)buf);
156 pRet = getenv( HackyConvertToSTR((LPWSTR)varname) );
157 if (NULL == pRet) return 2;
158 TP_scpy_s(buffer, sizeInWords, HackyConvertToWSTR(pRet));
163 error_t TP_putenv_s(LPTSTR name, LPTSTR value)
165 if (NULL == name || NULL == value) return 1;
168 if( 0 != _putenv_s(name, value))
174 char *assignment = (char*) malloc(sizeof(char) * (strlen(name) + strlen(value) + 1));
175 sprintf(assignment, "%s=%s", name, value);
177 if (0 != putenv(assignment))
184 void TP_ZeroMemory(LPVOID buffer, size_t sizeInBytes)
188 // clear out the memory with 0's
189 if (NULL == buffer) return;
191 bBuf = (BYTE*)buffer;
192 for(int i=0; i<sizeInBytes; i++)
198 error_t TP_itow_s(int num, LPWSTR buffer, size_t sizeInCharacters, int radix)
203 // only support radix == 10 and only positive numbers
204 if (10 != radix) return 1;
205 if (0 > num) return 2;
206 if (NULL == buffer) return 3;
207 if (2 > sizeInCharacters) return 4;
209 // take care of the trivial case
216 // get length of final string (dumb implementation)
225 if (len >= sizeInCharacters) return 5;
227 // convert num into a string (backwards)
230 while(0 < num && 0 <= len)
232 buffer[len] = (WCHAR)((num % 10) + '0');
240 LPWSTR TP_sstr(LPWSTR str, LPWSTR searchStr)
244 LPWSTR searchCurrent;
246 if (NULL == str || NULL == searchStr) return NULL;
248 // return a pointer to where searchStr
252 searchCurrent = searchStr;
253 while('\0' != *current)
255 if (NULL != start && '\0' == *searchCurrent)
260 if (*current == *searchCurrent)
263 if (NULL == start) start = current;
267 searchCurrent = searchStr;
276 DWORD TP_GetFullPathName(LPWSTR fileName, DWORD nBufferLength, LPWSTR lpBuffer)
279 return GetFullPathNameW(fileName, nBufferLength, lpBuffer, NULL);
281 char nativeFullPath[MAX_PATH];
282 realpath(HackyConvertToSTR(fileName), nativeFullPath);
283 LPWSTR fullPathForCLR = HackyConvertToWSTR(nativeFullPath);
284 wcscpy_s(lpBuffer, MAX_PATH, fullPathForCLR);
285 return wcslen(lpBuffer);
288 DWORD TP_CreateThread(THREAD_ID* tThread, LPTHREAD_START_ROUTINE worker, LPVOID lpParameter)
292 *tThread = CreateThread(
307 // This is a major kludge...64 bit posix threads just can't be cast into a DWORD and there just isn't
308 // a great way to get what we're using for the ID. The fact that we're casting this at all is kind of
309 // silly since we're returing the actual thread handle and everything being done to manipulate the thread
310 // is done with that. Anyhow, the only thing done with the dword returned from this method is a printf
311 // which is good since this DWORD really shouldn't be reliably used. Just in case it is though, return
312 // a value that can be traced back to here.
315 return (DWORD)*tThread;
320 void TP_JoinThread(THREAD_ID tThread)
323 WaitForSingleObject(tThread, INFINITE);
325 pthread_join(tThread, NULL);
333 __asm__ __volatile__("trap");
335 __asm__ __volatile__("int $3");