2020-08-26 15:38:01 +08:00
|
|
|
#pragma once
|
2022-10-02 04:29:51 +08:00
|
|
|
#include <blah_common.h>
|
|
|
|
#include <blah_string.h>
|
|
|
|
#include <blah_vector.h>
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
namespace Blah
|
|
|
|
{
|
2021-05-10 08:23:02 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-05-10 08:23:02 +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;
|
|
|
|
|
2022-02-13 04:19:53 +08:00
|
|
|
// Gets the Mode the File was opened with
|
|
|
|
FileMode mode() const;
|
|
|
|
|
2021-05-10 08:23:02 +08:00
|
|
|
// 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
|
2022-02-13 04:19:53 +08:00
|
|
|
virtual size_t read(void* buffer, size_t length) = 0;
|
2021-05-10 08:23:02 +08:00
|
|
|
|
|
|
|
// Writes from the buffer into the File, nd returns how many bytes were successfully written
|
2022-02-13 04:19:53 +08:00
|
|
|
virtual size_t write(const void* buffer, size_t length) = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileMode m_mode;
|
2021-05-10 08:23:02 +08:00
|
|
|
};
|
|
|
|
|
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
|
2021-05-10 08:23:02 +08:00
|
|
|
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)
|
|
|
|
{
|
2021-12-13 12:41:23 +08:00
|
|
|
return join(a, join(b, args...));
|
2021-01-15 10:42:12 +08:00
|
|
|
}
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|
|
|
|
}
|