ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Binary.cpp
(Generate patch)

Comparing:
Represent/represent.cpp (file contents), Revision 366 by douglas, 2004-12-21T03:22:38-08:00 vs.
Represent/Binary.cpp (file contents), Revision 370 by douglas, 2004-12-21T22:04:06-08:00

# Line 4 | Line 4
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 <        std::string::size_type off(string.size() % 8);
29 <
30 <        if (off != 0)
11 >        if (string.IsEmpty())
12          {
13 <                bytes.push_back(0);
33 <                string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0');
34 <        }
13 >                bytes.InsertLast(0);
14  
15 <        std::string::size_type index(0);
16 <
17 <        _rmforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit;
39 < }
40 <
41 < template <typename Type>
42 < Binary::Binary(const Type& type) : bytes(sizeof (type))
43 < {
44 <        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_++;
47 < }
19 >        size_t index(string.GetSize() % 8), offset(index);
20  
21 < template <typename Type>
50 < Type Binary::convert(bool signed_)
51 < {
52 <        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   }
71
72 inline std::ostream& operator<<(std::ostream& out, const Binary& binary)
73 {
74        return out << std::string(binary);
75 }
76
77 int main(int argc, char* argv[])
78 {
79        std::string hello("Hello, World!");
80
81        _foreach (std::string, atom, hello)
82        {
83                std::cout << Binary(*atom) << " = " << *atom << std::endl;
84        }
85
86        _fori (index, -10, 11)
87        {
88                std::cout << Binary(index) << " = " << index << std::endl;
89        }
90
91        _foru (index, -10, 11)
92        {
93                std::cout << Binary(index) << " = " << index << std::endl;
94        }
95
96        for (float index(-1.0); index < 1.0; index += 0.1)
97        {
98                std::cout << Binary(index) << " = " << index << std::endl;
99        }
100
101        for (double index(-1.0); index < 1.0; index += 0.1)
102        {
103                std::cout << Binary(index) << " = " << index << std::endl;
104        }
105
106        std::string string("101001101001");
107        Binary binary(string, true);
108
109        std::cout << binary << " = " << string << std::endl;
110
111        float value(binary.convert<float>());
112
113        std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl;
114
115        return 0;
116 }

Comparing:
Represent/represent.cpp (property svn:eol-style), Revision 366 by douglas, 2004-12-21T03:22:38-08:00 vs.
Represent/Binary.cpp (property svn:eol-style), Revision 370 by douglas, 2004-12-21T22:04:06-08:00

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines