• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2012 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Author: Patrick Mezard
16 
17 #include "phonenumbers/geocoding/phonenumber_offline_geocoder.h"
18 
19 #include <algorithm>
20 #include <string>
21 
22 #include <unicode/unistr.h>  // NOLINT(build/include_order)
23 
24 #include "phonenumbers/geocoding/area_code_map.h"
25 #include "phonenumbers/geocoding/geocoding_data.h"
26 #include "phonenumbers/geocoding/mapping_file_provider.h"
27 #include "phonenumbers/phonenumberutil.h"
28 #include "phonenumbers/stl_util.h"
29 
30 namespace i18n {
31 namespace phonenumbers {
32 
33 using icu::UnicodeString;
34 using std::string;
35 
36 namespace {
37 
38 // Returns true if s1 comes strictly before s2 in lexicographic order.
IsLowerThan(const char * s1,const char * s2)39 bool IsLowerThan(const char* s1, const char* s2) {
40   return strcmp(s1, s2) < 0;
41 }
42 
43 }  // namespace
44 
PhoneNumberOfflineGeocoder()45 PhoneNumberOfflineGeocoder::PhoneNumberOfflineGeocoder() {
46   Init(get_country_calling_codes(), get_country_calling_codes_size(),
47        get_country_languages, get_prefix_language_code_pairs(),
48        get_prefix_language_code_pairs_size(), get_prefix_descriptions);
49 }
50 
PhoneNumberOfflineGeocoder(const int * country_calling_codes,int country_calling_codes_size,country_languages_getter get_country_languages,const char ** prefix_language_code_pairs,int prefix_language_code_pairs_size,prefix_descriptions_getter get_prefix_descriptions)51 PhoneNumberOfflineGeocoder::PhoneNumberOfflineGeocoder(
52     const int* country_calling_codes, int country_calling_codes_size,
53     country_languages_getter get_country_languages,
54     const char** prefix_language_code_pairs,
55     int prefix_language_code_pairs_size,
56     prefix_descriptions_getter get_prefix_descriptions) {
57   Init(country_calling_codes, country_calling_codes_size,
58        get_country_languages, prefix_language_code_pairs,
59        prefix_language_code_pairs_size, get_prefix_descriptions);
60 }
61 
Init(const int * country_calling_codes,int country_calling_codes_size,country_languages_getter get_country_languages,const char ** prefix_language_code_pairs,int prefix_language_code_pairs_size,prefix_descriptions_getter get_prefix_descriptions)62 void PhoneNumberOfflineGeocoder::Init(
63     const int* country_calling_codes, int country_calling_codes_size,
64     country_languages_getter get_country_languages,
65     const char** prefix_language_code_pairs,
66     int prefix_language_code_pairs_size,
67     prefix_descriptions_getter get_prefix_descriptions) {
68   phone_util_ = PhoneNumberUtil::GetInstance();
69   provider_.reset(new MappingFileProvider(country_calling_codes,
70                                           country_calling_codes_size,
71                                           get_country_languages));
72   prefix_language_code_pairs_ = prefix_language_code_pairs;
73   prefix_language_code_pairs_size_ = prefix_language_code_pairs_size;
74   get_prefix_descriptions_ = get_prefix_descriptions;
75 }
76 
~PhoneNumberOfflineGeocoder()77 PhoneNumberOfflineGeocoder::~PhoneNumberOfflineGeocoder() {
78   gtl::STLDeleteContainerPairSecondPointers(
79       available_maps_.begin(), available_maps_.end());
80 }
81 
GetPhonePrefixDescriptions(int prefix,const string & language,const string & script,const string & region) const82 const AreaCodeMap* PhoneNumberOfflineGeocoder::GetPhonePrefixDescriptions(
83     int prefix, const string& language, const string& script,
84     const string& region) const {
85   string filename;
86   provider_->GetFileName(prefix, language, script, region, &filename);
87   if (filename.empty()) {
88     return NULL;
89   }
90   AreaCodeMaps::const_iterator it = available_maps_.find(filename);
91   if (it == available_maps_.end()) {
92     it = LoadAreaCodeMapFromFile(filename);
93     if (it == available_maps_.end()) {
94       return NULL;
95     }
96   }
97   return it->second;
98 }
99 
100 PhoneNumberOfflineGeocoder::AreaCodeMaps::const_iterator
LoadAreaCodeMapFromFile(const string & filename) const101 PhoneNumberOfflineGeocoder::LoadAreaCodeMapFromFile(
102     const string& filename) const {
103   const char** const prefix_language_code_pairs_end =
104       prefix_language_code_pairs_ + prefix_language_code_pairs_size_;
105   const char** const prefix_language_code_pair =
106       std::lower_bound(prefix_language_code_pairs_,
107                        prefix_language_code_pairs_end,
108                        filename.c_str(), IsLowerThan);
109   if (prefix_language_code_pair != prefix_language_code_pairs_end &&
110       filename.compare(*prefix_language_code_pair) == 0) {
111     AreaCodeMap* const m = new AreaCodeMap();
112     m->ReadAreaCodeMap(get_prefix_descriptions_(
113             prefix_language_code_pair - prefix_language_code_pairs_));
114     return available_maps_.insert(AreaCodeMaps::value_type(filename, m)).first;
115   }
116   return available_maps_.end();
117 }
118 
GetCountryNameForNumber(const PhoneNumber & number,const Locale & language) const119 string PhoneNumberOfflineGeocoder::GetCountryNameForNumber(
120     const PhoneNumber& number, const Locale& language) const {
121   string region_code;
122   phone_util_->GetRegionCodeForNumber(number, &region_code);
123   return GetRegionDisplayName(&region_code, language);
124 }
125 
GetRegionDisplayName(const string * region_code,const Locale & language) const126 string PhoneNumberOfflineGeocoder::GetRegionDisplayName(
127     const string* region_code, const Locale& language) const {
128   if (region_code == NULL || region_code->compare("ZZ") == 0 ||
129       region_code->compare(
130          PhoneNumberUtil::kRegionCodeForNonGeoEntity) == 0) {
131     return "";
132   }
133   UnicodeString udisplay_country;
134   icu::Locale("", region_code->c_str()).getDisplayCountry(
135       language, udisplay_country);
136   string display_country;
137   udisplay_country.toUTF8String(display_country);
138   return display_country;
139 }
140 
GetDescriptionForValidNumber(const PhoneNumber & number,const Locale & language) const141 string PhoneNumberOfflineGeocoder::GetDescriptionForValidNumber(
142     const PhoneNumber& number, const Locale& language) const {
143   const char* const description = GetAreaDescription(
144       number, language.getLanguage(), "", language.getCountry());
145   return *description != '\0'
146         ? description
147         : GetCountryNameForNumber(number, language);
148 }
149 
GetDescriptionForValidNumber(const PhoneNumber & number,const Locale & language,const string & user_region) const150 string PhoneNumberOfflineGeocoder::GetDescriptionForValidNumber(
151     const PhoneNumber& number, const Locale& language,
152     const string& user_region) const {
153   // If the user region matches the number's region, then we just show the
154   // lower-level description, if one exists - if no description exists, we will
155   // show the region(country) name for the number.
156   string region_code;
157   phone_util_->GetRegionCodeForNumber(number, &region_code);
158   if (user_region.compare(region_code) == 0) {
159     return GetDescriptionForValidNumber(number, language);
160   }
161   // Otherwise, we just show the region(country) name for now.
162   return GetRegionDisplayName(&region_code, language);
163 }
164 
GetDescriptionForNumber(const PhoneNumber & number,const Locale & locale) const165 string PhoneNumberOfflineGeocoder::GetDescriptionForNumber(
166     const PhoneNumber& number, const Locale& locale) const {
167   PhoneNumberUtil::PhoneNumberType number_type =
168       phone_util_->GetNumberType(number);
169   if (number_type == PhoneNumberUtil::UNKNOWN) {
170     return "";
171   } else if (!phone_util_->IsNumberGeographical(number_type,
172                                                 number.country_code())) {
173     return GetCountryNameForNumber(number, locale);
174   }
175   return GetDescriptionForValidNumber(number, locale);
176 }
177 
GetDescriptionForNumber(const PhoneNumber & number,const Locale & language,const string & user_region) const178 string PhoneNumberOfflineGeocoder::GetDescriptionForNumber(
179     const PhoneNumber& number, const Locale& language,
180     const string& user_region) const {
181   PhoneNumberUtil::PhoneNumberType number_type =
182       phone_util_->GetNumberType(number);
183   if (number_type == PhoneNumberUtil::UNKNOWN) {
184     return "";
185   } else if (!phone_util_->IsNumberGeographical(number_type,
186                                                 number.country_code())) {
187     return GetCountryNameForNumber(number, language);
188   }
189   return GetDescriptionForValidNumber(number, language, user_region);
190 }
191 
GetAreaDescription(const PhoneNumber & number,const string & lang,const string & script,const string & region) const192 const char* PhoneNumberOfflineGeocoder::GetAreaDescription(
193     const PhoneNumber& number, const string& lang, const string& script,
194     const string& region) const {
195   const int country_calling_code = number.country_code();
196   // NANPA area is not split in C++ code.
197   const int phone_prefix = country_calling_code;
198   const AreaCodeMap* const descriptions = GetPhonePrefixDescriptions(
199       phone_prefix, lang, script, region);
200   const char* description = descriptions ? descriptions->Lookup(number) : NULL;
201   // When a location is not available in the requested language, fall back to
202   // English.
203   if ((!description || *description == '\0') && MayFallBackToEnglish(lang)) {
204     const AreaCodeMap* default_descriptions = GetPhonePrefixDescriptions(
205         phone_prefix, "en", "", "");
206     if (!default_descriptions) {
207       return "";
208     }
209     description = default_descriptions->Lookup(number);
210   }
211   return description ? description : "";
212 }
213 
214 // Don't fall back to English if the requested language is among the following:
215 // - Chinese
216 // - Japanese
217 // - Korean
MayFallBackToEnglish(const string & lang) const218 bool PhoneNumberOfflineGeocoder::MayFallBackToEnglish(
219     const string& lang) const {
220   return lang.compare("zh") && lang.compare("ja") && lang.compare("ko");
221 }
222 
223 }  // namespace phonenumbers
224 }  // namespace i18n
225