-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stream.cpp
109 lines (95 loc) · 2.72 KB
/
Stream.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
#include "Stream.hpp"
#include <algorithm>
#include <cstring>
#include <vector>
bool isLittleEndianMachine() {
union {
unsigned short value;
char bytes[2];
} test = {0x0201};
return test.bytes[0] == 1;
}
bool IS_LITTLE_ENDIAN_MACHINE = isLittleEndianMachine();
bool InputStream::readBool() {
char buffer[1];
readBytes(buffer, 1);
return buffer[0] != 0;
}
int InputStream::readInt() {
char buffer[sizeof(int)];
readBytes(buffer, sizeof(int));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(int));
}
return *reinterpret_cast<int *>(buffer);
}
long long InputStream::readLongLong() {
char buffer[sizeof(long long)];
readBytes(buffer, sizeof(long long));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(long long));
}
return *reinterpret_cast<long long *>(buffer);
}
float InputStream::readFloat() {
char buffer[sizeof(float)];
readBytes(buffer, sizeof(float));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(float));
}
return *reinterpret_cast<float *>(buffer);
}
double InputStream::readDouble() {
char buffer[sizeof(double)];
readBytes(buffer, sizeof(double));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(double));
}
return *reinterpret_cast<double *>(buffer);
}
std::string InputStream::readString() {
std::vector<char> buffer(readInt());
readBytes(&buffer[0], buffer.size());
return std::string(&buffer[0], buffer.size());
}
void OutputStream::write(bool value) {
char buffer[sizeof(bool)];
std::memcpy(buffer, &value, sizeof(bool));
writeBytes(buffer, sizeof(bool));
}
void OutputStream::write(int value) {
char buffer[sizeof(int)];
std::memcpy(buffer, &value, sizeof(int));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(int));
}
writeBytes(buffer, sizeof(int));
}
void OutputStream::write(long long value) {
char buffer[sizeof(long long)];
std::memcpy(buffer, &value, sizeof(long long));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(long long));
}
writeBytes(buffer, sizeof(long long));
}
void OutputStream::write(float value) {
char buffer[sizeof(float)];
std::memcpy(buffer, &value, sizeof(float));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(float));
}
writeBytes(buffer, sizeof(float));
}
void OutputStream::write(double value) {
char buffer[sizeof(double)];
std::memcpy(buffer, &value, sizeof(double));
if (!IS_LITTLE_ENDIAN_MACHINE) {
std::reverse(buffer, buffer + sizeof(double));
}
writeBytes(buffer, sizeof(double));
}
void OutputStream::write(const std::string &value) {
write((int)(value.length()));
writeBytes(value.c_str(), value.length());
}