-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiriclib_bstream.cpp
67 lines (55 loc) · 1.42 KB
/
iriclib_bstream.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
#include "iriclib_bstream.h"
#include <stdlib.h>
#include <string.h>
using namespace iRICLib;
InputBStream::InputBStream(std::istream &stream)
{
m_stream = &stream;
}
InputBStream& InputBStream::operator >> (int& value)
{
int val;
m_stream->read(reinterpret_cast<char*>(&val), sizeof(int));
value = val;
return *this;
}
InputBStream& InputBStream::operator >> (double& value)
{
double val;
m_stream->read(reinterpret_cast<char*>(&val), sizeof(double));
value = val;
return *this;
}
InputBStream& InputBStream::operator >> (std::string& str)
{
char* buffer;
int size;
m_stream->read(reinterpret_cast<char*>(&size), sizeof(int));
buffer = new char[size];
m_stream->read(buffer, sizeof(char) * size);
str = buffer;
delete buffer;
return *this;
}
OutputBStream::OutputBStream(std::ostream &stream)
{
m_stream = &stream;
}
OutputBStream& OutputBStream::operator << (int value)
{
m_stream->write(reinterpret_cast<char*>(&value), sizeof(int));
return *this;
}
OutputBStream& OutputBStream::operator << (double value)
{
m_stream->write(reinterpret_cast<char*>(&value), sizeof(double));
return *this;
}
OutputBStream& OutputBStream::operator << (const std::string& str)
{
const char* cstr = str.c_str();
size_t size = strlen(cstr) + 1;
m_stream->write(reinterpret_cast<char*>(&size), sizeof(int));
m_stream->write(cstr, sizeof(char) * size);
return *this;
}