-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.h
74 lines (63 loc) · 2.39 KB
/
File.h
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
#pragma once
#include <memory>
#include <string>
enum class SeekPoint
{
begin,
current,
end
};
class File
{
public:
static std::shared_ptr<File> create_always(const std::string &filename);
static std::shared_ptr<File> open_existing(const std::string &filename);
static std::shared_ptr<File> try_open_existing(const std::string& filename)
{
try
{
return open_existing(filename);
}
catch (...)
{
return {};
}
}
static void write_all_bytes(const std::string& filename, const void* data, size_t size);
static void write_all_text(const std::string& filename, const std::string& text);
static std::vector<uint8_t> read_all_bytes(const std::string& filename);
static std::string read_all_text(const std::string& filename);
uint8_t read_uint8() { uint8_t v; read(&v, sizeof(uint8_t)); return v; }
int8_t read_int8() { int8_t v; read(&v, sizeof(int8_t)); return v; }
uint16_t read_uint16() { uint16_t v; read(&v, sizeof(uint16_t)); return v; }
int16_t read_int16() { int16_t v; read(&v, sizeof(int16_t)); return v; }
uint32_t read_uint32() { uint32_t v; read(&v, sizeof(uint32_t)); return v; }
int32_t read_int32() { int32_t v; read(&v, sizeof(int32_t)); return v; }
uint64_t read_uint64() { uint64_t v; read(&v, sizeof(uint64_t)); return v; }
int64_t read_int64() { int64_t v; read(&v, sizeof(int64_t)); return v; }
uint16_t read_uint16_be() { return _byteswap_ushort(read_uint16()); }
int16_t read_int16_be() { return _byteswap_ushort(read_int16()); }
uint32_t read_uint32_be() { return _byteswap_ulong(read_uint32()); }
int32_t read_int32_be() { return _byteswap_ulong(read_int32()); }
virtual ~File() = default;
virtual int64_t size() = 0;
virtual void read(void *data, size_t size) = 0;
virtual void write(const void *data, size_t size) = 0;
virtual void seek(int64_t offset, SeekPoint origin = SeekPoint::begin) = 0;
virtual uint64_t tell() = 0;
};
class Directory
{
public:
static std::vector<std::string> files(const std::string &filename);
};
class FilePath
{
public:
static bool has_extension(const std::string &filename, const char *extension);
static std::string extension(const std::string &filename);
static std::string remove_extension(const std::string &filename);
static std::string last_component(const std::string &path);
static std::string remove_last_component(const std::string &path);
static std::string combine(const std::string &path1, const std::string &path2);
};