-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCP_IO.h
172 lines (154 loc) · 3.1 KB
/
GCP_IO.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
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
#ifndef GCP_IOH
#define GCP_IOH
#include <fstream>
#include <sstream>
#include <string.h>
#include "SDL.h"
namespace gcp
{
//ÐÀÁÁÎÒÀ Ñ ÂÂÎÄÎÌ ÂÛÂÎÄÎÌ
class GPC_IO_ERROR
{
};
class GCP_IO
{
private:
std::fstream fs;
int _iMode;
bool _isClosed;
public:
SDL_RWops *io;
const static int GCP_IO_INPUT = 0;
const static int GCP_IO_OUTPUT = 1;
const static int GCP_IO_OUTPUTTXT = 2;
const static int GCP_IO_INPUTTXT = 3;
GCP_IO(std::string filename, int mode)
{
_isClosed = false;
switch (mode)
{
case GCP_IO_INPUT:
io = SDL_RWFromFile(filename.c_str(), "rb"); break;
case GCP_IO_OUTPUT:
io = SDL_RWFromFile(filename.c_str(), "wb"); break;
case GCP_IO_OUTPUTTXT:
io = SDL_RWFromFile(filename.c_str(), "w"); break;
case GCP_IO_INPUTTXT:
io = SDL_RWFromFile(filename.c_str(), "r"); break;
}
_iMode = mode;
}
~GCP_IO()
{
if (!_isClosed)
io->close(io);
}
void read(void *type, size_t size, int count = 1)
{
io->read(io, type, size, count);
}
void write(const void *type, size_t size, int count = 1)
{
io->write(io, type, size, count);
}
int readInt()
{
int value;
readInt(&value);
return value;
}
unsigned int readUInt()
{
unsigned int value;
readInt(&value);
return value;
}
double readDouble()
{
double value;
readDouble(&value);
return value;
}
std::string readString()
{
std::string value;
readString(value);
return value;
}
void readInt(int *value)
{
io->read(io, value, sizeof(int), 1);
}
void readInt(unsigned int *value)
{
io->read(io, value, sizeof(unsigned int), 1);
}
void readDouble(double *value)
{
io->read(io, value, sizeof(double), 1);
}
void readString(std::string& value)
{
value = "";
if (GCP_IO_INPUTTXT == _iMode)
{
char str = 0;
io->read(io, &str, sizeof(str), 1);
while (str != 0) //line separators, ending
{
value += str;
io->read(io, &str, sizeof(str), 1);
if (str == 13) //0D0A
{
io->read(io, &str, sizeof(str), 1);
if (str == 10)
break;
}
}
return;
}
else
{
char str[256];
io->read(io, &str, sizeof(str), 1);
value = str;
}
}
void writeInt(int value)
{
io->write(io, &value, sizeof(int), 1);
}
void writeDouble(double value)
{
io->write(io, &value, sizeof(double), 1);
}
void writeString(std::string const value)
{
char* str = new char[value.size()];
strncpy(str, value.c_str(), value.size());
str[value.size()] = 0;
if (GCP_IO_OUTPUTTXT == _iMode)
{
size_t len = SDL_strlen(str);
if (SDL_RWwrite(io, str, 1, len) != len)
throw GPC_IO_ERROR();
}
else
io->write(io, &str, sizeof(str), 1);
}
void writeln()
{
char str = 13;
SDL_RWwrite(io, &str, 1, 1);
str = '\n';
SDL_RWwrite(io, &str, 1, 1);
}
void close()
{
if (!_isClosed)
io->close(io);
_isClosed = true;
}
};
}
#endif