harfang3d/harfang/foundation/file.h

89 lines
2.1 KiB
C
Raw Normal View History

2021-10-13 12:40:31 +00:00
// HARFANG(R) Copyright (C) 2021 Emmanuel Julien, NWNC HARFANG. Released under GPL/LGPL/Commercial Licence, see licence.txt for details.
#pragma once
#include "foundation/data.h"
#include "foundation/generational_vector_list.h"
#include "foundation/seek_mode.h"
#include "foundation/time.h"
#include <cstddef>
#include <string>
namespace hg {
struct File {
gen_ref ref;
};
File Open(const char *path, bool silent = false);
File OpenText(const char *path, bool silent = false);
File OpenWrite(const char *path);
File OpenWriteText(const char *path);
2021-12-15 19:23:12 +00:00
File OpenAppendText(const char *path);
2021-10-13 12:40:31 +00:00
File OpenTemp(const char *tmplt);
2022-06-05 12:04:47 +00:00
/// Close a file handle.
2021-10-13 12:40:31 +00:00
bool Close(File file);
bool IsValid(File file);
bool IsEOF(File file);
size_t GetSize(File file);
size_t Read(File file, void *data, size_t size);
size_t Write(File file, const void *data, size_t size);
bool Seek(File file, ptrdiff_t offset, SeekMode mode);
size_t Tell(File file);
void Rewind(File file);
bool IsFile(const char *path);
bool Unlink(const char *path);
struct FileInfo {
2022-02-22 10:28:28 +00:00
bool is_file;
2021-10-13 12:40:31 +00:00
size_t size;
time_ns created;
time_ns modified;
};
FileInfo GetFileInfo(const char *path);
//
std::string ReadString(File file);
bool WriteString(File file, const std::string &v);
bool WriteStringAsText(File file, const std::string &v);
//
template <typename T> T Read(File file) {
T v;
Read(file, &v, sizeof(T));
return v;
}
template <typename T> bool Write(File file, const T &v) { return Write(file, &v, sizeof(T)) == sizeof(T); }
2022-06-05 12:04:47 +00:00
/// Copy a file on the local filesystem.
2021-10-13 12:40:31 +00:00
bool CopyFile(const char *src, const char *dst);
2022-06-05 12:04:47 +00:00
/// Return the content of a file on the local filesystem as a string.
2022-05-02 15:25:11 +00:00
std::string FileToString(const char *path, bool silent = false);
2022-06-05 12:04:47 +00:00
/// Write a string as a file on the local filesystem.
2021-10-13 12:40:31 +00:00
bool StringToFile(const char *path, const char *str);
2022-05-02 15:25:11 +00:00
Data FileToData(const char *path, bool silent = false);
2021-10-13 12:40:31 +00:00
//
struct ScopedFile {
ScopedFile(File file) : f(file) {}
~ScopedFile() { Close(f); }
operator const File &() const { return f; }
operator bool() const { return IsValid(f); }
File f;
};
} // namespace hg