about summary refs log tree commit diff stats
path: root/ext/wittle_generator/Serializer.cpp
blob: 420926f645942b54f468ff8dba9432e6c9c8477c (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Serializer.h"

#include <iostream>
#include <string>

#include "Base64.h"

void Serializer::writeByte(char val) { buffer_.push_back(val); }

void Serializer::writeInt(int val) {
  int b1 = (val & 0x000000FF) >> 0;
  int b2 = (val & 0x0000FF00) >> 8;
  int b3 = (val & 0x00FF0000) >> 16;
  int b4 = (val & 0xFF000000) >> 24;
  writeByte(b1);
  writeByte(b2);
  writeByte(b3);
  writeByte(b4);
}

void Serializer::writeLong(long val) {
  long i1 = val & 0xFFFFFFFF;
  long i2 = (val - i1) / 0x100000000;
  writeInt(i1);
  writeInt(i2);
}

void Serializer::writeColor(int val) {
  switch (val) {
    case 0x1: {
      // Black
      writeByte(0xFF);
      writeByte(0xFF);
      writeByte(0xFF);
      writeByte(0xFF);
      break;
    }
    case 0x2: {
      // White
      writeByte(0x00);
      writeByte(0x00);
      writeByte(0x00);
      writeByte(0xFF);
      break;
    }
    case 0x3: {
      // Red
      writeByte(0xFF);
      writeByte(0x00);
      writeByte(0x00);
      writeByte(0xFF);
      break;
    }
    case 0x4: {
      // Purple
      writeByte(0x80);
      writeByte(0x00);
      writeByte(0x80);
      writeByte(0xFF);
      break;
    }
    case 0x5: {
      // Green
      writeByte(0x00);
      writeByte(0xFF);
      writeByte(0x00);
      writeByte(0xFF);
      break;
    }
    case 0x6: {
      // Cyan
      writeByte(0x00);
      writeByte(0xFF);
      writeByte(0xFF);
      writeByte(0xFF);
      break;
    }
    case 0x7: {
      // Magenta
      writeByte(0xFF);
      writeByte(0x00);
      writeByte(0xFF);
      writeByte(0xFF);
      break;
    }
    case 0x8: {
      // Yellow
      writeByte(0xFF);
      writeByte(0xFF);
      writeByte(0x00);
      writeByte(0xFF);
      break;
    }
    case 0x9: {
      // Blue
      writeByte(0x00);
      writeByte(0x00);
      writeByte(0xFF);
      writeByte(0xFF);
      break;
    }
    case 0xA: {
      // Orange
      writeByte(0xFF);
      writeByte(0x8C);
      writeByte(0x00);
      writeByte(0xFF);
      break;
    }
    case 0xF: {
      // X????
      break;
    }
  }
}

void Serializer::writeString(const std::string& val) {
  writeInt(val.length());
  for (char ch : val) {
    writeByte(ch);
  }
}

std::string Serializer::str() const {
  int i = 0;
  for (char ch : buffer_) {
    std::cout << std::hex << static_cast<int>(ch);
    if (i++ % 4 == 3) {
      std::cout << " ";
    }
  }
  std::cout << std::endl;

  return "_" + macaron::Base64::Encode(buffer_);
}