OpenCPN Partial API docs
chartdata_input_stream.h
1 /******************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: Support XZ compressed charts
5  * Author: Sean D'Epagnier
6  *
7  ***************************************************************************
8  * Copyright (C) 2016 by David S. Register *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24  ***************************************************************************
25  *
26  */
27 
28 #include "config.h"
29 #include <wx/ffile.h>
30 
31 #ifdef OCPN_USE_LZMA
32 #include <lzma.h>
33 
34 // this implements a non-seekable input stream of xz compressed file
35 class wxCompressedFFileInputStream : public wxInputStream {
36 public:
37  wxCompressedFFileInputStream(const wxString &fileName);
38  virtual ~wxCompressedFFileInputStream();
39 
40  virtual bool IsOk() const {
41  return wxStreamBase::IsOk() && m_file->IsOpened();
42  }
43  bool IsSeekable() const { return false; }
44 
45 protected:
46  size_t OnSysRead(void *buffer, size_t size);
47  wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
48  wxFileOffset OnSysTell() const;
49 
50  wxFFile *m_file;
51  lzma_stream strm;
52 
53 private:
54  void init_lzma();
55 
56  uint8_t inbuf[BUFSIZ];
57 
58  wxDECLARE_NO_COPY_CLASS(wxCompressedFFileInputStream);
59 };
60 
61 // non-seekable stream for either non-compressed or compressed files
62 class ChartDataNonSeekableInputStream : public wxInputStream {
63 public:
64  ChartDataNonSeekableInputStream(const wxString &fileName);
65  virtual ~ChartDataNonSeekableInputStream();
66 
67  virtual bool IsOk() const { return m_stream->IsOk(); }
68  bool IsSeekable() const { return false; }
69 
70 protected:
71  size_t OnSysRead(void *buffer, size_t size);
72  wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
73  wxFileOffset OnSysTell() const;
74 
75 private:
76  wxInputStream *m_stream;
77 
78  wxDECLARE_NO_COPY_CLASS(ChartDataNonSeekableInputStream);
79 };
80 
81 // seekable stream for either non-compressed or compressed files
82 // it must decompress the file to a temporary file to make it seekable
83 class ChartDataInputStream : public wxInputStream {
84 public:
85  ChartDataInputStream(const wxString &fileName);
86  virtual ~ChartDataInputStream();
87 
88  virtual bool IsOk() const { return m_stream->IsOk(); }
89  bool IsSeekable() const { return m_stream->IsSeekable(); }
90 
91  wxString TempFileName() const { return m_tempfilename; }
92 
93 protected:
94  size_t OnSysRead(void *buffer, size_t size);
95  wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
96  wxFileOffset OnSysTell() const;
97 
98 private:
99  wxString m_tempfilename;
100  wxInputStream *m_stream;
101 
102  wxDECLARE_NO_COPY_CLASS(ChartDataInputStream);
103 };
104 
105 #else
106 
107 typedef wxFFileInputStream ChartDataInputStream;
108 typedef wxFFileInputStream ChartDataNonSeekableInputStream;
109 
110 #endif // OCPN_USE_LZMA
111 
112 bool DecompressXZFile(const wxString &input_path, const wxString &output_path);