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 #ifndef API_UTIL_IOUTIL_H 17 #define API_UTIL_IOUTIL_H 18 19 #include <base/containers/string.h> 20 #include <base/containers/string_view.h> 21 #include <base/containers/unordered_map.h> 22 #include <base/containers/array_view.h> 23 #include <base/containers/unique_ptr.h> 24 25 #include <core/json/json.h> 26 #include <core/io/intf_file_manager.h> 27 28 #include <util/namespace.h> 29 30 namespace IoUtil { 31 32 enum class Status : uint32_t { 33 SUCCESS = 0, 34 ERROR_GENERAL = 1, 35 ERROR_COMPATIBILITY_MISMATCH = 3, 36 }; 37 38 struct CompatibilityInfo { 39 uint32_t versionMajor{ 0 }; 40 uint32_t versionMinor{ 0 }; 41 ::string type; 42 }; 43 44 struct CompatibilityRange { 45 static const uint32_t IGNORE_VERSION = { ~0u }; 46 47 uint32_t versionMajorMin{ IGNORE_VERSION }; 48 uint32_t versionMajorMax{ IGNORE_VERSION }; 49 uint32_t versionMinorMin{ IGNORE_VERSION }; 50 uint32_t versionMinorMax{ IGNORE_VERSION }; 51 ::string type{}; 52 }; 53 54 struct SerializationResult { 55 Status status{ Status::SUCCESS }; 56 CompatibilityInfo compatibilityInfo{}; 57 ::string error{}; 58 operator boolIoUtil::SerializationResult59 operator bool() 60 { 61 return status == Status::SUCCESS; 62 } 63 }; 64 65 bool WriteCompatibilityInfo(CORE_NS::json::standalone_value& jsonOut, const CompatibilityInfo& info); 66 SerializationResult CheckCompatibility( 67 const CORE_NS::json::value& json, ::array_view<CompatibilityRange const> validVersions); 68 69 bool FileExists(CORE_NS::IFileManager& fileManager, ::string_view folder, ::string_view file); 70 71 bool CreateDirectories(CORE_NS::IFileManager& fileManager, ::string_view pathUri); 72 bool DeleteDirectory(CORE_NS::IFileManager& fileManager, ::string_view directoryUri); 73 74 bool Copy(CORE_NS::IFileManager& fileManager, ::string_view sourceUri, ::string_view destinationUri); 75 bool Move(CORE_NS::IFileManager& fileManager, ::string_view sourceUri, ::string_view destinationUri); 76 bool CopyFile(CORE_NS::IFileManager& fileManager, ::string_view sourceUri, ::string_view destinationUri); 77 bool CopyDirectoryContents( 78 CORE_NS::IFileManager& fileManager, ::string_view sourceUri, ::string_view destinationUri); 79 80 bool SaveTextFile(CORE_NS::IFileManager& fileManager, ::string_view fileUri, ::string_view fileContents); 81 bool LoadTextFile(CORE_NS::IFileManager& fileManager, ::string_view fileUri, ::string& fileContentsOut); 82 83 // Copy files from fromUri dir to toUri dir, renaming the files to filename (excluding the filetype suffix) 84 // e.g. template.h, template.cpp -> filename.h, filename.cpp 85 bool CopyAndRenameFiles(CORE_NS::IFileManager& fileManager, ::string_view fromUri, ::string_view toUri, 86 ::string_view filename); 87 88 // Replace all instances of text dir's files with replaceWith, recursively 89 // only affects .h .cpp .txt and .json files 90 void ReplaceTextInFiles(CORE_NS::IFileManager& fileManager, ::string_view dir, ::string_view text, 91 ::string_view replaceWith); 92 93 struct Replacement { 94 ::string from; 95 ::string to; 96 }; 97 // A version of ReplaceTextInFiles that does multiple replacements while the file is open 98 void ReplaceTextInFiles( 99 CORE_NS::IFileManager& fileManager, ::string_view folderUri, ::vector<Replacement> replacements); 100 101 void ReplaceTextInFile( 102 CORE_NS::IFileManager& fileManager, ::string_view uri, ::vector<Replacement> replacements); 103 104 enum class InsertType : char { 105 TAG, // Finds the first instance of the tag string in the file and inserts a newline and insertion after it 106 SIGNATURE // Finds signature string in file, seeks next '{' character, 107 // and inserts a new line and the insertion string before the matching closing '}' 108 }; 109 struct Insertion { 110 ::string searchStr; 111 ::string insertStr; 112 InsertType type; 113 }; 114 void InsertInFile( 115 CORE_NS::IFileManager& fileManager, ::string_view fileUri, ::vector<Insertion> insertions); 116 void InsertInFile(CORE_NS::IFileManager& fileManager, ::string_view fileUri, ::string search, 117 ::string insertion, InsertType type); 118 void ReplaceInString(::string& string, const ::string& target, const ::string& replacement); 119 120 } // namespace IoUtil 121 122 UTIL_END_NAMESPACE() 123 124 #endif // API_UTIL_IOUTIL_H 125