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 "napi/native_api.h"
17 #include <cstring>
18 #include <js_native_api.h>
19 #include <js_native_api_types.h>
20 #include <langinfo.h>
21 #include <locale>
22 #include <node_api.h>
23 #include <sys/select.h>
24 #include <utmp.h>
25 
26 #define STRLENGTH 64
27 #define FALSE 0
28 #define ERROR (-1)
29 #define FIVE 5
30 #define PARAM_0 0
31 #define ONE 1
32 #define DAY_2 0x20008
33 
NlLanginfo(napi_env env, napi_callback_info info)34 static napi_value NlLanginfo(napi_env env, napi_callback_info info)
35 {
36     setlocale(LC_CTYPE, "UTF-8");
37     char *ret = nl_langinfo(DAY_2);
38     napi_value result;
39     napi_create_string_utf8(env, ret, NAPI_AUTO_LENGTH, &result);
40     return result;
41 }
42 
NlLanginfoL(napi_env env, napi_callback_info info)43 static napi_value NlLanginfoL(napi_env env, napi_callback_info info)
44 {
45     locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL);
46     char *getInfo = nl_langinfo_l(CODESET, c_locale);
47     napi_value result = nullptr;
48     freelocale(c_locale);
49     napi_create_string_utf8(env, getInfo, NAPI_AUTO_LENGTH, &result);
50     return result;
51 }
52 
53 EXTERN_C_START
Init(napi_env env, napi_value exports)54 static napi_value Init(napi_env env, napi_value exports)
55 {
56     napi_property_descriptor desc[] = {
57         {"nlLanginfo", nullptr, NlLanginfo, nullptr, nullptr, nullptr, napi_default, nullptr},
58         {"nlLanginfoL", nullptr, NlLanginfoL, nullptr, nullptr, nullptr, napi_default, nullptr},
59     };
60     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
61     return exports;
62 }
63 EXTERN_C_END
64 
65 static napi_module demoModule = {
66     .nm_version = 1,
67     .nm_flags = 0,
68     .nm_filename = nullptr,
69     .nm_register_func = Init,
70     .nm_modname = "langinfo",
71     .nm_priv = ((void *)0),
72     .reserved = {0},
73 };
74 
RegisterEntryModule(void)75 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
76