File: Writer.cpp

package info (click to toggle)
pentobi 29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: cpp: 25,719; javascript: 875; xml: 40; makefile: 13; sh: 6
file content (79 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download | duplicates (2)
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
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Writer.cpp
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#include "Writer.h"

#include <sstream>

namespace libboardgame_base {

//-----------------------------------------------------------------------------

Writer::Writer(ostream& out)
    : m_out(out)
{ }

void Writer::begin_node()
{
    write_indent();
    m_out << ';';
}

void Writer::begin_tree()
{
    write_indent();
    m_out << '(';
    // Don't indent the first level
    if (m_level > 0 && m_indent >= 0)
        m_current_indent += static_cast<unsigned>(m_indent);
    ++m_level;
    if (m_indent >= 0)
        m_out << '\n';
}

void Writer::end_node()
{
    if (m_indent >= 0)
        m_out << '\n';
}

void Writer::end_tree()
{
    --m_level;
    if (m_level > 0 && m_indent >= 0)
        m_current_indent -= static_cast<unsigned>(m_indent);
    write_indent();
    m_out << ')';
    if (m_indent >= 0)
        m_out << '\n';
}

string Writer::get_escaped(const string& s)
{
    ostringstream buffer;
    for (char c : s)
    {
        if (c == ']' || c == '\\')
            buffer << '\\' << c;
        else if (c == '\t' || c == '\f' || c == '\v')
            // Replace whitespace as required by the SGF standard.
            buffer << ' ';
        else
            buffer << c;
    }
    return buffer.str();
}

void Writer::write_indent()
{
    if (m_indent >= 0)
        for (unsigned i = 0; i < m_current_indent; ++i)
            m_out << ' ';
}

//-----------------------------------------------------------------------------

} // namespace libboardgame_base