1 /*
2  * Copyright (c) 2024 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 #ifndef NATIVE_PRINT_CONVERTER_H
16 #define NATIVE_PRINT_CONVERTER_H
17 
18 #include <vector>
19 #include <string>
20 
21 #include "ohprint.h"
22 
23 namespace OHOS::Print {
24 #define SAFE_DELETE(address)        \
25     {                               \
26         if ((address) != nullptr) { \
27             delete (address);       \
28             (address) = nullptr;    \
29         }                           \
30     }
31 
32 #define SAFE_DELETE_ARRAY(address)  \
33     {                               \
34         if ((address) != nullptr) { \
35             delete[] (address);     \
36             (address) = nullptr;    \
37         }                           \
38     }
39 
IsValidString(const char * address)40 inline bool IsValidString(const char *address)
41 {
42     if (address == nullptr || address[0] == '\0') {
43         return false;
44     }
45     return true;
46 }
47 
48 template <typename T>
CopyArray(const std::vector<T> & list,uint32_t & count)49 T *CopyArray(const std::vector<T> &list, uint32_t &count)
50 {
51     count = 0;
52     size_t len = list.size();
53     if (len == 0) {
54         return nullptr;
55     }
56     T *dest = new (std::nothrow) T[len];
57     if (dest == nullptr) {
58         return nullptr;
59     }
60     if (memset_s(dest, len * sizeof(T), 0, len * sizeof(T)) != 0) {
61         delete[] dest;
62         dest = nullptr;
63         return nullptr;
64     }
65     for (size_t i = 0; i < len; i++) {
66         *(dest + i) = list[i];
67     }
68     count = len;
69     return dest;
70 }
71 
72 template <typename T1, typename T2>
CopyArray(const std::vector<T1> & list,uint32_t & count,bool (* ConvertFunction)(const T1 & src,T2 & dst))73 T2 *CopyArray(const std::vector<T1> &list, uint32_t &count, bool (*ConvertFunction)(const T1 &src, T2 &dst))
74 {
75     count = 0;
76     size_t len = list.size();
77     if (len == 0) {
78         return nullptr;
79     }
80     T2 *dest = new (std::nothrow) T2[len];
81     if (dest == nullptr) {
82         return nullptr;
83     }
84     if (memset_s(dest, len * sizeof(T2), 0, len * sizeof(T2)) != 0) {
85         delete[] dest;
86         dest = nullptr;
87         return nullptr;
88     }
89     for (size_t i = 0; i < len; i++) {
90         if (ConvertFunction(list[i], *(dest + count))) {
91             count++;
92         }
93     }
94     return dest;
95 }
96 
97 Print_ErrorCode ConvertToNativeErrorCode(int32_t errorCode);
98 Print_PrinterState ConvertPrinterState(uint32_t state);
99 bool ConvertStringToInt(const char *src, int &dst);
100 bool ConvertOrientationMode(const uint32_t &src, Print_OrientationMode &dst);
101 bool ConvertColorMode(const uint32_t &src, Print_ColorMode &dst);
102 bool ConvertDuplexMode(const uint32_t &src, Print_DuplexMode &dst);
103 bool ConvertQuality(const uint32_t &src, Print_Quality &dst);
104 std::string GetDocumentFormatString(Print_DocumentFormat format);
105 std::string GetResolutionString(Print_Resolution resolution);
106 }  // namespace OHOS::Print
107 #endif  // NATIVE_PRINT_CONVERTER_H