winss
filesystem_interface.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIB_WINSS_FILESYSTEM_INTERFACE_HPP_
18 #define LIB_WINSS_FILESYSTEM_INTERFACE_HPP_
19 
20 #include <windows.h>
21 #include <filesystem>
22 #include <vector>
23 #include <memory>
24 #include <string>
25 
26 #define FILESYSTEM winss::FilesystemInterface::GetInstance()
27 
28 namespace fs = std::experimental::filesystem;
29 
30 namespace winss {
35  protected:
37  static std::shared_ptr<FilesystemInterface> instance;
38 
39  public:
44  FilesystemInterface(const FilesystemInterface&) = delete;
54  virtual std::string Read(const fs::path& path) const;
55 
63  virtual bool Write(const fs::path& path, const std::string& content) const;
64 
71  virtual bool ChangeDirectory(const fs::path& dir) const;
72 
79  virtual bool DirectoryExists(const fs::path& dir) const;
80 
87  virtual bool CreateDirectory(const fs::path& dir) const;
88 
96  virtual bool Rename(const fs::path& from, const fs::path& to) const;
97 
104  virtual bool Remove(const fs::path& path) const;
105 
112  virtual bool FileExists(const fs::path& path) const;
113 
120  virtual fs::path Absolute(const fs::path& path) const;
121 
128  virtual fs::path CanonicalUncPath(const fs::path& path) const;
129 
136  virtual std::vector<fs::path> GetDirectories(const fs::path& path) const;
137 
144  virtual std::vector<fs::path> GetFiles(const fs::path& path) const;
145 
151  static const FilesystemInterface& GetInstance();
152 
157 };
158 } // namespace winss
159 
160 #endif // LIB_WINSS_FILESYSTEM_INTERFACE_HPP_
virtual bool ChangeDirectory(const fs::path &dir) const
Change the current directory to the one given.
Definition: filesystem_interface.cpp:70
Definition: case_ignore.hpp:23
FilesystemInterface()
Creates the file system interface.
Definition: filesystem_interface.hpp:43
virtual bool DirectoryExists(const fs::path &dir) const
Checks if the directory exists.
Definition: filesystem_interface.cpp:81
An interface for interacting with the file system.
Definition: filesystem_interface.hpp:34
FilesystemInterface & operator=(const FilesystemInterface &)=delete
No copy.
virtual bool CreateDirectory(const fs::path &dir) const
Creates a directory.
Definition: filesystem_interface.cpp:91
virtual fs::path CanonicalUncPath(const fs::path &path) const
Gets the canonical UNC path.
Definition: filesystem_interface.cpp:158
virtual fs::path Absolute(const fs::path &path) const
Gets the absolute path.
Definition: filesystem_interface.cpp:149
virtual std::vector< fs::path > GetDirectories(const fs::path &path) const
Gets a list of directories at the given path.
Definition: filesystem_interface.cpp:199
static std::shared_ptr< FilesystemInterface > instance
A singleton instance.
Definition: filesystem_interface.hpp:37
virtual std::string Read(const fs::path &path) const
Reads the contents of the file at the given path.
Definition: filesystem_interface.cpp:32
virtual std::vector< fs::path > GetFiles(const fs::path &path) const
Gets a list of files at the given path.
Definition: filesystem_interface.cpp:222
virtual bool Write(const fs::path &path, const std::string &content) const
Writes the given content to a file.
Definition: filesystem_interface.cpp:51
static const FilesystemInterface & GetInstance()
Gets the singleton instance.
Definition: filesystem_interface.cpp:245
virtual bool Remove(const fs::path &path) const
Remove a file or empty directory.
Definition: filesystem_interface.cpp:126
virtual bool FileExists(const fs::path &path) const
Checks if a file exists.
Definition: filesystem_interface.cpp:137
virtual bool Rename(const fs::path &from, const fs::path &to) const
Renames a file/directory.
Definition: filesystem_interface.cpp:107