Dawn/src/dawntools/util/File.hpp

115 lines
2.9 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "assert/assert.hpp"
#include "util/mathutils.hpp"
#if defined(_MSC_VER)
#include <direct.h>
#include <windows.h>
#define getcwd _getcwd
#define FILE_PATH_SEP '\\'
#define fileMkdir(path, perms) _mkdir(path)
#elif defined(__GNUC__)
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#define FILE_PATH_SEP '/'
#define fileMkdir(path, perms) mkdir(path, perms)
#endif
#define FILE_BUFFER_SIZE 512
namespace Dawn {
enum FileMode {
FILE_MODE_READ,
FILE_MODE_WRITE
};
class File {
private:
FILE *file = nullptr;
enum FileMode mode;
size_t length;
public:
static std::string normalizeSlashes(std::string str);
static void mkdirp(std::string path);
std::string filename;
/**
* Constructs a new File interface class.
*
* @param filename Filename that you want to interface with.
*/
File(std::string filename);
/**
* Opens a connection to the file.
*
* @param mode File mode to use for this interface.
* @return True if success, otherwise for failure.
*/
bool_t open(enum FileMode mode);
/**
* Returns whether or not the file connection is currently opened.
*
* @return True if the connection is open, otherwise if it's not.
*/
bool_t isOpen();
/**
* Returns whether or not the file exists. Will open the connection if it
* does exist.
*
* @return True if exists, otherwsie if it doesn't.
*/
bool_t exists();
/**
* Closes the currently open interface to the file. Done automatically
* when this object is disposed.
*/
void close();
/**
* Makes all directories above this file's filename.
*
* @return True if successful, otherwise false.
*/
bool_t mkdirp();
/**
* Reads the entire contents of a file to the given output string buffer.
* This is a bit dangerous since the length of the file isn't checked
* against the memory to be consumed.
*
* @param out Pointer to a string where the output data will be stored.
* @return True if the read was successful, otherwise false.
*/
bool_t readString(std::string *out);
/**
* Writes the entire contents of a string to a file.
*
* @param in String to write to this file.
* @return True if written successfully, otherwise false.
*/
bool_t writeString(std::string in);
/**
* Write raw bytes to the file.
*
* @param data Data to write.
* @return True if written successfully, otherwise false.
*/
bool_t writeRaw(char *data, size_t );
~File();
};
}