1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
/**
* @file FileSystem.h
* @brief Abstracts locating of content on different platforms
* @author Tobi Vollebregt
*
* Copyright (C) 2006. Licensed under the terms of the
* GNU GPL, v2 or later
*/
#ifndef FILESYSTEM_H
#define FILESYSTEM_H
#include <vector>
#include <string>
// winapi redifines these which breaks things
#if defined(CreateDirectory)
#undef CreateDirectory
#endif
#if defined(DeleteFile)
#undef DeleteFile
#endif
/**
* @brief filesystem interface
*
* Use this from the rest of the spring code, not FileSystemHandler!
*/
class FileSystem
{
public:
// flags for FindFiles
enum FindFilesBits {
RECURSE = (1 << 0),
INCLUDE_DIRS = (1 << 1),
ONLY_DIRS = (1 << 2),
};
// flags for LocateFile
enum LocateFileBits {
WRITE = (1 << 0),
CREATE_DIRS = (1 << 1),
};
public:
FileSystem() {}
// generic functions
/**
* @brief locate a file
*
* Attempts to locate a file.
*
* If the FileSystem::WRITE flag is set, the path is assembled in the
* writeable data-dir.
* If FileSystem::CREATE_DIRS is set it tries to create the subdirectory
* the file should live in.
*
* Otherwise (if flags == 0), it dispatches the call to
* FileSystemHandler::LocateFile(), which either searches for it
* in all the data directories.
*
* @return an absolute path to file on success, or the argument
* (relative path) on failure
*/
std::string LocateFile(std::string file, int flags = 0) const;
bool Remove(std::string file) const;
std::string LocateDir(std::string dir, int flags = 0) const;
std::vector<std::string> LocateDirs(const std::string& dir) const;
std::vector<std::string> FindDirsInDirectSubDirs(
const std::string& relPath) const;
// metadata read functions
size_t GetFilesize(std::string path) const;
// directory functions
bool CreateDirectory(std::string dir) const;
std::vector<std::string> FindFiles(std::string dir, const std::string& pattern, int flags = 0) const;
// convenience functions
std::string GetDirectory (const std::string& path) const;
std::string GetFilename (const std::string& path) const;
std::string GetBasename (const std::string& path) const;
std::string GetExtension (const std::string& path) const;
std::string glob_to_regex(const std::string& glob) const;
std::string& FixSlashes (std::string& path) const;
std::string& ForwardSlashes(std::string& path) const;
// access check functions
/**
* Returns true if path is a relative path that exists in one of the
* data-dirs.
*/
bool InReadDir(const std::string& path);
/**
* Returns true if path is a relative path that exists in the writable
* data-dir.
* As it is not known what the person initially creating
* this function, intended to do with prefix, it is ignored.
*/
bool InWriteDir(const std::string& path, const std::string& prefix = "");
private:
bool CheckFile(const std::string& file) const;
// bool CheckDir(const std::string& dir) const;
// FileSystem(const FileSystem&);
// FileSystem& operator=(const FileSystem&);
};
// basically acts like a namespace, but with semantics like configHandler has
extern FileSystem filesystem;
#endif // !FILESYSTEM_H
|