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.h | |
| 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.h')
| -rw-r--r-- | src/core/file_sys/vfs.h | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index db3c77eac..4a13b8378 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h | |||
| @@ -6,11 +6,11 @@ | |||
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include <string> | 8 | #include <string> |
| 9 | #include <string_view> | ||
| 9 | #include <type_traits> | 10 | #include <type_traits> |
| 10 | #include <vector> | 11 | #include <vector> |
| 11 | #include "boost/optional.hpp" | 12 | #include "boost/optional.hpp" |
| 12 | #include "common/common_types.h" | 13 | #include "common/common_types.h" |
| 13 | #include "common/file_util.h" | ||
| 14 | 14 | ||
| 15 | namespace FileSys { | 15 | namespace FileSys { |
| 16 | struct VfsFile; | 16 | struct VfsFile; |
| @@ -112,7 +112,7 @@ struct VfsFile : NonCopyable { | |||
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | // Renames the file to name. Returns whether or not the operation was successsful. | 114 | // Renames the file to name. Returns whether or not the operation was successsful. |
| 115 | virtual bool Rename(const std::string& name) = 0; | 115 | virtual bool Rename(std::string_view name) = 0; |
| 116 | }; | 116 | }; |
| 117 | 117 | ||
| 118 | // A class representing a directory in an abstract filesystem. | 118 | // A class representing a directory in an abstract filesystem. |
| @@ -121,27 +121,27 @@ struct VfsDirectory : NonCopyable { | |||
| 121 | 121 | ||
| 122 | // Retrives the file located at path as if the current directory was root. Returns nullptr if | 122 | // Retrives the file located at path as if the current directory was root. Returns nullptr if |
| 123 | // not found. | 123 | // not found. |
| 124 | virtual std::shared_ptr<VfsFile> GetFileRelative(const std::string& path) const; | 124 | virtual std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const; |
| 125 | // Calls GetFileRelative(path) on the root of the current directory. | 125 | // Calls GetFileRelative(path) on the root of the current directory. |
| 126 | virtual std::shared_ptr<VfsFile> GetFileAbsolute(const std::string& path) const; | 126 | virtual std::shared_ptr<VfsFile> GetFileAbsolute(std::string_view path) const; |
| 127 | 127 | ||
| 128 | // Retrives the directory located at path as if the current directory was root. Returns nullptr | 128 | // Retrives the directory located at path as if the current directory was root. Returns nullptr |
| 129 | // if not found. | 129 | // if not found. |
| 130 | virtual std::shared_ptr<VfsDirectory> GetDirectoryRelative(const std::string& path) const; | 130 | virtual std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const; |
| 131 | // Calls GetDirectoryRelative(path) on the root of the current directory. | 131 | // Calls GetDirectoryRelative(path) on the root of the current directory. |
| 132 | virtual std::shared_ptr<VfsDirectory> GetDirectoryAbsolute(const std::string& path) const; | 132 | virtual std::shared_ptr<VfsDirectory> GetDirectoryAbsolute(std::string_view path) const; |
| 133 | 133 | ||
| 134 | // Returns a vector containing all of the files in this directory. | 134 | // Returns a vector containing all of the files in this directory. |
| 135 | virtual std::vector<std::shared_ptr<VfsFile>> GetFiles() const = 0; | 135 | virtual std::vector<std::shared_ptr<VfsFile>> GetFiles() const = 0; |
| 136 | // Returns the file with filename matching name. Returns nullptr if directory dosen't have a | 136 | // Returns the file with filename matching name. Returns nullptr if directory dosen't have a |
| 137 | // file with name. | 137 | // file with name. |
| 138 | virtual std::shared_ptr<VfsFile> GetFile(const std::string& name) const; | 138 | virtual std::shared_ptr<VfsFile> GetFile(std::string_view name) const; |
| 139 | 139 | ||
| 140 | // Returns a vector containing all of the subdirectories in this directory. | 140 | // Returns a vector containing all of the subdirectories in this directory. |
| 141 | virtual std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const = 0; | 141 | virtual std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const = 0; |
| 142 | // Returns the directory with name matching name. Returns nullptr if directory dosen't have a | 142 | // Returns the directory with name matching name. Returns nullptr if directory dosen't have a |
| 143 | // directory with name. | 143 | // directory with name. |
| 144 | virtual std::shared_ptr<VfsDirectory> GetSubdirectory(const std::string& name) const; | 144 | virtual std::shared_ptr<VfsDirectory> GetSubdirectory(std::string_view name) const; |
| 145 | 145 | ||
| 146 | // Returns whether or not the directory can be written to. | 146 | // Returns whether or not the directory can be written to. |
| 147 | virtual bool IsWritable() const = 0; | 147 | virtual bool IsWritable() const = 0; |
| @@ -161,53 +161,56 @@ struct VfsDirectory : NonCopyable { | |||
| 161 | 161 | ||
| 162 | // Creates a new subdirectory with name name. Returns a pointer to the new directory or nullptr | 162 | // Creates a new subdirectory with name name. Returns a pointer to the new directory or nullptr |
| 163 | // if the operation failed. | 163 | // if the operation failed. |
| 164 | virtual std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) = 0; | 164 | virtual std::shared_ptr<VfsDirectory> CreateSubdirectory(std::string_view name) = 0; |
| 165 | // Creates a new file with name name. Returns a pointer to the new file or nullptr if the | 165 | // Creates a new file with name name. Returns a pointer to the new file or nullptr if the |
| 166 | // operation failed. | 166 | // operation failed. |
| 167 | virtual std::shared_ptr<VfsFile> CreateFile(const std::string& name) = 0; | 167 | virtual std::shared_ptr<VfsFile> CreateFile(std::string_view name) = 0; |
| 168 | 168 | ||
| 169 | // Creates a new file at the path relative to this directory. Also creates directories if | 169 | // Creates a new file at the path relative to this directory. Also creates directories if |
| 170 | // they do not exist and is supported by this implementation. Returns nullptr on any failure. | 170 | // they do not exist and is supported by this implementation. Returns nullptr on any failure. |
| 171 | virtual std::shared_ptr<VfsFile> CreateFileRelative(const std::string& path); | 171 | virtual std::shared_ptr<VfsFile> CreateFileRelative(std::string_view path); |
| 172 | 172 | ||
| 173 | // Creates a new file at the path relative to root of this directory. Also creates directories | 173 | // Creates a new file at the path relative to root of this directory. Also creates directories |
| 174 | // if they do not exist and is supported by this implementation. Returns nullptr on any failure. | 174 | // if they do not exist and is supported by this implementation. Returns nullptr on any failure. |
| 175 | virtual std::shared_ptr<VfsFile> CreateFileAbsolute(const std::string& path); | 175 | virtual std::shared_ptr<VfsFile> CreateFileAbsolute(std::string_view path); |
| 176 | 176 | ||
| 177 | // Creates a new directory at the path relative to this directory. Also creates directories if | 177 | // Creates a new directory at the path relative to this directory. Also creates directories if |
| 178 | // they do not exist and is supported by this implementation. Returns nullptr on any failure. | 178 | // they do not exist and is supported by this implementation. Returns nullptr on any failure. |
| 179 | virtual std::shared_ptr<VfsDirectory> CreateDirectoryRelative(const std::string& path); | 179 | virtual std::shared_ptr<VfsDirectory> CreateDirectoryRelative(std::string_view path); |
| 180 | 180 | ||
| 181 | // Creates a new directory at the path relative to root of this directory. Also creates | 181 | // Creates a new directory at the path relative to root of this directory. Also creates |
| 182 | // directories if they do not exist and is supported by this implementation. Returns nullptr on | 182 | // directories if they do not exist and is supported by this implementation. Returns nullptr on |
| 183 | // any failure. | 183 | // any failure. |
| 184 | virtual std::shared_ptr<VfsDirectory> CreateDirectoryAbsolute(const std::string& path); | 184 | virtual std::shared_ptr<VfsDirectory> CreateDirectoryAbsolute(std::string_view path); |
| 185 | 185 | ||
| 186 | // Deletes the subdirectory with name and returns true on success. | 186 | // Deletes the subdirectory with name and returns true on success. |
| 187 | virtual bool DeleteSubdirectory(const std::string& name) = 0; | 187 | virtual bool DeleteSubdirectory(std::string_view name) = 0; |
| 188 | // Deletes all subdirectories and files of subdirectory with name recirsively and then deletes | 188 | // Deletes all subdirectories and files of subdirectory with name recirsively and then deletes |
| 189 | // the subdirectory. Returns true on success. | 189 | // the subdirectory. Returns true on success. |
| 190 | virtual bool DeleteSubdirectoryRecursive(const std::string& name); | 190 | virtual bool DeleteSubdirectoryRecursive(std::string_view name); |
| 191 | // Returnes whether or not the file with name name was deleted successfully. | 191 | // Returnes whether or not the file with name name was deleted successfully. |
| 192 | virtual bool DeleteFile(const std::string& name) = 0; | 192 | virtual bool DeleteFile(std::string_view name) = 0; |
| 193 | 193 | ||
| 194 | // Returns whether or not this directory was renamed to name. | 194 | // Returns whether or not this directory was renamed to name. |
| 195 | virtual bool Rename(const std::string& name) = 0; | 195 | virtual bool Rename(std::string_view name) = 0; |
| 196 | 196 | ||
| 197 | // Returns whether or not the file with name src was successfully copied to a new file with name | 197 | // Returns whether or not the file with name src was successfully copied to a new file with name |
| 198 | // dest. | 198 | // dest. |
| 199 | virtual bool Copy(const std::string& src, const std::string& dest); | 199 | virtual bool Copy(std::string_view src, std::string_view dest); |
| 200 | 200 | ||
| 201 | // Interprets the file with name file instead as a directory of type directory. | 201 | // Interprets the file with name file instead as a directory of type directory. |
| 202 | // The directory must have a constructor that takes a single argument of type | 202 | // The directory must have a constructor that takes a single argument of type |
| 203 | // std::shared_ptr<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a | 203 | // std::shared_ptr<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a |
| 204 | // subdirectory in one call. | 204 | // subdirectory in one call. |
| 205 | template <typename Directory> | 205 | template <typename Directory> |
| 206 | bool InterpretAsDirectory(const std::string& file) { | 206 | bool InterpretAsDirectory(std::string_view file) { |
| 207 | auto file_p = GetFile(file); | 207 | auto file_p = GetFile(file); |
| 208 | if (file_p == nullptr) | 208 | |
| 209 | if (file_p == nullptr) { | ||
| 209 | return false; | 210 | return false; |
| 210 | return ReplaceFileWithSubdirectory(file, std::make_shared<Directory>(file_p)); | 211 | } |
| 212 | |||
| 213 | return ReplaceFileWithSubdirectory(file_p, std::make_shared<Directory>(file_p)); | ||
| 211 | } | 214 | } |
| 212 | 215 | ||
| 213 | protected: | 216 | protected: |
| @@ -221,10 +224,10 @@ protected: | |||
| 221 | struct ReadOnlyVfsDirectory : public VfsDirectory { | 224 | struct ReadOnlyVfsDirectory : public VfsDirectory { |
| 222 | bool IsWritable() const override; | 225 | bool IsWritable() const override; |
| 223 | bool IsReadable() const override; | 226 | bool IsReadable() const override; |
| 224 | std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) override; | 227 | std::shared_ptr<VfsDirectory> CreateSubdirectory(std::string_view name) override; |
| 225 | std::shared_ptr<VfsFile> CreateFile(const std::string& name) override; | 228 | std::shared_ptr<VfsFile> CreateFile(std::string_view name) override; |
| 226 | bool DeleteSubdirectory(const std::string& name) override; | 229 | bool DeleteSubdirectory(std::string_view name) override; |
| 227 | bool DeleteFile(const std::string& name) override; | 230 | bool DeleteFile(std::string_view name) override; |
| 228 | bool Rename(const std::string& name) override; | 231 | bool Rename(std::string_view name) override; |
| 229 | }; | 232 | }; |
| 230 | } // namespace FileSys | 233 | } // namespace FileSys |