-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtinyfs.h
74 lines (58 loc) · 1.6 KB
/
tinyfs.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
#ifndef included_tinyfs_h
#define included_tinyfs_h
#include <string>
#include <streams.h>
namespace tiny {
struct file {
CAutoFile* m_af{nullptr};
FILE* m_fp{nullptr};
long m_fsize{0};
file(file&& other) {
m_fp = other.m_fp;
m_fsize = other.m_fsize;
other.m_fp = nullptr;
}
file(FILE* fp) : m_fp(fp) {
if (m_fp) {
long pos = ftell(m_fp);
fseek(m_fp, SEEK_END, 0);
m_fsize = ftell(m_fp);
if (pos < m_fsize) fseek(m_fp, SEEK_SET, pos);
}
}
file(const std::string& path, const std::string& mode) : file(fopen(path.c_str(), mode.c_str())) {}
~file() {
if (m_fp) fclose(m_fp);
if (m_af) delete m_af;
}
bool has_data() { return m_fp && ftell(m_fp) < m_fsize; }
template<typename T>
inline size_t write(const std::vector<T>& tv) {
return fwrite(tv.data(), sizeof(T), tv.size(), m_fp);
}
template<typename T>
inline size_t write(const T& t) {
return fwrite(&t, sizeof(T), 1, m_fp);
}
template<typename T>
inline size_t read(T& t) {
return fread(&t, sizeof(T), 1, m_fp);
}
void close() {
if (m_fp) fclose(m_fp);
m_fp = nullptr;
}
CAutoFile& autofile() {
if (!m_af) {
m_af = new CAutoFile(m_fp, SER_DISK, 0);
m_fp = nullptr;
}
return *m_af;
}
};
typedef std::shared_ptr<file> File;
static inline File OpenFile(const std::string& path, const std::string& mode) {
return std::make_shared<file>(path, mode);
}
}
#endif // included_tinyfs_h