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 "fs_reader_iterator.h" 17 18 #include "file_utils.h" 19 #include "filemgmt_libhilog.h" 20 #include "rust_file.h" 21 22 namespace OHOS::FileManagement::ModuleFileIO { 23 using namespace std; 24 Constructor()25FsResult<FsReaderIterator *> FsReaderIterator::Constructor() 26 { 27 auto entity = CreateUniquePtr<ReaderIteratorEntity>(); 28 if (entity == nullptr) { 29 HILOGE("Failed to request heap memory."); 30 return FsResult<FsReaderIterator *>::Error(ENOMEM); 31 } 32 33 FsReaderIterator *it = new FsReaderIterator(move(entity)); 34 35 if (it == nullptr) { 36 HILOGE("Failed to create FsReaderIterator object on heap."); 37 return FsResult<FsReaderIterator *>::Error(ENOMEM); 38 } 39 return FsResult<FsReaderIterator *>::Success(move(it)); 40 } 41 Next()42FsResult<ReaderIteratorResult> FsReaderIterator::Next() 43 { 44 if (entity == nullptr) { 45 HILOGE("Failed to get reader iterator entity"); 46 return FsResult<ReaderIteratorResult>::Error(UNKNOWN_ERR); 47 } 48 49 Str *str = NextLine(entity->iterator); 50 if (str == nullptr && entity->offset != 0) { 51 HILOGE("Failed to get next line, error:%{public}d", errno); 52 return FsResult<ReaderIteratorResult>::Error(errno); 53 } 54 55 ReaderIteratorResult result; 56 bool done = entity->offset == 0; 57 result.done = done; 58 if (str != nullptr) { 59 string value(str->str, str->len); 60 result.value = value; 61 entity->offset -= static_cast<int64_t>(str->len); 62 } else { 63 result.value = ""; 64 } 65 StrFree(str); 66 67 return FsResult<ReaderIteratorResult>::Success(move(result)); 68 } 69 70 } // namespace OHOS::FileManagement::ModuleFileIO