13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci
163f4cbf05Sopenharmony_ci/** @file file_ex.h
173f4cbf05Sopenharmony_ci*
183f4cbf05Sopenharmony_ci*  @brief Provides global file operation functions implemented in c_utils.
193f4cbf05Sopenharmony_ci*/
203f4cbf05Sopenharmony_ci
213f4cbf05Sopenharmony_ci/**
223f4cbf05Sopenharmony_ci* @defgroup FileReadWrite
233f4cbf05Sopenharmony_ci* @{
243f4cbf05Sopenharmony_ci* @brief Provides interfaces for reading data from and writing data to files.
253f4cbf05Sopenharmony_ci*
263f4cbf05Sopenharmony_ci* You can use the interfaces to read data from a file, write data to a file,
273f4cbf05Sopenharmony_ci* and search for the specified string.
283f4cbf05Sopenharmony_ci*/
293f4cbf05Sopenharmony_ci
303f4cbf05Sopenharmony_ci#ifndef UTILS_BASE_FILE_EX_H
313f4cbf05Sopenharmony_ci#define UTILS_BASE_FILE_EX_H
323f4cbf05Sopenharmony_ci
333f4cbf05Sopenharmony_ci#include <string>
343f4cbf05Sopenharmony_ci#include <vector>
353f4cbf05Sopenharmony_ci#ifdef UTILS_CXX_RUST
363f4cbf05Sopenharmony_ci#include "cxx.h"
373f4cbf05Sopenharmony_ci#endif
383f4cbf05Sopenharmony_ci
393f4cbf05Sopenharmony_cinamespace OHOS {
403f4cbf05Sopenharmony_ci#ifdef UTILS_CXX_RUST
413f4cbf05Sopenharmony_cibool RustLoadStringFromFile(const rust::String& filePath, rust::String& content);
423f4cbf05Sopenharmony_cibool RustLoadStringFromFd(int fd, rust::String& content);
433f4cbf05Sopenharmony_cibool RustLoadBufferFromFile(const rust::String& filePath, rust::vec<char>& content);
443f4cbf05Sopenharmony_cibool RustSaveBufferToFile(const rust::String& filePath, const rust::vec<char>& content, bool truncated);
453f4cbf05Sopenharmony_cibool RustSaveStringToFile(const rust::String& filePath, const rust::String& content, bool truncated);
463f4cbf05Sopenharmony_cibool RustSaveStringToFd(int fd, const rust::String& content);
473f4cbf05Sopenharmony_cibool RustFileExists(const rust::String& fileName);
483f4cbf05Sopenharmony_cibool RustStringExistsInFile(const rust::String& fileName, const rust::String& subStr, bool caseSensitive);
493f4cbf05Sopenharmony_ciint  RustCountStrInFile(const rust::String& fileName, const rust::String& subStr, bool caseSensitive);
503f4cbf05Sopenharmony_ci#endif
513f4cbf05Sopenharmony_ci/**
523f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
533f4cbf05Sopenharmony_ci * @brief Reads a string from a file.
543f4cbf05Sopenharmony_ci *
553f4cbf05Sopenharmony_ci * @param filePath Indicates the path of the target file.
563f4cbf05Sopenharmony_ci * @param content Indicates the <b>std::string</b> object used to hold
573f4cbf05Sopenharmony_ci * the data read.
583f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the string is read successfully;
593f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
603f4cbf05Sopenharmony_ci * @note The maximum file size is 32 MB.
613f4cbf05Sopenharmony_ci */
623f4cbf05Sopenharmony_cibool LoadStringFromFile(const std::string& filePath, std::string& content);
633f4cbf05Sopenharmony_ci
643f4cbf05Sopenharmony_ci/**
653f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
663f4cbf05Sopenharmony_ci * @brief Writes a string to a file.
673f4cbf05Sopenharmony_ci *
683f4cbf05Sopenharmony_ci * @param filePath Indicates the path of the target file.
693f4cbf05Sopenharmony_ci * @param content Indicates the <b>std::string</b> object to write.
703f4cbf05Sopenharmony_ci * @param truncated Indicates whether to truncate the original file.
713f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the string is written successfully;
723f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
733f4cbf05Sopenharmony_ci */
743f4cbf05Sopenharmony_cibool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated = true);
753f4cbf05Sopenharmony_ci
763f4cbf05Sopenharmony_ci/**
773f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
783f4cbf05Sopenharmony_ci * @brief Reads a string from a file specified by its file descriptor (FD).
793f4cbf05Sopenharmony_ci *
803f4cbf05Sopenharmony_ci * @param fd Indicates the FD of the file to read.
813f4cbf05Sopenharmony_ci * @param content Indicates the <b>std::string</b> object used to hold
823f4cbf05Sopenharmony_ci * the data read.
833f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the string is read successfully;
843f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
853f4cbf05Sopenharmony_ci */
863f4cbf05Sopenharmony_cibool LoadStringFromFd(int fd, std::string& content);
873f4cbf05Sopenharmony_ci
883f4cbf05Sopenharmony_ci/**
893f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
903f4cbf05Sopenharmony_ci * @brief Writes a string to a file specified by its FD.
913f4cbf05Sopenharmony_ci *
923f4cbf05Sopenharmony_ci * @param fd Indicates the FD of the file to write.
933f4cbf05Sopenharmony_ci * @param content Indicates the <b>std::string</b> object to write.
943f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the string is written successfully;
953f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
963f4cbf05Sopenharmony_ci */
973f4cbf05Sopenharmony_cibool SaveStringToFd(int fd, const std::string& content);
983f4cbf05Sopenharmony_ci
993f4cbf05Sopenharmony_ci/**
1003f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
1013f4cbf05Sopenharmony_ci * @brief Reads data as a vector from a file.
1023f4cbf05Sopenharmony_ci *
1033f4cbf05Sopenharmony_ci * @param filePath Indicates the path of the target file.
1043f4cbf05Sopenharmony_ci * @param content Indicates the <b>std::vector</b> object used to hold
1053f4cbf05Sopenharmony_ci * the data read.
1063f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the data is read successfully;
1073f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
1083f4cbf05Sopenharmony_ci */
1093f4cbf05Sopenharmony_cibool LoadBufferFromFile(const std::string& filePath, std::vector<char>& content);
1103f4cbf05Sopenharmony_ci
1113f4cbf05Sopenharmony_ci/**
1123f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
1133f4cbf05Sopenharmony_ci * @brief Writes data of a vector to a file.
1143f4cbf05Sopenharmony_ci *
1153f4cbf05Sopenharmony_ci * @param filePath Indicates the path of the target file.
1163f4cbf05Sopenharmony_ci * @param content Indicates the <b>std::vector</b> object to write.
1173f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the data is written successfully;
1183f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
1193f4cbf05Sopenharmony_ci */
1203f4cbf05Sopenharmony_cibool SaveBufferToFile(const std::string& filePath, const std::vector<char>& content, bool truncated = true);
1213f4cbf05Sopenharmony_ci
1223f4cbf05Sopenharmony_ci/**
1233f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
1243f4cbf05Sopenharmony_ci * @brief Checks whether a file exists.
1253f4cbf05Sopenharmony_ci *
1263f4cbf05Sopenharmony_ci * @param filePath Indicates the file to check.
1273f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the file exists; returns <b>false</b>
1283f4cbf05Sopenharmony_ci * if any error (e.g. Permission Denied) occurs.
1293f4cbf05Sopenharmony_ci */
1303f4cbf05Sopenharmony_cibool FileExists(const std::string& fileName);
1313f4cbf05Sopenharmony_ci
1323f4cbf05Sopenharmony_ci/**
1333f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
1343f4cbf05Sopenharmony_ci * @brief Checks whether a file contains the specified string.
1353f4cbf05Sopenharmony_ci *
1363f4cbf05Sopenharmony_ci * @param fileName Indicates the path of the target file.
1373f4cbf05Sopenharmony_ci * @param subStr Indicates the <b>std::string</b> object to check.
1383f4cbf05Sopenharmony_ci * @param caseSensitive Indicates whether the string is case-sensitive.
1393f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the file contains the specified string;
1403f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
1413f4cbf05Sopenharmony_ci */
1423f4cbf05Sopenharmony_cibool StringExistsInFile(const std::string& fileName, const std::string& subStr, bool caseSensitive = true);
1433f4cbf05Sopenharmony_ci
1443f4cbf05Sopenharmony_ci/**
1453f4cbf05Sopenharmony_ci * @ingroup FileReadWrite
1463f4cbf05Sopenharmony_ci * @brief Obtains the number of occurrences of the specified string in a file.
1473f4cbf05Sopenharmony_ci *
1483f4cbf05Sopenharmony_ci * @param fileName Indicates the path of the target file.
1493f4cbf05Sopenharmony_ci * @param subStr Indicates the <b>std::string</b> object to search.
1503f4cbf05Sopenharmony_ci * @param caseSensitive Indicates whether the string is case-sensitive.
1513f4cbf05Sopenharmony_ci * @return Returns the number of occurrences of the string in the file;
1523f4cbf05Sopenharmony_ci * returns <b>0</b> if <b>subStr</b> is null.
1533f4cbf05Sopenharmony_ci */
1543f4cbf05Sopenharmony_ciint  CountStrInFile(const std::string& fileName, const std::string& subStr, bool caseSensitive = true);
1553f4cbf05Sopenharmony_ci}
1563f4cbf05Sopenharmony_ci
1573f4cbf05Sopenharmony_ci#endif
1583f4cbf05Sopenharmony_ci
1593f4cbf05Sopenharmony_ci/**@}*/
160