1 /*
2  * Copyright (c) 2023 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 #ifndef PRINT_UTIL_H
17 #define PRINT_UTIL_H
18 
19 #include <algorithm>
20 #include <charconv>
21 #include <iostream>
22 #include <sstream>
23 #include <list>
24 #include <vector>
25 #include <string>
26 #include <regex>
27 
28 #include "print_log.h"
29 
30 namespace OHOS::Print {
31 const uint32_t MAX_PRINTER_NAME_LENGTH = 127;
32 const uint32_t MIN_INT_LIST_STRLENGTH = 2;
33 class PrintUtil {
34 public:
35     static std::string ParseListToString(const std::vector<std::string> &list);
36 
37     static std::string SplitStr(const std::string& str, char delimiter, int index);
38 
39     static std::string ToUpper(const std::string& str);
40 
41     static bool CheckContains(const std::string& str, const std::string& content);
42 
43     static std::string StandardizePrinterName(std::string printerName);
44 
45     static std::string RemoveUnderlineFromPrinterName(std::string printerName);
46 
47     static std::vector<uint32_t> Str2Vec(std::string str);
48 
49     static void Str2VecStr(std::string& str, std::vector<std::string>& vec);
50 
51     static bool startsWith(const std::string& str, const std::string& prefix);
52 
53     static bool ConvertToInt(const std::string& str, int32_t& value);
54 };
55 
Str2Vec(std::string str)56 inline std::vector<uint32_t> PrintUtil::Str2Vec(std::string str)
57 {
58     if (str.size() < MIN_INT_LIST_STRLENGTH) {
59         return {};
60     }
61     str.pop_back();
62     str.erase(str.begin());
63     std::vector<uint32_t> vec;
64     std::istringstream is(str);
65     std::string temp;
66     while (getline(is, temp, ',')) {
67         int32_t tempNum = 0;
68         if (!ConvertToInt(temp, tempNum)) {
69             return {};
70         }
71         vec.push_back(tempNum);
72     }
73     return vec;
74 }
75 
Str2VecStr(std::string & str,std::vector<std::string> & vec)76 inline void PrintUtil::Str2VecStr(std::string& str, std::vector<std::string>& vec)
77 {
78     if (!str.empty()) {
79         str.pop_back();
80         str.erase(str.begin());
81         std::istringstream is(str);
82         std::string temp;
83         while (getline(is, temp, ',')) {
84             vec.push_back(temp);
85         }
86     }
87 }
88 
ParseListToString(const std::vector<std::string> & list)89 inline std::string PrintUtil::ParseListToString(const std::vector<std::string> &list)
90 {
91     std::string str;
92     if (!list.empty()) {
93         uint32_t count = 1;
94         uint32_t size = list.size();
95         for (auto val: list) {
96             str += val;
97             if (count < size) {
98                 str += ",";
99             }
100             count ++;
101         }
102     }
103     return str;
104 }
105 
SplitStr(const std::string & str,char delimiter,int index)106 inline std::string PrintUtil::SplitStr(const std::string& str, char delimiter, int index)
107 {
108     if (!str.empty()) {
109         std::string token;
110         std::istringstream tokenStream(str);
111         int count = 0;
112         while (std::getline(tokenStream, token, delimiter)) {
113             if (count == index) {
114                 return token;
115             }
116             count ++;
117         }
118     }
119     return str;
120 }
121 
ToUpper(const std::string & val)122 inline std::string PrintUtil::ToUpper(const std::string& val)
123 {
124     std::string str = val;
125     if (!str.empty()) {
126         std::transform(str.begin(), str.end(), str.begin(), toupper);
127     }
128     return str;
129 }
130 
CheckContains(const std::string & str,const std::string & content)131 inline bool PrintUtil::CheckContains(const std::string& str, const std::string& content)
132 {
133     if (str.empty() || content.empty()) {
134         return false;
135     }
136 
137     return str.find(content) != std::string::npos;
138 }
139 
StandardizePrinterName(std::string printerName)140 inline std::string PrintUtil::StandardizePrinterName(std::string printerName)
141 {
142     std::regex pattern("[ /#]");
143     std::string name = std::regex_replace(printerName, pattern, "_");
144     if (name.length() < MAX_PRINTER_NAME_LENGTH) {
145         return name;
146     }
147     return name.substr(0, MAX_PRINTER_NAME_LENGTH - 1);
148 }
149 
RemoveUnderlineFromPrinterName(std::string printerName)150 inline std::string PrintUtil::RemoveUnderlineFromPrinterName(std::string printerName)
151 {
152     std::regex pattern("[_]");
153     std::string name = std::regex_replace(printerName, pattern, " ");
154     if (name.length() < MAX_PRINTER_NAME_LENGTH) {
155         return name;
156     }
157     return name.substr(0, MAX_PRINTER_NAME_LENGTH - 1);
158 }
159 
startsWith(const std::string & str,const std::string & prefix)160 inline bool PrintUtil::startsWith(const std::string& str, const std::string& prefix)
161 {
162     if (str.length() < prefix.length()) {
163         return false;
164     }
165     return str.compare(0, prefix.length(), prefix) == 0;
166 }
167 
ConvertToInt(const std::string & str,int32_t & value)168 inline bool PrintUtil::ConvertToInt(const std::string& str, int32_t& value)
169 {
170     auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
171     return ec == std::errc{} && ptr == str.data() + str.size();
172 }
173 
174 } // namespace OHOS::Print
175 
176 #endif // PRINT_UTIL_H