join.h
Go to the documentation of this file.
1 #ifndef _utl_join_h_
2 #define _utl_join_h_
3 
4 #include <boost/iostreams/categories.hpp>
5 #include <boost/iostreams/positioning.hpp>
6 #include <boost/iostreams/read.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include <vector>
9 
10 
11 namespace utl {
12 
13  template<class ifstream>
14  class join_source {
15  public:
16  typedef char char_type;
17  struct category :
18  boost::iostreams::input,
19  boost::iostreams::device_tag,
20  boost::iostreams::closable_tag
21  { };
22 
24 
25  join_source(const std::string& filename)
26  { open(std::vector<std::string>(1, filename)); }
27 
28  join_source(const std::vector<std::string>& filenames)
29  { open(filenames); }
30 
31  void open(const std::string& filename)
32  { open(std::vector<std::string>(1, filename)); }
33 
34  void
35  open(const std::vector<std::string>& filenames)
36  {
37  _ifstream.reset();
38  _filenames = filenames;
40  if (filenames.empty())
41  return;
42  _ifstream.reset(new ifstream(filenames.front()));
43  }
44 
45  void
47  {
48  _ifstream.reset();
49  _filenames.clear();
51  }
52 
53  boost::iostreams::stream_offset
54  get_current_file_index()
55  const
56  {
57  if (_ifstream && _ifstream->is_open() &&_current_file_index < _filenames.size())
58  return _current_file_index;
59  return -1;
60  }
61 
62  std::string
63  get_current_filename()
64  const
65  {
66  const auto i = get_current_file_index();
67  return i >= 0 ? _filenames[i] : "";
68  }
69 
70  std::streamsize
71  read(char* const s, const std::streamsize n)
72  {
73  if (!(_ifstream && _ifstream->is_open()))
74  return -1;
75  const auto size = boost::iostreams::read(*_ifstream, s, n);
76  if (size != -1)
77  return size;
78  next();
79  return read(s, n);
80  }
81 
82  private:
83  void
84  next()
85  {
86  _ifstream.reset();
88  if (_current_file_index < _filenames.size())
89  _ifstream.reset(new ifstream(_filenames[_current_file_index]));
90  }
91 
92  boost::shared_ptr<ifstream> _ifstream;
93  std::vector<std::string> _filenames;
94  std::size_t _current_file_index = 0;
95  };
96 
97 
98  /*
99  \class join join.h "utl/join.h"
100 
101  \author Darko Veberic
102  \date 21 Jan 2017
103  \version $Id: join.h 531 2017-01-26 20:00:08Z darko $
104  */
105 
106  template<class ifstream>
107  class join : public boost::iostreams::stream<join_source<ifstream>> {
108  private:
110  typedef boost::iostreams::stream<source> base;
111 
112  public:
113  join() : base(source()) { }
114 
115  join(const std::string& filename) : base(source(filename)) { }
116 
117  join(const std::vector<std::string>& filenames) : base(source(filenames)) { }
118 
119  void open(const std::string& filename) { (**this).open(filename); }
120 
121  void open(const std::vector<std::string>& filenames) { (**this).open(filenames); }
122  };
123 
124 }
125 
126 
127 #endif
join_source(const std::string &filename)
Definition: join.h:25
join(const std::vector< std::string > &filenames)
Definition: join.h:117
join_source< ifstream > source
Definition: join.h:109
void open(const std::vector< std::string > &filenames)
Definition: join.h:35
std::vector< std::string > _filenames
Definition: join.h:93
constexpr double s
Definition: AugerUnits.h:163
join(const std::string &filename)
Definition: join.h:115
boost::iostreams::stream< source > base
Definition: join.h:110
void open(const std::vector< std::string > &filenames)
Definition: join.h:121
return i
Definition: join.h:67
std::streamsize read(char *const s, const std::streamsize n)
Definition: join.h:71
void close()
Definition: join.h:46
join()
Definition: join.h:113
boost::shared_ptr< ifstream > _ifstream
Definition: join.h:92
void open(const std::string &filename)
Definition: join.h:31
void next()
Definition: join.h:84
char char_type
Definition: join.h:16
join_source(const std::vector< std::string > &filenames)
Definition: join.h:28
char * filename
Definition: dump1090.h:266
std::size_t _current_file_index
Definition: join.h:94
void open(const std::string &filename)
Definition: join.h:119

, generated on Tue Sep 26 2023.