Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "filter/msfilter/countryid.hxx"
21 :
22 : #include <algorithm>
23 : #include <sal/macros.h>
24 :
25 :
26 :
27 : namespace msfilter {
28 :
29 : // Mapping table ==============================================================
30 :
31 : namespace {
32 :
33 :
34 :
35 : /** Table entry for Windows country ID <-> language type conversion.
36 :
37 : The first member is the Windows country ID, as defined in the header.
38 :
39 : The second member contains the corresponding language type for each country
40 : ID. This must be a full language, not only the primary language type.
41 :
42 : The last bool flag defines, if the sub language type should be evaluated to
43 : find the country ID from a language. If not set, all languages map to the
44 : country which contain the given primary language type.
45 :
46 : Example: The language entry (COUNTRY_USA,LANGUAGE_ENGLISH_US,false) maps
47 : the country ID for USA to the language LANGUAGE_ENGLISH_US. The clear sub
48 : language flag causes all english languages LANGUAGE_ENGLISH_*** to map to
49 : this country ID by default. To map the special case LANGUAGE_ENGLISH_EIRE
50 : to the country ID COUNTRY_IRELAND, the sub language flag must be set in the
51 : respective table entry, here (COUNTRY_IRELAND,LANGUAGE_ENGLISH_EIRE,true).
52 : */
53 : struct CountryEntry
54 : {
55 : CountryId meCountry; /// Windows country ID.
56 : LanguageType meLanguage; /// Corresponding language type.
57 : bool mbUseSubLang; /// false = Primary only, true = Primary and sub language.
58 : };
59 :
60 :
61 :
62 : /** Table for Windows country ID <-> language type conversion.
63 :
64 : To map the same language to different country IDs, some of the entries
65 : should contain a set sub language flag (see description of CountryEntry).
66 : All table entries with a set flag take priority over the entry with the
67 : same primary language, but cleared sub language flag, regardless of the
68 : position in the table.
69 :
70 : To map different languages to the same country ID, several entries with the
71 : same country ID may be inserted. In this case the conversion to a language
72 : is done with the first found entry (starting from top) containing the given
73 : country ID.
74 :
75 : For now all entries are sorted by country ID, but this is not required.
76 : */
77 : static const CountryEntry pTable[] =
78 : {
79 : { COUNTRY_USA, LANGUAGE_ENGLISH_US, false },
80 : { COUNTRY_DOMINICAN_REPUBLIC, LANGUAGE_SPANISH_DOMINICAN_REPUBLIC, true },
81 : { COUNTRY_JAMAICA, LANGUAGE_ENGLISH_JAMAICA, true },
82 : { COUNTRY_PUERTO_RICO, LANGUAGE_SPANISH_PUERTO_RICO, true },
83 : { COUNTRY_TRINIDAD_Y_TOBAGO, LANGUAGE_ENGLISH_TRINIDAD, true },
84 : { COUNTRY_CANADA, LANGUAGE_ENGLISH_CAN, true },
85 : { COUNTRY_CANADA, LANGUAGE_FRENCH_CANADIAN, true },
86 : { COUNTRY_RUSSIA, LANGUAGE_RUSSIAN, false },
87 : { COUNTRY_KAZAKHSTAN, LANGUAGE_KAZAKH, false },
88 : { COUNTRY_TATARSTAN, LANGUAGE_TATAR, false },
89 : { COUNTRY_EGYPT, LANGUAGE_ARABIC_EGYPT, true },
90 : { COUNTRY_SOUTH_AFRICA, LANGUAGE_AFRIKAANS, false },
91 : { COUNTRY_SOUTH_AFRICA, LANGUAGE_ENGLISH_SAFRICA, true },
92 : { COUNTRY_SOUTH_AFRICA, LANGUAGE_TSONGA, false },
93 : { COUNTRY_SOUTH_AFRICA, LANGUAGE_VENDA, false },
94 : { COUNTRY_SOUTH_AFRICA, LANGUAGE_XHOSA, false },
95 : { COUNTRY_SOUTH_AFRICA, LANGUAGE_ZULU, false },
96 : { COUNTRY_GREECE, LANGUAGE_GREEK, false },
97 : { COUNTRY_NETHERLANDS, LANGUAGE_DUTCH, false },
98 : { COUNTRY_NETHERLANDS, LANGUAGE_FRISIAN_NETHERLANDS, false },
99 : { COUNTRY_BELGIUM, LANGUAGE_DUTCH_BELGIAN, true },
100 : { COUNTRY_BELGIUM, LANGUAGE_FRENCH_BELGIAN, true },
101 : { COUNTRY_FRANCE, LANGUAGE_FRENCH, false },
102 : { COUNTRY_SPAIN, LANGUAGE_SPANISH_MODERN, false },
103 : { COUNTRY_SPAIN, LANGUAGE_SPANISH_DATED, false },
104 : { COUNTRY_SPAIN, LANGUAGE_CATALAN, false },
105 : { COUNTRY_SPAIN, LANGUAGE_BASQUE, false },
106 : { COUNTRY_SPAIN, LANGUAGE_GALICIAN, false },
107 : { COUNTRY_HUNGARY, LANGUAGE_HUNGARIAN, false },
108 : { COUNTRY_ITALY, LANGUAGE_ITALIAN, false },
109 : { COUNTRY_ROMANIA, LANGUAGE_ROMANIAN, false },
110 : { COUNTRY_SWITZERLAND, LANGUAGE_GERMAN_SWISS, true },
111 : { COUNTRY_SWITZERLAND, LANGUAGE_FRENCH_SWISS, true },
112 : { COUNTRY_SWITZERLAND, LANGUAGE_ITALIAN_SWISS, true },
113 : { COUNTRY_SWITZERLAND, LANGUAGE_RHAETO_ROMAN, false },
114 : { COUNTRY_AUSTRIA, LANGUAGE_GERMAN_AUSTRIAN, true },
115 : { COUNTRY_UNITED_KINGDOM, LANGUAGE_ENGLISH_UK, true },
116 : { COUNTRY_UNITED_KINGDOM, LANGUAGE_GAELIC_SCOTLAND, true },
117 : { COUNTRY_UNITED_KINGDOM, LANGUAGE_WELSH, false },
118 : { COUNTRY_DENMARK, LANGUAGE_DANISH, false },
119 : { COUNTRY_SWEDEN, LANGUAGE_SWEDISH, false },
120 : { COUNTRY_SWEDEN, LANGUAGE_SAMI_LAPPISH, false },
121 : { COUNTRY_NORWAY, LANGUAGE_NORWEGIAN_BOKMAL, false },
122 : { COUNTRY_POLAND, LANGUAGE_POLISH, false },
123 : { COUNTRY_GERMANY, LANGUAGE_GERMAN, false },
124 : { COUNTRY_GERMANY, LANGUAGE_SORBIAN, false },
125 : { COUNTRY_PERU, LANGUAGE_SPANISH_PERU, true },
126 : { COUNTRY_MEXICO, LANGUAGE_SPANISH_MEXICAN, true },
127 : { COUNTRY_ARGENTINIA, LANGUAGE_SPANISH_ARGENTINA, true },
128 : { COUNTRY_BRAZIL, LANGUAGE_PORTUGUESE_BRAZILIAN, true },
129 : { COUNTRY_CHILE, LANGUAGE_SPANISH_CHILE, true },
130 : { COUNTRY_COLOMBIA, LANGUAGE_SPANISH_COLOMBIA, true },
131 : { COUNTRY_VENEZUELA, LANGUAGE_SPANISH_VENEZUELA, true },
132 : { COUNTRY_MALAYSIA, LANGUAGE_MALAY_MALAYSIA, false },
133 : { COUNTRY_AUSTRALIA, LANGUAGE_ENGLISH_AUS, true },
134 : { COUNTRY_INDONESIA, LANGUAGE_INDONESIAN, false },
135 : { COUNTRY_PHILIPPINES, LANGUAGE_ENGLISH_PHILIPPINES, true },
136 : { COUNTRY_NEW_ZEALAND, LANGUAGE_MAORI_NEW_ZEALAND, false },
137 : { COUNTRY_NEW_ZEALAND, LANGUAGE_ENGLISH_NZ, true },
138 : { COUNTRY_SINGAPORE, LANGUAGE_CHINESE_SINGAPORE, true },
139 : { COUNTRY_THAILAND, LANGUAGE_THAI, false },
140 : { COUNTRY_JAPAN, LANGUAGE_JAPANESE, false },
141 : { COUNTRY_SOUTH_KOREA, LANGUAGE_KOREAN, false },
142 : { COUNTRY_VIET_NAM, LANGUAGE_VIETNAMESE, false },
143 : { COUNTRY_PR_CHINA, LANGUAGE_CHINESE_SIMPLIFIED, false },
144 : { COUNTRY_TIBET, LANGUAGE_TIBETAN, false },
145 : { COUNTRY_TURKEY, LANGUAGE_TURKISH, false },
146 : { COUNTRY_INDIA, LANGUAGE_HINDI, false },
147 : { COUNTRY_INDIA, LANGUAGE_URDU_INDIA, true },
148 : { COUNTRY_INDIA, LANGUAGE_PUNJABI, false },
149 : { COUNTRY_INDIA, LANGUAGE_GUJARATI, false },
150 : { COUNTRY_INDIA, LANGUAGE_ODIA, false },
151 : { COUNTRY_INDIA, LANGUAGE_TAMIL, false },
152 : { COUNTRY_INDIA, LANGUAGE_TELUGU, false },
153 : { COUNTRY_INDIA, LANGUAGE_KANNADA, false },
154 : { COUNTRY_INDIA, LANGUAGE_MALAYALAM, false },
155 : { COUNTRY_INDIA, LANGUAGE_ASSAMESE, false },
156 : { COUNTRY_INDIA, LANGUAGE_MARATHI, false },
157 : { COUNTRY_INDIA, LANGUAGE_SANSKRIT, false },
158 : { COUNTRY_INDIA, LANGUAGE_KONKANI, false },
159 : { COUNTRY_INDIA, LANGUAGE_MANIPURI, false },
160 : { COUNTRY_INDIA, LANGUAGE_SINDHI, false },
161 : { COUNTRY_INDIA, LANGUAGE_KASHMIRI, false },
162 : { COUNTRY_PAKISTAN, LANGUAGE_URDU_PAKISTAN, false },
163 : { COUNTRY_MYANMAR, LANGUAGE_BURMESE, false },
164 : { COUNTRY_MOROCCO, LANGUAGE_ARABIC_MOROCCO, true },
165 : { COUNTRY_ALGERIA, LANGUAGE_ARABIC_ALGERIA, true },
166 : { COUNTRY_TUNISIA, LANGUAGE_ARABIC_TUNISIA, true },
167 : { COUNTRY_LIBYA, LANGUAGE_ARABIC_LIBYA, true },
168 : { COUNTRY_SENEGAL, LANGUAGE_FRENCH_SENEGAL, true },
169 : { COUNTRY_MALI, LANGUAGE_FRENCH_MALI, true },
170 : { COUNTRY_COTE_D_IVOIRE, LANGUAGE_FRENCH_COTE_D_IVOIRE, true },
171 : { COUNTRY_CAMEROON, LANGUAGE_FRENCH_CAMEROON, true },
172 : { COUNTRY_ZAIRE, LANGUAGE_FRENCH_ZAIRE, true },
173 : { COUNTRY_RWANDA, LANGUAGE_KINYARWANDA_RWANDA, false },
174 : { COUNTRY_KENYA, LANGUAGE_SWAHILI, false },
175 : { COUNTRY_REUNION, LANGUAGE_FRENCH_REUNION, true },
176 : { COUNTRY_ZIMBABWE, LANGUAGE_ENGLISH_ZIMBABWE, true },
177 : { COUNTRY_LESOTHO, LANGUAGE_SESOTHO, false },
178 : { COUNTRY_BOTSWANA, LANGUAGE_TSWANA, false },
179 : { COUNTRY_FAEROE_ISLANDS, LANGUAGE_FAEROESE, false },
180 : { COUNTRY_PORTUGAL, LANGUAGE_PORTUGUESE, false },
181 : { COUNTRY_LUXEMBOURG, LANGUAGE_GERMAN_LUXEMBOURG, true },
182 : { COUNTRY_LUXEMBOURG, LANGUAGE_FRENCH_LUXEMBOURG, true },
183 : { COUNTRY_IRELAND, LANGUAGE_ENGLISH_EIRE, true },
184 : { COUNTRY_IRELAND, LANGUAGE_GAELIC_IRELAND, true },
185 : { COUNTRY_ICELAND, LANGUAGE_ICELANDIC, false },
186 : { COUNTRY_ALBANIA, LANGUAGE_ALBANIAN, false },
187 : { COUNTRY_MALTA, LANGUAGE_MALTESE, false },
188 : { COUNTRY_FINLAND, LANGUAGE_FINNISH, false },
189 : { COUNTRY_FINLAND, LANGUAGE_SWEDISH_FINLAND, true },
190 : { COUNTRY_BULGARIA, LANGUAGE_BULGARIAN, false },
191 : { COUNTRY_LITHUANIA, LANGUAGE_LITHUANIAN, false },
192 : { COUNTRY_LATVIA, LANGUAGE_LATVIAN, false },
193 : { COUNTRY_ESTONIA, LANGUAGE_ESTONIAN, false },
194 : { COUNTRY_MOLDOVA, LANGUAGE_ROMANIAN_MOLDOVA, true },
195 : { COUNTRY_MOLDOVA, LANGUAGE_RUSSIAN_MOLDOVA, true },
196 : { COUNTRY_ARMENIA, LANGUAGE_ARMENIAN, false },
197 : { COUNTRY_BELARUS, LANGUAGE_BELARUSIAN, false },
198 : { COUNTRY_MONACO, LANGUAGE_FRENCH_MONACO, true },
199 : { COUNTRY_UKRAINE, LANGUAGE_UKRAINIAN, false },
200 : { COUNTRY_SERBIA, LANGUAGE_SERBIAN_LATIN_SAM, false },
201 : { COUNTRY_CROATIA, LANGUAGE_CROATIAN, true }, // sub type of LANGUAGE_SERBIAN
202 : { COUNTRY_SLOVENIA, LANGUAGE_SLOVENIAN, false },
203 : { COUNTRY_MACEDONIA, LANGUAGE_MACEDONIAN, false },
204 : { COUNTRY_CZECH, LANGUAGE_CZECH, false },
205 : { COUNTRY_SLOVAK, LANGUAGE_SLOVAK, false },
206 : { COUNTRY_LIECHTENSTEIN, LANGUAGE_GERMAN_LIECHTENSTEIN, true },
207 : { COUNTRY_BELIZE, LANGUAGE_ENGLISH_BELIZE, true },
208 : { COUNTRY_GUATEMALA, LANGUAGE_SPANISH_GUATEMALA, true },
209 : { COUNTRY_EL_SALVADOR, LANGUAGE_SPANISH_EL_SALVADOR, true },
210 : { COUNTRY_HONDURAS, LANGUAGE_SPANISH_HONDURAS, true },
211 : { COUNTRY_NICARAGUA, LANGUAGE_SPANISH_NICARAGUA, true },
212 : { COUNTRY_COSTA_RICA, LANGUAGE_SPANISH_COSTARICA, true },
213 : { COUNTRY_PANAMA, LANGUAGE_SPANISH_PANAMA, true },
214 : { COUNTRY_BOLIVIA, LANGUAGE_SPANISH_BOLIVIA, true },
215 : { COUNTRY_ECUADOR, LANGUAGE_SPANISH_ECUADOR, true },
216 : { COUNTRY_PARAGUAY, LANGUAGE_SPANISH_PARAGUAY, true },
217 : { COUNTRY_URUGUAY, LANGUAGE_SPANISH_URUGUAY, true },
218 : { COUNTRY_BRUNEI_DARUSSALAM, LANGUAGE_MALAY_BRUNEI_DARUSSALAM, true },
219 : { COUNTRY_HONG_KONG, LANGUAGE_CHINESE_HONGKONG, true },
220 : { COUNTRY_MACAU, LANGUAGE_CHINESE_MACAU, true },
221 : { COUNTRY_CAMBODIA, LANGUAGE_KHMER, false },
222 : { COUNTRY_LAOS, LANGUAGE_LAO, false },
223 : { COUNTRY_BANGLADESH, LANGUAGE_BENGALI, false },
224 : { COUNTRY_TAIWAN, LANGUAGE_CHINESE_TRADITIONAL, true },
225 : { COUNTRY_MALDIVES, LANGUAGE_DHIVEHI, false },
226 : { COUNTRY_LEBANON, LANGUAGE_ARABIC_LEBANON, true },
227 : { COUNTRY_JORDAN, LANGUAGE_ARABIC_JORDAN, true },
228 : { COUNTRY_SYRIA, LANGUAGE_ARABIC_SYRIA, true },
229 : { COUNTRY_IRAQ, LANGUAGE_ARABIC_IRAQ, true },
230 : { COUNTRY_KUWAIT, LANGUAGE_ARABIC_KUWAIT, true },
231 : { COUNTRY_SAUDI_ARABIA, LANGUAGE_ARABIC_SAUDI_ARABIA, true },
232 : { COUNTRY_YEMEN, LANGUAGE_ARABIC_YEMEN, true },
233 : { COUNTRY_OMAN, LANGUAGE_ARABIC_OMAN, true },
234 : { COUNTRY_UAE, LANGUAGE_ARABIC_UAE, true },
235 : { COUNTRY_ISRAEL, LANGUAGE_HEBREW, false },
236 : { COUNTRY_BAHRAIN, LANGUAGE_ARABIC_BAHRAIN, true },
237 : { COUNTRY_QATAR, LANGUAGE_ARABIC_QATAR, true },
238 : { COUNTRY_MONGOLIA, LANGUAGE_MONGOLIAN_CYRILLIC_MONGOLIA, false },
239 : { COUNTRY_NEPAL, LANGUAGE_NEPALI, false },
240 : { COUNTRY_IRAN, LANGUAGE_FARSI, false },
241 : { COUNTRY_TAJIKISTAN, LANGUAGE_TAJIK, false },
242 : { COUNTRY_TURKMENISTAN, LANGUAGE_TURKMEN, false },
243 : { COUNTRY_AZERBAIJAN, LANGUAGE_AZERI_LATIN, false },
244 : { COUNTRY_GEORGIA, LANGUAGE_GEORGIAN, false },
245 : { COUNTRY_KYRGYZSTAN, LANGUAGE_KIRGHIZ, false },
246 : { COUNTRY_UZBEKISTAN, LANGUAGE_UZBEK_LATIN, false }
247 : };
248 :
249 : const CountryEntry * const pEnd = pTable + SAL_N_ELEMENTS( pTable );
250 :
251 : /** Predicate comparing a country ID with the member of a CountryEntry. */
252 : struct CountryEntryPred_Country
253 : {
254 : CountryId meCountry;
255 :
256 168 : inline explicit CountryEntryPred_Country( CountryId eCountry ) :
257 168 : meCountry( eCountry ) {}
258 :
259 3034 : inline bool operator()( const CountryEntry& rCmp ) const
260 3034 : { return rCmp.meCountry == meCountry; }
261 : };
262 :
263 : /** Predicate comparing a language type with the member of a CountryEntry.
264 :
265 : Compares by primary language only, if the passed CountryEntry allows it
266 : (the member mbUseSubLang is cleared), otherwise by full language type. */
267 : struct CountryEntryPred_Language
268 : {
269 : LanguageType meLanguage;
270 :
271 34 : inline explicit CountryEntryPred_Language( LanguageType eLanguage ) :
272 34 : meLanguage( eLanguage ) {}
273 :
274 : inline bool operator()( const CountryEntry& rCmp ) const;
275 : };
276 :
277 2856 : inline bool CountryEntryPred_Language::operator()( const CountryEntry& rCmp ) const
278 : {
279 : // rCmp.mbUseSubLang==true -> compare full language type
280 : // rCmp.mbUseSubLang==false -> compare primary language only
281 1275 : return rCmp.mbUseSubLang ? (meLanguage == rCmp.meLanguage) :
282 4131 : ((meLanguage & 0x03FF) == (rCmp.meLanguage & 0x03FF));
283 : }
284 :
285 : } // namespace
286 :
287 : // Country ID <-> Language type conversion ====================================
288 :
289 17 : CountryId ConvertLanguageToCountry( LanguageType eLanguage )
290 : {
291 : // country of a found primary language type
292 17 : CountryId ePrimCountry = COUNTRY_DONTKNOW;
293 :
294 : // find an exact match and a primary-language-only match, in one pass
295 17 : const CountryEntry* pEntry = pTable;
296 34 : do
297 : {
298 34 : pEntry = std::find_if( pEntry, pEnd, CountryEntryPred_Language( eLanguage ) );
299 34 : if( pEntry != pEnd )
300 : {
301 17 : if( pEntry->mbUseSubLang )
302 0 : return pEntry->meCountry; // exact match found -> return
303 17 : if( ePrimCountry == COUNTRY_DONTKNOW )
304 17 : ePrimCountry = pEntry->meCountry;
305 17 : ++pEntry; // one entry forward for next find_if() call
306 : }
307 : }
308 : while( pEntry != pEnd );
309 :
310 17 : return ePrimCountry;
311 : }
312 :
313 168 : LanguageType ConvertCountryToLanguage( CountryId eCountry )
314 : {
315 : // just find the first occurrence of eCountry and return the language type
316 168 : const CountryEntry* pEntry = std::find_if( pTable, pEnd, CountryEntryPred_Country( eCountry ) );
317 168 : return (pEntry != pEnd) ? pEntry->meLanguage : LANGUAGE_DONTKNOW;
318 : }
319 :
320 : }
321 :
322 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|