summaryrefslogtreecommitdiff
path: root/geocoding/phonenumber_offline_geocoder.cc
blob: 9eac002cee0952b5176e5de2820183eb3d7c8064 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (C) 2012 The Libphonenumber Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Author: Patrick Mezard

#include "phonenumbers/geocoding/phonenumber_offline_geocoder.h"

#include <algorithm>
#include <map>
#include <string>

#include <unicode/unistr.h>  // NOLINT(build/include_order)

#include "phonenumbers/geocoding/area_code_map.h"
#include "phonenumbers/geocoding/geocoding_data.h"
#include "phonenumbers/geocoding/mapping_file_provider.h"
#include "phonenumbers/phonenumberutil.h"
#include "phonenumbers/stl_util.h"

namespace i18n {
namespace phonenumbers {

using icu::UnicodeString;
using std::map;
using std::string;

namespace {

// Returns true if s1 comes strictly before s2 in lexicographic order.
bool IsLowerThan(const char* s1, const char* s2) {
  return strcmp(s1, s2) < 0;
}

}  // namespace

PhoneNumberOfflineGeocoder::PhoneNumberOfflineGeocoder() {
  Init(get_country_calling_codes(), get_country_calling_codes_size(),
       get_country_languages, get_prefix_language_code_pairs(),
       get_prefix_language_code_pairs_size(), get_prefix_descriptions);
}

PhoneNumberOfflineGeocoder::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) {
  Init(country_calling_codes, country_calling_codes_size,
       get_country_languages, prefix_language_code_pairs,
       prefix_language_code_pairs_size, get_prefix_descriptions);
}

void PhoneNumberOfflineGeocoder::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) {
  phone_util_ = PhoneNumberUtil::GetInstance();
  provider_.reset(new MappingFileProvider(country_calling_codes,
                                          country_calling_codes_size,
                                          get_country_languages));
  prefix_language_code_pairs_ = prefix_language_code_pairs;
  prefix_language_code_pairs_size_ = prefix_language_code_pairs_size;
  get_prefix_descriptions_ = get_prefix_descriptions;
}

PhoneNumberOfflineGeocoder::~PhoneNumberOfflineGeocoder() {
  STLDeleteContainerPairSecondPointers(
      available_maps_.begin(), available_maps_.end());
}

const AreaCodeMap* PhoneNumberOfflineGeocoder::GetPhonePrefixDescriptions(
    int prefix, const string& language, const string& script,
    const string& region) const {
  string filename;
  provider_->GetFileName(prefix, language, script, region, &filename);
  if (filename.empty()) {
    return NULL;
  }
  AreaCodeMaps::const_iterator it = available_maps_.find(filename);
  if (it == available_maps_.end()) {
    it = LoadAreaCodeMapFromFile(filename);
    if (it == available_maps_.end()) {
      return NULL;
    }
  }
  return it->second;
}

PhoneNumberOfflineGeocoder::AreaCodeMaps::const_iterator
PhoneNumberOfflineGeocoder::LoadAreaCodeMapFromFile(
    const string& filename) const {
  const char** const prefix_language_code_pairs_end =
      prefix_language_code_pairs_ + prefix_language_code_pairs_size_;
  const char** const prefix_language_code_pair =
      std::lower_bound(prefix_language_code_pairs_,
                       prefix_language_code_pairs_end,
                       filename.c_str(), IsLowerThan);
  if (prefix_language_code_pair != prefix_language_code_pairs_end &&
      filename.compare(*prefix_language_code_pair) == 0) {
    AreaCodeMap* const m = new AreaCodeMap();
    m->ReadAreaCodeMap(get_prefix_descriptions_(
            prefix_language_code_pair - prefix_language_code_pairs_));
    return available_maps_.insert(AreaCodeMaps::value_type(filename, m)).first;
  }
  return available_maps_.end();
}

string PhoneNumberOfflineGeocoder::GetCountryNameForNumber(
    const PhoneNumber& number, const Locale& language) const {
  string region_code;
  phone_util_->GetRegionCodeForNumber(number, &region_code);
  return GetRegionDisplayName(&region_code, language);
}

string PhoneNumberOfflineGeocoder::GetRegionDisplayName(
    const string* region_code, const Locale& language) const {
  if (region_code == NULL || region_code->compare("ZZ") == 0 ||
      region_code->compare(
         PhoneNumberUtil::kRegionCodeForNonGeoEntity) == 0) {
    return "";
  }
  UnicodeString udisplay_country;
  icu::Locale("", region_code->c_str()).getDisplayCountry(
      language, udisplay_country);
  string display_country;
  udisplay_country.toUTF8String(display_country);
  return display_country;
}

string PhoneNumberOfflineGeocoder::GetDescriptionForValidNumber(
    const PhoneNumber& number, const Locale& language) const {
  const char* const description = GetAreaDescription(
      number, language.getLanguage(), "", language.getCountry());
  return *description != '\0'
        ? description
        : GetCountryNameForNumber(number, language);
}

string PhoneNumberOfflineGeocoder::GetDescriptionForValidNumber(
    const PhoneNumber& number, const Locale& language,
    const string& user_region) const {
  // If the user region matches the number's region, then we just show the
  // lower-level description, if one exists - if no description exists, we will
  // show the region(country) name for the number.
  string region_code;
  phone_util_->GetRegionCodeForNumber(number, &region_code);
  if (user_region.compare(region_code) == 0) {
    return GetDescriptionForValidNumber(number, language);
  }
  // Otherwise, we just show the region(country) name for now.
  return GetRegionDisplayName(&region_code, language);
}

string PhoneNumberOfflineGeocoder::GetDescriptionForNumber(
    const PhoneNumber& number, const Locale& locale) const {
  if (!phone_util_->IsValidNumber(number)) {
    return "";
  }
  return GetDescriptionForValidNumber(number, locale);
}

string PhoneNumberOfflineGeocoder::GetDescriptionForNumber(
    const PhoneNumber& number, const Locale& language,
    const string& user_region) const {
  if (!phone_util_->IsValidNumber(number)) {
    return "";
  }
  return GetDescriptionForValidNumber(number, language, user_region);
}

const char* PhoneNumberOfflineGeocoder::GetAreaDescription(
    const PhoneNumber& number, const string& lang, const string& script,
    const string& region) const {
  const int country_calling_code = number.country_code();
  // NANPA area is not split in C++ code.
  const int phone_prefix = country_calling_code;
  const AreaCodeMap* const descriptions = GetPhonePrefixDescriptions(
      phone_prefix, lang, script, region);
  const char* description = descriptions ? descriptions->Lookup(number) : NULL;
  // When a location is not available in the requested language, fall back to
  // English.
  if ((!description || *description == '\0') && MayFallBackToEnglish(lang)) {
    const AreaCodeMap* default_descriptions = GetPhonePrefixDescriptions(
        phone_prefix, "en", "", "");
    if (!default_descriptions) {
      return "";
    }
    description = default_descriptions->Lookup(number);
  }
  return description ? description : "";
}

// Don't fall back to English if the requested language is among the following:
// - Chinese
// - Japanese
// - Korean
bool PhoneNumberOfflineGeocoder::MayFallBackToEnglish(
    const string& lang) const {
  return lang.compare("zh") && lang.compare("ja") && lang.compare("ko");
}

}  // namespace phonenumbers
}  // namespace i18n