1 /*
2 * Copyright (c) 2022 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 "utilities.h"
17
18 #include <fstream>
19 #include <iostream>
20
21 namespace OHOS {
22 namespace Developtools {
23 namespace Hiebpf {
ReadFileToString(const std::string & fileName)24 std::string ReadFileToString(const std::string &fileName)
25 {
26 std::ifstream inputString(fileName, std::ios::in);
27 if (!inputString or !inputString.is_open()) {
28 return EMPTY_STRING;
29 }
30 std::istreambuf_iterator<char> firstIt = {inputString};
31 std::istreambuf_iterator<char> lastIt = {};
32
33 std::string content(firstIt, lastIt);
34 return content;
35 }
36
StringSplit(std::string source,const std::string & split)37 std::vector<std::string> StringSplit(std::string source, const std::string &split)
38 {
39 std::vector<std::string> result;
40
41 // find
42 if (!split.empty()) {
43 size_t pos = 0;
44 while ((pos = source.find(split)) != std::string::npos) {
45 // split
46 std::string token = source.substr(0, pos);
47 if (!token.empty()) {
48 result.push_back(token);
49 }
50 source.erase(0, pos + split.length());
51 }
52 }
53 // add last token
54 if (!source.empty()) {
55 result.push_back(source);
56 }
57 return result;
58 }
59
StringEndsWith(const std::string & string,const std::string & with)60 bool StringEndsWith(const std::string &string, const std::string &with)
61 {
62 if (string.empty()) {
63 // empty string only end with empty string
64 if (with.empty()) {
65 return true;
66 } else {
67 return false;
68 }
69 }
70 return string.rfind(with) == (string.length() - with.length());
71 }
72 } // namespace Hiebpf
73 } // namespace Developtools
74 } // namespace OHOS
75