4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include <iostream> |
8 |
< |
#include <string> |
9 |
< |
#include <vector> |
7 |
> |
#include "Binary.hpp" |
8 |
|
|
9 |
< |
#include <foreach.hpp> |
12 |
< |
|
13 |
< |
class Binary |
14 |
< |
{ |
15 |
< |
private: |
16 |
< |
std::vector<char> bytes; |
17 |
< |
public: |
18 |
< |
Binary(std::string& string, bool signed_ = false); |
19 |
< |
template <typename Type> |
20 |
< |
Binary(const Type& type); |
21 |
< |
template <typename Type> |
22 |
< |
Type convert(bool signed_ = false); |
23 |
< |
operator std::string() const; |
24 |
< |
}; |
25 |
< |
|
26 |
< |
Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) |
9 |
> |
Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0) |
10 |
|
{ |
11 |
< |
if (string.find_first_not_of("01") != std::string::npos) throw; |
29 |
< |
|
30 |
< |
std::string::size_type off(string.size() % 8); |
31 |
< |
|
32 |
< |
if (off != 0) |
11 |
> |
if (string.IsEmpty()) |
12 |
|
{ |
13 |
< |
bytes.push_back(0); |
35 |
< |
string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); |
36 |
< |
} |
37 |
< |
|
38 |
< |
std::string::size_type index(0); |
39 |
< |
|
40 |
< |
_rmforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
41 |
< |
} |
13 |
> |
bytes.InsertLast(0); |
14 |
|
|
15 |
< |
template <typename Type> |
16 |
< |
Binary::Binary(const Type& type) : bytes(sizeof (type)) |
17 |
< |
{ |
46 |
< |
char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type))); |
15 |
> |
return; |
16 |
> |
} |
17 |
> |
// string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); |
18 |
|
|
19 |
< |
_mforeach (std::vector<char>, byte, bytes) *byte = *type_++; |
49 |
< |
} |
19 |
> |
size_t index(string.GetSize() % 8), offset(index); |
20 |
|
|
21 |
< |
template <typename Type> |
52 |
< |
Type Binary::convert(bool signed_) |
53 |
< |
{ |
54 |
< |
Type type; |
21 |
> |
_rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
22 |
|
|
23 |
< |
if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); |
23 |
> |
if (offset != 0) |
24 |
> |
{ |
25 |
> |
bytes.InsertLast(0); |
26 |
|
|
27 |
< |
char* type_(reinterpret_cast<char*>(&type)); |
27 |
> |
_rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit; |
28 |
|
|
29 |
< |
_foreach (std::vector<char>, byte, bytes) *type_++ = *byte; |
29 |
> |
index = 0; |
30 |
|
|
31 |
< |
return type; |
31 |
> |
_rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit; |
32 |
> |
} |
33 |
|
} |
34 |
|
|
35 |
< |
Binary::operator std::string() const |
35 |
> |
Binary::operator ext::String() const |
36 |
|
{ |
37 |
< |
std::string string; |
37 |
> |
ext::String string; |
38 |
|
|
39 |
< |
_rforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; |
39 |
> |
_rforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0'); |
40 |
|
|
41 |
|
return string; |
42 |
|
} |
73 |
– |
|
74 |
– |
inline std::ostream& operator<<(std::ostream& out, const Binary& binary) |
75 |
– |
{ |
76 |
– |
return out << std::string(binary); |
77 |
– |
} |
78 |
– |
|
79 |
– |
int main(int argc, char* argv[]) |
80 |
– |
{ |
81 |
– |
std::string hello("Hello, World!"); |
82 |
– |
|
83 |
– |
_foreach (std::string, atom, hello) std::cout << Binary(*atom) << " = " << *atom << std::endl; |
84 |
– |
|
85 |
– |
_fori (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; |
86 |
– |
|
87 |
– |
_foru (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; |
88 |
– |
|
89 |
– |
for (float index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; |
90 |
– |
|
91 |
– |
for (double index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; |
92 |
– |
|
93 |
– |
std::string string("101001101001"); |
94 |
– |
Binary binary(string, true); |
95 |
– |
|
96 |
– |
std::cout << binary << " = " << string << std::endl; |
97 |
– |
|
98 |
– |
float value(binary.convert<float>()); |
99 |
– |
|
100 |
– |
std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; |
101 |
– |
|
102 |
– |
return 0; |
103 |
– |
} |