• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "read_lines_ani.h"
17 
18 #include "ani_helper.h"
19 #include "error_handler.h"
20 #include "filemgmt_libhilog.h"
21 #include "read_lines_core.h"
22 #include "reader_iterator_ani.h"
23 #include "type_converter.h"
24 
25 namespace OHOS {
26 namespace FileManagement {
27 namespace ModuleFileIO {
28 namespace ANI {
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 
ToReadLinesOptions(ani_env * env,ani_object obj)31 static tuple<bool, optional<Options>> ToReadLinesOptions(ani_env *env, ani_object obj)
32 {
33     Options options;
34 
35     ani_boolean isUndefined;
36     env->Reference_IsUndefined(obj, &isUndefined);
37     if (isUndefined) {
38         return { true, nullopt };
39     }
40 
41     auto [succEncoding, encoding] = AniHelper::ParseEncoding(env, obj);
42     if (!succEncoding) {
43         HILOGE("Illegal option.encoding parameter");
44         return { false, nullopt };
45     }
46     options.encoding = encoding.value();
47 
48     return { true, make_optional<Options>(move(options)) };
49 }
50 
ReadLinesSync(ani_env * env,ani_class clazz,ani_string path,ani_object options)51 ani_object ReadLinesAni::ReadLinesSync(
52     ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_object options)
53 {
54     auto [succPath, filePath] = TypeConverter::ToUTF8String(env, path);
55     if (!succPath) {
56         HILOGE("Invalid path from ETS first argument");
57         ErrorHandler::Throw(env, EINVAL);
58         return nullptr;
59     }
60 
61     auto [succMode, opt] = ToReadLinesOptions(env, options);
62     if (!succMode) {
63         HILOGE("Invalid options");
64         ErrorHandler::Throw(env, EINVAL);
65         return nullptr;
66     }
67 
68     FsResult<FsReaderIterator *> ret = ReadLinesCore::DoReadLines(filePath, opt);
69     if (!ret.IsSuccess()) {
70         HILOGE("Readlines failed");
71         const auto &err = ret.GetError();
72         ErrorHandler::Throw(env, err);
73         return nullptr;
74     }
75 
76     const FsReaderIterator *readerIterator = ret.GetData().value();
77     auto result = ReaderIteratorAni::Wrap(env, move(readerIterator));
78     if (result == nullptr) {
79         ErrorHandler::Throw(env, UNKNOWN_ERR);
80         return nullptr;
81     }
82     return result;
83 }
84 
85 } // namespace ANI
86 } // namespace ModuleFileIO
87 } // namespace FileManagement
88 } // namespace OHOS