15bebb993Sopenharmony_ci/* 25bebb993Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 35bebb993Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45bebb993Sopenharmony_ci * you may not use this file except in compliance with the License. 55bebb993Sopenharmony_ci * You may obtain a copy of the License at 65bebb993Sopenharmony_ci * 75bebb993Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85bebb993Sopenharmony_ci * 95bebb993Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105bebb993Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115bebb993Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125bebb993Sopenharmony_ci * See the License for the specific language governing permissions and 135bebb993Sopenharmony_ci * limitations under the License. 145bebb993Sopenharmony_ci */ 155bebb993Sopenharmony_ci 165bebb993Sopenharmony_ci#ifndef RUST_FILE_H 175bebb993Sopenharmony_ci#define RUST_FILE_H 185bebb993Sopenharmony_ci 195bebb993Sopenharmony_ci#ifdef __cplusplus 205bebb993Sopenharmony_ci#if __cplusplus 215bebb993Sopenharmony_ciextern "C" { 225bebb993Sopenharmony_ci#endif 235bebb993Sopenharmony_ci#endif 245bebb993Sopenharmony_ci 255bebb993Sopenharmony_ci/** 265bebb993Sopenharmony_ci * @ingroup rust 275bebb993Sopenharmony_ci * @brief Enumeration of `lseek` interface to seek within a file. 285bebb993Sopenharmony_ci * @param Start Sets the offset from the head of the file. 295bebb993Sopenharmony_ci * @param Current Sets the offset from the current position. 305bebb993Sopenharmony_ci * @param End Sets the offset from th tail of the file. 315bebb993Sopenharmony_ci */ 325bebb993Sopenharmony_cienum SeekPos { 335bebb993Sopenharmony_ci START, 345bebb993Sopenharmony_ci CURRENT, 355bebb993Sopenharmony_ci END 365bebb993Sopenharmony_ci}; 375bebb993Sopenharmony_ci 385bebb993Sopenharmony_ci/** 395bebb993Sopenharmony_ci * @ingroup rust 405bebb993Sopenharmony_ci * @brief Enumeration of `mkdirs` interface to choose ways to create the direction. 415bebb993Sopenharmony_ci * @param Single Creates a single level directory. 425bebb993Sopenharmony_ci * @param Multiple Creates a multi-level directory. 435bebb993Sopenharmony_ci */ 445bebb993Sopenharmony_cienum MakeDirectionMode { 455bebb993Sopenharmony_ci SINGLE, 465bebb993Sopenharmony_ci MULTIPLE 475bebb993Sopenharmony_ci}; 485bebb993Sopenharmony_ci 495bebb993Sopenharmony_ci/** 505bebb993Sopenharmony_ci * @ingroup rust 515bebb993Sopenharmony_ci * @struct Str 525bebb993Sopenharmony_ci * @brief Stores string and its effective length. 535bebb993Sopenharmony_ci */ 545bebb993Sopenharmony_citypedef struct { 555bebb993Sopenharmony_ci const char* str; 565bebb993Sopenharmony_ci unsigned int len; 575bebb993Sopenharmony_ci} Str; 585bebb993Sopenharmony_ci 595bebb993Sopenharmony_ci/** 605bebb993Sopenharmony_ci * @ingroup rust 615bebb993Sopenharmony_ci * @brief Gets a iterator to read the content of a file in a path and split it by line. 625bebb993Sopenharmony_ci * @param path file path. 635bebb993Sopenharmony_ci * @retval NULL Fails to read the file, stores error information from errno. 645bebb993Sopenharmony_ci * @retval !NULL Reads the file successfully, valid reader iterator pointer. 655bebb993Sopenharmony_ci * @attention 665bebb993Sopenharmony_ci * 1.The return value needs to be released by calling the `stringVectorFree` method. 675bebb993Sopenharmony_ci * 2.The input `path` must be in valid UTF-8 format. 685bebb993Sopenharmony_ci * 3.Errors are stored in errno. 695bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 705bebb993Sopenharmony_ci */ 715bebb993Sopenharmony_civoid* ReaderIterator(const char* path); 725bebb993Sopenharmony_ci 735bebb993Sopenharmony_ci/** 745bebb993Sopenharmony_ci * @ingroup rust 755bebb993Sopenharmony_ci * @brief Drop iterator to release fd 765bebb993Sopenharmony_ci * @param iter pointer to reader iterator. 775bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 785bebb993Sopenharmony_ci */ 795bebb993Sopenharmony_civoid DropReaderIterator(void* iter); 805bebb993Sopenharmony_ci 815bebb993Sopenharmony_ci/** 825bebb993Sopenharmony_ci * @ingroup rust 835bebb993Sopenharmony_ci * @brief Reads a line from the reader iterator. 845bebb993Sopenharmony_ci * @param iter pointer to reader iterator. 855bebb993Sopenharmony_ci * @retval NULL and error stored in errno Invalid pointer to iterator. 865bebb993Sopenharmony_ci * @retval NULL and no error in errno Gets the last line of content from the iterator. 875bebb993Sopenharmony_ci * @retval !NULL Valid `Str` pointer, Gets a line of content successfully. 885bebb993Sopenharmony_ci * @attention 895bebb993Sopenharmony_ci * 1.The input `lines` must be a valid pointer to `StringVector`. 905bebb993Sopenharmony_ci * 2.Errors are stored in errno. 915bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 925bebb993Sopenharmony_ci */ 935bebb993Sopenharmony_ciStr* NextLine(void* iter); 945bebb993Sopenharmony_ci 955bebb993Sopenharmony_ci/** 965bebb993Sopenharmony_ci * @ingroup rust 975bebb993Sopenharmony_ci * @brief Seeks to an offset, in bytes, in a file. 985bebb993Sopenharmony_ci * @param fd file descriptor. 995bebb993Sopenharmony_ci * @param offset seek offset. 1005bebb993Sopenharmony_ci * @param pos seek position. 1015bebb993Sopenharmony_ci * @retval >=0 the resulting offset location. 1025bebb993Sopenharmony_ci * @retval -1 error occurs. 1035bebb993Sopenharmony_ci * @attention 1045bebb993Sopenharmony_ci * 1.It can fail because it may involve flushing a buffer or seek to a negative offset. 1055bebb993Sopenharmony_ci * 2.Errors are stored in errno. 1065bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 1075bebb993Sopenharmony_ci */ 1085bebb993Sopenharmony_cilong long int Lseek(int fd, long long offset, enum SeekPos pos); 1095bebb993Sopenharmony_ci 1105bebb993Sopenharmony_ci/** 1115bebb993Sopenharmony_ci * @ingroup rust 1125bebb993Sopenharmony_ci * @brief Creates a new directory at the given path. 1135bebb993Sopenharmony_ci * @param path direction path. 1145bebb993Sopenharmony_ci * @param mode creating ways. 1155bebb993Sopenharmony_ci * @retval 0 Created successfully. 1165bebb993Sopenharmony_ci * @retval -1 Creation failed. 1175bebb993Sopenharmony_ci * @attention 1185bebb993Sopenharmony_ci * 1.The input `path` must be in valid UTF-8 format. 1195bebb993Sopenharmony_ci * 2.Errors are stored in errno. 1205bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 1215bebb993Sopenharmony_ci */ 1225bebb993Sopenharmony_ciint Mkdirs(const char* path, enum MakeDirectionMode mode); 1235bebb993Sopenharmony_ci 1245bebb993Sopenharmony_ci/** 1255bebb993Sopenharmony_ci * @ingroup rust 1265bebb993Sopenharmony_ci * @brief Get the parent directory of the specified file. 1275bebb993Sopenharmony_ci * @param fd file descriptor. 1285bebb993Sopenharmony_ci * @retval NULL The path terminates in a root or prefix or the file is closed. 1295bebb993Sopenharmony_ci * @retval !NULL The parent directory of the specified file. 1305bebb993Sopenharmony_ci * @attention 1315bebb993Sopenharmony_ci * 1.Errors are stored in errno. 1325bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 1335bebb993Sopenharmony_ci */ 1345bebb993Sopenharmony_ciStr* GetParent(int fd); 1355bebb993Sopenharmony_ci 1365bebb993Sopenharmony_ci/** 1375bebb993Sopenharmony_ci * @ingroup rust 1385bebb993Sopenharmony_ci * @brief Cut the file name by the specified length. 1395bebb993Sopenharmony_ci * @param path file name. 1405bebb993Sopenharmony_ci * @param size specified length. 1415bebb993Sopenharmony_ci * @retval NULL The file name is empty or error. 1425bebb993Sopenharmony_ci * @retval !NULL The result of the file name after cutting. 1435bebb993Sopenharmony_ci * @attention 1445bebb993Sopenharmony_ci * 1.Errors are stored in errno. 1455bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 1465bebb993Sopenharmony_ci */ 1475bebb993Sopenharmony_ciStr* CutFileName(const char* path, size_t size); 1485bebb993Sopenharmony_ci 1495bebb993Sopenharmony_ci/** 1505bebb993Sopenharmony_ci * @ingroup rust 1515bebb993Sopenharmony_ci * @brief Releases the memory that the `Str` pointer points to. 1525bebb993Sopenharmony_ci * @param lines pointer to `Str`. 1535bebb993Sopenharmony_ci * @li rust_file.h:The file where the interface is located. 1545bebb993Sopenharmony_ci */ 1555bebb993Sopenharmony_civoid StrFree(Str* str); 1565bebb993Sopenharmony_ci 1575bebb993Sopenharmony_ci#ifdef __cplusplus 1585bebb993Sopenharmony_ci#if __cplusplus 1595bebb993Sopenharmony_ci} 1605bebb993Sopenharmony_ci#endif 1615bebb993Sopenharmony_ci#endif 1625bebb993Sopenharmony_ci 1635bebb993Sopenharmony_ci#endif //RUST_FILE_H