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