-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrcp_helper.cpp
127 lines (115 loc) · 2.52 KB
/
rrcp_helper.cpp
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
#include "rrcp_helper.hpp"
#include <cstddef>
#include <string>
constexpr const char ESC = 0x1B;
constexpr const char REPLACE_LF = 0x01;
constexpr const char REPLACE_CR = 0x02;
constexpr const char REPLACE_ESC = 0x03;
constexpr const char SP = 0x20;
constexpr const char LF = 0x0A;
constexpr const char CR = 0x0D;
// constexpr const char DOT = 0x2E;
// constexpr const char SEMICOLON = 0x3B;
// constexpr const char COMMA = 0x2C;
// constexpr const char SHARP = 0x23;
// constexpr const char DQUOTE = '\"';
// constexpr const char BACKSLASH = '\\';
// constexpr const char COLON = 0x3A;
std::string esc2char(const std::string& data)
{
std::string message;
size_t const len = data.size();
char c;
unsigned int i = 0;
while (i < len)
{
// get next char
c = data[i];
// end mark found, the message is complete, the
// CR is not needed anymore
if (c == CR)
{
return message;
}
// An escape is found thus we want to
// replace the escape sequence
if (c == ESC)
{
// On the ESC should follow an replacement character
// REPLACE_LF... REPLACE_ESC
if (i != (len - 1))
{
// get next character
i++;
c = data[i];
if (REPLACE_LF == c)
{
c = static_cast< char >(LF);
}
else if (REPLACE_CR == c)
{
c = static_cast< char >(CR);
}
else if (REPLACE_ESC == c)
{
c = static_cast< char >(ESC);
}
else
{
//"Parser Error contains unexpected ESC character"
return "";
}
}
else
{
//"Parser Error message ends with escape character"
return "";
}
}
// enter next character
message += c;
// next character
i++;
}
return message;
}
std::string char2esc(const std::string& data)
{
std::string message;
size_t const len = data.size();
char c;
unsigned int i = 0;
while (i < len)
{
// get next char
c = data[i++];
// and replace CR and LF and the ESC itself
switch (c)
{
case LF:
{
message += static_cast< char >(ESC);
message += static_cast< char >(REPLACE_LF);
};
break;
case CR:
{
message += static_cast< char >(ESC);
message += static_cast< char >(REPLACE_CR);
};
break;
case ESC:
{
message += static_cast< char >(ESC);
message += static_cast< char >(REPLACE_ESC);
};
break;
default:
{
message += c;
}
break;
}
}
return message;
}