diff options
| author | 2018-07-22 11:32:28 -0700 | |
|---|---|---|
| committer | 2018-07-22 11:32:28 -0700 | |
| commit | 5ee4c49c3049497fe1880e7edd722b931c688dee (patch) | |
| tree | b19bbe84497f300318b99ceaf2ec2aedf04ac6b5 /src/core/file_sys/vfs.cpp | |
| parent | Merge pull request #770 from lioncash/construct (diff) | |
| parent | vfs: Correct file_p variable usage within InterpretAsDirectory() (diff) | |
| download | yuzu-5ee4c49c3049497fe1880e7edd722b931c688dee.tar.gz yuzu-5ee4c49c3049497fe1880e7edd722b931c688dee.tar.xz yuzu-5ee4c49c3049497fe1880e7edd722b931c688dee.zip | |
Merge pull request #768 from lioncash/string-view
file_util, vfs: Use std::string_view where applicable
Diffstat (limited to 'src/core/file_sys/vfs.cpp')
| -rw-r--r-- | src/core/file_sys/vfs.cpp | 111 |
1 files changed, 73 insertions, 38 deletions
diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index 3f690f12a..b99a4fd5b 100644 --- a/src/core/file_sys/vfs.cpp +++ b/src/core/file_sys/vfs.cpp | |||
| @@ -13,7 +13,7 @@ namespace FileSys { | |||
| 13 | VfsFile::~VfsFile() = default; | 13 | VfsFile::~VfsFile() = default; |
| 14 | 14 | ||
| 15 | std::string VfsFile::GetExtension() const { | 15 | std::string VfsFile::GetExtension() const { |
| 16 | return FileUtil::GetExtensionFromFilename(GetName()); | 16 | return std::string(FileUtil::GetExtensionFromFilename(GetName())); |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | VfsDirectory::~VfsDirectory() = default; | 19 | VfsDirectory::~VfsDirectory() = default; |
| @@ -46,64 +46,80 @@ size_t VfsFile::WriteBytes(const std::vector<u8>& data, size_t offset) { | |||
| 46 | return Write(data.data(), data.size(), offset); | 46 | return Write(data.data(), data.size(), offset); |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | std::shared_ptr<VfsFile> VfsDirectory::GetFileRelative(const std::string& path) const { | 49 | std::shared_ptr<VfsFile> VfsDirectory::GetFileRelative(std::string_view path) const { |
| 50 | auto vec = FileUtil::SplitPathComponents(path); | 50 | auto vec = FileUtil::SplitPathComponents(path); |
| 51 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), | 51 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), |
| 52 | vec.end()); | 52 | vec.end()); |
| 53 | if (vec.empty()) | 53 | if (vec.empty()) { |
| 54 | return nullptr; | 54 | return nullptr; |
| 55 | if (vec.size() == 1) | 55 | } |
| 56 | |||
| 57 | if (vec.size() == 1) { | ||
| 56 | return GetFile(vec[0]); | 58 | return GetFile(vec[0]); |
| 59 | } | ||
| 60 | |||
| 57 | auto dir = GetSubdirectory(vec[0]); | 61 | auto dir = GetSubdirectory(vec[0]); |
| 58 | for (size_t component = 1; component < vec.size() - 1; ++component) { | 62 | for (size_t component = 1; component < vec.size() - 1; ++component) { |
| 59 | if (dir == nullptr) | 63 | if (dir == nullptr) { |
| 60 | return nullptr; | 64 | return nullptr; |
| 65 | } | ||
| 66 | |||
| 61 | dir = dir->GetSubdirectory(vec[component]); | 67 | dir = dir->GetSubdirectory(vec[component]); |
| 62 | } | 68 | } |
| 63 | if (dir == nullptr) | 69 | |
| 70 | if (dir == nullptr) { | ||
| 64 | return nullptr; | 71 | return nullptr; |
| 72 | } | ||
| 73 | |||
| 65 | return dir->GetFile(vec.back()); | 74 | return dir->GetFile(vec.back()); |
| 66 | } | 75 | } |
| 67 | 76 | ||
| 68 | std::shared_ptr<VfsFile> VfsDirectory::GetFileAbsolute(const std::string& path) const { | 77 | std::shared_ptr<VfsFile> VfsDirectory::GetFileAbsolute(std::string_view path) const { |
| 69 | if (IsRoot()) | 78 | if (IsRoot()) { |
| 70 | return GetFileRelative(path); | 79 | return GetFileRelative(path); |
| 80 | } | ||
| 71 | 81 | ||
| 72 | return GetParentDirectory()->GetFileAbsolute(path); | 82 | return GetParentDirectory()->GetFileAbsolute(path); |
| 73 | } | 83 | } |
| 74 | 84 | ||
| 75 | std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryRelative(const std::string& path) const { | 85 | std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryRelative(std::string_view path) const { |
| 76 | auto vec = FileUtil::SplitPathComponents(path); | 86 | auto vec = FileUtil::SplitPathComponents(path); |
| 77 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), | 87 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), |
| 78 | vec.end()); | 88 | vec.end()); |
| 79 | if (vec.empty()) | 89 | if (vec.empty()) { |
| 80 | // TODO(DarkLordZach): Return this directory if path is '/' or similar. Can't currently | 90 | // TODO(DarkLordZach): Return this directory if path is '/' or similar. Can't currently |
| 81 | // because of const-ness | 91 | // because of const-ness |
| 82 | return nullptr; | 92 | return nullptr; |
| 93 | } | ||
| 94 | |||
| 83 | auto dir = GetSubdirectory(vec[0]); | 95 | auto dir = GetSubdirectory(vec[0]); |
| 84 | for (size_t component = 1; component < vec.size(); ++component) { | 96 | for (size_t component = 1; component < vec.size(); ++component) { |
| 85 | if (dir == nullptr) | 97 | if (dir == nullptr) { |
| 86 | return nullptr; | 98 | return nullptr; |
| 99 | } | ||
| 100 | |||
| 87 | dir = dir->GetSubdirectory(vec[component]); | 101 | dir = dir->GetSubdirectory(vec[component]); |
| 88 | } | 102 | } |
| 103 | |||
| 89 | return dir; | 104 | return dir; |
| 90 | } | 105 | } |
| 91 | 106 | ||
| 92 | std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryAbsolute(const std::string& path) const { | 107 | std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryAbsolute(std::string_view path) const { |
| 93 | if (IsRoot()) | 108 | if (IsRoot()) { |
| 94 | return GetDirectoryRelative(path); | 109 | return GetDirectoryRelative(path); |
| 110 | } | ||
| 95 | 111 | ||
| 96 | return GetParentDirectory()->GetDirectoryAbsolute(path); | 112 | return GetParentDirectory()->GetDirectoryAbsolute(path); |
| 97 | } | 113 | } |
| 98 | 114 | ||
| 99 | std::shared_ptr<VfsFile> VfsDirectory::GetFile(const std::string& name) const { | 115 | std::shared_ptr<VfsFile> VfsDirectory::GetFile(std::string_view name) const { |
| 100 | const auto& files = GetFiles(); | 116 | const auto& files = GetFiles(); |
| 101 | const auto iter = std::find_if(files.begin(), files.end(), | 117 | const auto iter = std::find_if(files.begin(), files.end(), |
| 102 | [&name](const auto& file1) { return name == file1->GetName(); }); | 118 | [&name](const auto& file1) { return name == file1->GetName(); }); |
| 103 | return iter == files.end() ? nullptr : *iter; | 119 | return iter == files.end() ? nullptr : *iter; |
| 104 | } | 120 | } |
| 105 | 121 | ||
| 106 | std::shared_ptr<VfsDirectory> VfsDirectory::GetSubdirectory(const std::string& name) const { | 122 | std::shared_ptr<VfsDirectory> VfsDirectory::GetSubdirectory(std::string_view name) const { |
| 107 | const auto& subs = GetSubdirectories(); | 123 | const auto& subs = GetSubdirectories(); |
| 108 | const auto iter = std::find_if(subs.begin(), subs.end(), | 124 | const auto iter = std::find_if(subs.begin(), subs.end(), |
| 109 | [&name](const auto& file1) { return name == file1->GetName(); }); | 125 | [&name](const auto& file1) { return name == file1->GetName(); }); |
| @@ -128,77 +144,96 @@ size_t VfsDirectory::GetSize() const { | |||
| 128 | return file_total + subdir_total; | 144 | return file_total + subdir_total; |
| 129 | } | 145 | } |
| 130 | 146 | ||
| 131 | std::shared_ptr<VfsFile> VfsDirectory::CreateFileRelative(const std::string& path) { | 147 | std::shared_ptr<VfsFile> VfsDirectory::CreateFileRelative(std::string_view path) { |
| 132 | auto vec = FileUtil::SplitPathComponents(path); | 148 | auto vec = FileUtil::SplitPathComponents(path); |
| 133 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), | 149 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), |
| 134 | vec.end()); | 150 | vec.end()); |
| 135 | if (vec.empty()) | 151 | if (vec.empty()) { |
| 136 | return nullptr; | 152 | return nullptr; |
| 137 | if (vec.size() == 1) | 153 | } |
| 154 | |||
| 155 | if (vec.size() == 1) { | ||
| 138 | return CreateFile(vec[0]); | 156 | return CreateFile(vec[0]); |
| 157 | } | ||
| 158 | |||
| 139 | auto dir = GetSubdirectory(vec[0]); | 159 | auto dir = GetSubdirectory(vec[0]); |
| 140 | if (dir == nullptr) { | 160 | if (dir == nullptr) { |
| 141 | dir = CreateSubdirectory(vec[0]); | 161 | dir = CreateSubdirectory(vec[0]); |
| 142 | if (dir == nullptr) | 162 | if (dir == nullptr) { |
| 143 | return nullptr; | 163 | return nullptr; |
| 164 | } | ||
| 144 | } | 165 | } |
| 145 | 166 | ||
| 146 | return dir->CreateFileRelative(FileUtil::GetPathWithoutTop(path)); | 167 | return dir->CreateFileRelative(FileUtil::GetPathWithoutTop(path)); |
| 147 | } | 168 | } |
| 148 | 169 | ||
| 149 | std::shared_ptr<VfsFile> VfsDirectory::CreateFileAbsolute(const std::string& path) { | 170 | std::shared_ptr<VfsFile> VfsDirectory::CreateFileAbsolute(std::string_view path) { |
| 150 | if (IsRoot()) | 171 | if (IsRoot()) { |
| 151 | return CreateFileRelative(path); | 172 | return CreateFileRelative(path); |
| 173 | } | ||
| 174 | |||
| 152 | return GetParentDirectory()->CreateFileAbsolute(path); | 175 | return GetParentDirectory()->CreateFileAbsolute(path); |
| 153 | } | 176 | } |
| 154 | 177 | ||
| 155 | std::shared_ptr<VfsDirectory> VfsDirectory::CreateDirectoryRelative(const std::string& path) { | 178 | std::shared_ptr<VfsDirectory> VfsDirectory::CreateDirectoryRelative(std::string_view path) { |
| 156 | auto vec = FileUtil::SplitPathComponents(path); | 179 | auto vec = FileUtil::SplitPathComponents(path); |
| 157 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), | 180 | vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), |
| 158 | vec.end()); | 181 | vec.end()); |
| 159 | if (vec.empty()) | 182 | if (vec.empty()) { |
| 160 | return nullptr; | 183 | return nullptr; |
| 161 | if (vec.size() == 1) | 184 | } |
| 185 | |||
| 186 | if (vec.size() == 1) { | ||
| 162 | return CreateSubdirectory(vec[0]); | 187 | return CreateSubdirectory(vec[0]); |
| 188 | } | ||
| 189 | |||
| 163 | auto dir = GetSubdirectory(vec[0]); | 190 | auto dir = GetSubdirectory(vec[0]); |
| 164 | if (dir == nullptr) { | 191 | if (dir == nullptr) { |
| 165 | dir = CreateSubdirectory(vec[0]); | 192 | dir = CreateSubdirectory(vec[0]); |
| 166 | if (dir == nullptr) | 193 | if (dir == nullptr) { |
| 167 | return nullptr; | 194 | return nullptr; |
| 195 | } | ||
| 168 | } | 196 | } |
| 197 | |||
| 169 | return dir->CreateDirectoryRelative(FileUtil::GetPathWithoutTop(path)); | 198 | return dir->CreateDirectoryRelative(FileUtil::GetPathWithoutTop(path)); |
| 170 | } | 199 | } |
| 171 | 200 | ||
| 172 | std::shared_ptr<VfsDirectory> VfsDirectory::CreateDirectoryAbsolute(const std::string& path) { | 201 | std::shared_ptr<VfsDirectory> VfsDirectory::CreateDirectoryAbsolute(std::string_view path) { |
| 173 | if (IsRoot()) | 202 | if (IsRoot()) { |
| 174 | return CreateDirectoryRelative(path); | 203 | return CreateDirectoryRelative(path); |
| 204 | } | ||
| 205 | |||
| 175 | return GetParentDirectory()->CreateDirectoryAbsolute(path); | 206 | return GetParentDirectory()->CreateDirectoryAbsolute(path); |
| 176 | } | 207 | } |
| 177 | 208 | ||
| 178 | bool VfsDirectory::DeleteSubdirectoryRecursive(const std::string& name) { | 209 | bool VfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) { |
| 179 | auto dir = GetSubdirectory(name); | 210 | auto dir = GetSubdirectory(name); |
| 180 | if (dir == nullptr) | 211 | if (dir == nullptr) { |
| 181 | return false; | 212 | return false; |
| 213 | } | ||
| 182 | 214 | ||
| 183 | bool success = true; | 215 | bool success = true; |
| 184 | for (const auto& file : dir->GetFiles()) { | 216 | for (const auto& file : dir->GetFiles()) { |
| 185 | if (!DeleteFile(file->GetName())) | 217 | if (!DeleteFile(file->GetName())) { |
| 186 | success = false; | 218 | success = false; |
| 219 | } | ||
| 187 | } | 220 | } |
| 188 | 221 | ||
| 189 | for (const auto& sdir : dir->GetSubdirectories()) { | 222 | for (const auto& sdir : dir->GetSubdirectories()) { |
| 190 | if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) | 223 | if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) { |
| 191 | success = false; | 224 | success = false; |
| 225 | } | ||
| 192 | } | 226 | } |
| 193 | 227 | ||
| 194 | return success; | 228 | return success; |
| 195 | } | 229 | } |
| 196 | 230 | ||
| 197 | bool VfsDirectory::Copy(const std::string& src, const std::string& dest) { | 231 | bool VfsDirectory::Copy(std::string_view src, std::string_view dest) { |
| 198 | const auto f1 = GetFile(src); | 232 | const auto f1 = GetFile(src); |
| 199 | auto f2 = CreateFile(dest); | 233 | auto f2 = CreateFile(dest); |
| 200 | if (f1 == nullptr || f2 == nullptr) | 234 | if (f1 == nullptr || f2 == nullptr) { |
| 201 | return false; | 235 | return false; |
| 236 | } | ||
| 202 | 237 | ||
| 203 | if (!f2->Resize(f1->GetSize())) { | 238 | if (!f2->Resize(f1->GetSize())) { |
| 204 | DeleteFile(dest); | 239 | DeleteFile(dest); |
| @@ -216,23 +251,23 @@ bool ReadOnlyVfsDirectory::IsReadable() const { | |||
| 216 | return true; | 251 | return true; |
| 217 | } | 252 | } |
| 218 | 253 | ||
| 219 | std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectory::CreateSubdirectory(const std::string& name) { | 254 | std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectory::CreateSubdirectory(std::string_view name) { |
| 220 | return nullptr; | 255 | return nullptr; |
| 221 | } | 256 | } |
| 222 | 257 | ||
| 223 | std::shared_ptr<VfsFile> ReadOnlyVfsDirectory::CreateFile(const std::string& name) { | 258 | std::shared_ptr<VfsFile> ReadOnlyVfsDirectory::CreateFile(std::string_view name) { |
| 224 | return nullptr; | 259 | return nullptr; |
| 225 | } | 260 | } |
| 226 | 261 | ||
| 227 | bool ReadOnlyVfsDirectory::DeleteSubdirectory(const std::string& name) { | 262 | bool ReadOnlyVfsDirectory::DeleteSubdirectory(std::string_view name) { |
| 228 | return false; | 263 | return false; |
| 229 | } | 264 | } |
| 230 | 265 | ||
| 231 | bool ReadOnlyVfsDirectory::DeleteFile(const std::string& name) { | 266 | bool ReadOnlyVfsDirectory::DeleteFile(std::string_view name) { |
| 232 | return false; | 267 | return false; |
| 233 | } | 268 | } |
| 234 | 269 | ||
| 235 | bool ReadOnlyVfsDirectory::Rename(const std::string& name) { | 270 | bool ReadOnlyVfsDirectory::Rename(std::string_view name) { |
| 236 | return false; | 271 | return false; |
| 237 | } | 272 | } |
| 238 | } // namespace FileSys | 273 | } // namespace FileSys |