libzypp  13.10.6
CountryCode.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/String.h"
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/Tr1hash.h"
18 
19 #include "zypp/CountryCode.h"
20 
21 using std::endl;
22 
24 namespace zypp
25 {
26 
28  namespace
29  {
30 
32  struct CodeMaps // singleton
33  {
34  typedef std::tr1::unordered_map<std::string,std::string> CodeMap;
35  typedef CodeMap::const_iterator Index;
36 
38  static Index getIndex( const std::string & code_r )
39  {
40  static CodeMaps _maps; // the singleton instance
41  return _maps.lookup( code_r );
42  }
43 
44  private:
48  CodeMaps();
49 
51  inline Index lookup( const std::string & code_r );
52 
53  private:
55  CodeMap codes;
56  };
57 
58  inline CodeMaps::Index CodeMaps::lookup( const std::string & code_r )
59  {
60  Index it = codes.find( code_r );
61  if ( it != codes.end() )
62  return it;
63 
64  // not found: Remember a new code
65  CodeMap::value_type nval( code_r, std::string() );
66 
67  if ( code_r.size() != 2 )
68  WAR << "Malformed CountryCode '" << code_r << "' (expect 2-letter)" << endl;
69 
70  std::string lcode( str::toUpper( code_r ) );
71  if ( lcode != code_r )
72  {
73  WAR << "Malformed CountryCode '" << code_r << "' (not upper case)" << endl;
74  // but maybe we're lucky with the upper case code
75  // and find a country name.
76  it = codes.find( lcode );
77  if ( it != codes.end() )
78  nval.second = it->second;
79  }
80 
81  MIL << "Remember CountryCode '" << code_r << "': '" << nval.second << "'" << endl;
82  return codes.insert( nval ).first;
83  }
84 
86  } // namespace
88 
90  //
91  // CLASS NAME : CountryCode::Impl
92  //
98  {
99  Impl()
100  : _index( CodeMaps::getIndex( std::string() ) )
101  {}
102 
103  Impl( const std::string & code_r )
104  : _index( CodeMaps::getIndex( code_r ) )
105  {}
106 
107  std::string code() const
108  { return _index->first; }
109 
110  std::string name() const {
111  if ( _index->second.empty() )
112  {
113  std::string ret( _("Unknown country: ") );
114  ret += "'";
115  ret += _index->first;
116  ret += "'";
117  return ret;
118  }
119  return _( _index->second.c_str() );
120  }
121 
122  private:
124  CodeMaps::Index _index;
125 
126  public:
128  static shared_ptr<Impl> nullimpl()
129  {
130  static shared_ptr<Impl> _nullimpl( new Impl );
131  return _nullimpl;
132  }
133  };
135 
137  //
138  // CLASS NAME : CountryCode
139  //
141 
143 
145  //
146  // METHOD NAME : CountryCode::CountryCode
147  // METHOD TYPE : Ctor
148  //
150  : _pimpl( Impl::nullimpl() )
151  {}
152 
154  //
155  // METHOD NAME : CountryCode::CountryCode
156  // METHOD TYPE : Ctor
157  //
158  CountryCode::CountryCode( const std::string & code_r )
159  : _pimpl( new Impl( code_r ) )
160  {}
161 
163  //
164  // METHOD NAME : CountryCode::~CountryCode
165  // METHOD TYPE : Dtor
166  //
168  {}
169 
171  //
172  // METHOD NAME : CountryCode::code
173  // METHOD TYPE : std::string
174  //
175  std::string CountryCode::code() const
176  { return _pimpl->code(); }
177 
179  //
180  // METHOD NAME : CountryCode::name
181  // METHOD TYPE : std::string
182  //
183  std::string CountryCode::name() const
184  { return _pimpl->name(); }
185 
187  namespace
188  {
189 
190  CodeMaps::CodeMaps()
191  {
192  // Defined CountryCode constants
193  codes[""] = N_( "No Code" );
194 
195  struct Init
196  {
197  const char *iso3166;
198  const char *name;
199  };
200 
201  const Init init[] = {
202  {"AD", N_( "Andorra" ) }, // :AND:020:
203  {"AE", N_( "United Arab Emirates" ) }, // :ARE:784:
204  {"AF", N_( "Afghanistan" ) }, // :AFG:004:
205  {"AG", N_( "Antigua and Barbuda" ) }, // :ATG:028:
206  {"AI", N_( "Anguilla" ) }, // :AIA:660:
207  {"AL", N_( "Albania" ) }, // :ALB:008:
208  {"AM", N_( "Armenia" ) }, // :ARM:051:
209  {"AN", N_( "Netherlands Antilles" ) }, // :ANT:530:
210  {"AO", N_( "Angola" ) }, // :AGO:024:
211  {"AQ", N_( "Antarctica" ) }, // :ATA:010:
212  {"AR", N_( "Argentina" ) }, // :ARG:032:
213  {"AS", N_( "American Samoa" ) }, // :ASM:016:
214  {"AT", N_( "Austria" ) }, // :AUT:040:
215  {"AU", N_( "Australia" ) }, // :AUS:036:
216  {"AW", N_( "Aruba" ) }, // :ABW:533:
217  {"AX", N_( "Aland Islands" ) }, // :ALA:248:
218  {"AZ", N_( "Azerbaijan" ) }, // :AZE:031:
219  {"BA", N_( "Bosnia and Herzegovina" ) }, // :BIH:070:
220  {"BB", N_( "Barbados" ) }, // :BRB:052:
221  {"BD", N_( "Bangladesh" ) }, // :BGD:050:
222  {"BE", N_( "Belgium" ) }, // :BEL:056:
223  {"BF", N_( "Burkina Faso" ) }, // :BFA:854:
224  {"BG", N_( "Bulgaria" ) }, // :BGR:100:
225  {"BH", N_( "Bahrain" ) }, // :BHR:048:
226  {"BI", N_( "Burundi" ) }, // :BDI:108:
227  {"BJ", N_( "Benin" ) }, // :BEN:204:
228  {"BM", N_( "Bermuda" ) }, // :BMU:060:
229  {"BN", N_( "Brunei Darussalam" ) }, // :BRN:096:
230  {"BO", N_( "Bolivia" ) }, // :BOL:068:
231  {"BR", N_( "Brazil" ) }, // :BRA:076:
232  {"BS", N_( "Bahamas" ) }, // :BHS:044:
233  {"BT", N_( "Bhutan" ) }, // :BTN:064:
234  {"BV", N_( "Bouvet Island" ) }, // :BVT:074:
235  {"BW", N_( "Botswana" ) }, // :BWA:072:
236  {"BY", N_( "Belarus" ) }, // :BLR:112:
237  {"BZ", N_( "Belize" ) }, // :BLZ:084:
238  {"CA", N_( "Canada" ) }, // :CAN:124:
239  {"CC", N_( "Cocos (Keeling) Islands" ) }, // :CCK:166:
240  {"CD", N_( "Congo" ) }, // :COD:180:
241  {"CF", N_( "Central African Republic" ) }, // :CAF:140:
242  {"CG", N_( "Congo" ) }, // :COG:178:
243  {"CH", N_( "Switzerland" ) }, // :CHE:756:
244  {"CI", N_( "Cote D'Ivoire" ) }, // :CIV:384:
245  {"CK", N_( "Cook Islands" ) }, // :COK:184:
246  {"CL", N_( "Chile" ) }, // :CHL:152:
247  {"CM", N_( "Cameroon" ) }, // :CMR:120:
248  {"CN", N_( "China" ) }, // :CHN:156:
249  {"CO", N_( "Colombia" ) }, // :COL:170:
250  {"CR", N_( "Costa Rica" ) }, // :CRI:188:
251  {"CU", N_( "Cuba" ) }, // :CUB:192:
252  {"CV", N_( "Cape Verde" ) }, // :CPV:132:
253  {"CX", N_( "Christmas Island" ) }, // :CXR:162:
254  {"CY", N_( "Cyprus" ) }, // :CYP:196:
255  {"CZ", N_( "Czech Republic" ) }, // :CZE:203:
256  {"DE", N_( "Germany" ) }, // :DEU:276:
257  {"DJ", N_( "Djibouti" ) }, // :DJI:262:
258  {"DK", N_( "Denmark" ) }, // :DNK:208:
259  {"DM", N_( "Dominica" ) }, // :DMA:212:
260  {"DO", N_( "Dominican Republic" ) }, // :DOM:214:
261  {"DZ", N_( "Algeria" ) }, // :DZA:012:
262  {"EC", N_( "Ecuador" ) }, // :ECU:218:
263  {"EE", N_( "Estonia" ) }, // :EST:233:
264  {"EG", N_( "Egypt" ) }, // :EGY:818:
265  {"EH", N_( "Western Sahara" ) }, // :ESH:732:
266  {"ER", N_( "Eritrea" ) }, // :ERI:232:
267  {"ES", N_( "Spain" ) }, // :ESP:724:
268  {"ET", N_( "Ethiopia" ) }, // :ETH:231:
269  {"FI", N_( "Finland" ) }, // :FIN:246:
270  {"FJ", N_( "Fiji" ) }, // :FJI:242:
271  {"FK", N_( "Falkland Islands (Malvinas)" ) }, // :FLK:238:
272  {"FM", N_( "Federated States of Micronesia" ) }, // :FSM:583:
273  {"FO", N_( "Faroe Islands" ) }, // :FRO:234:
274  {"FR", N_( "France" ) }, // :FRA:250:
275  {"FX", N_( "Metropolitan France" ) }, // :FXX:249:
276  {"GA", N_( "Gabon" ) }, // :GAB:266:
277  {"GB", N_( "United Kingdom" ) }, // :GBR:826:
278  {"GD", N_( "Grenada" ) }, // :GRD:308:
279  {"GE", N_( "Georgia" ) }, // :GEO:268:
280  {"GF", N_( "French Guiana" ) }, // :GUF:254:
281  {"GG", N_( "Guernsey" ) },
282  {"GH", N_( "Ghana" ) }, // :GHA:288:
283  {"GI", N_( "Gibraltar" ) }, // :GIB:292:
284  {"GL", N_( "Greenland" ) }, // :GRL:304:
285  {"GM", N_( "Gambia" ) }, // :GMB:270:
286  {"GN", N_( "Guinea" ) }, // :GIN:324:
287  {"GP", N_( "Guadeloupe" ) }, // :GLP:312:
288  {"GQ", N_( "Equatorial Guinea" ) }, // :GNQ:226:
289  {"GR", N_( "Greece" ) }, // :GRC:300:
290  {"GS", N_( "South Georgia and the South Sandwich Islands" ) }, // :SGS:239:
291  {"GT", N_( "Guatemala" ) }, // :GTM:320:
292  {"GU", N_( "Guam" ) }, // :GUM:316:
293  {"GW", N_( "Guinea-Bissau" ) }, // :GNB:624:
294  {"GY", N_( "Guyana" ) }, // :GUY:328:
295  {"HK", N_( "Hong Kong" ) }, // :HKG:344:
296  {"HM", N_( "Heard Island and McDonald Islands" ) }, // :HMD:334:
297  {"HN", N_( "Honduras" ) }, // :HND:340:
298  {"HR", N_( "Croatia" ) }, // :HRV:191:
299  {"HT", N_( "Haiti" ) }, // :HTI:332:
300  {"HU", N_( "Hungary" ) }, // :HUN:348:
301  {"ID", N_( "Indonesia" ) }, // :IDN:360:
302  {"IE", N_( "Ireland" ) }, // :IRL:372:
303  {"IL", N_( "Israel" ) }, // :ISR:376:
304  {"IM", N_( "Isle of Man" ) },
305  {"IN", N_( "India" ) }, // :IND:356:
306  {"IO", N_( "British Indian Ocean Territory" ) }, // :IOT:086:
307  {"IQ", N_( "Iraq" ) }, // :IRQ:368:
308  {"IR", N_( "Iran" ) }, // :IRN:364:
309  {"IS", N_( "Iceland" ) }, // :ISL:352:
310  {"IT", N_( "Italy" ) }, // :ITA:380:
311  {"JE", N_( "Jersey" ) },
312  {"JM", N_( "Jamaica" ) }, // :JAM:388:
313  {"JO", N_( "Jordan" ) }, // :JOR:400:
314  {"JP", N_( "Japan" ) }, // :JPN:392:
315  {"KE", N_( "Kenya" ) }, // :KEN:404:
316  {"KG", N_( "Kyrgyzstan" ) }, // :KGZ:417:
317  {"KH", N_( "Cambodia" ) }, // :KHM:116:
318  {"KI", N_( "Kiribati" ) }, // :KIR:296:
319  {"KM", N_( "Comoros" ) }, // :COM:174:
320  {"KN", N_( "Saint Kitts and Nevis" ) }, // :KNA:659:
321  {"KP", N_( "North Korea" ) }, // :PRK:408:
322  {"KR", N_( "South Korea" ) }, // :KOR:410:
323  {"KW", N_( "Kuwait" ) }, // :KWT:414:
324  {"KY", N_( "Cayman Islands" ) }, // :CYM:136:
325  {"KZ", N_( "Kazakhstan" ) }, // :KAZ:398:
326  {"LA", N_( "Lao People's Democratic Republic" ) }, // :LAO:418:
327  {"LB", N_( "Lebanon" ) }, // :LBN:422:
328  {"LC", N_( "Saint Lucia" ) }, // :LCA:662:
329  {"LI", N_( "Liechtenstein" ) }, // :LIE:438:
330  {"LK", N_( "Sri Lanka" ) }, // :LKA:144:
331  {"LR", N_( "Liberia" ) }, // :LBR:430:
332  {"LS", N_( "Lesotho" ) }, // :LSO:426:
333  {"LT", N_( "Lithuania" ) }, // :LTU:440:
334  {"LU", N_( "Luxembourg" ) }, // :LUX:442:
335  {"LV", N_( "Latvia" ) }, // :LVA:428:
336  {"LY", N_( "Libya" ) }, // :LBY:434:
337  {"MA", N_( "Morocco" ) }, // :MAR:504:
338  {"MC", N_( "Monaco" ) }, // :MCO:492:
339  {"MD", N_( "Moldova" ) }, // :MDA:498:
340  {"ME", N_( "Montenegro" ) },
341  {"MF", N_( "Saint Martin" ) },
342  {"MG", N_( "Madagascar" ) }, // :MDG:450:
343  {"MH", N_( "Marshall Islands" ) }, // :MHL:584:
344  {"MK", N_( "Macedonia" ) }, // :MKD:807:
345  {"ML", N_( "Mali" ) }, // :MLI:466:
346  {"MM", N_( "Myanmar" ) }, // :MMR:104:
347  {"MN", N_( "Mongolia" ) }, // :MNG:496:
348  {"MO", N_( "Macao" ) }, // :MAC:446:
349  {"MP", N_( "Northern Mariana Islands" ) }, // :MNP:580:
350  {"MQ", N_( "Martinique" ) }, // :MTQ:474:
351  {"MR", N_( "Mauritania" ) }, // :MRT:478:
352  {"MS", N_( "Montserrat" ) }, // :MSR:500:
353  {"MT", N_( "Malta" ) }, // :MLT:470:
354  {"MU", N_( "Mauritius" ) }, // :MUS:480:
355  {"MV", N_( "Maldives" ) }, // :MDV:462:
356  {"MW", N_( "Malawi" ) }, // :MWI:454:
357  {"MX", N_( "Mexico" ) }, // :MEX:484:
358  {"MY", N_( "Malaysia" ) }, // :MYS:458:
359  {"MZ", N_( "Mozambique" ) }, // :MOZ:508:
360  {"NA", N_( "Namibia" ) }, // :NAM:516:
361  {"NC", N_( "New Caledonia" ) }, // :NCL:540:
362  {"NE", N_( "Niger" ) }, // :NER:562:
363  {"NF", N_( "Norfolk Island" ) }, // :NFK:574:
364  {"NG", N_( "Nigeria" ) }, // :NGA:566:
365  {"NI", N_( "Nicaragua" ) }, // :NIC:558:
366  {"NL", N_( "Netherlands" ) }, // :NLD:528:
367  {"NO", N_( "Norway" ) }, // :NOR:578:
368  {"NP", N_( "Nepal" ) }, // :NPL:524:
369  {"NR", N_( "Nauru" ) }, // :NRU:520:
370  {"NU", N_( "Niue" ) }, // :NIU:570:
371  {"NZ", N_( "New Zealand" ) }, // :NZL:554:
372  {"OM", N_( "Oman" ) }, // :OMN:512:
373  {"PA", N_( "Panama" ) }, // :PAN:591:
374  {"PE", N_( "Peru" ) }, // :PER:604:
375  {"PF", N_( "French Polynesia" ) }, // :PYF:258:
376  {"PG", N_( "Papua New Guinea" ) }, // :PNG:598:
377  {"PH", N_( "Philippines" ) }, // :PHL:608:
378  {"PK", N_( "Pakistan" ) }, // :PAK:586:
379  {"PL", N_( "Poland" ) }, // :POL:616:
380  {"PM", N_( "Saint Pierre and Miquelon" ) }, // :SPM:666:
381  {"PN", N_( "Pitcairn" ) }, // :PCN:612:
382  {"PR", N_( "Puerto Rico" ) }, // :PRI:630:
383  {"PS", N_( "Palestinian Territory" ) }, // :PSE:275:
384  {"PT", N_( "Portugal" ) }, // :PRT:620:
385  {"PW", N_( "Palau" ) }, // :PLW:585:
386  {"PY", N_( "Paraguay" ) }, // :PRY:600:
387  {"QA", N_( "Qatar" ) }, // :QAT:634:
388  {"RE", N_( "Reunion" ) }, // :REU:638:
389  {"RO", N_( "Romania" ) }, // :ROU:642:
390  {"RS", N_( "Serbia" ) },
391  {"RU", N_( "Russian Federation" ) }, // :RUS:643:
392  {"RW", N_( "Rwanda" ) }, // :RWA:646:
393  {"SA", N_( "Saudi Arabia" ) }, // :SAU:682:
394  {"SB", N_( "Solomon Islands" ) }, // :SLB:090:
395  {"SC", N_( "Seychelles" ) }, // :SYC:690:
396  {"SD", N_( "Sudan" ) }, // :SDN:736:
397  {"SE", N_( "Sweden" ) }, // :SWE:752:
398  {"SG", N_( "Singapore" ) }, // :SGP:702:
399  {"SH", N_( "Saint Helena" ) }, // :SHN:654:
400  {"SI", N_( "Slovenia" ) }, // :SVN:705:
401  {"SJ", N_( "Svalbard and Jan Mayen" ) }, // :SJM:744:
402  {"SK", N_( "Slovakia" ) }, // :SVK:703:
403  {"SL", N_( "Sierra Leone" ) }, // :SLE:694:
404  {"SM", N_( "San Marino" ) }, // :SMR:674:
405  {"SN", N_( "Senegal" ) }, // :SEN:686:
406  {"SO", N_( "Somalia" ) }, // :SOM:706:
407  {"SR", N_( "Suriname" ) }, // :SUR:740:
408  {"ST", N_( "Sao Tome and Principe" ) }, // :STP:678:
409  {"SV", N_( "El Salvador" ) }, // :SLV:222:
410  {"SY", N_( "Syria" ) }, // :SYR:760:
411  {"SZ", N_( "Swaziland" ) }, // :SWZ:748:
412  {"TC", N_( "Turks and Caicos Islands" ) }, // :TCA:796:
413  {"TD", N_( "Chad" ) }, // :TCD:148:
414  {"TF", N_( "French Southern Territories" ) }, // :ATF:260:
415  {"TG", N_( "Togo" ) }, // :TGO:768:
416  {"TH", N_( "Thailand" ) }, // :THA:764:
417  {"TJ", N_( "Tajikistan" ) }, // :TJK:762:
418  {"TK", N_( "Tokelau" ) }, // :TKL:772:
419  {"TM", N_( "Turkmenistan" ) }, // :TKM:795:
420  {"TN", N_( "Tunisia" ) }, // :TUN:788:
421  {"TO", N_( "Tonga" ) }, // :TON:776:
422  {"TL", N_( "East Timor" ) }, // :TLS:626:
423  {"TR", N_( "Turkey" ) }, // :TUR:792:
424  {"TT", N_( "Trinidad and Tobago" ) }, // :TTO:780:
425  {"TV", N_( "Tuvalu" ) }, // :TUV:798:
426  {"TW", N_( "Taiwan" ) }, // :TWN:158:
427  {"TZ", N_( "Tanzania" ) }, // :TZA:834:
428  {"UA", N_( "Ukraine" ) }, // :UKR:804:
429  {"UG", N_( "Uganda" ) }, // :UGA:800:
430  {"UM", N_( "United States Minor Outlying Islands" ) },// :UMI:581:
431  {"US", N_( "United States" ) }, // :USA:840:
432  {"UY", N_( "Uruguay" ) }, // :URY:858:
433  {"UZ", N_( "Uzbekistan" ) }, // :UZB:860:
434  {"VA", N_( "Holy See (Vatican City State)" ) }, // :VAT:336:
435  {"VC", N_( "Saint Vincent and the Grenadines" ) }, // :VCT:670:
436  {"VE", N_( "Venezuela" ) }, // :VEN:862:
437  {"VG", N_( "British Virgin Islands" ) }, // :VGB:092:
438  {"VI", N_( "Virgin Islands, U.S." ) }, // :VIR:850:
439  {"VN", N_( "Vietnam" ) }, // :VNM:704:
440  {"VU", N_( "Vanuatu" ) }, // :VUT:548:
441  {"WF", N_( "Wallis and Futuna" ) }, // :WLF:876:
442  {"WS", N_( "Samoa" ) }, // :WSM:882:
443  {"YE", N_( "Yemen" ) }, // :YEM:887:
444  {"YT", N_( "Mayotte" ) }, // :MYT:175:
445  {"ZA", N_( "South Africa" ) }, // :ZAF:710:
446  {"ZM", N_( "Zambia" ) }, // :ZMB:894:
447  {"ZW", N_( "Zimbabwe" ) }, // :ZWE:716:
448 
449  { NULL, NULL }
450  };
451 
452  for (const Init * i = init; i->iso3166 != NULL; ++i)
453  codes[i->iso3166] = i->name;
454  }
455 
457  } // namespace
459 
461 } // namespace zypp
Interface to gettext.
#define MIL
Definition: Logger.h:47
std::string name() const
Definition: CountryCode.cc:110
#define N_(MSG)
Just tag text for translation.
Definition: Gettext.h:18
std::string code() const
Definition: CountryCode.cc:107
RW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: CountryCode.h:77
CountryCode()
Default ctor.
Definition: CountryCode.cc:149
CodeMaps::Index _index
index into code map.
Definition: CountryCode.cc:124
#define WAR
Definition: Logger.h:48
#define _(MSG)
Return translated text.
Definition: Gettext.h:21
CodeMap codes
All the codes.
Definition: CountryCode.cc:55
std::string name() const
Return the country name; if not available the country code.
Definition: CountryCode.cc:183
Impl(const std::string &code_r)
Definition: CountryCode.cc:103
static const CountryCode noCode
No or empty code.
Definition: CountryCode.h:61
std::string code() const
Return the country code.
Definition: CountryCode.cc:175
CountryCode implementation.
Definition: CountryCode.cc:97
std::string toUpper(const std::string &s)
Return uppercase version of s.
Definition: String.cc:185
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Definition: CountryCode.cc:128
Country codes (iso3166-1-alpha-2).
Definition: CountryCode.h:37