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_text_ani.h"
17
18 #include <optional>
19 #include "ani_helper.h"
20 #include "error_handler.h"
21 #include "filemgmt_libhilog.h"
22 #include "read_text_core.h"
23 #include "type_converter.h"
24
25 namespace OHOS {
26 namespace FileManagement {
27 namespace ModuleFileIO {
28 namespace ANI {
29
30 using namespace std;
31 using namespace OHOS::FileManagement::ModuleFileIO;
32 using namespace OHOS::FileManagement::ModuleFileIO::ANI;
33
ToReadTextOptions(ani_env * env,ani_object obj)34 static tuple<bool, optional<ReadTextOptions>> ToReadTextOptions(ani_env *env, ani_object obj)
35 {
36 ReadTextOptions options;
37
38 ani_boolean isUndefined;
39 env->Reference_IsUndefined(obj, &isUndefined);
40 if (isUndefined) {
41 return { true, nullopt };
42 }
43
44 auto [succOffset, offset] = AniHelper::ParseInt64Option(env, obj, "offset");
45 if (!succOffset) {
46 HILOGE("Illegal option.offset parameter");
47 return { false, nullopt };
48 }
49 options.offset = offset;
50
51 auto [succLength, length] = AniHelper::ParseInt64Option(env, obj, "length");
52 if (!succLength) {
53 HILOGE("Illegal option.length parameter");
54 return { false, nullopt };
55 }
56 options.length = length;
57
58 auto [succEncoding, encoding] = AniHelper::ParseEncoding(env, obj);
59 if (!succEncoding) {
60 HILOGE("Illegal option.encoding parameter");
61 return { false, nullopt };
62 }
63 options.encoding = encoding;
64
65 return { true, make_optional<ReadTextOptions>(move(options)) };
66 }
67
ReadTextSync(ani_env * env,ani_class clazz,ani_string filePath,ani_object obj)68 ani_string ReadTextAni::ReadTextSync(
69 ani_env *env, [[maybe_unused]] ani_class clazz, ani_string filePath, ani_object obj)
70 {
71 auto [succOpt, options] = ToReadTextOptions(env, obj);
72 if (!succOpt) {
73 HILOGE("Invalid options");
74 ErrorHandler::Throw(env, EINVAL);
75 return nullptr;
76 }
77
78 auto [succPath, path] = TypeConverter::ToUTF8String(env, filePath);
79 if (!succPath) {
80 HILOGE("Invalid Path");
81 ErrorHandler::Throw(env, EINVAL);
82 return nullptr;
83 }
84
85 auto ret = ReadTextCore::DoReadText(path, options);
86 if (!ret.IsSuccess()) {
87 HILOGE("DoReadText failed");
88 const auto &err = ret.GetError();
89 ErrorHandler::Throw(env, err);
90 return nullptr;
91 }
92
93 const auto &resText = ret.GetData().value();
94 string res = std::get<0>(resText);
95 size_t size = std::get<1>(resText);
96 auto [succ, result] = TypeConverter::ToAniString(env, res, size);
97 if (!succ) {
98 HILOGE("Convert result to ani string failed");
99 ErrorHandler::Throw(env, UNKNOWN_ERR);
100 return nullptr;
101 }
102 return result;
103 }
104
105 } // namespace ANI
106 } // namespace ModuleFileIO
107 } // namespace FileManagement
108 } // namespace OHOS