-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfio.hpp
138 lines (107 loc) · 2.94 KB
/
fio.hpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @file fio.hpp
* @author cosine (cosinehit@gmail.com)
* @brief file i/o
* @version 0.1
* @date 2021-09-17
*
* @copyright Copyright (c) 2021
*
*/
#pragma once
#ifndef _FIO_HPP_
#define _FIO_HPP_
#include <cstdio>
#if __cplusplus <= 199711L && (!defined(_MSC_VER) || _MSC_VER < 1900) && \
(!defined(__GNUC__) || \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40603))
#ifndef CXX11_NOT_SUPPORT
#define CXX11_NOT_SUPPORT
#endif // CXX11_NOT_SUPPORT
#endif // __cplusplus<=199711L
#ifdef CXX11_NOT_SUPPORT
#define nullptr NULL
#define constexpr const
#define noexcept throw()
#define override
#endif // CXX11_NOT_SUPPORT
namespace fio {
enum enum_fio_mode { enum_mode_read = 0, enum_mode_rdwr };
class fio {
private:
FILE *p_file;
unsigned char p_mode;
bool p_opened;
inline const bool path_empty(const char *path) { return !path || !(*path); }
public:
fio() noexcept : p_file(nullptr), p_mode(enum_mode_read), p_opened(false) {}
~fio() noexcept { close_file(); }
bool create_file(const char *path) {
if (p_opened || path_empty(path)) {
return false;
}
p_file = fopen(path, "wb+");
if (p_file == nullptr) {
return false;
}
return p_opened = true;
}
bool open_file(const char *path, enum_fio_mode mode) {
if (p_opened || path_empty(path)) {
return false;
}
if (mode == enum_mode_read) {
p_file = fopen(path, "rb");
} else if (mode == enum_mode_rdwr) {
p_file = fopen(path, "wb+");
}
if (p_file == nullptr) {
return false;
}
p_mode = mode;
return p_opened = true;
}
void close_file() {
if (p_opened) {
fclose(p_file);
p_opened = false;
}
}
size_t file_size() {
if (!p_opened) {
return 0;
}
long pos = ftell(p_file);
fseek(p_file, 0, SEEK_END);
long len = ftell(p_file);
fseek(p_file, pos, SEEK_SET);
return len;
}
size_t read_file(void *buffer, size_t readnum) {
if (!p_opened || buffer == nullptr || !readnum) {
return 0;
}
return fread(buffer, sizeof(char), readnum, p_file);
}
size_t write_file(const void *buffer, size_t writenum) {
if (!p_opened || buffer == nullptr || !writenum) {
return 0;
}
return fwrite(buffer, sizeof(char), writenum, p_file);
}
off_t seek_file(int pos, off_t offset) {
if (!p_opened || p_file == nullptr) {
return -1;
}
fseek(p_file, offset, pos);
return ftell(p_file);
}
bool flush_file() {
if (!p_opened || p_mode == enum_mode_read) {
return false;
}
return (fflush(p_file) == 0);
}
};
} // namespace fio
#endif