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 "readeriterator_n_exporter.h"
17 
18 #include "filemgmt_libhilog.h"
19 #include "file_utils.h"
20 #include "readeriterator_entity.h"
21 #include "rust_file.h"
22 
23 namespace OHOS::FileManagement::ModuleFileIO {
24 using namespace std;
25 using namespace OHOS::FileManagement::LibN;
26 
Constructor(napi_env env,napi_callback_info info)27 napi_value ReaderIteratorNExporter::Constructor(napi_env env, napi_callback_info info)
28 {
29     NFuncArg funcArg(env, info);
30     if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
31         HILOGE("Number of arguments unmatched");
32         NError(EINVAL).ThrowErr(env);
33         return nullptr;
34     }
35 
36     auto readerIteratorEntity = CreateUniquePtr<ReaderIteratorEntity>();
37     if (readerIteratorEntity == nullptr) {
38         HILOGE("Failed to request heap memory.");
39         NError(ENOMEM).ThrowErr(env);
40         return nullptr;
41     }
42     if (!NClass::SetEntityFor<ReaderIteratorEntity>(env, funcArg.GetThisVar(), move(readerIteratorEntity))) {
43         HILOGE("Failed to set reader iterator entity");
44         NError(UNKROWN_ERR).ThrowErr(env);
45         return nullptr;
46     }
47     return funcArg.GetThisVar();
48 }
49 
Next(napi_env env,napi_callback_info info)50 napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info)
51 {
52     NFuncArg funcArg(env, info);
53     if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
54         HILOGE("Number of arguments unmatched");
55         NError(EINVAL).ThrowErr(env);
56         return nullptr;
57     }
58     auto readerIteratorEntity = NClass::GetEntityOf<ReaderIteratorEntity>(env, funcArg.GetThisVar());
59     if (!readerIteratorEntity) {
60         HILOGE("Failed to get reader iterator entity");
61         NError(UNKROWN_ERR).ThrowErr(env);
62         return nullptr;
63     }
64 
65     Str *str = NextLine(readerIteratorEntity->iterator);
66     if (str == nullptr && readerIteratorEntity->offset != 0) {
67         HILOGE("Failed to get next line, error:%{public}d", errno);
68         NError(errno).ThrowErr(env);
69         return nullptr;
70     }
71 
72     NVal objReaderIteratorResult = NVal::CreateObject(env);
73     objReaderIteratorResult.AddProp("done", NVal::CreateBool(env, (readerIteratorEntity->offset == 0)).val_);
74     if (str != nullptr) {
75         objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, str->str, str->len).val_);
76         readerIteratorEntity->offset -= static_cast<int64_t>(str->len);
77     } else {
78         objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, "").val_);
79         (void)NClass::RemoveEntityOfFinal<ReaderIteratorEntity>(env, funcArg.GetThisVar());
80     }
81     StrFree(str);
82 
83     return objReaderIteratorResult.val_;
84 }
85 
Export()86 bool ReaderIteratorNExporter::Export()
87 {
88     vector<napi_property_descriptor> props = {
89         NVal::DeclareNapiFunction("next", Next),
90     };
91 
92     string className = GetClassName();
93 
94     bool succ = false;
95     napi_value classValue = nullptr;
96     tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, ReaderIteratorNExporter::Constructor,
97         std::move(props));
98     if (!succ) {
99         HILOGE("Failed to define class");
100         NError(UNKROWN_ERR).ThrowErr(exports_.env_);
101         return false;
102     }
103     succ = NClass::SaveClass(exports_.env_, className, classValue);
104     if (!succ) {
105         HILOGE("Failed to save class");
106         NError(UNKROWN_ERR).ThrowErr(exports_.env_);
107         return false;
108     }
109 
110     return exports_.AddProp(className, classValue);
111 }
112 
GetClassName()113 string ReaderIteratorNExporter::GetClassName()
114 {
115     return ReaderIteratorNExporter::className_;
116 }
117 
ReaderIteratorNExporter(napi_env env,napi_value exports)118 ReaderIteratorNExporter::ReaderIteratorNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
119 
~ReaderIteratorNExporter()120 ReaderIteratorNExporter::~ReaderIteratorNExporter() {}
121 } // namespace OHOS::FileManagement::ModuleFileIO