1f7a47986Sopenharmony_ci/*
2f7a47986Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f7a47986Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f7a47986Sopenharmony_ci * you may not use this file except in compliance with the License.
5f7a47986Sopenharmony_ci * You may obtain a copy of the License at
6f7a47986Sopenharmony_ci *
7f7a47986Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f7a47986Sopenharmony_ci *
9f7a47986Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f7a47986Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f7a47986Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f7a47986Sopenharmony_ci * See the License for the specific language governing permissions and
13f7a47986Sopenharmony_ci * limitations under the License.
14f7a47986Sopenharmony_ci */
15f7a47986Sopenharmony_ci
16f7a47986Sopenharmony_ci#include "edm_utils.h"
17f7a47986Sopenharmony_ci
18f7a47986Sopenharmony_ci#include <codecvt>
19f7a47986Sopenharmony_ci#include "edm_constants.h"
20f7a47986Sopenharmony_ci#include "edm_log.h"
21f7a47986Sopenharmony_ci#include "securec.h"
22f7a47986Sopenharmony_ci
23f7a47986Sopenharmony_cinamespace OHOS {
24f7a47986Sopenharmony_cinamespace EDM {
25f7a47986Sopenharmony_ciconst std::string ERROR_STRING = "error";
26f7a47986Sopenharmony_ciconst std::u16string ERROR_USTRING = u"error";
27f7a47986Sopenharmony_ci
28f7a47986Sopenharmony_ciErrCode EdmUtils::ParseStringToInt(const std::string &str, int32_t &result)
29f7a47986Sopenharmony_ci{
30f7a47986Sopenharmony_ci    int64_t tmp;
31f7a47986Sopenharmony_ci    int32_t ret = EdmUtils::ParseStringToLong(str, tmp);
32f7a47986Sopenharmony_ci    result = tmp;
33f7a47986Sopenharmony_ci    return ret;
34f7a47986Sopenharmony_ci}
35f7a47986Sopenharmony_ci
36f7a47986Sopenharmony_ciErrCode EdmUtils::ParseStringToLong(const std::string &str, int64_t &result)
37f7a47986Sopenharmony_ci{
38f7a47986Sopenharmony_ci    char* end = nullptr;
39f7a47986Sopenharmony_ci    const char* p = str.c_str();
40f7a47986Sopenharmony_ci    errno = 0;
41f7a47986Sopenharmony_ci    result = strtol(p, &end, EdmConstants::DECIMAL);
42f7a47986Sopenharmony_ci    if (errno == ERANGE || end ==p || *end != '\0') {
43f7a47986Sopenharmony_ci        EDMLOGE("EdmUtils ParseStringToLong: parse str failed: %{public}s", p);
44f7a47986Sopenharmony_ci        return EdmReturnErrCode::PARAM_ERROR;
45f7a47986Sopenharmony_ci    }
46f7a47986Sopenharmony_ci    return ERR_OK;
47f7a47986Sopenharmony_ci}
48f7a47986Sopenharmony_ci
49f7a47986Sopenharmony_cistd::string EdmUtils::Utf16ToUtf8(const std::u16string &str16)
50f7a47986Sopenharmony_ci{
51f7a47986Sopenharmony_ci    if (str16 == ERROR_USTRING) {
52f7a47986Sopenharmony_ci        return ERROR_STRING;
53f7a47986Sopenharmony_ci    }
54f7a47986Sopenharmony_ci    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert(ERROR_STRING);
55f7a47986Sopenharmony_ci    std::string result = convert.to_bytes(str16);
56f7a47986Sopenharmony_ci    return result == ERROR_STRING ? "" : result;
57f7a47986Sopenharmony_ci}
58f7a47986Sopenharmony_ci
59f7a47986Sopenharmony_civoid EdmUtils::ClearString(std::string &str)
60f7a47986Sopenharmony_ci{
61f7a47986Sopenharmony_ci    std::fill(str.begin(), str.end(), '\0');
62f7a47986Sopenharmony_ci    str.clear();
63f7a47986Sopenharmony_ci}
64f7a47986Sopenharmony_ci
65f7a47986Sopenharmony_civoid EdmUtils::ClearCharArray(char* &str, size_t size)
66f7a47986Sopenharmony_ci{
67f7a47986Sopenharmony_ci    if (str != nullptr) {
68f7a47986Sopenharmony_ci        memset_s(str, size, '\0', size);
69f7a47986Sopenharmony_ci        free(str);
70f7a47986Sopenharmony_ci        str = nullptr;
71f7a47986Sopenharmony_ci    }
72f7a47986Sopenharmony_ci}
73f7a47986Sopenharmony_ci} // namespace EDM
74f7a47986Sopenharmony_ci} // namespace OHOS