• 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 class PrintUtil {
32 public:
33     static std::string ParseListToString(const std::vector<std::string> &list);
34 
35     static std::string SplitStr(const std::string& str, char delimiter, int index);
36 
37     static std::string ToUpper(const std::string& str);
38 
39     static bool CheckContains(const std::string& str, const std::string& content);
40 
41     static std::string StandardizePrinterName(std::string printerName);
42 };
43 
ParseListToString(const std::vector<std::string> & list)44 inline std::string PrintUtil::ParseListToString(const std::vector<std::string> &list)
45 {
46     std::string str;
47     if (!list.empty()) {
48         uint32_t count = 1;
49         uint32_t size = list.size();
50         for (auto val: list) {
51             str += val;
52             if (count < size) {
53                 str += ",";
54             }
55             count ++;
56         }
57     }
58     return str;
59 }
60 
SplitStr(const std::string & str,char delimiter,int index)61 inline std::string PrintUtil::SplitStr(const std::string& str, char delimiter, int index)
62 {
63     if (!str.empty()) {
64         std::string token;
65         std::istringstream tokenStream(str);
66         int count = 0;
67         while (std::getline(tokenStream, token, delimiter)) {
68             if (count == index) {
69                 return token;
70             }
71             count ++;
72         }
73     }
74     return str;
75 }
76 
ToUpper(const std::string & val)77 inline std::string PrintUtil::ToUpper(const std::string& val)
78 {
79     std::string str = val;
80     if (!str.empty()) {
81         std::transform(str.begin(), str.end(), str.begin(), toupper);
82     }
83     return str;
84 }
85 
CheckContains(const std::string & str,const std::string & content)86 inline bool PrintUtil::CheckContains(const std::string& str, const std::string& content)
87 {
88     if (str.empty() || content.empty()) {
89         return false;
90     }
91 
92     return str.find(content) != std::string::npos;
93 }
94 
StandardizePrinterName(std::string printerName)95 inline std::string PrintUtil::StandardizePrinterName(std::string printerName)
96 {
97     std::regex pattern("[ /#]");
98     std::string name = std::regex_replace(printerName, pattern, "_");
99     if (name.length() < MAX_PRINTER_NAME_LENGTH) {
100         return name;
101     }
102     return name.substr(0, MAX_PRINTER_NAME_LENGTH - 1);
103 }
104 } // namespace OHOS::Print
105 
106 #endif // PRINT_UTIL_H