diff options
Diffstat (limited to 'ext/wittle_generator/Serializer.cpp')
-rw-r--r-- | ext/wittle_generator/Serializer.cpp | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/ext/wittle_generator/Serializer.cpp b/ext/wittle_generator/Serializer.cpp new file mode 100644 index 0000000..420926f --- /dev/null +++ b/ext/wittle_generator/Serializer.cpp | |||
@@ -0,0 +1,135 @@ | |||
1 | #include "Serializer.h" | ||
2 | |||
3 | #include <iostream> | ||
4 | #include <string> | ||
5 | |||
6 | #include "Base64.h" | ||
7 | |||
8 | void Serializer::writeByte(char val) { buffer_.push_back(val); } | ||
9 | |||
10 | void Serializer::writeInt(int val) { | ||
11 | int b1 = (val & 0x000000FF) >> 0; | ||
12 | int b2 = (val & 0x0000FF00) >> 8; | ||
13 | int b3 = (val & 0x00FF0000) >> 16; | ||
14 | int b4 = (val & 0xFF000000) >> 24; | ||
15 | writeByte(b1); | ||
16 | writeByte(b2); | ||
17 | writeByte(b3); | ||
18 | writeByte(b4); | ||
19 | } | ||
20 | |||
21 | void Serializer::writeLong(long val) { | ||
22 | long i1 = val & 0xFFFFFFFF; | ||
23 | long i2 = (val - i1) / 0x100000000; | ||
24 | writeInt(i1); | ||
25 | writeInt(i2); | ||
26 | } | ||
27 | |||
28 | void Serializer::writeColor(int val) { | ||
29 | switch (val) { | ||
30 | case 0x1: { | ||
31 | // Black | ||
32 | writeByte(0xFF); | ||
33 | writeByte(0xFF); | ||
34 | writeByte(0xFF); | ||
35 | writeByte(0xFF); | ||
36 | break; | ||
37 | } | ||
38 | case 0x2: { | ||
39 | // White | ||
40 | writeByte(0x00); | ||
41 | writeByte(0x00); | ||
42 | writeByte(0x00); | ||
43 | writeByte(0xFF); | ||
44 | break; | ||
45 | } | ||
46 | case 0x3: { | ||
47 | // Red | ||
48 | writeByte(0xFF); | ||
49 | writeByte(0x00); | ||
50 | writeByte(0x00); | ||
51 | writeByte(0xFF); | ||
52 | break; | ||
53 | } | ||
54 | case 0x4: { | ||
55 | // Purple | ||
56 | writeByte(0x80); | ||
57 | writeByte(0x00); | ||
58 | writeByte(0x80); | ||
59 | writeByte(0xFF); | ||
60 | break; | ||
61 | } | ||
62 | case 0x5: { | ||
63 | // Green | ||
64 | writeByte(0x00); | ||
65 | writeByte(0xFF); | ||
66 | writeByte(0x00); | ||
67 | writeByte(0xFF); | ||
68 | break; | ||
69 | } | ||
70 | case 0x6: { | ||
71 | // Cyan | ||
72 | writeByte(0x00); | ||
73 | writeByte(0xFF); | ||
74 | writeByte(0xFF); | ||
75 | writeByte(0xFF); | ||
76 | break; | ||
77 | } | ||
78 | case 0x7: { | ||
79 | // Magenta | ||
80 | writeByte(0xFF); | ||
81 | writeByte(0x00); | ||
82 | writeByte(0xFF); | ||
83 | writeByte(0xFF); | ||
84 | break; | ||
85 | } | ||
86 | case 0x8: { | ||
87 | // Yellow | ||
88 | writeByte(0xFF); | ||
89 | writeByte(0xFF); | ||
90 | writeByte(0x00); | ||
91 | writeByte(0xFF); | ||
92 | break; | ||
93 | } | ||
94 | case 0x9: { | ||
95 | // Blue | ||
96 | writeByte(0x00); | ||
97 | writeByte(0x00); | ||
98 | writeByte(0xFF); | ||
99 | writeByte(0xFF); | ||
100 | break; | ||
101 | } | ||
102 | case 0xA: { | ||
103 | // Orange | ||
104 | writeByte(0xFF); | ||
105 | writeByte(0x8C); | ||
106 | writeByte(0x00); | ||
107 | writeByte(0xFF); | ||
108 | break; | ||
109 | } | ||
110 | case 0xF: { | ||
111 | // X???? | ||
112 | break; | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | |||
117 | void Serializer::writeString(const std::string& val) { | ||
118 | writeInt(val.length()); | ||
119 | for (char ch : val) { | ||
120 | writeByte(ch); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | std::string Serializer::str() const { | ||
125 | int i = 0; | ||
126 | for (char ch : buffer_) { | ||
127 | std::cout << std::hex << static_cast<int>(ch); | ||
128 | if (i++ % 4 == 3) { | ||
129 | std::cout << " "; | ||
130 | } | ||
131 | } | ||
132 | std::cout << std::endl; | ||
133 | |||
134 | return "_" + macaron::Base64::Encode(buffer_); | ||
135 | } | ||