1 /*
2 * Copyright (c) 2024 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_impl.h"
17
18 using namespace OHOS::FileManagement;
19 using namespace OHOS::CJSystemapi::FileFs;
20
21 namespace OHOS::CJSystemapi {
ReadIteratorImpl(std::shared_ptr<OHOS::FileManagement::ModuleFileIO::ReaderIteratorEntity> entity)22 ReadIteratorImpl::ReadIteratorImpl(std::shared_ptr<OHOS::FileManagement::ModuleFileIO::ReaderIteratorEntity> entity)
23 {
24 entity_ = entity;
25 }
Next()26 std::tuple<int32_t, bool, char*> ReadIteratorImpl::Next()
27 {
28 Str *str = NextLine(entity_->iterator);
29 if (str == nullptr) {
30 if (entity_->offset != 0) {
31 HILOGE("Failed to get next line, error:%{public}d", errno);
32 return {GetErrorCode(errno), entity_->offset == 0, nullptr};
33 }
34 entity_ = nullptr;
35 return {SUCCESS_CODE, true, nullptr};
36 }
37 bool done = entity_->offset == 0;
38 char* value = static_cast<char*>(malloc((str->len + 1) * sizeof(char)));
39 if (value == nullptr) {
40 return {GetErrorCode(ENOMEM), done, nullptr};
41 }
42 int ret = memcpy_s(value, str->len + 1, str->str, str->len + 1);
43 if (ret != EOK) {
44 free(value);
45 value = nullptr;
46 return { GetErrorCode(ENOMEM), done, value};
47 }
48 entity_->offset -= static_cast<int64_t>(str->len);
49 StrFree(str);
50 return { SUCCESS_CODE, done, value};
51 }
52 }