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