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#include "file_ex.h" 173f4cbf05Sopenharmony_ci#include <fstream> 183f4cbf05Sopenharmony_ci#include <iostream> 193f4cbf05Sopenharmony_ci#include <algorithm> 203f4cbf05Sopenharmony_ci#include <iterator> 213f4cbf05Sopenharmony_ci#include <sys/stat.h> 223f4cbf05Sopenharmony_ci#include <unistd.h> 233f4cbf05Sopenharmony_ci#include <fcntl.h> 243f4cbf05Sopenharmony_ci#include <cstdio> 253f4cbf05Sopenharmony_ci#include <securec.h> 263f4cbf05Sopenharmony_ci#include <cstring> 273f4cbf05Sopenharmony_ci#include "directory_ex.h" 283f4cbf05Sopenharmony_ci#include "utils_log.h" 293f4cbf05Sopenharmony_ci 303f4cbf05Sopenharmony_ciusing namespace std; 313f4cbf05Sopenharmony_ci 323f4cbf05Sopenharmony_ciconst int MAX_FILE_LENGTH = 32 * 1024 * 1024; 333f4cbf05Sopenharmony_ci 343f4cbf05Sopenharmony_cinamespace OHOS { 353f4cbf05Sopenharmony_ci#ifdef UTILS_CXX_RUST 363f4cbf05Sopenharmony_cibool RustLoadStringFromFile(const rust::String& filePath, rust::String& content) 373f4cbf05Sopenharmony_ci{ 383f4cbf05Sopenharmony_ci std::string tmpPath(filePath); 393f4cbf05Sopenharmony_ci std::string tmpContent(content); 403f4cbf05Sopenharmony_ci if (LoadStringFromFile(tmpPath, tmpContent)) { 413f4cbf05Sopenharmony_ci content = tmpContent; 423f4cbf05Sopenharmony_ci return true; 433f4cbf05Sopenharmony_ci } 443f4cbf05Sopenharmony_ci return false; 453f4cbf05Sopenharmony_ci} 463f4cbf05Sopenharmony_ci 473f4cbf05Sopenharmony_cibool RustLoadStringFromFd(int fd, rust::String& content) 483f4cbf05Sopenharmony_ci{ 493f4cbf05Sopenharmony_ci std::string tmpContent(content); 503f4cbf05Sopenharmony_ci if (LoadStringFromFd(fd, tmpContent)) { 513f4cbf05Sopenharmony_ci content = tmpContent; 523f4cbf05Sopenharmony_ci return true; 533f4cbf05Sopenharmony_ci } 543f4cbf05Sopenharmony_ci return false; 553f4cbf05Sopenharmony_ci} 563f4cbf05Sopenharmony_ci 573f4cbf05Sopenharmony_cibool RustLoadBufferFromFile(const rust::String& filePath, rust::vec<char>& content) 583f4cbf05Sopenharmony_ci{ 593f4cbf05Sopenharmony_ci std::string tmpPath(filePath); 603f4cbf05Sopenharmony_ci std::vector<char> tmpContent(content.begin(), content.end()); 613f4cbf05Sopenharmony_ci if (LoadBufferFromFile(tmpPath, tmpContent)) { 623f4cbf05Sopenharmony_ci std::copy(tmpContent.begin(), tmpContent.end(), std::back_inserter(content)); 633f4cbf05Sopenharmony_ci return true; 643f4cbf05Sopenharmony_ci } 653f4cbf05Sopenharmony_ci return false; 663f4cbf05Sopenharmony_ci} 673f4cbf05Sopenharmony_ci 683f4cbf05Sopenharmony_cibool RustSaveBufferToFile(const rust::String& filePath, const rust::vec<char>& content, bool truncated) 693f4cbf05Sopenharmony_ci{ 703f4cbf05Sopenharmony_ci std::string tmpPath(filePath); 713f4cbf05Sopenharmony_ci std::vector<char> tmpContent(content.begin(), content.end()); 723f4cbf05Sopenharmony_ci return SaveBufferToFile(tmpPath, tmpContent, truncated); 733f4cbf05Sopenharmony_ci} 743f4cbf05Sopenharmony_ci 753f4cbf05Sopenharmony_cibool RustSaveStringToFile(const rust::String& filePath, const rust::String& content, bool truncated) 763f4cbf05Sopenharmony_ci{ 773f4cbf05Sopenharmony_ci std::string tmpPath(filePath); 783f4cbf05Sopenharmony_ci std::string tmpContent(content); 793f4cbf05Sopenharmony_ci return SaveStringToFile(tmpPath, tmpContent, truncated); 803f4cbf05Sopenharmony_ci} 813f4cbf05Sopenharmony_ci 823f4cbf05Sopenharmony_cibool RustSaveStringToFd(int fd, const rust::String& content) 833f4cbf05Sopenharmony_ci{ 843f4cbf05Sopenharmony_ci std::string tmpContent(content); 853f4cbf05Sopenharmony_ci return SaveStringToFd(fd, tmpContent); 863f4cbf05Sopenharmony_ci} 873f4cbf05Sopenharmony_ci 883f4cbf05Sopenharmony_cibool RustFileExists(const rust::String& fileName) 893f4cbf05Sopenharmony_ci{ 903f4cbf05Sopenharmony_ci std::string tmpName(fileName); 913f4cbf05Sopenharmony_ci return FileExists(tmpName); 923f4cbf05Sopenharmony_ci} 933f4cbf05Sopenharmony_ci 943f4cbf05Sopenharmony_cibool RustStringExistsInFile(const rust::String& fileName, const rust::String& subStr, bool caseSensitive) 953f4cbf05Sopenharmony_ci{ 963f4cbf05Sopenharmony_ci std::string tmpName(fileName); 973f4cbf05Sopenharmony_ci std::string tmpStr(subStr); 983f4cbf05Sopenharmony_ci return StringExistsInFile(tmpName, tmpStr, caseSensitive); 993f4cbf05Sopenharmony_ci} 1003f4cbf05Sopenharmony_ci 1013f4cbf05Sopenharmony_ciint RustCountStrInFile(const rust::String& fileName, const rust::String& subStr, bool caseSensitive) 1023f4cbf05Sopenharmony_ci{ 1033f4cbf05Sopenharmony_ci std::string tmpName(fileName); 1043f4cbf05Sopenharmony_ci std::string tmpStr(subStr); 1053f4cbf05Sopenharmony_ci return CountStrInFile(tmpName, tmpStr, caseSensitive); 1063f4cbf05Sopenharmony_ci} 1073f4cbf05Sopenharmony_ci 1083f4cbf05Sopenharmony_ci#endif 1093f4cbf05Sopenharmony_ci 1103f4cbf05Sopenharmony_cibool LoadStringFromFile(const string& filePath, string& content) 1113f4cbf05Sopenharmony_ci{ 1123f4cbf05Sopenharmony_ci ifstream file(filePath.c_str()); 1133f4cbf05Sopenharmony_ci if (!file.is_open()) { 1143f4cbf05Sopenharmony_ci UTILS_LOGD("open file failed! filePath:%{public}s", filePath.c_str()); 1153f4cbf05Sopenharmony_ci return false; 1163f4cbf05Sopenharmony_ci } 1173f4cbf05Sopenharmony_ci 1183f4cbf05Sopenharmony_ci file.seekg(0, ios::end); 1193f4cbf05Sopenharmony_ci const long long fileLength = file.tellg(); 1203f4cbf05Sopenharmony_ci if (fileLength > MAX_FILE_LENGTH) { 1213f4cbf05Sopenharmony_ci UTILS_LOGD("invalid file length(%{public}lld)!", fileLength); 1223f4cbf05Sopenharmony_ci return false; 1233f4cbf05Sopenharmony_ci } 1243f4cbf05Sopenharmony_ci 1253f4cbf05Sopenharmony_ci content.clear(); 1263f4cbf05Sopenharmony_ci file.seekg(0, ios::beg); 1273f4cbf05Sopenharmony_ci copy(istreambuf_iterator<char>(file), istreambuf_iterator<char>(), back_inserter(content)); 1283f4cbf05Sopenharmony_ci return true; 1293f4cbf05Sopenharmony_ci} 1303f4cbf05Sopenharmony_ci 1313f4cbf05Sopenharmony_cistring GetFileNameByFd(const int fd) 1323f4cbf05Sopenharmony_ci{ 1333f4cbf05Sopenharmony_ci if (fd <= 0) { 1343f4cbf05Sopenharmony_ci return string(); 1353f4cbf05Sopenharmony_ci } 1363f4cbf05Sopenharmony_ci 1373f4cbf05Sopenharmony_ci string fdPath = "/proc/self/fd/" + std::to_string(fd); 1383f4cbf05Sopenharmony_ci char fileName[PATH_MAX + 1] = {0}; 1393f4cbf05Sopenharmony_ci 1403f4cbf05Sopenharmony_ci ssize_t ret = readlink(fdPath.c_str(), fileName, PATH_MAX); 1413f4cbf05Sopenharmony_ci if (ret < 0 || ret > PATH_MAX) { 1423f4cbf05Sopenharmony_ci UTILS_LOGD("Get fileName failed, ret is: %{public}d!", ret); 1433f4cbf05Sopenharmony_ci return string(); 1443f4cbf05Sopenharmony_ci } 1453f4cbf05Sopenharmony_ci fileName[ret] = '\0'; 1463f4cbf05Sopenharmony_ci return string(fileName); 1473f4cbf05Sopenharmony_ci} 1483f4cbf05Sopenharmony_ci 1493f4cbf05Sopenharmony_cibool LoadStringFromFdToFile(int fd, string& content) 1503f4cbf05Sopenharmony_ci{ 1513f4cbf05Sopenharmony_ci string fileName = GetFileNameByFd(fd); 1523f4cbf05Sopenharmony_ci if (fileName.empty()) { 1533f4cbf05Sopenharmony_ci UTILS_LOGD("LoadStringFromFd get file name by fd failed!"); 1543f4cbf05Sopenharmony_ci return false; 1553f4cbf05Sopenharmony_ci } 1563f4cbf05Sopenharmony_ci 1573f4cbf05Sopenharmony_ci if (!LoadStringFromFile(fileName, content)) { 1583f4cbf05Sopenharmony_ci UTILS_LOGE("LoadStringFromFd get string from file failed!"); 1593f4cbf05Sopenharmony_ci return false; 1603f4cbf05Sopenharmony_ci } 1613f4cbf05Sopenharmony_ci return true; 1623f4cbf05Sopenharmony_ci} 1633f4cbf05Sopenharmony_ci 1643f4cbf05Sopenharmony_cibool LoadStringFromFd(int fd, string& content) 1653f4cbf05Sopenharmony_ci{ 1663f4cbf05Sopenharmony_ci if (fd <= 0) { 1673f4cbf05Sopenharmony_ci UTILS_LOGD("invalid fd:%{public}d", fd); 1683f4cbf05Sopenharmony_ci return false; 1693f4cbf05Sopenharmony_ci } 1703f4cbf05Sopenharmony_ci 1713f4cbf05Sopenharmony_ci const off_t fileLength = lseek(fd, 0, SEEK_END); 1723f4cbf05Sopenharmony_ci if (fileLength > MAX_FILE_LENGTH) { 1733f4cbf05Sopenharmony_ci UTILS_LOGE("invalid file length(%{public}jd)!", static_cast<intmax_t>(fileLength)); 1743f4cbf05Sopenharmony_ci return false; 1753f4cbf05Sopenharmony_ci } 1763f4cbf05Sopenharmony_ci 1773f4cbf05Sopenharmony_ci // lseek is not support the linux file node 1783f4cbf05Sopenharmony_ci if (fileLength < 0) { 1793f4cbf05Sopenharmony_ci return LoadStringFromFdToFile(fd, content); 1803f4cbf05Sopenharmony_ci } 1813f4cbf05Sopenharmony_ci 1823f4cbf05Sopenharmony_ci if (fileLength == 0) { 1833f4cbf05Sopenharmony_ci return true; 1843f4cbf05Sopenharmony_ci } 1853f4cbf05Sopenharmony_ci 1863f4cbf05Sopenharmony_ci content.resize(fileLength); 1873f4cbf05Sopenharmony_ci off_t loc = lseek(fd, 0, SEEK_SET); 1883f4cbf05Sopenharmony_ci if (loc == -1) { 1893f4cbf05Sopenharmony_ci UTILS_LOGE("lseek file to begin failed!"); 1903f4cbf05Sopenharmony_ci return false; 1913f4cbf05Sopenharmony_ci } 1923f4cbf05Sopenharmony_ci 1933f4cbf05Sopenharmony_ci const ssize_t len = read(fd, content.data(), fileLength); 1943f4cbf05Sopenharmony_ci if (len != fileLength) { 1953f4cbf05Sopenharmony_ci UTILS_LOGE("the length read from file is not equal to fileLength!len:%{public}zd,fileLen:%{public}jd", 1963f4cbf05Sopenharmony_ci len, static_cast<intmax_t>(fileLength)); 1973f4cbf05Sopenharmony_ci return false; 1983f4cbf05Sopenharmony_ci } 1993f4cbf05Sopenharmony_ci 2003f4cbf05Sopenharmony_ci return true; 2013f4cbf05Sopenharmony_ci} 2023f4cbf05Sopenharmony_ci 2033f4cbf05Sopenharmony_cibool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated /*= true*/) 2043f4cbf05Sopenharmony_ci{ 2053f4cbf05Sopenharmony_ci if (content.empty()) { 2063f4cbf05Sopenharmony_ci UTILS_LOGI("content is empty, no need to save!"); 2073f4cbf05Sopenharmony_ci return true; 2083f4cbf05Sopenharmony_ci } 2093f4cbf05Sopenharmony_ci 2103f4cbf05Sopenharmony_ci ofstream file; 2113f4cbf05Sopenharmony_ci if (truncated) { 2123f4cbf05Sopenharmony_ci file.open(filePath.c_str(), ios::out | ios::trunc); 2133f4cbf05Sopenharmony_ci } else { 2143f4cbf05Sopenharmony_ci file.open(filePath.c_str(), ios::out | ios::app); 2153f4cbf05Sopenharmony_ci } 2163f4cbf05Sopenharmony_ci 2173f4cbf05Sopenharmony_ci if (!file.is_open()) { 2183f4cbf05Sopenharmony_ci UTILS_LOGD("open file failed! filePath:%{private}s", filePath.c_str()); 2193f4cbf05Sopenharmony_ci return false; 2203f4cbf05Sopenharmony_ci } 2213f4cbf05Sopenharmony_ci 2223f4cbf05Sopenharmony_ci file.write(content.c_str(), content.length()); 2233f4cbf05Sopenharmony_ci if (file.fail()) { 2243f4cbf05Sopenharmony_ci UTILS_LOGE("write content to file failed!file:%{private}s, content:%{private}s", 2253f4cbf05Sopenharmony_ci filePath.c_str(), content.c_str()); 2263f4cbf05Sopenharmony_ci return false; 2273f4cbf05Sopenharmony_ci } 2283f4cbf05Sopenharmony_ci return true; 2293f4cbf05Sopenharmony_ci} 2303f4cbf05Sopenharmony_ci 2313f4cbf05Sopenharmony_cibool SaveStringToFd(int fd, const std::string& content) 2323f4cbf05Sopenharmony_ci{ 2333f4cbf05Sopenharmony_ci if (fd <= 0) { 2343f4cbf05Sopenharmony_ci UTILS_LOGD("invalid fd:%{public}d", fd); 2353f4cbf05Sopenharmony_ci return false; 2363f4cbf05Sopenharmony_ci } 2373f4cbf05Sopenharmony_ci 2383f4cbf05Sopenharmony_ci if (content.empty()) { 2393f4cbf05Sopenharmony_ci UTILS_LOGI("content is empty, no need to save!"); 2403f4cbf05Sopenharmony_ci return true; 2413f4cbf05Sopenharmony_ci } 2423f4cbf05Sopenharmony_ci 2433f4cbf05Sopenharmony_ci const ssize_t len = write(fd, content.c_str(), content.length()); 2443f4cbf05Sopenharmony_ci if (len < 0) { 2453f4cbf05Sopenharmony_ci UTILS_LOGE("write file failed!errno:%{public}d, err:%{public}s", errno, strerror(errno)); 2463f4cbf05Sopenharmony_ci return false; 2473f4cbf05Sopenharmony_ci } 2483f4cbf05Sopenharmony_ci 2493f4cbf05Sopenharmony_ci if (static_cast<unsigned long>(len) != content.length()) { 2503f4cbf05Sopenharmony_ci UTILS_LOGE("the length write to file is not equal to fileLength!len:%{public}zd, fileLen:%{public}zu", 2513f4cbf05Sopenharmony_ci len, content.length()); 2523f4cbf05Sopenharmony_ci return false; 2533f4cbf05Sopenharmony_ci } 2543f4cbf05Sopenharmony_ci 2553f4cbf05Sopenharmony_ci return true; 2563f4cbf05Sopenharmony_ci} 2573f4cbf05Sopenharmony_ci 2583f4cbf05Sopenharmony_cibool LoadBufferFromNodeFile(const string& filePath, vector<char>& content) 2593f4cbf05Sopenharmony_ci{ 2603f4cbf05Sopenharmony_ci string realPath; 2613f4cbf05Sopenharmony_ci if (!PathToRealPath(filePath, realPath)) { 2623f4cbf05Sopenharmony_ci UTILS_LOGD("filePath to realPath failed! filePath:%{private}s", filePath.c_str()); 2633f4cbf05Sopenharmony_ci return false; 2643f4cbf05Sopenharmony_ci } 2653f4cbf05Sopenharmony_ci 2663f4cbf05Sopenharmony_ci FILE *fp = fopen(realPath.c_str(), "r"); 2673f4cbf05Sopenharmony_ci if (fp == nullptr) { 2683f4cbf05Sopenharmony_ci UTILS_LOGD("open file failed! filePath:%{private}s", realPath.c_str()); 2693f4cbf05Sopenharmony_ci return false; 2703f4cbf05Sopenharmony_ci } 2713f4cbf05Sopenharmony_ci 2723f4cbf05Sopenharmony_ci char ch = fgetc(fp); 2733f4cbf05Sopenharmony_ci int byteCount = 1; 2743f4cbf05Sopenharmony_ci while (!feof(fp)) { 2753f4cbf05Sopenharmony_ci if (byteCount > MAX_FILE_LENGTH) { 2763f4cbf05Sopenharmony_ci UTILS_LOGE("LoadBufferFromNodeFile invalid file length(%{public}d)!", byteCount); 2773f4cbf05Sopenharmony_ci fclose(fp); 2783f4cbf05Sopenharmony_ci fp = nullptr; 2793f4cbf05Sopenharmony_ci content.clear(); 2803f4cbf05Sopenharmony_ci return false; 2813f4cbf05Sopenharmony_ci } 2823f4cbf05Sopenharmony_ci 2833f4cbf05Sopenharmony_ci content.push_back(ch); 2843f4cbf05Sopenharmony_ci ch = fgetc(fp); 2853f4cbf05Sopenharmony_ci byteCount++; 2863f4cbf05Sopenharmony_ci } 2873f4cbf05Sopenharmony_ci 2883f4cbf05Sopenharmony_ci fclose(fp); 2893f4cbf05Sopenharmony_ci fp = nullptr; 2903f4cbf05Sopenharmony_ci return true; 2913f4cbf05Sopenharmony_ci} 2923f4cbf05Sopenharmony_ci 2933f4cbf05Sopenharmony_ci/* load file to buffer. If the buffer is not empty,then overwrite */ 2943f4cbf05Sopenharmony_cibool LoadBufferFromFile(const string& filePath, vector<char>& content) 2953f4cbf05Sopenharmony_ci{ 2963f4cbf05Sopenharmony_ci ifstream file; 2973f4cbf05Sopenharmony_ci file.open(filePath.c_str(), ios::in | ios::binary); 2983f4cbf05Sopenharmony_ci if (!file.is_open()) { 2993f4cbf05Sopenharmony_ci UTILS_LOGD("open file failed! filePath:%{private}s", filePath.c_str()); 3003f4cbf05Sopenharmony_ci return false; 3013f4cbf05Sopenharmony_ci } 3023f4cbf05Sopenharmony_ci 3033f4cbf05Sopenharmony_ci file.seekg(0, std::ios::end); 3043f4cbf05Sopenharmony_ci const long long fileLength = file.tellg(); 3053f4cbf05Sopenharmony_ci if (fileLength > MAX_FILE_LENGTH) { 3063f4cbf05Sopenharmony_ci UTILS_LOGD("invalid file length(%{public}lld)!", fileLength); 3073f4cbf05Sopenharmony_ci return false; 3083f4cbf05Sopenharmony_ci } 3093f4cbf05Sopenharmony_ci 3103f4cbf05Sopenharmony_ci // lseek is not support the linux file node 3113f4cbf05Sopenharmony_ci if (fileLength < 0) { 3123f4cbf05Sopenharmony_ci return LoadBufferFromNodeFile(filePath, content); 3133f4cbf05Sopenharmony_ci } 3143f4cbf05Sopenharmony_ci 3153f4cbf05Sopenharmony_ci if (fileLength == 0) { 3163f4cbf05Sopenharmony_ci content.clear(); 3173f4cbf05Sopenharmony_ci return true; 3183f4cbf05Sopenharmony_ci } 3193f4cbf05Sopenharmony_ci 3203f4cbf05Sopenharmony_ci file.seekg(0, std::ios::beg); 3213f4cbf05Sopenharmony_ci if (file.fail()) { 3223f4cbf05Sopenharmony_ci UTILS_LOGE("seekg file to begin failed!filePath:%{private}s", filePath.c_str()); 3233f4cbf05Sopenharmony_ci return false; 3243f4cbf05Sopenharmony_ci } 3253f4cbf05Sopenharmony_ci 3263f4cbf05Sopenharmony_ci content.resize(fileLength); 3273f4cbf05Sopenharmony_ci file.read(&content[0], fileLength); 3283f4cbf05Sopenharmony_ci return true; 3293f4cbf05Sopenharmony_ci} 3303f4cbf05Sopenharmony_ci 3313f4cbf05Sopenharmony_cibool SaveBufferToFile(const string& filePath, const vector<char>& content, bool truncated /*= true*/) 3323f4cbf05Sopenharmony_ci{ 3333f4cbf05Sopenharmony_ci if (content.empty()) { 3343f4cbf05Sopenharmony_ci UTILS_LOGI("content is empty, no need to save!"); 3353f4cbf05Sopenharmony_ci return true; 3363f4cbf05Sopenharmony_ci } 3373f4cbf05Sopenharmony_ci 3383f4cbf05Sopenharmony_ci // if the file is not exist,create it first! 3393f4cbf05Sopenharmony_ci uint32_t mode = truncated ? (ios::out | ios::binary | ios::trunc) : (ios::out | ios::binary | ios::app); 3403f4cbf05Sopenharmony_ci ofstream file; 3413f4cbf05Sopenharmony_ci file.open(filePath.c_str(), mode); 3423f4cbf05Sopenharmony_ci if (!file.is_open()) { 3433f4cbf05Sopenharmony_ci UTILS_LOGD("open file failed! filePath:%{private}s, mode:%{private}d", filePath.c_str(), mode); 3443f4cbf05Sopenharmony_ci return false; 3453f4cbf05Sopenharmony_ci } 3463f4cbf05Sopenharmony_ci 3473f4cbf05Sopenharmony_ci file.write(&content[0], content.size()); 3483f4cbf05Sopenharmony_ci file.flush(); 3493f4cbf05Sopenharmony_ci return true; 3503f4cbf05Sopenharmony_ci} 3513f4cbf05Sopenharmony_ci 3523f4cbf05Sopenharmony_cibool FileExists(const string& fileName) 3533f4cbf05Sopenharmony_ci{ 3543f4cbf05Sopenharmony_ci return (access(fileName.c_str(), F_OK) == 0); 3553f4cbf05Sopenharmony_ci} 3563f4cbf05Sopenharmony_ci 3573f4cbf05Sopenharmony_cibool StringExistsInFile(const string& fileName, const string& subStr, bool caseSensitive /*= true*/) 3583f4cbf05Sopenharmony_ci{ 3593f4cbf05Sopenharmony_ci if (subStr.empty()) { 3603f4cbf05Sopenharmony_ci UTILS_LOGD("String is empty"); 3613f4cbf05Sopenharmony_ci return false; 3623f4cbf05Sopenharmony_ci } 3633f4cbf05Sopenharmony_ci 3643f4cbf05Sopenharmony_ci string str; 3653f4cbf05Sopenharmony_ci if (!LoadStringFromFile(fileName, str)) { 3663f4cbf05Sopenharmony_ci UTILS_LOGD("File load fail, filePath:%{private}s", fileName.c_str()); 3673f4cbf05Sopenharmony_ci return false; 3683f4cbf05Sopenharmony_ci } 3693f4cbf05Sopenharmony_ci 3703f4cbf05Sopenharmony_ci if (caseSensitive) { 3713f4cbf05Sopenharmony_ci return (str.find(subStr) != string::npos); 3723f4cbf05Sopenharmony_ci } 3733f4cbf05Sopenharmony_ci 3743f4cbf05Sopenharmony_ci string strlower(str); 3753f4cbf05Sopenharmony_ci string sublower(subStr); 3763f4cbf05Sopenharmony_ci transform(str.begin(), str.end(), strlower.begin(), ::tolower); 3773f4cbf05Sopenharmony_ci transform(subStr.begin(), subStr.end(), sublower.begin(), ::tolower); 3783f4cbf05Sopenharmony_ci return (strlower.find(sublower) != string::npos); 3793f4cbf05Sopenharmony_ci} 3803f4cbf05Sopenharmony_ci 3813f4cbf05Sopenharmony_ciint CountStrInStr(const string& str, const string& subStr) 3823f4cbf05Sopenharmony_ci{ 3833f4cbf05Sopenharmony_ci if (subStr.empty()) { 3843f4cbf05Sopenharmony_ci UTILS_LOGD("subStr is empty"); 3853f4cbf05Sopenharmony_ci return 0; 3863f4cbf05Sopenharmony_ci } 3873f4cbf05Sopenharmony_ci 3883f4cbf05Sopenharmony_ci size_t position = 0; 3893f4cbf05Sopenharmony_ci int count = 0; 3903f4cbf05Sopenharmony_ci size_t length = subStr.length(); 3913f4cbf05Sopenharmony_ci while ((position = str.find(subStr, position)) != string::npos) { 3923f4cbf05Sopenharmony_ci position += length; 3933f4cbf05Sopenharmony_ci count++; 3943f4cbf05Sopenharmony_ci } 3953f4cbf05Sopenharmony_ci 3963f4cbf05Sopenharmony_ci return count; 3973f4cbf05Sopenharmony_ci} 3983f4cbf05Sopenharmony_ci 3993f4cbf05Sopenharmony_ciint CountStrInFile(const string& fileName, const string& subStr, bool caseSensitive /*= true*/) 4003f4cbf05Sopenharmony_ci{ 4013f4cbf05Sopenharmony_ci if (subStr.empty()) { 4023f4cbf05Sopenharmony_ci UTILS_LOGD("String is empty"); 4033f4cbf05Sopenharmony_ci return -1; 4043f4cbf05Sopenharmony_ci } 4053f4cbf05Sopenharmony_ci 4063f4cbf05Sopenharmony_ci string str; 4073f4cbf05Sopenharmony_ci if (!LoadStringFromFile(fileName, str)) { 4083f4cbf05Sopenharmony_ci UTILS_LOGD("File load fail, filePath:%{private}s", fileName.c_str()); 4093f4cbf05Sopenharmony_ci return -1; 4103f4cbf05Sopenharmony_ci } 4113f4cbf05Sopenharmony_ci 4123f4cbf05Sopenharmony_ci // If case-insensitive, strings are converted to lowercase. 4133f4cbf05Sopenharmony_ci if (caseSensitive) { 4143f4cbf05Sopenharmony_ci return CountStrInStr(str, subStr); 4153f4cbf05Sopenharmony_ci } 4163f4cbf05Sopenharmony_ci 4173f4cbf05Sopenharmony_ci string strlower(str); 4183f4cbf05Sopenharmony_ci string sublower(subStr); 4193f4cbf05Sopenharmony_ci transform(str.begin(), str.end(), strlower.begin(), ::tolower); 4203f4cbf05Sopenharmony_ci transform(subStr.begin(), subStr.end(), sublower.begin(), ::tolower); 4213f4cbf05Sopenharmony_ci return CountStrInStr(strlower, sublower); 4223f4cbf05Sopenharmony_ci} 4233f4cbf05Sopenharmony_ci} 424