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 #include "common/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cerrno>
20 #include <clocale>
21 #include <ifaddrs.h>
22 #include <js_native_api_types.h>
23 #include <net/if.h>
24
25 #define MAX_NAMBER 80
26 #define FAIL (-1)
27 #define PARAM_0 0
28 #define PARAM_1 1
29 #define INIT (-1)
30 #define SUCCESS 0
31 #define ERRON_0 0
32
Localeconv(napi_env env, napi_callback_info info)33 static napi_value Localeconv(napi_env env, napi_callback_info info)
34 {
35 struct lconv *ptr = localeconv();
36 napi_value result;
37 if (ptr == nullptr) {
38 napi_create_int32(env, FAIL, &result);
39 } else {
40 napi_create_int32(env, PARAM_0, &result);
41 }
42 return result;
43 }
Freelocale(napi_env env, napi_callback_info info)44 static napi_value Freelocale(napi_env env, napi_callback_info info)
45 {
46 errno = ERRON_0;
47 setlocale(LC_ALL, "C");
48 locale_t newlocale_ = duplocale(LC_GLOBAL_LOCALE);
49 if (newlocale_) {
50 freelocale(newlocale_);
51 newlocale_ = nullptr;
52 }
53 napi_value result = nullptr;
54 napi_create_int32(env, errno, &result);
55 return result;
56 }
Newlocale(napi_env env, napi_callback_info info)57 static napi_value Newlocale(napi_env env, napi_callback_info info)
58 {
59 locale_t newLocale = newlocale(LC_ALL_MASK, "C.UTF-8", nullptr);
60 napi_value result;
61 if (newLocale == PARAM_0) {
62 napi_create_int32(env, FAIL, &result);
63 } else {
64 napi_create_int32(env, PARAM_0, &result);
65 }
66 return result;
67 }
68
Setlocale(napi_env env, napi_callback_info info)69 static napi_value Setlocale(napi_env env, napi_callback_info info)
70 {
71 size_t argc = PARAM_1;
72 napi_value args[1] = {nullptr};
73 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
74 char *valueFirst = NapiHelper::GetString(env, args[0]);
75 valueFirst = setlocale(LC_ALL, "C.UTF-8");
76 napi_value result;
77 napi_create_string_utf8(env, valueFirst, NAPI_AUTO_LENGTH, &result);
78 return result;
79 }
80
Uselocale(napi_env env, napi_callback_info info)81 static napi_value Uselocale(napi_env env, napi_callback_info info)
82 {
83 size_t argc = PARAM_1;
84 napi_value args[1] = {nullptr};
85 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
86
87 int caseNumber;
88 locale_t uselocaleResult;
89 int toJsValue = FAIL;
90 napi_get_value_int32(env, args[0], &caseNumber);
91
92 if (caseNumber == PARAM_0) {
93 uselocaleResult = uselocale(nullptr);
94 if (uselocaleResult != nullptr && uselocaleResult == LC_GLOBAL_LOCALE) {
95 toJsValue = PARAM_0;
96 }
97 } else if (caseNumber == PARAM_1) {
98 uselocale(nullptr);
99 locale_t newLocale = newlocale(LC_PAPER_MASK, "C.UTF-8", nullptr);
100 locale_t usenow = uselocale(newLocale);
101 if (usenow == LC_GLOBAL_LOCALE) {
102 toJsValue = PARAM_0;
103 }
104 }
105
106 napi_value result = nullptr;
107 napi_create_int32(env, toJsValue, &result);
108 return result;
109 }
110
DupLocale(napi_env env, napi_callback_info info)111 static napi_value DupLocale(napi_env env, napi_callback_info info)
112 {
113 locale_t locobj = LC_GLOBAL_LOCALE;
114 locale_t newLocale = duplocale(locobj);
115 int backResult = INIT;
116 napi_value result = nullptr;
117 if (newLocale) {
118 backResult = SUCCESS;
119 }
120 napi_create_int32(env, backResult, &result);
121 return result;
122 }
123
124 EXTERN_C_START
Init(napi_env env, napi_value exports)125 static napi_value Init(napi_env env, napi_value exports)
126 {
127 napi_property_descriptor desc[] = {
128 {"setlocale", nullptr, Setlocale, nullptr, nullptr, nullptr, napi_default, nullptr},
129 {"uselocale", nullptr, Uselocale, nullptr, nullptr, nullptr, napi_default, nullptr},
130 {"duplocale", nullptr, DupLocale, nullptr, nullptr, nullptr, napi_default, nullptr},
131 {"localeconv", nullptr, Localeconv, nullptr, nullptr, nullptr, napi_default, nullptr},
132 {"newlocale", nullptr, Newlocale, nullptr, nullptr, nullptr, napi_default, nullptr},
133 {"freelocale", nullptr, Freelocale, nullptr, nullptr, nullptr, napi_default, nullptr}};
134 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
135 return exports;
136 }
137
138 EXTERN_C_END
139
140 static napi_module demoModule = {
141 .nm_version = 1,
142 .nm_flags = 0,
143 .nm_filename = nullptr,
144 .nm_register_func = Init,
145 .nm_modname = "locale",
146 .nm_priv = ((void *)0),
147 .reserved = {0},
148 };
149
RegisterEntryModule(void)150 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
151