Sync with trunk (r48414)
[reactos.git] / dll / nls / idndl_redist / idndl.cpp
1 /*
2 * Copyright (c) 2008, KJK::Hyperion
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the ReactOS Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <unicode/uchar.h>
37 #include <unicode/uclean.h>
38 #include <unicode/umachine.h>
39 #include <unicode/uscript.h>
40
41 #define WIN32_LEAN_AND_MEAN
42 #define STRICT
43
44 #include <windows.h>
45
46 extern "C"
47 {
48 #include <idndl.h>
49 }
50
51 #include "scripts.h"
52 #include "data/idldata.cpp"
53
54 int
55 WINAPI
56 DownlevelGetLocaleScripts
57 (
58 LPCWSTR lpLocaleName,
59 LPWSTR lpScripts,
60 int cchScripts
61 )
62 {
63 void * lpFoundLocale = bsearch
64 (
65 lpLocaleName,
66 IDNDL_Locales,
67 //~ ARRAYSIZE(IDNDL_Locales),
68 sizeof(IDNDL_Locales) / sizeof(IDNDL_Locales[0]),
69 sizeof(IDNDL_Locales[0]),
70 (int (*)(const void *, const void *))_stricmp
71 );
72
73 if(lpFoundLocale == NULL)
74 {
75 SetLastError(ERROR_INVALID_PARAMETER);
76 return 0;
77 }
78
79 const IDNDL_ScriptSet * pScriptSet = IDNDL_ScriptSets[static_cast<const wchar_t **>(lpFoundLocale) - IDNDL_Locales];
80
81 if(pScriptSet->length > cchScripts)
82 SetLastError(ERROR_INSUFFICIENT_BUFFER);
83 else
84 memcpy(lpScripts, pScriptSet->scripts, pScriptSet->length * sizeof(WCHAR));
85
86 return pScriptSet->length;
87 }
88
89 static int IDNDL_CompareCharRange(const void * x, const void * y)
90 {
91 const IDNDL_CharRangeScript * pRangeX = static_cast<const IDNDL_CharRangeScript *>(x);
92 const IDNDL_CharRangeScript * pRangeY = static_cast<const IDNDL_CharRangeScript *>(y);
93
94 assert(pRangeX->lbound <= pRangeX->ubound);
95 assert(pRangeY->lbound <= pRangeY->ubound);
96
97 int cmp;
98
99 cmp = pRangeX->ubound - pRangeY->lbound;
100
101 if(cmp < 0)
102 return cmp;
103
104 cmp = pRangeX->lbound - pRangeY->ubound;
105
106 if(cmp > 0)
107 return cmp;
108
109 assert((pRangeX->lbound >= pRangeY->lbound && pRangeX->ubound <= pRangeY->ubound) || (pRangeY->lbound >= pRangeX->lbound && pRangeY->ubound <= pRangeX->ubound));
110 return 0;
111 }
112
113 extern "C" bool SCRIPTS_GetCharScriptCode(UChar32 c, int32_t * code)
114 {
115 assert(c >= UCHAR_MIN_VALUE && c <= UCHAR_MAX_VALUE);
116
117 IDNDL_CharRangeScript character;
118 character.lbound = c;
119 character.ubound = c;
120
121 void * pRange = bsearch
122 (
123 &character,
124 IDNDL_CharRangeScripts,
125 //~ ARRAYSIZE(IDNDL_CharRangeScripts),
126 sizeof(IDNDL_CharRangeScripts) / sizeof(IDNDL_CharRangeScripts[0]),
127 sizeof(IDNDL_CharRangeScripts[0]),
128 &IDNDL_CompareCharRange
129 );
130
131 if(pRange == NULL)
132 *code = USCRIPT_UNKNOWN;
133 else
134 *code = static_cast<IDNDL_CharRangeScript *>(pRange)->code;
135
136 return true;
137 }
138
139 extern "C" bool SCRIPTS_GetScriptCode(const SCRIPTS_Script * pScript, int32_t * code)
140 {
141 void * ppScript = bsearch
142 (
143 pScript,
144 IDNDL_ScriptNames,
145 //~ ARRAYSIZE(IDNDL_ScriptNames),
146 sizeof(IDNDL_ScriptNames) / sizeof(IDNDL_ScriptNames[0]),
147 sizeof(IDNDL_ScriptNames[0]),
148 (int (*)(const void *, const void *))_stricmp
149 );
150
151 bool retval;
152
153 retval = !!ppScript;
154
155 if(!retval)
156 {
157 SetLastError(ERROR_INVALID_PARAMETER);
158 return retval;
159 }
160
161 *code = static_cast<int32_t>(static_cast<const wchar_t **>(ppScript) - IDNDL_ScriptNames);
162 return retval;
163 }
164
165 extern "C" void SCRIPTS_GetScriptName(int32_t code, SCRIPTS_Script * pScript)
166 {
167 //~ assert(code >= 0 && static_cast<uint32_t>(code) < ARRAYSIZE(IDNDL_ScriptNames));
168 assert(code >= 0 && static_cast<uint32_t>(code) < (sizeof(IDNDL_ScriptNames) / sizeof(IDNDL_ScriptNames[0])));
169 memcpy(pScript->ScriptName, IDNDL_ScriptNames[code], sizeof(pScript->ScriptName));
170 }
171
172 // EOF