mirror of
https://gitee.com/openharmony/global_resource_management
synced 2024-11-23 07:59:51 +00:00
commit
f4c4c8066c
@ -109,8 +109,14 @@ public:
|
||||
static void CanonicalizePath(const char *path, char *outPath, size_t len);
|
||||
|
||||
static RState GetFiles(const std::string &strCurrentDir, std::vector<std::string> &vFiles);
|
||||
private:
|
||||
|
||||
static bool convertToInteger(const std::string& str, int& outValue);
|
||||
|
||||
static bool convertToUnsignedLong(const std::string& str, unsigned long& outValue);
|
||||
|
||||
static bool convertToDouble(const std::string& str, double& outValue);
|
||||
private:
|
||||
static bool IsValidValue(const char* end, const std::string& str);
|
||||
static uint16_t EncodeLanguageOrRegion(const char *str, char base);
|
||||
|
||||
static constexpr uint64_t ROOT_LOCALE = 0x0;
|
||||
|
@ -378,6 +378,7 @@ Locale *BuildFromString(const char *str, char sep, RState &rState)
|
||||
return nullptr;
|
||||
}
|
||||
Locale *retLocal = new Locale(temp);
|
||||
delete resLocale;
|
||||
return retLocal;
|
||||
}
|
||||
return nullptr;
|
||||
|
@ -783,11 +783,15 @@ RState ResourceManagerImpl::GetInteger(const std::shared_ptr<IdItem> idItem, int
|
||||
}
|
||||
std::string temp;
|
||||
RState state = ResolveReference(idItem->value_, temp);
|
||||
if (state == SUCCESS) {
|
||||
outValue = stoi(temp);
|
||||
if (state != SUCCESS) {
|
||||
return state;
|
||||
}
|
||||
int intValue;
|
||||
if (Utils::convertToInteger(temp, intValue)) {
|
||||
outValue = intValue;
|
||||
return SUCCESS;
|
||||
}
|
||||
return state;
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
RState ResourceManagerImpl::ProcessReference(const std::string value,
|
||||
@ -968,7 +972,11 @@ RState ResourceManagerImpl::GetIntArray(const std::shared_ptr<IdItem> idItem, st
|
||||
RESMGR_HILOGD(RESMGR_TAG, "ResolveReference failed, value:%{public}s", idItem->values_[i].c_str());
|
||||
return ERROR;
|
||||
}
|
||||
outValue.push_back(stoi(resolvedValue));
|
||||
int intValue;
|
||||
if (!Utils::convertToInteger(resolvedValue, intValue)) {
|
||||
return ERROR;
|
||||
}
|
||||
outValue.push_back(intValue);
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
*/
|
||||
|
||||
#include "utils/string_utils.h"
|
||||
|
||||
#include "utils/utils.h"
|
||||
#include <cctype>
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
@ -159,10 +159,13 @@ bool parseArgs(const std::string &inputOutputValue, va_list args,
|
||||
RESMGR_HILOGE(RESMGR_TAG, "index of placeholder is too large");
|
||||
return false;
|
||||
}
|
||||
if (std::stoul(placeholderIndex) == 0) {
|
||||
unsigned long index;
|
||||
if (!Utils::convertToUnsignedLong(placeholderIndex, index) || index < 1) {
|
||||
RESMGR_HILOGE(RESMGR_TAG, "convert value error, placeholderIndex = %{public}s",
|
||||
placeholderIndex.c_str());
|
||||
return false;
|
||||
}
|
||||
paramIndex = std::stoul(placeholderIndex) - 1;
|
||||
paramIndex = index - 1;
|
||||
paramsWithNum.push_back({paramIndex, placeholderType});
|
||||
} else {
|
||||
paramIndex = matchCount++;
|
||||
@ -173,23 +176,15 @@ bool parseArgs(const std::string &inputOutputValue, va_list args,
|
||||
return getJsParams(inputOutputValue, args, paramsWithOutNum, paramsWithNum, jsParams);
|
||||
}
|
||||
|
||||
bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig,
|
||||
const int32_t precision = INVALID_PRECISION)
|
||||
std::string GetLocalInfo(const ResLocale *resLocale)
|
||||
{
|
||||
#ifdef SUPPORT_GRAPHICS
|
||||
const ResLocale *resLocale = resConfig.GetResLocale();
|
||||
if (resLocale == nullptr) {
|
||||
RESMGR_HILOGW(RESMGR_TAG, "LocalizeNumber resLocale is null");
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string localeInfo;
|
||||
const char *language = resLocale->GetLanguage();
|
||||
if (language != nullptr && strlen(language) > 0) {
|
||||
localeInfo.assign(language);
|
||||
} else {
|
||||
RESMGR_HILOGW(RESMGR_TAG, "LocalizeNumber language is null");
|
||||
return true;
|
||||
RESMGR_HILOGW(RESMGR_TAG, "GetLocalInfo language is null");
|
||||
return localeInfo;
|
||||
}
|
||||
std::string temp;
|
||||
const char *script = resLocale->GetScript();
|
||||
@ -202,7 +197,23 @@ bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig,
|
||||
temp.assign(region);
|
||||
localeInfo += "-" + temp;
|
||||
}
|
||||
return localeInfo;
|
||||
}
|
||||
|
||||
bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig,
|
||||
const int32_t precision = INVALID_PRECISION)
|
||||
{
|
||||
#ifdef SUPPORT_GRAPHICS
|
||||
const ResLocale *resLocale = resConfig.GetResLocale();
|
||||
if (resLocale == nullptr) {
|
||||
RESMGR_HILOGW(RESMGR_TAG, "LocalizeNumber resLocale is null");
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string localeInfo = GetLocalInfo(resLocale);
|
||||
if (localeInfo.empty()) {
|
||||
return true;
|
||||
}
|
||||
icu::Locale locale(localeInfo.c_str());
|
||||
if (locale.isBogus()) {
|
||||
return true;
|
||||
@ -214,9 +225,12 @@ bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig,
|
||||
if (precision != INVALID_PRECISION) {
|
||||
numberFormat = numberFormat.precision(icu::number::Precision::fixedFraction(precision));
|
||||
}
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
double num = std::stod(inputOutputNum);
|
||||
double num;
|
||||
if (!Utils::convertToDouble(inputOutputNum, num)) {
|
||||
return false;
|
||||
}
|
||||
inputOutputNum.clear();
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
numberFormat.formatDouble(num, status).toString(status).toUTF8String(inputOutputNum);
|
||||
if (status == U_ZERO_ERROR) {
|
||||
RESMGR_HILOGE(RESMGR_TAG, "LocalizeNumber failed, status = %{public}d", status);
|
||||
@ -270,10 +284,11 @@ bool MatchPlaceholderIndex(std::string placeholderIndex, size_t ¶mIndex, siz
|
||||
RESMGR_HILOGE(RESMGR_TAG, "index of placeholder is too large");
|
||||
return false;
|
||||
}
|
||||
if (std::stoul(placeholderIndex) < 1) {
|
||||
unsigned long index;
|
||||
if (!Utils::convertToUnsignedLong(placeholderIndex, index) || index < 1) {
|
||||
return false;
|
||||
}
|
||||
paramIndex = std::stoul(placeholderIndex) - 1;
|
||||
paramIndex = index - 1;
|
||||
} else {
|
||||
paramIndex = matchCount++;
|
||||
}
|
||||
|
@ -14,6 +14,9 @@
|
||||
*/
|
||||
#include "utils/utils.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <sys/stat.h>
|
||||
@ -36,6 +39,7 @@ namespace OHOS {
|
||||
namespace Global {
|
||||
namespace Resource {
|
||||
constexpr int ERROR_RESULT = -1;
|
||||
constexpr int CONVERT_BASE = 10;
|
||||
|
||||
const std::set<std::string> Utils::tailSet {
|
||||
".hap",
|
||||
@ -258,7 +262,7 @@ uint32_t Utils::EncodeScriptByResLocale(const ResLocale *locale)
|
||||
|
||||
uint16_t Utils::EncodeRegion(const char *region)
|
||||
{
|
||||
if (Utils::IsStrEmpty(region)) {
|
||||
if (Utils::IsStrEmpty(region) || StrLen(region) < 1) {
|
||||
return NULL_REGION;
|
||||
}
|
||||
if (region[0] >= '0' && region[0] <= '9') {
|
||||
@ -279,7 +283,7 @@ uint16_t Utils::EncodeRegion(const char *region)
|
||||
*/
|
||||
uint32_t Utils::EncodeScript(const char *script)
|
||||
{
|
||||
if (Utils::IsStrEmpty(script)) {
|
||||
if (Utils::IsStrEmpty(script) || StrLen(script) < ArrayLen::LEN_FOUR) {
|
||||
return NULL_SCRIPT;
|
||||
}
|
||||
return ((uint8_t)script[ArrayIndex::INDEX_ZERO] << BitOperatorNum::BIT_TWENTY_FOUR) |
|
||||
@ -506,6 +510,51 @@ RState Utils::GetFiles(const std::string &strCurrentDir, std::vector<std::string
|
||||
#endif
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
bool Utils::IsValidValue(const char* end, const std::string& str)
|
||||
{
|
||||
if (end == str.c_str() || errno == ERANGE || *end != '\0') {
|
||||
RESMGR_HILOGE(RESMGR_TAG, "invalid value = %{public}s, errno = %{public}d", str.c_str(), errno);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utils::convertToInteger(const std::string& str, int& outValue)
|
||||
{
|
||||
char* end;
|
||||
errno = 0;
|
||||
long value = std::strtol(str.c_str(), &end, CONVERT_BASE);
|
||||
if (!IsValidValue(end, str)) {
|
||||
return false;
|
||||
}
|
||||
outValue = static_cast<int>(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utils::convertToUnsignedLong(const std::string& str, unsigned long& outValue)
|
||||
{
|
||||
char* end;
|
||||
errno = 0;
|
||||
unsigned long value = std::strtoul(str.c_str(), &end, CONVERT_BASE);
|
||||
if (!IsValidValue(end, str)) {
|
||||
return false;
|
||||
}
|
||||
outValue = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utils::convertToDouble(const std::string& str, double& outValue)
|
||||
{
|
||||
char* end;
|
||||
errno = 0;
|
||||
double value = std::strtod(str.c_str(), &end);
|
||||
if (!IsValidValue(end, str)) {
|
||||
return false;
|
||||
}
|
||||
outValue = value;
|
||||
return true;
|
||||
}
|
||||
} // namespace Resource
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
||||
|
@ -1020,6 +1020,10 @@ napi_value ResourceManagerNapiSyncImpl::AddResource(napi_env env, napi_callback_
|
||||
auto dataContext = std::make_unique<ResMgrDataContext>();
|
||||
dataContext->path_ = ResourceManagerNapiUtils::GetResNameOrPath(env, argc, argv);
|
||||
auto resMgr = GetNativeResoruceManager(env, info);
|
||||
if (resMgr == nullptr) {
|
||||
RESMGR_HILOGE(RESMGR_JS_TAG, "resMgr is null, add overlay path = %{public}s", dataContext->path_.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
if (!resMgr->AddAppOverlay(dataContext->path_)) {
|
||||
RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to add overlay path = %{public}s", dataContext->path_.c_str());
|
||||
ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_OVERLAY_RES_PATH_INVALID);
|
||||
@ -1038,8 +1042,12 @@ napi_value ResourceManagerNapiSyncImpl::RemoveResource(napi_env env, napi_callba
|
||||
auto dataContext = std::make_unique<ResMgrDataContext>();
|
||||
dataContext->path_ = ResourceManagerNapiUtils::GetResNameOrPath(env, argc, argv);
|
||||
auto resMgr = GetNativeResoruceManager(env, info);
|
||||
if (resMgr == nullptr) {
|
||||
RESMGR_HILOGE(RESMGR_JS_TAG, "resMgr is null, overlay path = %{public}s", dataContext->path_.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
if (!resMgr->RemoveAppOverlay(dataContext->path_)) {
|
||||
RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to add overlay path = %{public}s", dataContext->path_.c_str());
|
||||
RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to remove overlay path = %{public}s", dataContext->path_.c_str());
|
||||
ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_OVERLAY_RES_PATH_INVALID);
|
||||
return nullptr;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user