modified dll/win32/kernel32/misc/lang.c
[reactos.git] / reactos / 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 #define WINVER 0x0600
45
46 #include <windows.h>
47
48 extern "C"
49 {
50 #include <idndl.h>
51 }
52
53 #include "scripts.h"
54 #include "data/idldata.cpp"
55
56 int
57 WINAPI
58 DownlevelGetLocaleScripts
59 (
60 LPCWSTR lpLocaleName,
61 LPWSTR lpScripts,
62 int cchScripts
63 )
64 {
65 void * lpFoundLocale = bsearch
66 (
67 lpLocaleName,
68 IDNDL_Locales,
69 //~ ARRAYSIZE(IDNDL_Locales),
70 sizeof(IDNDL_Locales) / sizeof(IDNDL_Locales[0]),
71 sizeof(IDNDL_Locales[0]),
72 (int (*)(const void *, const void *))_stricmp
73 );
74
75 if(lpFoundLocale == NULL)
76 {
77 SetLastError(ERROR_INVALID_PARAMETER);
78 return 0;
79 }
80
81 const IDNDL_ScriptSet * pScriptSet = IDNDL_ScriptSets[static_cast<const wchar_t **>(lpFoundLocale) - IDNDL_Locales];
82
83 if(pScriptSet->length > cchScripts)
84 SetLastError(ERROR_INSUFFICIENT_BUFFER);
85 else
86 memcpy(lpScripts, pScriptSet->scripts, pScriptSet->length * sizeof(WCHAR));
87
88 return pScriptSet->length;
89 }
90
91 static int IDNDL_CompareCharRange(const void * x, const void * y)
92 {
93 const IDNDL_CharRangeScript * pRangeX = static_cast<const IDNDL_CharRangeScript *>(x);
94 const IDNDL_CharRangeScript * pRangeY = static_cast<const IDNDL_CharRangeScript *>(y);
95
96 assert(pRangeX->lbound <= pRangeX->ubound);
97 assert(pRangeY->lbound <= pRangeY->ubound);
98
99 int cmp;
100
101 cmp = pRangeX->ubound - pRangeY->lbound;
102
103 if(cmp < 0)
104 return cmp;
105
106 cmp = pRangeX->lbound - pRangeY->ubound;
107
108 if(cmp > 0)
109 return cmp;
110
111 assert((pRangeX->lbound >= pRangeY->lbound && pRangeX->ubound <= pRangeY->ubound) || (pRangeY->lbound >= pRangeX->lbound && pRangeY->ubound <= pRangeX->ubound));
112 return 0;
113 }
114
115 extern "C" bool SCRIPTS_GetCharScriptCode(UChar32 c, int32_t * code)
116 {
117 assert(c >= UCHAR_MIN_VALUE && c <= UCHAR_MAX_VALUE);
118
119 IDNDL_CharRangeScript character;
120 character.lbound = c;
121 character.ubound = c;
122
123 void * pRange = bsearch
124 (
125 &character,
126 IDNDL_CharRangeScripts,
127 //~ ARRAYSIZE(IDNDL_CharRangeScripts),
128 sizeof(IDNDL_CharRangeScripts) / sizeof(IDNDL_CharRangeScripts[0]),
129 sizeof(IDNDL_CharRangeScripts[0]),
130 &IDNDL_CompareCharRange
131 );
132
133 if(pRange == NULL)
134 *code = USCRIPT_UNKNOWN;
135 else
136 *code = static_cast<IDNDL_CharRangeScript *>(pRange)->code;
137
138 return true;
139 }
140
141 extern "C" bool SCRIPTS_GetScriptCode(const SCRIPTS_Script * pScript, int32_t * code)
142 {
143 void * ppScript = bsearch
144 (
145 pScript,
146 IDNDL_ScriptNames,
147 //~ ARRAYSIZE(IDNDL_ScriptNames),
148 sizeof(IDNDL_ScriptNames) / sizeof(IDNDL_ScriptNames[0]),
149 sizeof(IDNDL_ScriptNames[0]),
150 (int (*)(const void *, const void *))_stricmp
151 );
152
153 bool retval;
154
155 retval = !!ppScript;
156
157 if(!retval)
158 {
159 SetLastError(ERROR_INVALID_PARAMETER);
160 return retval;
161 }
162
163 *code = static_cast<int32_t>(static_cast<const wchar_t **>(ppScript) - IDNDL_ScriptNames);
164 return retval;
165 }
166
167 extern "C" void SCRIPTS_GetScriptName(int32_t code, SCRIPTS_Script * pScript)
168 {
169 //~ assert(code >= 0 && static_cast<uint32_t>(code) < ARRAYSIZE(IDNDL_ScriptNames));
170 assert(code >= 0 && static_cast<uint32_t>(code) < (sizeof(IDNDL_ScriptNames) / sizeof(IDNDL_ScriptNames[0])));
171 memcpy(pScript->ScriptName, IDNDL_ScriptNames[code], sizeof(pScript->ScriptName));
172 }
173
174 // EOF