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