tab_expanding_filter.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2005-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. // Adapted from an example of James Kanze, with suggestions from Rob Stewart.
  7. // See https://web.archive.org/web/20041222094942/http://www.gabi-soft.fr/codebase-en.html.
  8. #ifndef BOOST_IOSTREAMS_TAB_EXPANDING_FILTER_HPP_INCLUDED
  9. #define BOOST_IOSTREAMS_TAB_EXPANDING_FILTER_HPP_INCLUDED
  10. #include <cassert>
  11. #include <cstdio> // EOF.
  12. #include <iostream> // cin, cout.
  13. #include <boost/iostreams/concepts.hpp>
  14. #include <boost/iostreams/filter/stdio.hpp>
  15. #include <boost/iostreams/operations.hpp>
  16. namespace boost { namespace iostreams { namespace example {
  17. class tab_expanding_stdio_filter : public stdio_filter {
  18. public:
  19. explicit tab_expanding_stdio_filter(int tab_size = 8)
  20. : tab_size_(tab_size), col_no_(0)
  21. {
  22. assert(tab_size > 0);
  23. }
  24. private:
  25. void do_filter()
  26. {
  27. int c;
  28. while ((c = std::cin.get()) != EOF) {
  29. if (c == '\t') {
  30. int spaces = tab_size_ - (col_no_ % tab_size_);
  31. for (; spaces > 0; --spaces)
  32. put_char(' ');
  33. } else {
  34. put_char(c);
  35. }
  36. }
  37. }
  38. void do_close() { col_no_ = 0; }
  39. void put_char(int c)
  40. {
  41. std::cout.put(c);
  42. if (c == '\n') {
  43. col_no_ = 0;
  44. } else {
  45. ++col_no_;
  46. }
  47. }
  48. int tab_size_;
  49. int col_no_;
  50. };
  51. class tab_expanding_input_filter : public input_filter {
  52. public:
  53. explicit tab_expanding_input_filter(int tab_size = 8)
  54. : tab_size_(tab_size), col_no_(0), spaces_(0)
  55. {
  56. assert(tab_size > 0);
  57. }
  58. template<typename Source>
  59. int get(Source& src)
  60. {
  61. if (spaces_ > 0) {
  62. --spaces_;
  63. return get_char(' ');
  64. }
  65. int c;
  66. if ((c = iostreams::get(src)) == EOF || c == WOULD_BLOCK)
  67. return c;
  68. if (c != '\t')
  69. return get_char(c);
  70. // Found a tab. Call this filter recursively.
  71. spaces_ = tab_size_ - (col_no_ % tab_size_);
  72. return this->get(src);
  73. }
  74. template<typename Source>
  75. void close(Source&)
  76. {
  77. col_no_ = 0;
  78. spaces_ = 0;
  79. }
  80. private:
  81. int get_char(int c)
  82. {
  83. if (c == '\n') {
  84. col_no_ = 0;
  85. } else {
  86. ++col_no_;
  87. }
  88. return c;
  89. }
  90. int tab_size_;
  91. int col_no_;
  92. int spaces_;
  93. };
  94. class tab_expanding_output_filter : public output_filter {
  95. public:
  96. explicit tab_expanding_output_filter(int tab_size = 8)
  97. : tab_size_(tab_size), col_no_(0), spaces_(0)
  98. {
  99. assert(tab_size > 0);
  100. }
  101. template<typename Sink>
  102. bool put(Sink& dest, int c)
  103. {
  104. for (; spaces_ > 0; --spaces_)
  105. if (!put_char(dest, ' '))
  106. return false;
  107. if (c == '\t') {
  108. spaces_ = tab_size_ - (col_no_ % tab_size_) - 1;
  109. return this->put(dest, ' ');
  110. }
  111. return put_char(dest, c);
  112. }
  113. template<typename Sink>
  114. void close(Sink&)
  115. {
  116. col_no_ = 0;
  117. spaces_ = 0;
  118. }
  119. private:
  120. template<typename Sink>
  121. bool put_char(Sink& dest, int c)
  122. {
  123. if (!iostreams::put(dest, c))
  124. return false;
  125. if (c != '\n')
  126. ++col_no_;
  127. else
  128. col_no_ = 0;
  129. return true;
  130. }
  131. int tab_size_;
  132. int col_no_;
  133. int spaces_;
  134. };
  135. } } } // End namespaces example, iostreams, boost.
  136. #endif // #ifndef BOOST_IOSTREAMS_TAB_EXPANDING_FILTER_HPP_INCLUDED