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
|
#pragma once
#include "../base/assert.hpp"
#include "../std/fstream.hpp"
namespace tree
{
namespace detail
{
void PrintOffset(size_t offset, ostream & s)
{
for (size_t i = 0; i < offset; ++i)
s << " ";
}
template <class ToDo>
void PrintTextTree(size_t offset, ostream & s, ToDo & toDo)
{
PrintOffset(offset, s);
// print name as key
s << toDo.Name() << " ";
// serialize object
toDo.Serialize(s);
size_t const count = toDo.BeginChilds();
bool const isEmpty = (count == 0);
// put end marker
s << (isEmpty ? "-" : "+") << endl;
// print chils
if (!isEmpty)
{
offset += 4;
size_t i = 0;
while (i < count)
{
toDo.Start(i++);
PrintTextTree(offset, s, toDo);
toDo.End();
}
// end of structure
PrintOffset(offset, s);
s << "{}" << endl;
}
}
}
template <class ToDo>
void SaveTreeAsText(ostream & s, ToDo & toDo)
{
detail::PrintTextTree(0, s, toDo);
}
template <class ToDo>
bool LoadTreeAsText(istream & s, ToDo & toDo)
{
string name;
s >> name;
ASSERT ( !name.empty(), ("Error in classificator file") );
if (name == "{}") return false;
// set key name
toDo.Name(name);
// load object itself
string strkey;
s >> strkey;
while (strkey != "+" && strkey != "-")
{
toDo.Serialize(strkey);
s >> strkey;
}
// load children
if (strkey == "+")
{
size_t i = 0;
while (true)
{
toDo.Start(i++);
bool const isContinue = LoadTreeAsText(s, toDo);
toDo.End();
if (!isContinue)
{
toDo.EndChilds();
break;
}
}
ASSERT ( i <= 64, ("too many features at level = ", name) );
}
return true;
}
}
|