diff --git a/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc b/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc index bcbce926388753faae4645ec446c1fb98a526411..b78ac99f6df41996a7c830fa7da7f39688f5fc5a 100644 --- a/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc +++ b/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc @@ -1,24 +1,37 @@ // Copyright (C) 2012 The Libphonenumber Authors -#include "phonenumbers/geocoding/phonenumber_offline_geocoder.h" -#include "phonenumbers/geocoding/geocoding_warpper.h" #include +#include "geocoding_warpper.h" +#include "phonenumbers/geocoding/phonenumber_offline_geocoder.h" +#include "phonenumbers/phonenumberutil.h" +#include #include - +#include #include // NOLINT(build/include_order) #include using icu::UnicodeString; -using std::string; using i18n::phonenumbers::PhoneNumber; +using i18n::phonenumbers::PhoneNumberUtil; using i18n::phonenumbers::PhoneNumberOfflineGeocoder; using icu::Locale; -const char* exposeLocationName(const PhoneNumber& number, const Locale& language) { +extern "C" void exposeLocationName(const char* pNumber, const char* locale, char* res) { if(offlineGeocoder == NULL) { offlineGeocoder = new PhoneNumberOfflineGeocoder(); } - const char* chars = offlineGeocoder->GetDescriptionForNumber(number, language).c_str(); - return chars; + if (util == NULL) { + util = PhoneNumberUtil::GetInstance(); + } + icu::Locale uLocale = icu::Locale::createFromName(locale); + i18n::phonenumbers::PhoneNumber phoneNumber; + std::string number = pNumber; + PhoneNumberUtil::ErrorType type = util->Parse(number, uLocale.getCountry(), &phoneNumber); + if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) { + std::string empty = ""; + std::strcpy(res, empty.c_str()); + } + std::string result = offlineGeocoder->GetDescriptionForNumber(phoneNumber, uLocale); + std::strcpy(res, result.c_str()); } diff --git a/cpp/src/phonenumbers/geocoding/geocoding_warpper.h b/cpp/src/phonenumbers/geocoding/geocoding_warpper.h index 06f81edeb500944838f24d8de38588afdd8f3d6d..ca5141fb4391932c7e4607ba45c1ce2120b27907 100644 --- a/cpp/src/phonenumbers/geocoding/geocoding_warpper.h +++ b/cpp/src/phonenumbers/geocoding/geocoding_warpper.h @@ -1,19 +1,12 @@ // Copyright (C) 2012 The Libphonenumber Authors -#ifdef __cplusplus -extern "C" { -#endif - #include - #include - - #include - using i18n::phonenumbers::PhoneNumber; - using i18n::phonenumbers::PhoneNumberOfflineGeocoder; - using icu::Locale; - - PhoneNumberOfflineGeocoder* offlineGeocoder = NULL; - - const char* exposeLocationName(const PhoneNumber& number, const Locale& language); -#ifdef __cplusplus -} -#endif +#include "phonenumbers/geocoding/phonenumber_offline_geocoder.h" +#include "phonenumbers/phonenumberutil.h" +#include +#include + +using i18n::phonenumbers::PhoneNumberOfflineGeocoder; + +PhoneNumberOfflineGeocoder* offlineGeocoder = NULL; +i18n::phonenumbers::PhoneNumberUtil* util = NULL; +extern "C" void exposeLocationName(const char* pNumber, const char* locale, char* res);