1 // Copyright (C) 2012 The Libphonenumber Authors
2
3 #include <algorithm>
4 #include "geocoding_warpper.h"
5 #include "phonenumbers/geocoding/phonenumber_offline_geocoder.h"
6 #include "phonenumbers/ohos/update_libgeocoding.h"
7 #include "phonenumbers/phonenumberutil.h"
8 #include <iostream>
9 #include <string>
10 #include <stdlib.h>
11 #include <unicode/unistr.h> // NOLINT(build/include_order)
12 #include <unicode/locid.h>
13 #include "securec.h"
14
15 using icu::UnicodeString;
16 using i18n::phonenumbers::PhoneNumber;
17 using i18n::phonenumbers::PhoneNumberUtil;
18 using i18n::phonenumbers::PhoneNumberOfflineGeocoder;
19 using icu::Locale;
20
exposeLocationName(const char * pNumber,const char * locale,char * res,const int resLength=128)21 extern "C" int exposeLocationName(const char* pNumber, const char* locale, char* res, const int resLength = 128) {
22 i18n::phonenumbers::UpdateLibgeocoding::LoadUpdateData();
23 if(offlineGeocoder == NULL) {
24 offlineGeocoder = new PhoneNumberOfflineGeocoder();
25 }
26 if (util == NULL) {
27 util = PhoneNumberUtil::GetInstance();
28 }
29 icu::Locale uLocale = icu::Locale::createFromName(locale);
30 i18n::phonenumbers::PhoneNumber phoneNumber;
31 std::string number = pNumber;
32 PhoneNumberUtil::ErrorType type = util->Parse(number, uLocale.getCountry(), &phoneNumber);
33 if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
34 std::string empty = "";
35 return strcpy_s(res, resLength, empty.c_str());
36 }
37 std::string result = offlineGeocoder->GetDescriptionForNumber(phoneNumber, uLocale);
38 return strcpy_s(res, resLength, result.c_str());
39 }
40
41