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
UTIL_BEGIN_NAMESPACE()30 UTIL_BEGIN_NAMESPACE()
31
32 namespace IoUtil {
33
34 enum class Status : uint32_t {
35 SUCCESS = 0,
36 ERROR_GENERAL = 1,
37 ERROR_COMPATIBILITY_MISMATCH = 3,
38 };
39
40 struct CompatibilityInfo {
41 uint32_t versionMajor{ 0 };
42 uint32_t versionMinor{ 0 };
43 BASE_NS::string type;
44 };
45
46 struct CompatibilityRange {
47 static const uint32_t IGNORE_VERSION = { ~0u };
48
49 uint32_t versionMajorMin{ IGNORE_VERSION };
50 uint32_t versionMajorMax{ IGNORE_VERSION };
51 uint32_t versionMinorMin{ IGNORE_VERSION };
52 uint32_t versionMinorMax{ IGNORE_VERSION };
53 BASE_NS::string type{};
54 };
55
56 struct SerializationResult {
57 Status status{ Status::SUCCESS };
58 CompatibilityInfo compatibilityInfo{};
59 BASE_NS::string error{};
60
61 operator bool()
62 {
63 return status == Status::SUCCESS;
64 }
65 };
66
67 bool WriteCompatibilityInfo(CORE_NS::json::standalone_value& jsonOut, const CompatibilityInfo& info);
68 SerializationResult CheckCompatibility(
69 const CORE_NS::json::value& json, BASE_NS::array_view<CompatibilityRange const> validVersions);
70
71 bool CreateDirectories(CORE_NS::IFileManager& fileManager, BASE_NS::string_view pathUri);
72 bool DeleteDirectory(CORE_NS::IFileManager& fileManager, BASE_NS::string_view directoryUri);
73
74 bool Copy(CORE_NS::IFileManager& fileManager, BASE_NS::string_view sourceUri, BASE_NS::string_view destinationUri);
75 bool Move(CORE_NS::IFileManager& fileManager, BASE_NS::string_view sourceUri, BASE_NS::string_view destinationUri);
76 bool CopyFile(CORE_NS::IFileManager& fileManager, BASE_NS::string_view sourceUri, BASE_NS::string_view destinationUri);
77 bool CopyDirectoryContents(
78 CORE_NS::IFileManager& fileManager, BASE_NS::string_view sourceUri, BASE_NS::string_view destinationUri);
79
80 bool SaveTextFile(CORE_NS::IFileManager& fileManager, BASE_NS::string_view fileUri, BASE_NS::string_view fileContents);
81 bool LoadTextFile(CORE_NS::IFileManager& fileManager, BASE_NS::string_view fileUri, BASE_NS::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, BASE_NS::string_view fromUri, BASE_NS::string_view toUri,
86 BASE_NS::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, BASE_NS::string_view dir, BASE_NS::string_view text,
91 BASE_NS::string_view replaceWith);
92
93 struct Replacement {
94 BASE_NS::string from;
95 BASE_NS::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, BASE_NS::string_view folderUri, BASE_NS::vector<Replacement> replacements);
100
101 void ReplaceTextInFile(
102 CORE_NS::IFileManager& fileManager, BASE_NS::string_view uri, BASE_NS::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 BASE_NS::string searchStr;
111 BASE_NS::string insertStr;
112 InsertType type;
113 };
114 void InsertInFile(
115 CORE_NS::IFileManager& fileManager, BASE_NS::string_view fileUri, BASE_NS::vector<Insertion> insertions);
116 void InsertInFile(CORE_NS::IFileManager& fileManager, BASE_NS::string_view fileUri, BASE_NS::string search,
117 BASE_NS::string insertion, InsertType type);
118 void ReplaceInString(BASE_NS::string& string, const BASE_NS::string& target, const BASE_NS::string& replacement);
119
120 } // namespace IoUtil
121
122 UTIL_END_NAMESPACE()
123
124 #endif // API_UTIL_IOUTIL_H
125