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