-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreplayreader.cpp
More file actions
198 lines (145 loc) · 4.02 KB
/
replayreader.cpp
File metadata and controls
198 lines (145 loc) · 4.02 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "replayreader.h"
const char * WEEKDAYS[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "[ERROR]" };
char WEEKDAY_ERROR[8];
const char * weekday(unsigned int d)
{
if (d > 6) { sprintf(WEEKDAY_ERROR, "[%hu]", d); return WEEKDAY_ERROR; }
return WEEKDAYS[d];
}
void codepointToUTF8(unsigned int cp, codepoint_t * szOut)
{
size_t len = 0;
szOut->u[0] = szOut->u[1] = szOut->u[2] = szOut->u[3] = 0;
if (cp < 0x0080) len++;
else if (cp < 0x0800) len += 2;
else len += 3;
int i = 0;
if (cp < 0x0080)
szOut->u[i++] = (unsigned char) cp;
else if (cp < 0x0800)
{
szOut->u[i++] = 0xc0 | (( cp ) >> 6 );
szOut->u[i++] = 0x80 | (( cp ) & 0x3F );
}
else
{
szOut->u[i++] = 0xE0 | (( cp ) >> 12 );
szOut->u[i++] = 0x80 | (( ( cp ) >> 6 ) & 0x3F );
szOut->u[i++] = 0x80 | (( cp ) & 0x3F );
}
}
std::string read1ByteString(std::istream & in)
{
std::string s;
char cbuf;
while (true)
{
in.read(&cbuf, sizeof(char));
if (cbuf == 0)
break;
s += cbuf;
}
return s;
}
std::string read2ByteString(std::istream & in)
{
codepoint_t ccp;
twobytestring_t cbuf;
std::string s;
while (true)
{
in.read(reinterpret_cast<char*>(&cbuf), sizeof(twobytestring_t));
if (READ_UINT16LE(cbuf.byte1, cbuf.byte2) == 0)
break;
codepointToUTF8(READ_UINT16LE(cbuf.byte1, cbuf.byte2), &ccp);
s += std::string(ccp.c);
}
return s;
}
std::string read2ByteStringN(std::istream & in, size_t N)
{
codepoint_t ccp;
twobytestring_t cbuf;
std::string s;
size_t n = N;
while (n--)
{
in.read(reinterpret_cast<char*>(&cbuf), sizeof(twobytestring_t));
codepointToUTF8(READ_UINT16LE(cbuf.byte1, cbuf.byte2), &ccp);
s += std::string(ccp.c);
}
return s;
}
std::string read2ByteString(const char * in, size_t N)
{
codepoint_t ccp;
twobytestring_t cbuf;
std::string s;
while (N > 1)
{
cbuf.byte1 = *in++; --N;
cbuf.byte2 = *in++; --N;
if (READ_UINT16LE(cbuf.byte1, cbuf.byte2) == 0)
break;
codepointToUTF8(READ_UINT16LE(cbuf.byte1, cbuf.byte2), &ccp);
s += std::string(ccp.c);
}
return s;
}
std::vector<std::string> tokenize(const std::string & str, const std::string & delimiters)
{
std::vector<std::string> tokens;
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
return tokens;
}
void asciiprint(FILE * out, unsigned char c)
{
if (c < 32 || c > 126) fprintf(out, ".");
else fprintf(out, "%c", c);
}
void hexdump(FILE * out, const unsigned char * buf, size_t length, const char * delim)
{
for (size_t k = 0; 16*k < length; k++)
{
fprintf(out, "%s", delim);
for (int i = 0; i < 16 && 16*k + i < length; ++i)
fprintf(out, "0x%02X ", buf[16*k + i]);
if (16*(k+1) > length) for (size_t i = 0; i < 16*(k+1)-length; ++i) fprintf(out, " ");
fprintf(out, " ");
for (size_t i = 0; i < 16 && 16*k + i < length; ++i)
asciiprint(out, buf[16*k + i]);
fprintf(out, "\n");
}
}
std::string timecode_to_string(unsigned int tc)
{
std::ostringstream os;
os << tc/15/60 << ":" << std::setw(2) << std::setfill('0') << (tc/15)%60 << "::" << std::setw(2) << std::setfill('0') << tc%15;
return os.str();
}
std::set<int> parse_int_sequence_arg(char * str)
{
char * s = str;
std::set<int> result;
while (*s)
{
const char * p = s;
long int n = std::strtol(p, &s, 0);
if (s == p) break; // error
result.insert(n);
while (*s && *s == ',') ++s;
}
return result;
}