1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2010 - DIGITEO - Antoine ELIAS
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 
16 #ifndef __OS_STRING_H__
17 #define __OS_STRING_H__
18 #include <wchar.h>
19 #include <string.h>
20 // Windows
21 #ifdef _MSC_VER
22 #define os_wcsdup       _wcsdup
23 #define os_strdup       _strdup
24 #define os_swprintf     swprintf_s
25 #define os_sprintf      sprintf_s
26 #define os_wcsicmp      _wcsicmp
27 #define stricmp         _stricmp
28 #define strnicmp        _strnicmp
29 #define wcsicmp         _wcsicmp
30 #define wcsnicmp        _wcsnicmp
31 #define os_strcpy       strcpy_s
32 #define os_splitpath    _splitpath_s
33 #define os_wsplitpath   _wsplitpath_s
34 #define os_makepath     _makepath_s
35 
36 #else
37 
38 #ifdef __APPLE__
39 wchar_t *_sciwcsdup(const wchar_t *_pwcsSource);
40 #define os_wcsdup       _sciwcsdup
41 #define os_strdup       strdup
42 #define os_swprintf     swprintf
43 #define os_sprintf      sprintf
44 
45 #include <stdlib.h>
46 #include <wchar.h>
47 #include <wctype.h>
48 
macOSwcscasecmp(const wchar_t * _pwcsS1,const wchar_t * _pwcsS2)49 static inline int macOSwcscasecmp(const wchar_t *_pwcsS1, const wchar_t *_pwcsS2)
50 {
51     int iResult = 0;
52     int i = 0;
53 
54     wchar_t *pwcsLowerS1 = (wchar_t *)malloc(sizeof(wchar_t) * wcslen(_pwcsS1) + 1);
55     wchar_t *pwcsLowerS2 = (wchar_t *)malloc(sizeof(wchar_t) * wcslen(_pwcsS2) + 1);;
56 
57     wcscpy(pwcsLowerS1, _pwcsS1);
58     wcscpy(pwcsLowerS2, _pwcsS2);
59 
60     // Lower S1
61     for (i = 0; i < wcslen(_pwcsS1); ++i)
62     {
63         pwcsLowerS1[i] = towlower(_pwcsS1[i]);
64     }
65 
66     // Lower S2
67     for (i = 0; i < wcslen(_pwcsS2); ++i)
68     {
69         pwcsLowerS2[i] = towlower(_pwcsS2[i]);
70     }
71 
72     iResult = wcscmp(pwcsLowerS1, pwcsLowerS2);
73     free(pwcsLowerS1);
74     free(pwcsLowerS2);
75     return iResult;
76 }
77 
78 #define os_wcsicmp      macOSwcscasecmp
79 #define stricmp         strcasecmp
80 #define strnicmp        strncasecmp
81 #define wcsicmp         wcscasecmp
82 #define wcsnicmp        wcsncasecmp
83 #define os_strcpy       strcpy
84 #define os_splitpath    _splitpath
85 #define os_wsplitpath   _wsplitpath
86 #define os_makepath     _makepath
87 
88 #else //linux or MinGw
89 
90 #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
91 /* Fixes crash issues with wcsdup when: */
92 /* - Scilab is compiled against old GLIBC (<2.10) */
93 /* - AND executed against recent GLIBC (>=2.10) */
94 /* See man wcsdup */
95 /* Using #define _GNU_SOURCE is not enough in Scilab because */
96 /* <wchar.h> can be included before "os_string.h" */
97 wchar_t *_sciwcsdup(const wchar_t *_pwcsSource);
98 #define os_wcsdup       _sciwcsdup
99 #else
100 #define os_wcsdup       wcsdup
101 #endif
102 
103 #define os_strdup       strdup
104 #define os_swprintf     swprintf
105 #define os_sprintf      sprintf
106 #define os_wcsicmp      wcscasecmp
107 #define stricmp         strcasecmp
108 #define strnicmp        strncasecmp
109 #define wcsicmp         wcscasecmp
110 #define wcsnicmp        wcsncasecmp
111 #define os_strcpy       strcpy
112 #define os_splitpath    _splitpath
113 #define os_wsplitpath   _wsplitpath
114 #define os_makepath     _makepath
115 
116 #endif //if __APPLE__
117 #endif //if _MSC_VER
118 
119 // MacOS X
120 
121 #endif /* !__OS_STRING_H__ */
122