shell_comments_filter.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 Peter Dimov.
  7. // See https://web.archive.org/web/20041222094942/http://www.gabi-soft.fr/codebase-en.html.
  8. #ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED
  9. #define BOOST_IOSTREAMS_SHELL_COMMENTS_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/detail/ios.hpp> // BOOST_IOS.
  15. #include <boost/iostreams/filter/stdio.hpp>
  16. #include <boost/iostreams/operations.hpp>
  17. namespace boost { namespace iostreams { namespace example {
  18. class shell_comments_stdio_filter : public stdio_filter {
  19. public:
  20. explicit shell_comments_stdio_filter(char comment_char = '#')
  21. : comment_char_(comment_char)
  22. { }
  23. private:
  24. void do_filter()
  25. {
  26. bool skip = false;
  27. int c;
  28. while ((c = std::cin.get()) != EOF) {
  29. skip = c == comment_char_ ?
  30. true :
  31. c == '\n' ?
  32. false :
  33. skip;
  34. if (!skip)
  35. std::cout.put(c);
  36. }
  37. }
  38. char comment_char_;
  39. };
  40. class shell_comments_input_filter : public input_filter {
  41. public:
  42. explicit shell_comments_input_filter(char comment_char = '#')
  43. : comment_char_(comment_char), skip_(false)
  44. { }
  45. template<typename Source>
  46. int get(Source& src)
  47. {
  48. int c;
  49. while (true) {
  50. if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK)
  51. break;
  52. skip_ = c == comment_char_ ?
  53. true :
  54. c == '\n' ?
  55. false :
  56. skip_;
  57. if (!skip_)
  58. break;
  59. }
  60. return c;
  61. }
  62. template<typename Source>
  63. void close(Source&) { skip_ = false; }
  64. private:
  65. char comment_char_;
  66. bool skip_;
  67. };
  68. class shell_comments_output_filter : public output_filter {
  69. public:
  70. explicit shell_comments_output_filter(char comment_char = '#')
  71. : comment_char_(comment_char), skip_(false)
  72. { }
  73. template<typename Sink>
  74. bool put(Sink& dest, int c)
  75. {
  76. skip_ = c == comment_char_ ?
  77. true :
  78. c == '\n' ?
  79. false :
  80. skip_;
  81. if (skip_)
  82. return true;
  83. return iostreams::put(dest, c);
  84. }
  85. template<typename Source>
  86. void close(Source&) { skip_ = false; }
  87. private:
  88. char comment_char_;
  89. bool skip_;
  90. };
  91. class shell_comments_dual_use_filter : public dual_use_filter {
  92. public:
  93. explicit shell_comments_dual_use_filter(char comment_char = '#')
  94. : comment_char_(comment_char), skip_(false)
  95. { }
  96. template<typename Source>
  97. int get(Source& src)
  98. {
  99. int c;
  100. while (true) {
  101. if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK)
  102. break;
  103. skip_ = c == comment_char_ ?
  104. true :
  105. c == '\n' ?
  106. false :
  107. skip_;
  108. if (!skip_)
  109. break;
  110. }
  111. return c;
  112. }
  113. template<typename Sink>
  114. bool put(Sink& dest, int c)
  115. {
  116. skip_ = c == comment_char_ ?
  117. true :
  118. c == '\n' ?
  119. false :
  120. skip_;
  121. if (skip_)
  122. return true;
  123. return iostreams::put(dest, c);
  124. }
  125. template<typename Device>
  126. void close(Device&, BOOST_IOS::openmode) { skip_ = false; }
  127. private:
  128. char comment_char_;
  129. bool skip_;
  130. };
  131. class shell_comments_multichar_input_filter : public multichar_input_filter {
  132. public:
  133. explicit shell_comments_multichar_input_filter(char comment_char = '#')
  134. : comment_char_(comment_char), skip_(false)
  135. { }
  136. template<typename Source>
  137. std::streamsize read(Source& src, char* s, std::streamsize n)
  138. {
  139. for (std::streamsize z = 0; z < n; ++z) {
  140. int c;
  141. while (true) {
  142. if ((c = boost::iostreams::get(src)) == EOF)
  143. return z != 0 ? z : -1;
  144. else if (c == WOULD_BLOCK)
  145. return z;
  146. skip_ = c == comment_char_ ?
  147. true :
  148. c == '\n' ?
  149. false :
  150. skip_;
  151. if (!skip_)
  152. break;
  153. }
  154. s[z] = c;
  155. }
  156. return n;
  157. }
  158. template<typename Source>
  159. void close(Source&) { skip_ = false; }
  160. private:
  161. char comment_char_;
  162. bool skip_;
  163. };
  164. class shell_comments_multichar_output_filter : public multichar_output_filter {
  165. public:
  166. explicit shell_comments_multichar_output_filter(char comment_char = '#')
  167. : comment_char_(comment_char), skip_(false)
  168. { }
  169. template<typename Sink>
  170. std::streamsize write(Sink& dest, const char* s, std::streamsize n)
  171. {
  172. std::streamsize z;
  173. for (z = 0; z < n; ++z) {
  174. int c = s[z];
  175. skip_ = c == comment_char_ ?
  176. true :
  177. c == '\n' ?
  178. false :
  179. skip_;
  180. if (skip_)
  181. continue;
  182. if (!iostreams::put(dest, c))
  183. break;
  184. }
  185. return z;
  186. }
  187. template<typename Source>
  188. void close(Source&) { skip_ = false; }
  189. private:
  190. char comment_char_;
  191. bool skip_;
  192. };
  193. } } } // End namespaces example, iostreams, boost.
  194. #endif // #ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED