From c076d73bd948c6b5f733f17c12fe43c4626e85c3 Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Tue, 26 Jul 2022 11:10:32 +0800 Subject: [PATCH 1/2] update files Signed-off-by: zhangdd_ewan Change-Id: I3d5ef7d988c36fa65c079bbc408d38549a08cc1b --- .../geocoding/geocoding_warpper.cc | 22 +++++++++++---- .../geocoding/geocoding_warpper.h | 27 +++++++------------ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc b/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc index bcbce92..e5eb98e 100644 --- a/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc +++ b/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc @@ -1,24 +1,36 @@ // 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 // 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" const char* exposeLocationName(const char* pNumber, const char* locale) { if(offlineGeocoder == NULL) { offlineGeocoder = new PhoneNumberOfflineGeocoder(); } - const char* chars = offlineGeocoder->GetDescriptionForNumber(number, language).c_str(); + if (util == NULL) { + util = PhoneNumberUtil::GetInstance(); + } + //const char *l_name = locale.c_str(); + 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) { + return ""; + } + const char* chars = offlineGeocoder->GetDescriptionForNumber(phoneNumber, uLocale).c_str(); return chars; } diff --git a/cpp/src/phonenumbers/geocoding/geocoding_warpper.h b/cpp/src/phonenumbers/geocoding/geocoding_warpper.h index 06f81ed..ca60ebf 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" const char* exposeLocationName(const char* pNumber, const char* locale); -- Gitee From f9ea326820067e54421e53fc26cbd990da4266ba Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Tue, 26 Jul 2022 20:01:15 +0800 Subject: [PATCH 2/2] update function Signed-off-by: zhangdd_ewan Change-Id: I2fdc17f57c5b20a494b9b93c4fbb88a33fbe89cc --- cpp/src/phonenumbers/geocoding/geocoding_warpper.cc | 13 +++++++------ cpp/src/phonenumbers/geocoding/geocoding_warpper.h | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc b/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc index e5eb98e..b78ac99 100644 --- a/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc +++ b/cpp/src/phonenumbers/geocoding/geocoding_warpper.cc @@ -4,8 +4,9 @@ #include "geocoding_warpper.h" #include "phonenumbers/geocoding/phonenumber_offline_geocoder.h" #include "phonenumbers/phonenumberutil.h" +#include #include - +#include #include // NOLINT(build/include_order) #include @@ -15,22 +16,22 @@ using i18n::phonenumbers::PhoneNumberUtil; using i18n::phonenumbers::PhoneNumberOfflineGeocoder; using icu::Locale; -extern "C" const char* exposeLocationName(const char* pNumber, const char* locale) { +extern "C" void exposeLocationName(const char* pNumber, const char* locale, char* res) { if(offlineGeocoder == NULL) { offlineGeocoder = new PhoneNumberOfflineGeocoder(); } if (util == NULL) { util = PhoneNumberUtil::GetInstance(); } - //const char *l_name = locale.c_str(); 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) { - return ""; + std::string empty = ""; + std::strcpy(res, empty.c_str()); } - const char* chars = offlineGeocoder->GetDescriptionForNumber(phoneNumber, uLocale).c_str(); - return chars; + 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 ca60ebf..ca5141f 100644 --- a/cpp/src/phonenumbers/geocoding/geocoding_warpper.h +++ b/cpp/src/phonenumbers/geocoding/geocoding_warpper.h @@ -9,4 +9,4 @@ using i18n::phonenumbers::PhoneNumberOfflineGeocoder; PhoneNumberOfflineGeocoder* offlineGeocoder = NULL; i18n::phonenumbers::PhoneNumberUtil* util = NULL; -extern "C" const char* exposeLocationName(const char* pNumber, const char* locale); +extern "C" void exposeLocationName(const char* pNumber, const char* locale, char* res); -- Gitee