📄 fileutils.h
字号:
#ifndef UTILS_BASE_FILEUTILS_H_#define UTILS_BASE_FILEUTILS_H_#include <string>#ifdef _WINDOWS#include <windows.h>#else#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <unistd.h>#endif#include "common.h"namespace utils_base {class FileStream;class Pathname;//////////////////////////// Directory Iterator ////////////////////////////// A DirectoryTraverser is created with a given directory. It originally points to// the first file in the directory, and can be advanecd with Next(). This allows you// to get information about each file.class DirectoryIterator { public: // Constructor DirectoryIterator(); // Destructor ~DirectoryIterator(); // Starts traversing a directory // dir is the directory to traverse // returns true if the directory exists and is valid // The iterator will point to the first entry in the directory bool Iterate(const Pathname &path); // Advances to the next file // returns true if there were more files in the directory. bool Next(); // returns true if the file currently pointed to is a directory bool IsDirectory() const; // returns the name of the file currently pointed to std::string Name() const; // returns the size of the file currently pointed to size_t FileSize() const; // returns the last modified time of the file currently poitned to time_t FileModifyTime() const; private: std::string directory_;#ifdef _WINDOWS WIN32_FIND_DATA data_; HANDLE handle_;#else DIR *dir_; struct dirent *dirent_; struct stat stat_;#endif};class Filesystem { public: virtual bool CreateFolderI(const Pathname &pathname) = 0; // Opens a file. Returns an open StreamInterface if function succeeds. Otherwise, // returns NULL. virtual FileStream *OpenFileI(const Pathname &filename, const std::string &mode) = 0; // This will attempt to delete the path located at filename. If filename is a file, // it will be unlinked. If the path is a directory, it will recursively unlink and remove // all the files and directory within it virtual bool DeleteFileI(const Pathname &filename) = 0; // Creates a directory. This will call itself recursively to create /foo/bar even if // /foo does not exist. // Returns TRUE if function succeeds // This moves a file from old_path to new_path, where "file" can be a plain file // or directory, which will be moved recursively. // Returns true if function succeeds. virtual bool MoveFileI(const Pathname &old_path, const Pathname &new_path) = 0; // This copies a file from old_path to _new_path where "file" can be a plain file // or directory, which will be copied recursively. // Returns true if function succeeds virtual bool CopyFileI(const Pathname &old_path, const Pathname &new_path) = 0; // Returns true if a pathname is a directory virtual bool IsFolderI(const Pathname& pathname) = 0; // Returns true if a file exists at this path virtual bool FileExistsI(const Pathname& pathname) = 0; // Returns true if pathname represents a temporary location on the system. virtual bool IsTemporaryPathI(const Pathname& pathname) = 0; // A folder appropriate for storing temporary files (Contents are // automatically deleted when the program exists) virtual bool GetTemporaryFolderI(Pathname &path, bool create, const std::string *append) = 0; virtual std::string TempFilenameI(const Pathname &dir, const std::string &prefix) = 0; virtual bool GetFileSizeI(const Pathname &dir, size_t *size) = 0; static Filesystem *default_filesystem(void) { ASSERT(default_filesystem_!=NULL); return default_filesystem_; } static void set_default_filesystem(Filesystem *filesystem) {default_filesystem_ = filesystem; } static bool CreateFolder(const Pathname &pathname); static FileStream *OpenFile(const Pathname &filename, const std::string &mode); static bool DeleteFile(const Pathname &filename); static bool MoveFile(const Pathname &old_path, const Pathname &new_path); static bool CopyFile(const Pathname &old_path, const Pathname &new_path); static bool IsFolder(const Pathname& pathname); static bool FileExists(const Pathname &pathname); static bool IsTemporaryPath(const Pathname& pathname); static bool GetTemporaryFolder(Pathname &path, bool create, const std::string *append); static std::string TempFilename(const Pathname &dir, const std::string &prefix); static bool GetFileSize(const Pathname &dir, size_t *size); private: static Filesystem *default_filesystem_; static Filesystem *EnsureDefaultFilesystem();};// Generates a unique temporary filename in 'directory' with the given 'prefix' std::string TempFilename(const Pathname &dir, const std::string &prefix); // Generates a unique filename based on the input path. If no path component // is specified, it uses the temporary directory. If a filename is provided, // up to 100 variations of form basename-N.extension are tried. When // create_empty is true, an empty file of this name is created (which // decreases the chance of a temporary filename collision with another // process). bool CreateUniqueFile(utils_base::Pathname& path, bool create_empty);}#endif // UTILS_BASE_FILEUTILS_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -