1 /*
2  * Copyright (c) 2022 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 UTIL_JS_TEXTENCODER_H
17 #define UTIL_JS_TEXTENCODER_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "native_engine.h"
24 #include "unicode/ucnv.h"
25 
26 namespace OHOS::Util {
27     constexpr const int32_t API_VERSION_MOD = 100; // 100: api version mod
28     class TextEncoder {
29     public:
30         /**
31          * Constructor of textdecoder.
32          *
33          * @param encoding Encoding format
34          */
TextEncoder(const std::string & encoding)35         explicit TextEncoder(const std::string &encoding) : encoding_(encoding) {}
36 
37         /**
38          * Destructor of textencoder.
39          */
~TextEncoder()40         virtual ~TextEncoder() {}
41 
42         /**
43          * Get encoding format.
44          *
45          * @param env NAPI environment parameters.
46          */
47         napi_value GetEncoding(napi_env env) const;
48 
49         /**
50          * Output the corresponding text after encoding the input parameters.
51          *
52          * @param env NAPI environment parameters.
53          * @param src A string that needs to be encoded.
54          */
55         napi_value Encode(napi_env env, napi_value src) const;
56 
57         /**
58          * Place the generated UTF-8 encoded text.
59          *
60          * @param env NAPI environment parameters.
61          * @param src A string that needs to be encoded.
62          * @param dest Uint8array object instance, which is used to put the generated UTF-8 encoded text into it.
63          */
64         napi_value EncodeInto(napi_env env, napi_value src, napi_value dest) const;
65 
SetOrgEncoding(std::string orgEncoding)66         void SetOrgEncoding(std::string orgEncoding)
67         {
68             orgEncoding_ = orgEncoding;
69         }
70     private:
71         std::string encoding_ {};
72         std::string orgEncoding_ {};
73     };
74 }
75 #endif // UTIL_JS_TEXTENCODER_H
76