• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "edm_utils.h"
17 
18 #include "edm_constants.h"
19 #include "edm_log.h"
20 
21 namespace OHOS {
22 namespace EDM {
ParseStringToInt(const std::string & str,int32_t & result)23 ErrCode EdmUtils::ParseStringToInt(const std::string &str, int32_t &result)
24 {
25     int64_t tmp;
26     int32_t ret = EdmUtils::ParseStringToLong(str, tmp);
27     result = tmp;
28     return ret;
29 }
30 
ParseStringToLong(const std::string & str,int64_t & result)31 ErrCode EdmUtils::ParseStringToLong(const std::string &str, int64_t &result)
32 {
33     char* end = nullptr;
34     const char* p = str.c_str();
35     errno = 0;
36     result = strtol(p, &end, EdmConstants::DECIMAL);
37     if (errno == ERANGE || end ==p || *end != '\0') {
38         EDMLOGE("EdmUtils ParseStringToLong: parse str failed: %{public}s", p);
39         return EdmReturnErrCode::PARAM_ERROR;
40     }
41     return ERR_OK;
42 }
43 } // namespace EDM
44 } // namespace OHOS