-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstdio-wrap.hpp
112 lines (90 loc) · 2.35 KB
/
stdio-wrap.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
#pragma once
#include <cstdint>
#include <cstdio> // make sure stdio isn't included after this
#include "engine/file.hpp"
// wrap stdio funcs around blit:: funcs
struct wrap_FILE
{
blit::File file;
uint32_t offset;
uint8_t getc_buffer[512];
int getc_buf_len, getc_buf_off;
};
inline wrap_FILE *wrap_fopen(const char *filename, const char *mode)
{
auto ret = new wrap_FILE;
ret->file.open(filename);
ret->offset = 0;
ret->getc_buf_len = ret->getc_buf_off = 0;
if(!ret->file.is_open())
{
delete ret;
return nullptr;
}
return ret;
}
// for MSVC
inline int wrap_fopen_s(wrap_FILE **f, const char *filename, const char *mode)
{
*f = wrap_fopen(filename, mode);
return *f == nullptr ? 1 : 0;
}
inline int wrap_fclose(wrap_FILE *file)
{
file->file.close();
delete file;
return 0;
}
inline size_t wrap_fread(void *buffer, size_t size, size_t count, wrap_FILE *file)
{
// invalidate getc buffer
if(file->getc_buf_len)
{
file->offset += file->getc_buf_off;
file->getc_buf_off = file->getc_buf_len = 0;
}
auto ret = file->file.read(file->offset, size * count, (char *)buffer);
file->offset += ret;
return ret < 0 ? 0 : ret / size;
}
inline int wrap_fgetc(wrap_FILE *file)
{
// refill getc buffer
if(file->getc_buf_off >= file->getc_buf_len)
{
file->offset += file->getc_buf_off;
file->getc_buf_len = file->file.read(file->offset, 512, (char*)file->getc_buffer);
file->getc_buf_off = 0;
}
if(file->getc_buf_len)
return file->getc_buffer[file->getc_buf_off++];
return EOF;
}
inline int wrap_fseek(wrap_FILE *file, long offset, int origin)
{
// invalidate getc buffer
if(file->getc_buf_len)
{
file->offset += file->getc_buf_off;
file->getc_buf_off = file->getc_buf_len = 0;
}
if(origin == SEEK_SET)
file->offset = offset;
else if(origin == SEEK_CUR)
file->offset += offset;
else
file->offset = file->file.get_length() - offset;
return 0;
}
inline long wrap_ftell(wrap_FILE *file)
{
return file->offset + file->getc_buf_off;
}
#define FILE wrap_FILE
#define fopen wrap_fopen
#define fopen_s wrap_fopen_s
#define fclose wrap_fclose
#define fread wrap_fread
#define fgetc wrap_fgetc
#define fseek wrap_fseek
#define ftell wrap_ftell