-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecode_type.cxx
More file actions
141 lines (128 loc) · 2.57 KB
/
decode_type.cxx
File metadata and controls
141 lines (128 loc) · 2.57 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
#include <iostream>
#include <vector>
#include <array>
#include <fstream>
#include <cassert>
std::string get_trailing_alpha(std::string const& text)
{
int end = text.size() - 1;
while (end >= 0 && std::isalpha(text[end]))
--end;
return text.substr(end + 1);
}
class Decoder
{
private:
std::string substring;
std::array<std::string, 1000> a;
int depth_ = 0;
int print_no_new_lines = 0;
bool consume_spaces = false;
public:
int depth() const { return depth_; }
public:
void add_char(char c)
{
if (consume_spaces && c == ' ')
return;
substring += c;
consume_spaces = false;
}
void add_substring()
{
if (!substring.empty())
{
add(substring);
substring.clear();
}
}
void open()
{
add_substring();
std::cout << '<';
std::string word = get_trailing_alpha(a[depth_]);
if (word == "Constant" || word == "Symbol" || word == "Sin")
++print_no_new_lines;
else if (!print_no_new_lines)
{
std::cout << '\n';
consume_spaces = true;
for (int i = 0; i <= depth_; ++i)
std::cout << " ";
}
else
++print_no_new_lines;
++depth_;
assert(depth_ < a.size());
}
void close()
{
add_substring();
assert(depth_ > 0);
--depth_;
if (print_no_new_lines)
--print_no_new_lines;
else
{
std::cout << '\n';
consume_spaces = true;
for (int i = 0; i < depth_; ++i)
std::cout << " ";
}
std::cout << '>';
}
void comma()
{
add_substring();
if (print_no_new_lines)
std::cout << ", ";
else
{
if (!print_no_new_lines)
{
std::cout << ",\n";
consume_spaces = true;
for (int i = 0; i < depth_; ++i)
std::cout << " ";
}
else
std::cout << ", ";
}
}
void add(std::string const& token)
{
std::cout << token;
a[depth_] = token;
}
};
int main()
{
Decoder decoder;
std::ifstream file("troep");
std::string line;
while (std::getline(file, line))
{
if (!line.empty() && *line.rbegin() == '\r')
line.erase(line.end() - 1);
if (line.rfind("cairowindow", 0) != 0)
{
decoder.add_substring();
std::cout << line << '\n';
continue;
}
for (char c : line)
{
if (c == '<')
decoder.open();
else if (c == '>')
decoder.close();
else if (c == ',')
decoder.comma();
else if (c != '\r')
decoder.add_char(c);
}
decoder.add_char('\n');
}
// In case the file doesn't end with a separator.
decoder.add_substring();
}