Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include <cctype>
11 : #include "log/adx_log.h"
12 : #include "string_utils.h"
13 : namespace Adx {
14 : /**
15 : * @brief Check if is digital
16 : * @param [in] digital: digital
17 : * @return
18 : * true: string all digital
19 : * false: string have other char
20 : */
21 22 : bool StringUtils::IsIntDigital(const std::string& digital)
22 : {
23 22 : if (digital.empty()) {
24 0 : return false;
25 : }
26 :
27 22 : size_t len = digital.length();
28 56 : for (size_t i = 0; i < len; i++) {
29 39 : if (!isdigit(digital[i])) {
30 5 : return false;
31 : }
32 : }
33 17 : return true;
34 : }
35 :
36 4 : bool StringUtils::IpValid(const std::string& ipStr)
37 : {
38 4 : if (ipStr.empty()) {
39 1 : return false;
40 : }
41 :
42 3 : std::string checkStr = ipStr;
43 : size_t num;
44 3 : size_t j = 0;
45 9 : for (size_t i = 0; i < IP_VALID_PART_NUM; ++i) {
46 7 : size_t index = checkStr.find('.', j);
47 7 : if (index == std::string::npos) {
48 0 : return false;
49 : }
50 : try {
51 7 : num = std::stoi(checkStr.substr(j, index - j));
52 0 : } catch (...) {
53 0 : return false;
54 0 : }
55 :
56 7 : if (num > IP_MAX_NUM) {
57 1 : return false;
58 : }
59 6 : j = index + 1;
60 : }
61 2 : std::string end = checkStr.substr(j);
62 4 : for (size_t i = 0; i < end.length(); ++i) {
63 3 : if (end[i] < '0' || end[i] > '9') {
64 1 : return false;
65 : }
66 : }
67 : try {
68 1 : num = stoi(end);
69 0 : } catch (...) {
70 0 : return false;
71 0 : }
72 1 : if (num > IP_MAX_NUM) {
73 0 : return false;
74 : }
75 1 : return true;
76 3 : }
77 :
78 14 : bool StringUtils::ParseConnectInfo(const std::string& connectInfo, std::string& hostId, std::string& hostPid)
79 : {
80 14 : std::string connectInfoStr;
81 : std::string::size_type idx;
82 :
83 14 : connectInfoStr = connectInfo;
84 :
85 14 : idx = connectInfoStr.find(";");
86 14 : if (idx == std::string::npos) {
87 2 : IDE_LOGE(
88 : "invalid private info %s format, valid format like \"host:port;host_id;host_pid\"", connectInfo.c_str());
89 2 : return false;
90 : }
91 12 : IDE_LOGD("info str check host:port;host_id success");
92 :
93 12 : std::string hostIdHostPidStr = connectInfoStr.substr(idx + 1);
94 12 : idx = hostIdHostPidStr.find(";");
95 12 : if (idx == std::string::npos) {
96 1 : IDE_LOGE(
97 : "invalid private info %s format, valid format like \"host:port;host_id;host_pid\"", connectInfo.c_str());
98 1 : return false;
99 : }
100 11 : IDE_LOGD("info str check host_id;host_pid success");
101 :
102 11 : hostId = hostIdHostPidStr.substr(0, idx);
103 11 : hostPid = hostIdHostPidStr.substr(idx + 1);
104 :
105 11 : if (!IsIntDigital(hostId)) {
106 2 : IDE_LOGE("hostId is not a number, %s", hostId.c_str());
107 2 : return false;
108 : }
109 :
110 9 : if (!IsIntDigital(hostPid)) {
111 2 : IDE_LOGE("hostPid is not a number, %s", hostPid.c_str());
112 2 : return false;
113 : }
114 :
115 7 : IDE_LOGI("hostId: %s, hostPid: %s", hostId.c_str(), hostPid.c_str());
116 7 : IDE_LOGD("info str check host_id and host_pid number format success");
117 7 : return true;
118 14 : }
119 : } // namespace Adx
|