• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "js_common_utils.h"
17 #include <cmath>
18 #include <media_errors.h>
19 #include <sstream>
20 #include <climits>
21 #include "media_log.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace {
26     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "JsCommonUtils"};
27     constexpr int32_t DECIMAL = 10;
28 } //namespace
29 
StrToULL(const std::string & str,uint64_t & value)30 bool __attribute__((visibility("default"))) StrToULL(const std::string &str, uint64_t &value)
31 {
32     CHECK_AND_RETURN_RET(!str.empty() && (isdigit(str.front())), false);
33     std::string valStr(str);
34     char* end = nullptr;
35     errno = 0;
36     unsigned long long result = strtoull(valStr.c_str(), &end, DECIMAL);
37     // end will not be nullptr here
38     CHECK_AND_RETURN_RET_LOG(result <= ULLONG_MAX, false,
39         "call StrToULL func false,  input str is: %{public}s!", valStr.c_str());
40     CHECK_AND_RETURN_RET_LOG(end != valStr.c_str() && end[0] == '\0' && errno != ERANGE, false,
41         "call StrToULL func false,  input str is: %{public}s!", valStr.c_str());
42     value = result;
43     return true;
44 }
45 } // namespace Media
46 } // namespace OHOS