c3719f51f2d2459aac55fe438d8889ac43a7e9ef
[reactos.git] / dll / win32 / kernel32 / winnls / string / japanese.c
1 /*
2 * PROJECT: ReactOS system libraries
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Japanese era support
5 * COPYRIGHT: Copyright 2019 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6 */
7 #include <k32.h>
8
9 #define NDEBUG
10 #include <debug.h>
11 #include "japanese.h"
12
13 #define JAPANESE_ERA_MAX 16
14
15 /* #define DONT_USE_REGISTRY */
16
17 static DWORD s_JapaneseEraCount = 0;
18 static JAPANESE_ERA s_JapaneseEraTable[JAPANESE_ERA_MAX]
19 #ifdef DONT_USE_REGISTRY
20 =
21 {
22 {1868, 1, 1, {0x660E, 0x6CBB}, {0x660E, 0}, L"Meiji", L"M"},
23 {1912, 7, 30, {0x5927, 0x6B63}, {0x5927, 0}, L"Taisho", L"T"},
24 {1926, 12, 25, {0x662D, 0x548C}, {0x662D, 0}, L"Showa", L"S"},
25 {1989, 1, 8, {0x5E73, 0x6210}, {0x5E73, 0}, L"Heisei", L"H"},
26 {2019, 5, 1, {0x4EE4, 0x548C}, {0x4EE4, 0}, L"Reiwa", L"R"},
27 }
28 #endif
29 ;
30
31 HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName);
32
33 BOOL NLS_RegEnumValue(HANDLE hKey, UINT ulIndex,
34 LPWSTR szValueName, ULONG valueNameSize,
35 LPWSTR szValueData, ULONG valueDataSize);
36
37 static INT JapaneseEra_Compare(const void *e1, const void *e2)
38 {
39 PCJAPANESE_ERA pEra1 = (PCJAPANESE_ERA)e1;
40 PCJAPANESE_ERA pEra2 = (PCJAPANESE_ERA)e2;
41 if (pEra1->wYear < pEra2->wYear)
42 return -1;
43 if (pEra1->wYear > pEra2->wYear)
44 return 1;
45 if (pEra1->wMonth < pEra2->wMonth)
46 return -1;
47 if (pEra1->wMonth > pEra2->wMonth)
48 return 1;
49 if (pEra1->wDay < pEra2->wDay)
50 return -1;
51 if (pEra1->wDay > pEra2->wDay)
52 return 1;
53 return 0;
54 }
55
56 /*
57 * SEE ALSO:
58 * https://en.wikipedia.org/wiki/Japanese_era_name
59 * https://docs.microsoft.com/en-us/windows/desktop/Intl/era-handling-for-the-japanese-calendar
60 */
61 static PCJAPANESE_ERA JapaneseEra_Load(DWORD *pdwCount)
62 {
63 #ifndef DONT_USE_REGISTRY
64 HANDLE KeyHandle = NULL;
65 DWORD dwIndex;
66 WCHAR szName[128], szValue[128];
67 JAPANESE_ERA *pEntry;
68 LPWSTR pch1, pch2, pch3, pch4;
69 #endif
70
71 ASSERT(pdwCount != NULL);
72
73 /* return cache if any */
74 if (s_JapaneseEraCount != 0)
75 {
76 *pdwCount = s_JapaneseEraCount;
77 return s_JapaneseEraTable;
78 }
79
80 #ifdef DONT_USE_REGISTRY
81 s_JapaneseEraCount = ARRAYSIZE(s_JapaneseEraTable);
82 #else
83 /* init */
84 *pdwCount = 0;
85 RtlZeroMemory(&s_JapaneseEraTable, sizeof(s_JapaneseEraTable));
86
87 /* open registry key */
88 KeyHandle = NLS_RegOpenKey(NULL, L"\\Registry\\Machine\\System\\"
89 L"CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras");
90 if (!KeyHandle)
91 return NULL;
92
93 /* for all values */
94 for (dwIndex = 0; dwIndex < JAPANESE_ERA_MAX; ++dwIndex)
95 {
96 pEntry = &s_JapaneseEraTable[dwIndex];
97
98 /* get name and data */
99 if (!NLS_RegEnumValue(KeyHandle, dwIndex, szName, sizeof(szName),
100 szValue, sizeof(szValue)))
101 {
102 break;
103 }
104
105 /* split fields */
106 pch1 = szName;
107 pch2 = wcschr(pch1, L' ');
108 if (pch2 == NULL)
109 {
110 break;
111 }
112 *pch2++ = UNICODE_NULL;
113
114 pch3 = wcschr(pch2, L' ');
115 if (pch3 == NULL)
116 {
117 break;
118 }
119 *pch3++ = UNICODE_NULL;
120
121 pEntry->wYear = _wtoi(pch1);
122 pEntry->wMonth = _wtoi(pch2);
123 pEntry->wDay = _wtoi(pch3);
124 if (pEntry->wYear == 0 || pEntry->wMonth == 0 || pEntry->wDay == 0)
125 {
126 break;
127 }
128
129 /* split fields */
130 pch1 = szValue;
131 pch2 = wcschr(pch1, L'_');
132 if (pch2 == NULL)
133 {
134 break;
135 }
136 *pch2++ = UNICODE_NULL;
137
138 pch3 = wcschr(pch2, L'_');
139 if (pch3 == NULL)
140 {
141 break;
142 }
143 *pch3++ = UNICODE_NULL;
144
145 pch4 = wcschr(pch3, L'_');
146 if (pch4 == NULL)
147 {
148 break;
149 }
150 *pch4++ = UNICODE_NULL;
151
152 /* store */
153 RtlStringCbCopyW(pEntry->szEraName, sizeof(pEntry->szEraName), pch1);
154 RtlStringCbCopyW(pEntry->szEraAbbrev, sizeof(pEntry->szEraAbbrev), pch2);
155 RtlStringCbCopyW(pEntry->szEnglishEraName, sizeof(pEntry->szEnglishEraName), pch3);
156 RtlStringCbCopyW(pEntry->szEnglishEraAbbrev, sizeof(pEntry->szEnglishEraAbbrev), pch4);
157 }
158
159 /* close key */
160 NtClose(KeyHandle);
161
162 /* sort */
163 qsort(s_JapaneseEraTable, s_JapaneseEraCount, sizeof(JAPANESE_ERA),
164 JapaneseEra_Compare);
165
166 /* make cache */
167 s_JapaneseEraCount = dwIndex;
168 #endif
169
170 *pdwCount = s_JapaneseEraCount;
171
172 return s_JapaneseEraTable;
173 }
174
175 static BOOL JapaneseEra_ToSystemTime(PCJAPANESE_ERA pEra, LPSYSTEMTIME pst)
176 {
177 ASSERT(pEra != NULL);
178 ASSERT(pst != NULL);
179
180 ZeroMemory(pst, sizeof(*pst));
181 pst->wYear = pEra->wYear;
182 pst->wMonth = pEra->wMonth;
183 pst->wDay = pEra->wDay;
184 return TRUE;
185 }
186
187 PCJAPANESE_ERA JapaneseEra_Find(const SYSTEMTIME *pst OPTIONAL)
188 {
189 DWORD dwIndex, dwCount = 0;
190 PCJAPANESE_ERA pTable, pEntry, pPrevEntry = NULL;
191 SYSTEMTIME st1, st2;
192 FILETIME ft1, ft2;
193 LONG nCompare;
194
195 /* pst --> ft1 */
196 if (pst == NULL)
197 {
198 GetLocalTime(&st1);
199 pst = &st1;
200 }
201 SystemTimeToFileTime(pst, &ft1);
202
203 /* load era table */
204 pTable = JapaneseEra_Load(&dwCount);
205 if (pTable == NULL || dwCount == 0 || dwCount > JAPANESE_ERA_MAX)
206 {
207 return NULL;
208 }
209
210 /* for all eras */
211 for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
212 {
213 pEntry = &pTable[dwIndex];
214
215 /* pEntry --> st2 --> ft2 */
216 JapaneseEra_ToSystemTime(pEntry, &st2);
217 SystemTimeToFileTime(&st2, &ft2);
218
219 /* ft1 <=> ft2 */
220 nCompare = CompareFileTime(&ft1, &ft2);
221 if (nCompare == 0)
222 return pEntry;
223 if (nCompare < 0)
224 return pPrevEntry;
225 pPrevEntry = pEntry;
226 }
227
228 return pPrevEntry;
229 }