Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / IndentStream.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "tools/rbd/IndentStream.h"
5
6 namespace rbd {
7
8 int IndentBuffer::overflow (int c) {
9   if (traits_type::eq_int_type(traits_type::eof(), c)) {
10     return traits_type::not_eof(c);
11   }
12
13   int r;
14   switch (c) {
15   case '\n':
16     m_buffer += c;
17     flush_line();
18     r = m_streambuf->sputn(m_buffer.c_str(), m_buffer.size());
19     m_buffer.clear();
20     return r;
21   case '\t':
22     // convert tab to single space and fall-through
23     c = ' ';
24   default:
25     if (m_indent + m_buffer.size() >= m_line_length) {
26       size_t word_offset = m_buffer.find_last_of(m_delim);
27       bool space_delim = (m_delim == " ");
28       if (word_offset == std::string::npos && !space_delim) {
29         word_offset = m_buffer.find_last_of(" ");
30       }
31
32       if (word_offset != std::string::npos) {
33         flush_line();
34         m_streambuf->sputn(m_buffer.c_str(), word_offset);
35         m_buffer = std::string(m_buffer,
36                                word_offset + (space_delim ? 1 : 0));
37       } else {
38         flush_line();
39         m_streambuf->sputn(m_buffer.c_str(), m_buffer.size());
40         m_buffer.clear();
41       }
42       m_streambuf->sputc('\n');
43     }
44     m_buffer += c;
45     return c;
46   }
47 }
48
49 void IndentBuffer::flush_line() {
50   if (m_initial_offset >= m_indent) {
51     m_initial_offset = 0;
52     m_streambuf->sputc('\n');
53   }
54
55   m_streambuf->sputn(m_indent_prefix.c_str(), m_indent - m_initial_offset);
56   m_initial_offset = 0;
57 }
58
59 } // namespace rbd