-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitWriter.cpp
More file actions
54 lines (46 loc) · 1.28 KB
/
BitWriter.cpp
File metadata and controls
54 lines (46 loc) · 1.28 KB
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
/**
* Created by Zakharov Sergey (aka Zakhse),
* Student of NRU HSE, the Faculty of Computer Science, Software Engineering
*
* 2k16, November
*/
#include "BitWriter.h"
#include <bitset>
BitWriter::BitWriter(std::string path)
{
file = new std::ofstream;
file->open(path, std::ios_base::out | std::ios_base::trunc);
buffer = "";
finished = false;
}
void BitWriter::add(std::string new_part)
{
if (finished) throw std::logic_error("BitWriter is finished!");
buffer += new_part;
if (buffer.length() >= 8)
{
unsigned long number_of_bytes = buffer.length() / 8;
char* bytes = new char[number_of_bytes];
for (unsigned long i = 0; i < number_of_bytes; ++i)
{
std::string temp_str = buffer.substr(i * 8, 8);
bytes[i] = (unsigned char) std::bitset<8>(temp_str).to_ulong();
}
buffer = buffer.substr(number_of_bytes * 8);
file->write(bytes, number_of_bytes);
delete[] bytes;
}
}
void BitWriter::finish()
{
if (finished) return;;
while (buffer.length() < 8)
buffer += "0";
char* byte = new char[1];
byte[0] = (unsigned char) std::bitset<8>(buffer).to_ulong();
file->write(byte, 1);
file->close();
buffer = "";
delete file;
finished = true;
}