-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmio.hpp
More file actions
363 lines (318 loc) · 7.67 KB
/
mio.hpp
File metadata and controls
363 lines (318 loc) · 7.67 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @file mio.hpp
* @author cosine (cosinehit@gmail.com)
* @brief multiplatform I/O class
* @version 0.1
*
* @copyright Copyright (c) 2021
*
*/
#pragma once
#ifndef _MIO_HPP_
#define _MIO_HPP_
#include <cstddef>
#include <cstring>
#ifdef _MSC_VER
#if (_MSC_VER < 1400)
typedef char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <cstdint>
#endif // _MSC_VER < 1400
#else
#include <cstdint>
#endif // _MSC_VER
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) // Windows
#ifndef _AFX // without MFC
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif // _AFX
#include <sys/types.h>
#else // Unix
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif // _WIN32
#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 mio {
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) // Windows
typedef HANDLE HMIO;
#ifndef invalid_handle
#define invalid_handle INVALID_HANDLE_VALUE
#endif // invalid_handle
#define _MIO_WIN
#else // Unix
#define invalid_handle -1
typedef int HMIO;
#endif // _WIN32
/**
* @brief I/O mode
*
*/
enum enum_mio_mode { enum_mode_read = 0, enum_mode_rdwr };
/**
* @brief seek base point
*
*/
enum enum_mio_pos { enum_pos_set = 0, enum_pos_cur, enum_pos_end };
class mio {
private:
HMIO p_file; //!< file discriptor
enum_mio_mode p_mode; //!< I/O mode
bool path_empty(const char *path) { return !path || !(*path); }
#ifndef CXX11_NOT_SUPPORT
mio(const mio &) = delete;
mio(const mio &&) = delete;
mio &operator=(const mio &) = delete;
mio &operator=(const mio &&) = delete;
#else
mio(const mio &);
mio &operator=(const mio &);
#endif // CXX11_NOT_SUPPORT
public:
/**
* @brief Construct a new mio object
*
*/
mio() noexcept { p_file = invalid_handle; }
/**
* @brief Destroy the mio object
*
*/
~mio() noexcept {
if (is_open()) {
close_file();
}
}
/**
* @brief file is open or not
*
* @return true already opened
* @return false already closed or not opened
*/
bool is_open() noexcept { return p_file != invalid_handle; }
bool create_file(const char *path, enum_mio_mode mode);
bool open_file(const char *path, enum_mio_mode mode);
void close_file();
size_t file_size();
size_t read_file(void *buffer, size_t readnum);
size_t write_file(const void *buffer, size_t writenum);
off_t seek_file(enum_mio_pos pos, off_t offset);
bool flush_file();
};
/**
* @brief create file
*
* @param path full path of file
* @param mode file mode
* @return true success
* @return false invalid path or create file failed
*/
inline bool mio::create_file(const char *path, enum_mio_mode mode) {
if (path_empty(path)) {
return false;
}
HMIO handle = invalid_handle;
#ifdef _MIO_WIN
handle =
CreateFile(path,
(mode == enum_mode_read) ? GENERIC_READ
: (GENERIC_READ | GENERIC_WRITE),
(FILE_SHARE_READ | FILE_SHARE_WRITE), 0, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0);
#else
handle = open(path,
(mode == enum_mode_read) ? (O_RDONLY | O_CREAT)
: (O_RDWR | O_CREAT),
(S_IRUSR | S_IWUSR));
#endif // _MIO_WIN
p_file = handle;
p_mode = mode;
return (handle != invalid_handle);
}
/**
* @brief open existing file
*
* @param path full path of file
* @param mode file mode
* @return true success
* @return false invalid path or open file failed
*/
inline bool mio::open_file(const char *path, enum_mio_mode mode) {
if (path_empty(path)) {
return false;
}
HMIO handle = invalid_handle;
#ifdef _MIO_WIN
handle =
CreateFile(path,
(mode == enum_mode_read) ? GENERIC_READ
: (GENERIC_READ | GENERIC_WRITE),
(FILE_SHARE_READ | FILE_SHARE_WRITE), 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
#else
handle = open(path, (mode == enum_mode_read) ? O_RDONLY : O_RDWR);
#endif // _MIO_WIN
p_file = handle;
p_mode = mode;
return (handle != invalid_handle);
}
/**
* @brief close file
*
*/
inline void mio::close_file() {
if (!is_open()) {
return;
}
#ifdef _MIO_WIN
CloseHandle(p_file);
#else
close(p_file);
#endif // _MIO_WIN
p_file = invalid_handle;
}
/**
* @brief get file size
*
* @return size_t file size on hardware
*/
inline size_t mio::file_size() {
if (!is_open()) {
return 0;
}
#ifdef _MIO_WIN
LARGE_INTEGER size;
if (GetFileSizeEx(p_file, &size) == FALSE) {
return 0;
}
return static_cast<size_t>(size.QuadPart);
#else
struct stat st;
if (fstat(p_file, &st) == -1) {
return 0;
}
return static_cast<size_t>(st.st_size);
#endif // _MIO_WIN
}
/**
* @brief read file
*
* @param buffer content buffer
* @param readnum desired read bytes
* @return size_t actual read bytes
*/
inline size_t mio::read_file(void *buffer, size_t readnum) {
if (!is_open()) {
return 0;
}
#ifdef _MIO_WIN
DWORD dwRead = 0;
if (!ReadFile(p_file, buffer, readnum, &dwRead, nullptr)) {
return 0;
}
return dwRead;
#else
return read(p_file, buffer, readnum);
#endif // _MIO_WIN
}
/**
* @brief write file
*
* @param buffer content buffer
* @param writenum desired writing bytes
* @return size_t actual written bytes
*/
inline size_t mio::write_file(const void *buffer, size_t writenum) {
if (!is_open()) {
return 0;
}
if (p_mode == enum_mode_read) {
return 0;
}
#ifdef _MIO_WIN
DWORD dwWrite = 0;
if (!WriteFile(p_file, buffer, writenum, &dwWrite, nullptr)) {
return 0;
}
return dwWrite;
#else
return write(p_file, buffer, writenum);
#endif // _MIO_WIN
}
/**
* @brief seek file
*
* @param pos seek file base point
* @param offset offset to the base point
* @return off_t new offset to the beginning of file
*/
inline off_t mio::seek_file(enum_mio_pos pos, off_t offset) {
if (!is_open()) {
return -1;
}
#ifdef _MIO_WIN
DWORD move;
#else
int move;
#endif // _MIO_WIN
switch (pos) {
case enum_pos_set:
move = 0;
break;
case enum_pos_cur:
move = 1;
break;
case enum_pos_end:
move = 2;
break;
default:
move = 1;
break;
}
#ifdef _MIO_WIN
DWORD newoff = SetFilePointer(p_file, offset, nullptr, move);
return static_cast<off_t>(newoff);
#else
off_t newoff = lseek(p_file, offset, move);
return newoff;
#endif // _MIO_WIN
}
/**
* @brief flush file
*
* @return true success
* @return false fail
*/
inline bool mio::flush_file() {
if (!is_open() || p_mode == enum_mode_read) {
return false;
}
#ifdef _MIO_WIN
return (FlushFileBuffers(p_file) == TRUE);
#else
return (fsync(p_file) == 0);
#endif // _MIO_WIN
}
} // namespace mio
#endif // _MIO_HPP_