forked from InfiniteRasa/Game-Server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileMgr.cpp
188 lines (162 loc) · 3.96 KB
/
fileMgr.cpp
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
#include"framework.h"
file_t *fileMgr_open(sint8 *name)
{
HANDLE hFile = CreateFile((LPCSTR)name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if( hFile == INVALID_HANDLE_VALUE )
return NULL;
file_t *file = (file_t*)malloc(sizeof(file_t));
file->hFile = hFile;
return file;
}
file_t *fileMgr_create(sint8 *name)
{
HANDLE hFile = CreateFile((LPCSTR)name, FILE_ALL_ACCESS, FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
if( hFile == INVALID_HANDLE_VALUE )
return NULL;
file_t *file = (file_t*)malloc(sizeof(file_t));
file->hFile = hFile;
return file;
}
void fileMgr_close(file_t *file)
{
if( file )
{
CloseHandle(file->hFile);
free((void*)file);
}
}
// todo: add cache for writing
void fileMgr_writeS8(file_t *file, sint8 value)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)&value, sizeof(sint8), &BT, NULL);
}
void fileMgr_writeS16(file_t *file, sint16 value)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)&value, sizeof(sint16), &BT, NULL);
}
void fileMgr_writeS32(file_t *file, sint32 value)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)&value, sizeof(sint32), &BT, NULL);
}
void fileMgr_writeU8(file_t *file, uint8 value)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)&value, sizeof(uint8), &BT, NULL);
}
void fileMgr_writeU16(file_t *file, uint16 value)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)&value, sizeof(uint16), &BT, NULL);
}
void fileMgr_writeU32(file_t *file, uint32 value)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)&value, sizeof(uint32), &BT, NULL);
}
void fileMgr_writeFloat(file_t *file, float value)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)&value, sizeof(float), &BT, NULL);
}
void fileMgr_writeData(file_t *file, void *data, sint32 len)
{
DWORD BT;
WriteFile(file->hFile, (LPCVOID)data, len, &BT, NULL);
}
sint8 fileMgr_readS8(file_t *file)
{
DWORD BT;
sint8 value;
ReadFile(file->hFile, (LPVOID)&value, sizeof(sint8), &BT, NULL);
return value;
}
sint16 fileMgr_readS16(file_t *file)
{
DWORD BT;
sint16 value;
ReadFile(file->hFile, (LPVOID)&value, sizeof(sint16), &BT, NULL);
return value;
}
sint32 fileMgr_readS32(file_t *file)
{
DWORD BT;
sint32 value;
ReadFile(file->hFile, (LPVOID)&value, sizeof(sint32), &BT, NULL);
return value;
}
uint8 fileMgr_readU8(file_t *file)
{
DWORD BT;
uint8 value;
ReadFile(file->hFile, (LPVOID)&value, sizeof(uint8), &BT, NULL);
return value;
}
uint16 fileMgr_readU16(file_t *file)
{
DWORD BT;
uint16 value;
ReadFile(file->hFile, (LPVOID)&value, sizeof(uint16), &BT, NULL);
return value;
}
uint32 fileMgr_readU32(file_t *file)
{
DWORD BT;
uint32 value;
ReadFile(file->hFile, (LPVOID)&value, sizeof(uint32), &BT, NULL);
return value;
}
float fileMgr_readFloat(file_t *file)
{
DWORD BT;
float value;
ReadFile(file->hFile, (LPVOID)&value, sizeof(float), &BT, NULL);
return value;
}
void fileMgr_readData(file_t *file, void *data, sint32 len)
{
DWORD BT;
ReadFile(file->hFile, (LPVOID)data, len, &BT, NULL);
}
sint8 *fileMgr_readLine(file_t *file)
{
// todo: optimize this..
DWORD currentSeek = SetFilePointer(file->hFile, 0, NULL, FILE_CURRENT);
DWORD fileSize = GetFileSize(file->hFile, NULL);
DWORD maxLen = fileSize - currentSeek;
if( maxLen == 0 )
return NULL; // eof reached
// begin parsing
sint8 *cstr = (sint8*)malloc(512);
sint32 size = 0;
sint32 limit = 512;
while( maxLen )
{
maxLen--;
sint8 n = fileMgr_readS8(file);
if( n == '\r' )
continue; // skip
if( n == '\n' )
break; // line end
cstr[size] = n;
size++;
if( size == limit )
__debugbreak();
}
cstr[size] = '\0';
return cstr;
}
void fileMgr_setSeek(file_t *file, uint32 seek)
{
SetFilePointer(file->hFile, seek, NULL, FILE_BEGIN);
}
uint32 fileMgr_getSeek(file_t *file)
{
return SetFilePointer(file->hFile, 0, NULL, FILE_CURRENT);
}
uint32 fileMgr_getSize(file_t *file)
{
return GetFileSize(file->hFile, NULL);
}