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("invalid private info %s format, valid format like \"host:port;host_id;host_pid\"", connectInfo.c_str());
88 2 : return false;
89 : }
90 12 : IDE_LOGD("info str check host:port;host_id success");
91 :
92 12 : std::string hostIdHostPidStr = connectInfoStr.substr(idx + 1);
93 12 : idx = hostIdHostPidStr.find(";");
94 12 : if (idx == std::string::npos) {
95 1 : IDE_LOGE("invalid private info %s format, valid format like \"host:port;host_id;host_pid\"", connectInfo.c_str());
96 1 : return false;
97 : }
98 11 : IDE_LOGD("info str check host_id;host_pid success");
99 :
100 11 : hostId = hostIdHostPidStr.substr(0, idx);
101 11 : hostPid = hostIdHostPidStr.substr(idx + 1);
102 :
103 11 : if (!IsIntDigital(hostId)) {
104 2 : IDE_LOGE("hostId is not a number, %s", hostId.c_str());
105 2 : return false;
106 : }
107 :
108 9 : if (!IsIntDigital(hostPid)) {
109 2 : IDE_LOGE("hostPid is not a number, %s", hostPid.c_str());
110 2 : return false;
111 : }
112 :
113 7 : IDE_LOGI("hostId: %s, hostPid: %s", hostId.c_str(), hostPid.c_str());
114 7 : IDE_LOGD("info str check host_id and host_pid number format success");
115 7 : return true;
116 14 : }
117 : }
|