ProteoWizard
random_access_compressed_ifstream.hpp
Go to the documentation of this file.
1 // $Id: random_access_compressed_ifstream.hpp 4140 2012-11-22 01:07:16Z pcbrefugee $
2 //
3 // This is just an istream which chooses its streambuf implementation
4 // based on whether the file is gzipped or not. Could be extended
5 // to work with other compression formats, too.
6 //
7 // What makes it interesting compared to the classic gzstream implementation
8 // is the ability to perform seeks in a reasonably efficient manner. In the
9 // event that a seek is requested (other than a rewind, or tellg()) the file
10 // is decompressed once and snapshots of the compression state are made
11 // every 1MB or so. Further seeks are then quite efficient since they don't
12 // have to begin at the head of the file.
13 //
14 // It also features threaded readahead with adaptive buffering - it will read
15 // increasingly larger chunks of the raw file as it perceives a sequential read
16 // in progress, and will launch a thread to grab the next probable chunk of the
17 // file while the previous chunk is being parsed. This is especially helpful for
18 // files being read across a slow network connection.
19 // If lots of seeking is going on, and the buffer size proves excessive, the read
20 // buffer size decreases. This is also true for the non-compressed case, so this
21 // is generally useful for file read speed optimization.
22 //
23 // Copyright (C) Insilicos LLC 2008,2011 All Rights Reserved.
24 //
25 // draws heavily on example code from the zlib distro, so
26 // for conditions of distribution and use, see copyright notice in zlib.h
27 //
28 // based on:
29 /* gzio.c -- IO on .gz files
30 * Copyright (C) 1995-2005 Jean-loup Gailly.
31 * For conditions of distribution and use, see copyright notice in zlib.h
32  Main idea there is as follows:
33 
34  This software is provided 'as-is', without any express or implied
35  warranty. In no event will the authors be held liable for any damages
36  arising from the use of this software.
37 
38  Permission is granted to anyone to use this software for any purpose,
39  including commercial applications, and to alter it and redistribute it
40  freely, subject to the following restrictions:
41 
42  1. The origin of this software must not be misrepresented; you must not
43  claim that you wrote the original software. If you use this software
44  in a product, an acknowledgment in the product documentation would be
45  appreciated but is not required.
46  2. Altered source versions must be plainly marked as such, and must not be
47  misrepresented as being the original software.
48  3. This notice may not be removed or altered from any source distribution.
49 
50 */
51 
52 // efficient random access stuff based on
53 /* zran.c -- example of zlib/gzip stream indexing and random access
54 * Copyright (C) 2005 Mark Adler
55 * For conditions of distribution and use, see copyright notice in zlib.h
56 Version 1.0 29 May 2005 Mark Adler */
57 
58 
59 
60 #ifndef RANDOM_ACCESS_COMPRESSED_IFSTREAM_INCL
61 #define RANDOM_ACCESS_COMPRESSED_IFSTREAM_INCL
62 
63 
65 #ifdef WIN32
66 #ifdef max //trouble with windef.h max(a,b) colliding with <limits> datatype.max()
67 #undef max
68 #undef min
69 #endif
70 #endif
71 #include "boost/iostreams/positioning.hpp"
72 #include <fstream>
73 
74 
75 namespace pwiz {
76 namespace util {
77 
78 typedef boost::iostreams::stream_offset random_access_compressed_ifstream_off_t;
79 //class random_access_compressed_streambuf; // forward ref
80 
81 class PWIZ_API_DECL random_access_compressed_ifstream : public std::istream {
82 public:
83  random_access_compressed_ifstream(); // default ctor
84  random_access_compressed_ifstream(const char *fname); // optional arg to learn compression state
85  virtual ~random_access_compressed_ifstream(); // destructor
86  void open(const char *fname); // for ease of use as ifstream replacement
87  bool is_open() const; // for ease of use as ifstream replacement
88  void close(); // for ease of use as ifstream replacement
89  enum eCompressionType {NONE, GZIP}; // maybe add bz2 etc later?
91  return compressionType;
92  }
93 private:
95 };
96 
97 
98 } // namespace util
99 } // namespace pwiz
100 
101 #endif // RANDOM_ACCESS_COMPRESSED_IFSTREAM_INCL