Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
parallel_ifstream.hpp
1 #ifndef PARALLEL_IFSTREAM
2 #define PARALLEL_IFSTREAM
3 
4 #include <string>
5 #include <fstream>
6 #include <iostream>
7 
8 
9 class parallel_filebuf : public std::filebuf
10 {
11 protected:
12  virtual int_type sync()
13  {
14  return std::filebuf::sync();
15  }
16 
17  virtual std::streamsize xsgetn( char_type* s, std::streamsize count )
18  {
19  return std::filebuf::xsgetn( s, count );
20  }
21 
22  virtual int_type overflow( int_type c = traits_type::eof() )
23  {
24  return std::filebuf::overflow( c );
25  }
26 };
27 
28 class parallel_ifstream : public std::ifstream
29 {
30 public:
31  parallel_ifstream() : std::ifstream( 0 )
32  {
33  init( &_buf );
34  }
35  parallel_ifstream( const char* filename, std::ios_base::openmode mode = std::ios_base::in )
36  : std::ifstream( 0 )
37  {
38  init( &_buf );
39  this->open( filename, mode );
40  }
41 
42  bool is_open() const
43  {
44  return _buf.is_open();
45  }
46 
47  void close()
48  {
49  _buf.close();
50  }
51 
52  void open(const char* filename, std::ios_base::openmode mode = std::ios_base::out )
53  {
54  _buf.open(filename, mode);
55  }
56 
57  std::filebuf* rdbuf()
58  {
59  return &_buf;
60  }
61 
62 private:
63  parallel_filebuf _buf;
64 };
65 
66 int main()
67 {
68  parallel_ifstream fs( "test.txt" );
69  while(!fs.eof()) {
70  std::string s;
71  std::getline(fs, s);
72  std::cout << s << std::endl;
73  }
74 
75  char c;
76  const size_t buffersize = 1e+6;
77  unsigned char checksum = 0;
78  char buffer[buffersize];
79  std::ifstream file;
80 
81  std::streambuf& sbuf = *(file.rdbuf());
82  sbuf.pubsetbuf(buffer, buffersize);
83  file.open("/home/igel/work/lirmm/Scaffolding/data/big/azucena.dot");
84  file.get(c);
85  checksum += (unsigned char)c;
86 
87  // read buffer state immediately after opening & 1 sec after opening
88  size_t avail1 = sbuf.in_avail();
89 
90  // read some lines from the buffer and repeat the experiment
91  for(size_t i = 0; i < (buffersize-100); ++i){
92  //while(!file.eof()) {
93  file.get(c);
94  checksum += (unsigned char)c;
95  }
96  size_t avail2 = sbuf.in_avail();
97 
98  while(!file.eof()) {
99  file.get(c);
100  checksum += (unsigned char)c;
101  }
102 
103  // finally, output results
104  std::cout << "available data: "<<avail1<<" "<<avail2<<" checksum is "<<(int)checksum<<std::endl;
105 
106 }
107 
108 #endif
Definition: parallel_ifstream.hpp:9
Definition: parallel_ifstream.hpp:28