blah/include/blah_filesystem.h

116 lines
3.0 KiB
C
Raw Permalink Normal View History

2020-08-26 15:38:01 +08:00
#pragma once
#include <blah_common.h>
#include <blah_string.h>
#include <blah_vector.h>
2020-08-26 15:38:01 +08:00
namespace Blah
{
class File;
using FileRef = Ref<File>;
2020-08-26 15:38:01 +08:00
enum class FileMode
{
2021-04-05 16:07:16 +08:00
// Opens an existing file for reading.
OpenRead,
// Opens an existing file for reading and writing.
Open,
// Creates a new file or overwrites an existing file for writing.
CreateWrite,
// Creates a new file or overwrites an existing file for reading and writing.
Create,
2020-08-26 15:38:01 +08:00
};
class File
{
protected:
File() = default;
public:
// Opens a file at the given path.
// If it fails, this will return an empty reference.
static FileRef open(const FilePath& path, FileMode mode);
// checks if the given file exists
static bool exists(const FilePath& path);
// deletes the given file
static bool destroy(const FilePath& path);
// Default Destructor
virtual ~File() = default;
// Gets the Mode the File was opened with
FileMode mode() const;
// Gets the File Length
virtual size_t length() = 0;
// Gets the current File Position
virtual size_t position() = 0;
// Seeks to the given position in the File
virtual size_t seek(size_t position) = 0;
// Reads from the File into the buffer, and returns how many bytes were successfully read
virtual size_t read(void* buffer, size_t length) = 0;
// Writes from the buffer into the File, nd returns how many bytes were successfully written
virtual size_t write(const void* buffer, size_t length) = 0;
private:
FileMode m_mode;
};
2020-08-26 15:38:01 +08:00
namespace Directory
{
2021-03-21 17:08:28 +08:00
// Creates a new directory at the given location.
// Returns false if unable to create the directory.
2020-08-26 15:38:01 +08:00
bool create(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Returns whether the given directory exists
2020-08-26 15:38:01 +08:00
bool exists(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Tries to delete a path and returns whether it was successful
bool destroy(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Enumerates over a directory and returns a list of files & directories
Vector<FilePath> enumerate(const FilePath& path, bool recursive = true);
// Opens the path in the File Explorer / Finder
2020-08-26 15:38:01 +08:00
void explore(const FilePath& path);
}
namespace Path
{
2021-03-21 17:08:28 +08:00
// Returns the file name of the path
2020-08-26 15:38:01 +08:00
FilePath get_file_name(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Returns the file name of the path, without the file extension
2020-08-26 15:38:01 +08:00
FilePath get_file_name_no_ext(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Returns the path without any file extensions
2020-08-26 15:38:01 +08:00
FilePath get_path_no_ext(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Returns relative path
2020-08-26 15:38:01 +08:00
FilePath get_path_after(const FilePath& path, const FilePath& after);
2021-03-21 17:08:28 +08:00
// Gets the top directory name from the path
2020-08-26 15:38:01 +08:00
FilePath get_directory_name(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Normalizes a path (removes ../, changes \\ to /, removes redundant slashes, etc)
2020-08-26 15:38:01 +08:00
FilePath normalize(const FilePath& path);
2021-03-21 17:08:28 +08:00
// Joins two paths together
2021-01-15 10:42:12 +08:00
FilePath join(const FilePath& a, const FilePath& b);
2021-03-21 17:08:28 +08:00
// Joins two paths together
2021-01-15 10:42:12 +08:00
template<typename ... Args>
FilePath join(const FilePath& a, const FilePath& b, const Args&... args)
{
return join(a, join(b, args...));
2021-01-15 10:42:12 +08:00
}
2020-08-26 15:38:01 +08:00
}
}