libzypp  10.5.0
CountryCode.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 
00014 #include "zypp/base/Logger.h"
00015 #include "zypp/base/String.h"
00016 #include "zypp/base/Gettext.h"
00017 #include "zypp/base/Tr1hash.h"
00018 
00019 #include "zypp/CountryCode.h"
00020 
00021 using std::endl;
00022 
00024 namespace zypp
00025 { 
00026 
00028   namespace
00029   { 
00030 
00032     struct CodeMaps // singleton
00033     {
00034       typedef std::tr1::unordered_map<std::string,std::string> CodeMap;
00035       typedef CodeMap::const_iterator Index;
00036 
00038       static Index getIndex( const std::string & code_r )
00039       {
00040         static CodeMaps _maps; // the singleton instance
00041         return _maps.lookup( code_r );
00042       }
00043 
00044     private:
00048       CodeMaps();
00049 
00051       inline Index lookup( const std::string & code_r );
00052 
00053     private:
00055       CodeMap codes;
00056     };
00057 
00058     inline CodeMaps::Index CodeMaps::lookup( const std::string & code_r )
00059     {
00060       Index it = codes.find( code_r );
00061       if ( it != codes.end() )
00062         return it;
00063 
00064       // not found: Remember a new code
00065       CodeMap::value_type nval( code_r, std::string() );
00066 
00067       if ( code_r.size() != 2 )
00068         WAR << "Malformed CountryCode '" << code_r << "' (expect 2-letter)" << endl;
00069 
00070       std::string lcode( str::toUpper( code_r ) );
00071       if ( lcode != code_r )
00072         {
00073           WAR << "Malformed CountryCode '" << code_r << "' (not upper case)" << endl;
00074           // but maybe we're lucky with the upper case code
00075           // and find a country name.
00076           it = codes.find( lcode );
00077           if ( it != codes.end() )
00078             nval.second = it->second;
00079         }
00080 
00081       MIL << "Remember CountryCode '" << code_r << "': '" << nval.second << "'" << endl;
00082       return codes.insert( nval ).first;
00083     }
00084 
00086   } // namespace
00088 
00090   //
00091   //    CLASS NAME : CountryCode::Impl
00092   //
00097   struct CountryCode::Impl
00098   {
00099     Impl()
00100     : _index( CodeMaps::getIndex( std::string() ) )
00101     {}
00102 
00103     Impl( const std::string & code_r )
00104     : _index( CodeMaps::getIndex( code_r ) )
00105     {}
00106 
00107     std::string code() const
00108     { return _index->first; }
00109 
00110     std::string name() const {
00111       if ( _index->second.empty() )
00112         {
00113           std::string ret( _("Unknown country: ") );
00114           ret += "'";
00115           ret += _index->first;
00116           ret += "'";
00117           return ret;
00118         }
00119       return _( _index->second.c_str() );
00120     }
00121 
00122   private:
00124     CodeMaps::Index _index;
00125 
00126   public:
00128     static shared_ptr<Impl> nullimpl()
00129     {
00130       static shared_ptr<Impl> _nullimpl( new Impl );
00131       return _nullimpl;
00132     }
00133   };
00135 
00137   //
00138   //    CLASS NAME : CountryCode
00139   //
00141 
00142   const CountryCode CountryCode::noCode;
00143 
00145   //
00146   //    METHOD NAME : CountryCode::CountryCode
00147   //    METHOD TYPE : Ctor
00148   //
00149   CountryCode::CountryCode()
00150   : _pimpl( Impl::nullimpl() )
00151   {}
00152 
00154   //
00155   //    METHOD NAME : CountryCode::CountryCode
00156   //    METHOD TYPE : Ctor
00157   //
00158   CountryCode::CountryCode( const std::string & code_r )
00159   : _pimpl( new Impl( code_r ) )
00160   {}
00161 
00163   //
00164   //    METHOD NAME : CountryCode::~CountryCode
00165   //    METHOD TYPE : Dtor
00166   //
00167   CountryCode::~CountryCode()
00168   {}
00169 
00171   //
00172   //    METHOD NAME : CountryCode::code
00173   //    METHOD TYPE : std::string
00174   //
00175   std::string CountryCode::code() const
00176   { return _pimpl->code(); }
00177 
00179   //
00180   //    METHOD NAME : CountryCode::name
00181   //    METHOD TYPE : std::string
00182   //
00183   std::string CountryCode::name() const
00184   { return _pimpl->name(); }
00185 
00187   namespace
00188   { 
00189 
00190     CodeMaps::CodeMaps()
00191     {
00192       // Defined CountryCode constants
00193       codes[""]        = N_( "No Code" );
00194 
00195       struct Init
00196       {
00197           const char *iso3166;
00198           const char *name;
00199       };
00200 
00201       const Init init[] = {
00202           {"AD", N_( "Andorra" ) },                             // :AND:020:
00203           {"AE", N_( "United Arab Emirates" ) },                // :ARE:784:
00204           {"AF", N_( "Afghanistan" ) },                         // :AFG:004:
00205           {"AG", N_( "Antigua and Barbuda" ) },                 // :ATG:028:
00206           {"AI", N_( "Anguilla" ) },                            // :AIA:660:
00207           {"AL", N_( "Albania" ) },                             // :ALB:008:
00208           {"AM", N_( "Armenia" ) },                             // :ARM:051:
00209           {"AN", N_( "Netherlands Antilles" ) },                // :ANT:530:
00210           {"AO", N_( "Angola" ) },                              // :AGO:024:
00211           {"AQ", N_( "Antarctica" ) },                          // :ATA:010:
00212           {"AR", N_( "Argentina" ) },                           // :ARG:032:
00213           {"AS", N_( "American Samoa" ) },                      // :ASM:016:
00214           {"AT", N_( "Austria" ) },                             // :AUT:040:
00215           {"AU", N_( "Australia" ) },                           // :AUS:036:
00216           {"AW", N_( "Aruba" ) },                               // :ABW:533:
00217           {"AX", N_( "Aland Islands" ) },                       // :ALA:248:
00218           {"AZ", N_( "Azerbaijan" ) },                          // :AZE:031:
00219           {"BA", N_( "Bosnia and Herzegovina" ) },              // :BIH:070:
00220           {"BB", N_( "Barbados" ) },                            // :BRB:052:
00221           {"BD", N_( "Bangladesh" ) },                          // :BGD:050:
00222           {"BE", N_( "Belgium" ) },                             // :BEL:056:
00223           {"BF", N_( "Burkina Faso" ) },                        // :BFA:854:
00224           {"BG", N_( "Bulgaria" ) },                            // :BGR:100:
00225           {"BH", N_( "Bahrain" ) },                             // :BHR:048:
00226           {"BI", N_( "Burundi" ) },                             // :BDI:108:
00227           {"BJ", N_( "Benin" ) },                               // :BEN:204:
00228           {"BM", N_( "Bermuda" ) },                             // :BMU:060:
00229           {"BN", N_( "Brunei Darussalam" ) },                   // :BRN:096:
00230           {"BO", N_( "Bolivia" ) },                             // :BOL:068:
00231           {"BR", N_( "Brazil" ) },                              // :BRA:076:
00232           {"BS", N_( "Bahamas" ) },                             // :BHS:044:
00233           {"BT", N_( "Bhutan" ) },                              // :BTN:064:
00234           {"BV", N_( "Bouvet Island" ) },                       // :BVT:074:
00235           {"BW", N_( "Botswana" ) },                            // :BWA:072:
00236           {"BY", N_( "Belarus" ) },                             // :BLR:112:
00237           {"BZ", N_( "Belize" ) },                              // :BLZ:084:
00238           {"CA", N_( "Canada" ) },                              // :CAN:124:
00239           {"CC", N_( "Cocos (Keeling) Islands" ) },             // :CCK:166:
00240           {"CD", N_( "Congo" ) },                               // :COD:180:
00241           {"CF", N_( "Central African Republic" ) },            // :CAF:140:
00242           {"CG", N_( "Congo" ) },                               // :COG:178:
00243           {"CH", N_( "Switzerland" ) },                         // :CHE:756:
00244           {"CI", N_( "Cote D'Ivoire" ) },                       // :CIV:384:
00245           {"CK", N_( "Cook Islands" ) },                        // :COK:184:
00246           {"CL", N_( "Chile" ) },                               // :CHL:152:
00247           {"CM", N_( "Cameroon" ) },                            // :CMR:120:
00248           {"CN", N_( "China" ) },                               // :CHN:156:
00249           {"CO", N_( "Colombia" ) },                            // :COL:170:
00250           {"CR", N_( "Costa Rica" ) },                          // :CRI:188:
00251           {"CU", N_( "Cuba" ) },                                // :CUB:192:
00252           {"CV", N_( "Cape Verde" ) },                          // :CPV:132:
00253           {"CX", N_( "Christmas Island" ) },                    // :CXR:162:
00254           {"CY", N_( "Cyprus" ) },                              // :CYP:196:
00255           {"CZ", N_( "Czech Republic" ) },                      // :CZE:203:
00256           {"DE", N_( "Germany" ) },                             // :DEU:276:
00257           {"DJ", N_( "Djibouti" ) },                            // :DJI:262:
00258           {"DK", N_( "Denmark" ) },                             // :DNK:208:
00259           {"DM", N_( "Dominica" ) },                            // :DMA:212:
00260           {"DO", N_( "Dominican Republic" ) },                  // :DOM:214:
00261           {"DZ", N_( "Algeria" ) },                             // :DZA:012:
00262           {"EC", N_( "Ecuador" ) },                             // :ECU:218:
00263           {"EE", N_( "Estonia" ) },                             // :EST:233:
00264           {"EG", N_( "Egypt" ) },                               // :EGY:818:
00265           {"EH", N_( "Western Sahara" ) },                      // :ESH:732:
00266           {"ER", N_( "Eritrea" ) },                             // :ERI:232:
00267           {"ES", N_( "Spain" ) },                               // :ESP:724:
00268           {"ET", N_( "Ethiopia" ) },                            // :ETH:231:
00269           {"FI", N_( "Finland" ) },                             // :FIN:246:
00270           {"FJ", N_( "Fiji" ) },                                // :FJI:242:
00271           {"FK", N_( "Falkland Islands (Malvinas)" ) },         // :FLK:238:
00272           {"FM", N_( "Federated States of Micronesia" ) },      // :FSM:583:
00273           {"FO", N_( "Faroe Islands" ) },                       // :FRO:234:
00274           {"FR", N_( "France" ) },                              // :FRA:250:
00275           {"FX", N_( "Metropolitan France" ) },                 // :FXX:249:
00276           {"GA", N_( "Gabon" ) },                               // :GAB:266:
00277           {"GB", N_( "United Kingdom" ) },                      // :GBR:826:
00278           {"GD", N_( "Grenada" ) },                             // :GRD:308:
00279           {"GE", N_( "Georgia" ) },                             // :GEO:268:
00280           {"GF", N_( "French Guiana" ) },                       // :GUF:254:
00281           {"GG", N_( "Guernsey" ) },
00282           {"GH", N_( "Ghana" ) },                               // :GHA:288:
00283           {"GI", N_( "Gibraltar" ) },                           // :GIB:292:
00284           {"GL", N_( "Greenland" ) },                           // :GRL:304:
00285           {"GM", N_( "Gambia" ) },                              // :GMB:270:
00286           {"GN", N_( "Guinea" ) },                              // :GIN:324:
00287           {"GP", N_( "Guadeloupe" ) },                          // :GLP:312:
00288           {"GQ", N_( "Equatorial Guinea" ) },                   // :GNQ:226:
00289           {"GR", N_( "Greece" ) },                              // :GRC:300:
00290           {"GS", N_( "South Georgia and the South Sandwich Islands" ) },        // :SGS:239:
00291           {"GT", N_( "Guatemala" ) },                           // :GTM:320:
00292           {"GU", N_( "Guam" ) },                                // :GUM:316:
00293           {"GW", N_( "Guinea-Bissau" ) },                       // :GNB:624:
00294           {"GY", N_( "Guyana" ) },                              // :GUY:328:
00295           {"HK", N_( "Hong Kong" ) },                           // :HKG:344:
00296           {"HM", N_( "Heard Island and McDonald Islands" ) }, // :HMD:334:
00297           {"HN", N_( "Honduras" ) },                            // :HND:340:
00298           {"HR", N_( "Croatia" ) },                             // :HRV:191:
00299           {"HT", N_( "Haiti" ) },                               // :HTI:332:
00300           {"HU", N_( "Hungary" ) },                             // :HUN:348:
00301           {"ID", N_( "Indonesia" ) },                           // :IDN:360:
00302           {"IE", N_( "Ireland" ) },                             // :IRL:372:
00303           {"IL", N_( "Israel" ) },                              // :ISR:376:
00304           {"IM", N_( "Isle of Man" ) },
00305           {"IN", N_( "India" ) },                               // :IND:356:
00306           {"IO", N_( "British Indian Ocean Territory" ) },      // :IOT:086:
00307           {"IQ", N_( "Iraq" ) },                                // :IRQ:368:
00308           {"IR", N_( "Iran" ) },                                // :IRN:364:
00309           {"IS", N_( "Iceland" ) },                             // :ISL:352:
00310           {"IT", N_( "Italy" ) },                               // :ITA:380:
00311           {"JE", N_( "Jersey" ) },
00312           {"JM", N_( "Jamaica" ) },                             // :JAM:388:
00313           {"JO", N_( "Jordan" ) },                              // :JOR:400:
00314           {"JP", N_( "Japan" ) },                               // :JPN:392:
00315           {"KE", N_( "Kenya" ) },                               // :KEN:404:
00316           {"KG", N_( "Kyrgyzstan" ) },                          // :KGZ:417:
00317           {"KH", N_( "Cambodia" ) },                            // :KHM:116:
00318           {"KI", N_( "Kiribati" ) },                            // :KIR:296:
00319           {"KM", N_( "Comoros" ) },                             // :COM:174:
00320           {"KN", N_( "Saint Kitts and Nevis" ) },               // :KNA:659:
00321           {"KP", N_( "North Korea" ) },                         // :PRK:408:
00322           {"KR", N_( "South Korea" ) },                         // :KOR:410:
00323           {"KW", N_( "Kuwait" ) },                              // :KWT:414:
00324           {"KY", N_( "Cayman Islands" ) },                      // :CYM:136:
00325           {"KZ", N_( "Kazakhstan" ) },                          // :KAZ:398:
00326           {"LA", N_( "Lao People's Democratic Republic" ) },    // :LAO:418:
00327           {"LB", N_( "Lebanon" ) },                             // :LBN:422:
00328           {"LC", N_( "Saint Lucia" ) },                         // :LCA:662:
00329           {"LI", N_( "Liechtenstein" ) },                       // :LIE:438:
00330           {"LK", N_( "Sri Lanka" ) },                           // :LKA:144:
00331           {"LR", N_( "Liberia" ) },                             // :LBR:430:
00332           {"LS", N_( "Lesotho" ) },                             // :LSO:426:
00333           {"LT", N_( "Lithuania" ) },                           // :LTU:440:
00334           {"LU", N_( "Luxembourg" ) },                          // :LUX:442:
00335           {"LV", N_( "Latvia" ) },                              // :LVA:428:
00336           {"LY", N_( "Libya" ) },                               // :LBY:434:
00337           {"MA", N_( "Morocco" ) },                             // :MAR:504:
00338           {"MC", N_( "Monaco" ) },                              // :MCO:492:
00339           {"MD", N_( "Moldova" ) },                             // :MDA:498:
00340           {"ME", N_( "Montenegro" ) },
00341           {"MF", N_( "Saint Martin" ) },
00342           {"MG", N_( "Madagascar" ) },                          // :MDG:450:
00343           {"MH", N_( "Marshall Islands" ) },                    // :MHL:584:
00344           {"MK", N_( "Macedonia" ) },                           // :MKD:807:
00345           {"ML", N_( "Mali" ) },                                // :MLI:466:
00346           {"MM", N_( "Myanmar" ) },                             // :MMR:104:
00347           {"MN", N_( "Mongolia" ) },                            // :MNG:496:
00348           {"MO", N_( "Macao" ) },                               // :MAC:446:
00349           {"MP", N_( "Northern Mariana Islands" ) },            // :MNP:580:
00350           {"MQ", N_( "Martinique" ) },                          // :MTQ:474:
00351           {"MR", N_( "Mauritania" ) },                          // :MRT:478:
00352           {"MS", N_( "Montserrat" ) },                          // :MSR:500:
00353           {"MT", N_( "Malta" ) },                               // :MLT:470:
00354           {"MU", N_( "Mauritius" ) },                           // :MUS:480:
00355           {"MV", N_( "Maldives" ) },                            // :MDV:462:
00356           {"MW", N_( "Malawi" ) },                              // :MWI:454:
00357           {"MX", N_( "Mexico" ) },                              // :MEX:484:
00358           {"MY", N_( "Malaysia" ) },                            // :MYS:458:
00359           {"MZ", N_( "Mozambique" ) },                          // :MOZ:508:
00360           {"NA", N_( "Namibia" ) },                             // :NAM:516:
00361           {"NC", N_( "New Caledonia" ) },                       // :NCL:540:
00362           {"NE", N_( "Niger" ) },                               // :NER:562:
00363           {"NF", N_( "Norfolk Island" ) },                      // :NFK:574:
00364           {"NG", N_( "Nigeria" ) },                             // :NGA:566:
00365           {"NI", N_( "Nicaragua" ) },                           // :NIC:558:
00366           {"NL", N_( "Netherlands" ) },                         // :NLD:528:
00367           {"NO", N_( "Norway" ) },                              // :NOR:578:
00368           {"NP", N_( "Nepal" ) },                               // :NPL:524:
00369           {"NR", N_( "Nauru" ) },                               // :NRU:520:
00370           {"NU", N_( "Niue" ) },                                // :NIU:570:
00371           {"NZ", N_( "New Zealand" ) },                         // :NZL:554:
00372           {"OM", N_( "Oman" ) },                                // :OMN:512:
00373           {"PA", N_( "Panama" ) },                              // :PAN:591:
00374           {"PE", N_( "Peru" ) },                                // :PER:604:
00375           {"PF", N_( "French Polynesia" ) },                    // :PYF:258:
00376           {"PG", N_( "Papua New Guinea" ) },                    // :PNG:598:
00377           {"PH", N_( "Philippines" ) },                         // :PHL:608:
00378           {"PK", N_( "Pakistan" ) },                            // :PAK:586:
00379           {"PL", N_( "Poland" ) },                              // :POL:616:
00380           {"PM", N_( "Saint Pierre and Miquelon" ) },           // :SPM:666:
00381           {"PN", N_( "Pitcairn" ) },                            // :PCN:612:
00382           {"PR", N_( "Puerto Rico" ) },                         // :PRI:630:
00383           {"PS", N_( "Palestinian Territory" ) },               // :PSE:275:
00384           {"PT", N_( "Portugal" ) },                            // :PRT:620:
00385           {"PW", N_( "Palau" ) },                               // :PLW:585:
00386           {"PY", N_( "Paraguay" ) },                            // :PRY:600:
00387           {"QA", N_( "Qatar" ) },                               // :QAT:634:
00388           {"RE", N_( "Reunion" ) },                             // :REU:638:
00389           {"RO", N_( "Romania" ) },                             // :ROU:642:
00390           {"RS", N_( "Serbia" ) },
00391           {"RU", N_( "Russian Federation" ) },                  // :RUS:643:
00392           {"RW", N_( "Rwanda" ) },                              // :RWA:646:
00393           {"SA", N_( "Saudi Arabia" ) },                        // :SAU:682:
00394           {"SB", N_( "Solomon Islands" ) },                     // :SLB:090:
00395           {"SC", N_( "Seychelles" ) },                          // :SYC:690:
00396           {"SD", N_( "Sudan" ) },                               // :SDN:736:
00397           {"SE", N_( "Sweden" ) },                              // :SWE:752:
00398           {"SG", N_( "Singapore" ) },                           // :SGP:702:
00399           {"SH", N_( "Saint Helena" ) },                        // :SHN:654:
00400           {"SI", N_( "Slovenia" ) },                            // :SVN:705:
00401           {"SJ", N_( "Svalbard and Jan Mayen" ) },              // :SJM:744:
00402           {"SK", N_( "Slovakia" ) },                            // :SVK:703:
00403           {"SL", N_( "Sierra Leone" ) },                        // :SLE:694:
00404           {"SM", N_( "San Marino" ) },                          // :SMR:674:
00405           {"SN", N_( "Senegal" ) },                             // :SEN:686:
00406           {"SO", N_( "Somalia" ) },                             // :SOM:706:
00407           {"SR", N_( "Suriname" ) },                            // :SUR:740:
00408           {"ST", N_( "Sao Tome and Principe" ) },               // :STP:678:
00409           {"SV", N_( "El Salvador" ) },                         // :SLV:222:
00410           {"SY", N_( "Syria" ) },                               // :SYR:760:
00411           {"SZ", N_( "Swaziland" ) },                           // :SWZ:748:
00412           {"TC", N_( "Turks and Caicos Islands" ) },            // :TCA:796:
00413           {"TD", N_( "Chad" ) },                                // :TCD:148:
00414           {"TF", N_( "French Southern Territories" ) },         // :ATF:260:
00415           {"TG", N_( "Togo" ) },                                // :TGO:768:
00416           {"TH", N_( "Thailand" ) },                            // :THA:764:
00417           {"TJ", N_( "Tajikistan" ) },                          // :TJK:762:
00418           {"TK", N_( "Tokelau" ) },                             // :TKL:772:
00419           {"TM", N_( "Turkmenistan" ) },                        // :TKM:795:
00420           {"TN", N_( "Tunisia" ) },                             // :TUN:788:
00421           {"TO", N_( "Tonga" ) },                               // :TON:776:
00422           {"TL", N_( "East Timor" ) },                          // :TLS:626:
00423           {"TR", N_( "Turkey" ) },                              // :TUR:792:
00424           {"TT", N_( "Trinidad and Tobago" ) },                 // :TTO:780:
00425           {"TV", N_( "Tuvalu" ) },                              // :TUV:798:
00426           {"TW", N_( "Taiwan" ) },                              // :TWN:158:
00427           {"TZ", N_( "Tanzania" ) },                            // :TZA:834:
00428           {"UA", N_( "Ukraine" ) },                             // :UKR:804:
00429           {"UG", N_( "Uganda" ) },                              // :UGA:800:
00430           {"UM", N_( "United States Minor Outlying Islands" ) },// :UMI:581:
00431           {"US", N_( "United States" ) },                       // :USA:840:
00432           {"UY", N_( "Uruguay" ) },                             // :URY:858:
00433           {"UZ", N_( "Uzbekistan" ) },                          // :UZB:860:
00434           {"VA", N_( "Holy See (Vatican City State)" ) },       // :VAT:336:
00435           {"VC", N_( "Saint Vincent and the Grenadines" ) },    // :VCT:670:
00436           {"VE", N_( "Venezuela" ) },                           // :VEN:862:
00437           {"VG", N_( "British Virgin Islands" ) },              // :VGB:092:
00438           {"VI", N_( "Virgin Islands, U.S." ) },                // :VIR:850:
00439           {"VN", N_( "Vietnam" ) },                             // :VNM:704:
00440           {"VU", N_( "Vanuatu" ) },                             // :VUT:548:
00441           {"WF", N_( "Wallis and Futuna" ) },                   // :WLF:876:
00442           {"WS", N_( "Samoa" ) },                               // :WSM:882:
00443           {"YE", N_( "Yemen" ) },                               // :YEM:887:
00444           {"YT", N_( "Mayotte" ) },                             // :MYT:175:
00445           {"ZA", N_( "South Africa" ) },                        // :ZAF:710:
00446           {"ZM", N_( "Zambia" ) },                              // :ZMB:894:
00447           {"ZW", N_( "Zimbabwe" ) },                            // :ZWE:716:
00448 
00449           { NULL, NULL }
00450       };
00451 
00452       for (const Init * i = init; i->iso3166 != NULL; ++i)
00453           codes[i->iso3166] = i->name;
00454     }
00455 
00457   } // namespace
00459 
00461 } // namespace zypp