1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 #ifndef INCLUDE_BASE_STRING_TO_NUMERICAL_H
17 #define INCLUDE_BASE_STRING_TO_NUMERICAL_H
18 
19 #include <iomanip>
20 #include <iostream>
21 #include <optional>
22 #include <sstream>
23 #include <string>
24 #include <typeinfo>
25 #include "log.h"
26 namespace SysTuning {
27 namespace base {
28 enum IntegerRadixType { INTEGER_RADIX_TYPE_DEC = 10, INTEGER_RADIX_TYPE_HEX = 16 };
number(uint64_t value, int32_t base = INTEGER_RADIX_TYPE_DEC)29 inline std::string number(uint64_t value, int32_t base = INTEGER_RADIX_TYPE_DEC)
30 {
31     std::stringstream ss;
32     if (base == INTEGER_RADIX_TYPE_DEC) {
33         ss << std::dec << value;
34     } else if (base == INTEGER_RADIX_TYPE_HEX) {
35         ss << std::hex << value;
36     }
37     return ss.str();
38 }
39 
ConvertTimestampToSecStr(uint64_t timestamp, uint8_t precision)40 inline std::string ConvertTimestampToSecStr(uint64_t timestamp, uint8_t precision)
41 {
42     double seconds = static_cast<double>(timestamp) / 1e9;
43     std::stringstream ss;
44     ss << std::fixed << std::setprecision(precision) << seconds;
45     return ss.str();
46 }
47 
48 template <typename T>
StrToInt(const std::string &str, int32_t base = INTEGER_RADIX_TYPE_DEC)49 std::optional<T> StrToInt(const std::string &str, int32_t base = INTEGER_RADIX_TYPE_DEC)
50 {
51     if (!str.empty()) {
52         char *endPtr = nullptr;
53         T value;
54         if constexpr (std::is_same_v<T, uint32_t>) {
55             value = static_cast<T>(std::strtoul(str.c_str(), &endPtr, base));
56         } else if constexpr (std::is_same_v<T, int32_t>) {
57             value = static_cast<T>(std::strtol(str.c_str(), &endPtr, base));
58         } else if constexpr (std::is_same_v<T, uint64_t>) {
59             value = static_cast<T>(std::strtoull(str.c_str(), &endPtr, base));
60         } else if constexpr (std::is_same_v<T, int64_t>) {
61             value = static_cast<T>(std::strtoll(str.c_str(), &endPtr, base));
62         }
63         if (!*endPtr) {
64             return std::make_optional(value);
65         }
66     }
67     TS_LOGD("Illegal value: %s", str.c_str());
68     return std::nullopt;
69 }
70 
StrToDouble(const std::string &str)71 inline std::optional<double> StrToDouble(const std::string &str)
72 {
73     if (!str.empty()) {
74 #ifdef WIN32_
75         char *end = nullptr;
76         double value = std::strtod(str.c_str(), &end);
77 #else
78         double value = std::stod(str);
79 #endif
80         return std::make_optional(value);
81     }
82     return std::nullopt;
83 }
84 } // namespace base
85 } // namespace SysTuning
86 
87 #endif // INCLUDE_TUNING_EXT_BASE_STRING_UTILS_H_
88