-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinaryio.h
242 lines (195 loc) · 7.46 KB
/
binaryio.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
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
#ifndef BINARY_IO_H_
#define BINARY_IO_H_
#include <filesystem>
#include <iostream>
#include <string>
#include <bitset>
#include <vector>
#include <fstream>
//If you are compiling with gnu g++, the namespace to access filesystem library is just fs::filesystem, otherwise, if you are on windows
//where gnu g++ minGW don't fully support filesystem, you need to use Visual Studio Visual C++ Compiler,
//and for accessing the filesystem library within VC++, you need to use fs::experimental::filesystem
#ifdef _WIN32
namespace fs = std::experimental::filesystem;
#else
namespace fs = std::filesystem;
#endif
//This define the size of the .ppm header file, so it skips the head and don't screw things up
#define HEADER_SIZE_IN_BYTES 15
//This writes a vector of unsigned char to a file
void writeBinary(fs::path & outputPath, std::vector<unsigned char> & dataBuffer)
{
std::ofstream outputFile(fs::absolute(outputPath).string().c_str(), std::ios::binary | std::ios::app);
//writes dataBuffer to file
for(auto & data : dataBuffer)
{
outputFile.write((const char*)&data, sizeof(data));
}
outputFile.close();
}
//This writes a vector of bitsets to a file
void writeBinary(fs::path & outputPath, std::vector<std::bitset<sizeof(unsigned char)*8>> & dataBuffer)
{
std::ofstream outputFile(fs::absolute(outputPath).string().c_str(), std::ios::binary | std::ios::app);
//Escrever o dataBuffer no arquivo
for(auto & data : dataBuffer)
{
char i = static_cast<char>(data.to_ulong());
outputFile.write((const char*)&i, sizeof(i));
}
outputFile.close();
}
//This reads a binary file and return the data on a vector of bitsets
std::vector<std::bitset<sizeof(unsigned char)*8>> readBinaryToBitSet(fs::path & inputPath)
{
std::vector<std::bitset<sizeof(unsigned char)*8>> dataBuffer;
if(!fs::exists(inputPath))
{
std::cerr << "[ERROR]: File does not exists\n";
return dataBuffer;
}
std::ifstream inputFile(fs::absolute(inputPath).string().c_str(), std::ios::binary);
std::size_t fileSizeInBytes = fs::file_size(inputPath);
dataBuffer.clear();
dataBuffer.reserve(fileSizeInBytes);
while(fileSizeInBytes--)
{
char charRead;
inputFile.read(reinterpret_cast<char*>(&charRead), sizeof(charRead));
dataBuffer.push_back(std::bitset<sizeof(unsigned char)*8>(charRead));
}
inputFile.close();
return dataBuffer;
}
//This reads a binary file and set the dataBuffer argument to the actual data
void readBinary(fs::path & inputPath, std::vector<std::bitset<sizeof(unsigned char)*8>> & dataBuffer)
{
if(!fs::exists(inputPath))
return;
std::ifstream inputFile(fs::absolute(inputPath).string().c_str(), std::ios::binary);
std::size_t fileSizeInBytes = fs::file_size(inputPath);
dataBuffer.clear();
dataBuffer.reserve(fileSizeInBytes);
while(fileSizeInBytes--)
{
char charRead;
inputFile.read(reinterpret_cast<char*>(&charRead), sizeof(charRead));
dataBuffer.push_back(std::bitset<sizeof(unsigned char)*8>(charRead));
}
inputFile.close();
}
//This reads a binary file and returns the data on a vector of unsigned char
std::vector<unsigned char> readBinaryToUnsignedChar(fs::path & inputPath)
{
std::vector<unsigned char> dataBuffer;
if(!fs::exists(inputPath))
return dataBuffer;
std::ifstream inputFile(fs::absolute(inputPath).string().c_str(), std::ios::binary);
std::size_t fileSizeInBytes = fs::file_size(inputPath);
dataBuffer.clear();
dataBuffer.reserve(fileSizeInBytes);
while(fileSizeInBytes--)
{
char charRead;
inputFile.read(reinterpret_cast<char*>(&charRead), sizeof(charRead));
dataBuffer.push_back(charRead);
}
inputFile.close();
return dataBuffer;
}
//This reads a binary file and set the dataBuffer argument to the actual data
void readBinary(fs::path & inputPath, std::vector<unsigned char> & dataBuffer)
{
if(!fs::exists(inputPath))
return;
std::ifstream inputFile(fs::absolute(inputPath).string().c_str(), std::ios::binary);
std::size_t fileSizeInBytes = fs::file_size(inputPath);
dataBuffer.clear();
dataBuffer.reserve(fileSizeInBytes);
while(fileSizeInBytes--)
{
char charRead;
inputFile.read(reinterpret_cast<char*>(&charRead), sizeof(charRead));
dataBuffer.push_back(charRead);
}
inputFile.close();
}
//This converts a vector of bitset to a vector of unsigned char
void vectorConvert_bitSetToUnsignedChar(std::vector<std::bitset<sizeof(unsigned char)*8>> & bytesOfData, std::vector<unsigned char> & vectorData)
{
vectorData.clear();
vectorData.reserve((unsigned)bytesOfData.size());
for(auto & b : bytesOfData)
{
vectorData.emplace_back((char)b.to_ulong());
}
}
//This converts a single bitset to a single unsigned char
void convert_bitSetToUnsignedChar(std::bitset<sizeof(unsigned char)*8> & byteOfData, unsigned char & unsignedCharData)
{
unsignedCharData = (char)byteOfData.to_ulong();
}
//This converts a single unsigned char to a single bitset
void convert_unsignedCharToBitSet(unsigned char & unsignedCharData, std::bitset<sizeof(unsigned char)*8> & byteOfData)
{
byteOfData = std::bitset<sizeof(unsigned char)*8>(unsignedCharData);
}
//This receives a single bitset and returns a single unsigned char (convert)
unsigned char convert_bitSetToUnsignedChar(std::bitset<sizeof(unsigned char)*8> & byteOfData)
{
return (char)byteOfData.to_ulong();
}
//This receives a single unsigned char and returns a single bitset (convert)
std::bitset<sizeof(unsigned char)*8> convert_unsignedCharToBitSet(unsigned char & unsignedCharData)
{
return std::bitset<sizeof(unsigned char)*8>(unsignedCharData);
}
//This converts a vector of bitset to a vector of unsigned char and return the vector of unsigned char
std::vector<unsigned char> vectorConvert_bitSetToUnsignedChar(std::vector<std::bitset<sizeof(unsigned char)*8>> & bytesOfData)
{
std::vector<unsigned char> vectorData;
vectorData.clear();
vectorData.reserve((unsigned)bytesOfData.size());
for(auto & b : bytesOfData)
{
vectorData.emplace_back((char)b.to_ulong());
}
return vectorData;
}
//This converts a vector of unsigned char to a vector of bitsets
void vectorConvert_unsignedCharToBitSet(std::vector<unsigned char> & vectorData, std::vector<std::bitset<sizeof(unsigned char)*8>> & bytesOfData)
{
bytesOfData.clear();
bytesOfData.reserve((unsigned)vectorData.size());
for(auto & c : vectorData)
{
bytesOfData.emplace_back(std::bitset<sizeof(unsigned char)*8>(c));
}
}
//This converts a vector of unsigned char to a vector of bitsets and return the vector of bitsets
std::vector<std::bitset<sizeof(unsigned char)*8>> vectorConvert_unsignedCharToBitSet(std::vector<unsigned char> & vectorData)
{
std::vector<std::bitset<sizeof(unsigned char)*8>> bytesOfData;
bytesOfData.clear();
bytesOfData.reserve((unsigned)vectorData.size());
for(auto & c : vectorData)
{
bytesOfData.emplace_back(std::bitset<sizeof(unsigned char)*8>(c));
}
return bytesOfData;
}
//This returns the size of the file in bytes.
std::size_t getFileSizeInBytes(fs::path & inputPath)
{
if(fs::exists(inputPath))
return fs::file_size(inputPath);
return 0;
}
//This returns the size of the file in bits.
std::size_t getFileSizeInBits(fs::path & inputPath)
{
if(fs::exists(inputPath))
return fs::file_size(inputPath)*8;
return 0;
}
#endif