SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
transpose.hpp
1 
22 #ifndef SHORE_STREAMS_TRANSPOSE_HPP__
23 #define SHORE_STREAMS_TRANSPOSE_HPP__
24 
25 #include <algorithm>
26 #include <vector>
27 #include <stdexcept>
28 
29 #include <boost/iostreams/concepts.hpp>
30 #include <boost/iostreams/traits.hpp>
31 
32 #include "shore/stream/streams.hpp"
34 #include "shore/base/memops.hpp"
35 
36 namespace shore {
37 
38 class block_transposition
39 {
40  public:
41 
42  typedef shore::stream_slicer::slice slice;
43 
44  private:
45 
46  std::ostream *m_os;
47  std::vector<const char *> m_lines;
48  slice m_slice;
49 
50  char *m_buf;
51  size_t m_bufsize;
52 
53 
54  block_transposition(const block_transposition &other);
55  block_transposition &operator=(const block_transposition &other);
56 
57  public:
58 
59  block_transposition();
60  ~block_transposition();
61 
62  void transpose(const slice &s,const bool change_first_line);
63 
64  const slice &current() const;
65 };
66 
67 class output_transposer
68 {
69  private:
70 
71  std::ostream *m_os;
72  size_t m_blocksize;
73  block_transposition m_blktr;
74  block_transposition::slice m_slice;
75  char *m_buf;
76 
77 
78  output_transposer(const output_transposer &other);
79  output_transposer &operator=(const output_transposer &other);
80 
81  public:
82 
83  output_transposer(std::ostream &os,const size_t bs);
84  ~output_transposer();
85 
86  std::streamsize write(const char* s,std::streamsize n);
87  void close();
88 };
89 
90 class transposing_sink
91 :public boost::iostreams::sink
92 {
93  private:
94 
95  output_transposer *m_otr;
96 
97  public:
98 
99  transposing_sink(output_transposer *const otr);
100 
101  std::streamsize write(const char* s,std::streamsize n);
102  void close();
103 };
104 
105 class transposing_ostream
106 :public boost::iostreams::stream<transposing_sink>
107 {
108  private:
109 
110  output_transposer m_otr;
111 
112  public:
113 
114  transposing_ostream(std::ostream *os,const size_t bs);
115  ~transposing_ostream();
116 };
117 
118 class input_backtransposer
119 {
120  private:
121 
122  std::istream *m_in;
123  int64_t m_streamsize;
124  bool m_is_transposed;
125  size_t m_headsize;
126 
127  size_t m_blocksize;
128  block_transposition m_blktr;
129  shore::stream_slicer *m_slicer;
130  size_t m_sliceoff;
131 
132  char *m_buf;
133  size_t m_bufoff;
134  size_t m_bufsize;
135 
136  shore::stream_slicer::slice m_slice;
137 
138 
139  input_backtransposer(const input_backtransposer &other);
140  input_backtransposer &operator=(const input_backtransposer &other);
141 
142  public:
143 
144  input_backtransposer(std::istream &in,int64_t stream_size=-1);
145  ~input_backtransposer();
146 
147  std::streamsize read(char* s,std::streamsize n);
148 
149  std::streampos seek(boost::iostreams::stream_offset off,
150  std::ios_base::seekdir way);
151 
152  int64_t stream_size() const;
153 };
154 
155 class backtransposing_source
156 :public boost::iostreams::device<boost::iostreams::input_seekable>
157 {
158  private:
159 
160  input_backtransposer *m_ibt;
161 
162  public:
163 
164  backtransposing_source(input_backtransposer *const ibt);
165 
166  std::streamsize read(char* s,std::streamsize n);
167 
168  std::streampos seek(boost::iostreams::stream_offset off,
169  std::ios_base::seekdir way);
170 };
171 
172 class backtransposing_istream
173 :public boost::iostreams::stream<backtransposing_source>
174 {
175  private:
176 
177  input_backtransposer m_ibt;
178 
179  public:
180 
181  backtransposing_istream(std::istream* is,const int64_t streamsize=-1);
182  ~backtransposing_istream();
183 
184  int64_t stream_size() const;
185 };
186 
187 } // namespace shore
188 
189 #endif // SHORE_STREAMS_TRANSPOSE_HPP__
190