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 <climits>
21 #include <clocale>
22 #include <ctime>
23 #include <ifaddrs.h>
24 #include <js_native_api_types.h>
25 #include <net/if.h>
26
27 #define MAX_NAMBER 80
28 #define FAIL (-1)
29 #define PARAM_0 0
30 #define PARAM_1 1
31 #define INIT (-1)
32 #define SUCCESS 0
33 #define ERRON_0 0
34 #define ONE_YEAR 31622400LL
35
Newlocale(napi_env env, napi_callback_info info)36 static napi_value Newlocale(napi_env env, napi_callback_info info)
37 {
38 locale_t newLocale = newlocale(LC_ALL_MASK, "de.UTF-8", nullptr);
39 napi_value result;
40 if (newLocale == PARAM_0) {
41 napi_create_int32(env, FAIL, &result);
42 } else {
43 napi_create_int32(env, PARAM_0, &result);
44 }
45 return result;
46 }
47
Setlocale(napi_env env, napi_callback_info info)48 static napi_value Setlocale(napi_env env, napi_callback_info info)
49 {
50 char *locale = setlocale(INIT, "");
51 int ret = SUCCESS;
52 if (locale == nullptr) {
53 ret = FAIL;
54 }
55 napi_value result;
56 napi_create_int32(env, ret, &result);
57 return result;
58 }
59
60 EXTERN_C_START
Init(napi_env env, napi_value exports)61 static napi_value Init(napi_env env, napi_value exports)
62 {
63 napi_property_descriptor desc[] = {
64 {"newlocale", nullptr, Newlocale, nullptr, nullptr, nullptr, napi_default, nullptr},
65 {"setlocale", nullptr, Setlocale, nullptr, nullptr, nullptr, napi_default, nullptr}};
66 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
67 return exports;
68 }
69
70 EXTERN_C_END
71
72 static napi_module demoModule = {
73 .nm_version = 1,
74 .nm_flags = 0,
75 .nm_filename = nullptr,
76 .nm_register_func = Init,
77 .nm_modname = "locale1",
78 .nm_priv = ((void *)0),
79 .reserved = {0},
80 };
81
RegisterEntryModule(void)82 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
83