aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/yaml-cpp/src/ostream_wrapper.cpp
blob: 357fc0094c402665500de31ed1021cd06ff2f644 (plain) (blame)
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
#include "yaml-cpp/ostream_wrapper.h"

#include <algorithm>
#include <cstring>
#include <iostream>

namespace YAML {
ostream_wrapper::ostream_wrapper()
    : m_buffer(1, '\0'),
      m_pStream(0),
      m_pos(0),
      m_row(0),
      m_col(0),
      m_comment(false) {}

ostream_wrapper::ostream_wrapper(std::ostream& stream)
    : m_pStream(&stream), m_pos(0), m_row(0), m_col(0), m_comment(false) {}

ostream_wrapper::~ostream_wrapper() {}

void ostream_wrapper::write(const std::string& str) {
  if (m_pStream) {
    m_pStream->write(str.c_str(), str.size());
  } else {
    m_buffer.resize(std::max(m_buffer.size(), m_pos + str.size() + 1));
    std::copy(str.begin(), str.end(), m_buffer.begin() + m_pos);
  }

  for (std::size_t i = 0; i < str.size(); i++) {
    update_pos(str[i]);
  }
}

void ostream_wrapper::write(const char* str, std::size_t size) {
  if (m_pStream) {
    m_pStream->write(str, size);
  } else {
    m_buffer.resize(std::max(m_buffer.size(), m_pos + size + 1));
    std::copy(str, str + size, m_buffer.begin() + m_pos);
  }

  for (std::size_t i = 0; i < size; i++) {
    update_pos(str[i]);
  }
}

void ostream_wrapper::update_pos(char ch) {
  m_pos++;
  m_col++;

  if (ch == '\n') {
    m_row++;
    m_col = 0;
    m_comment = false;
  }
}
}