]> git.saurik.com Git - wxWidgets.git/blame - src/common/zipstrm.cpp
another compilation fix
[wxWidgets.git] / src / common / zipstrm.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: zipstream.cpp
3// Purpose: input stream for ZIP archive access
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
6// Licence: wxWindows Licence
7/////////////////////////////////////////////////////////////////////////////
8
9#ifdef __GNUG__
5279a24d 10#pragma implementation "zipstrm.h"
5526e819
VS
11#endif
12
d1af991f
RR
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
5526e819 15
d1af991f
RR
16#ifdef __BORLANDC__
17 #pragma hdrstop
5526e819
VS
18#endif
19
e90c1d2a 20#if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
d78b3d64 21
d1af991f
RR
22#include "wx/log.h"
23#include "wx/intl.h"
24#include "wx/stream.h"
25#include "wx/wfstream.h"
6001e347 26#include "wx/zipstrm.h"
ea4f5235
JS
27
28/* Not the right solution (paths in makefiles) but... */
29#ifdef __BORLANDC__
30#include "../common/unzip.h"
31#else
5526e819 32#include "unzip.h"
ea4f5235
JS
33#endif
34
5526e819 35
5526e819
VS
36wxZipInputStream::wxZipInputStream(const wxString& archive, const wxString& file) : wxInputStream()
37{
38 unz_file_info zinfo;
39
40 m_Pos = 0;
41 m_Size = 0;
f6bcfd97 42 m_Archive = (void*) unzOpen(archive.mb_str());
e90c1d2a 43 if (m_Archive == NULL)
d1af991f 44 {
5526e819
VS
45 m_lasterror = wxStream_READ_ERR;
46 return;
47 }
f6bcfd97 48 if (unzLocateFile((unzFile)m_Archive, file.mb_str(), 0) != UNZ_OK)
d1af991f 49 {
5526e819
VS
50 m_lasterror = wxStream_READ_ERR;
51 return;
52 }
e90c1d2a 53
d1af991f 54 unzGetCurrentFileInfo((unzFile)m_Archive, &zinfo, (char*) NULL, 0, (void*) NULL, 0, (char*) NULL, 0);
5526e819 55
e90c1d2a 56 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK)
d1af991f 57 {
5526e819
VS
58 m_lasterror = wxStream_READ_ERR;
59 return;
60 }
479cd5de 61 m_Size = (size_t)zinfo.uncompressed_size;
5526e819
VS
62}
63
64
65
66wxZipInputStream::~wxZipInputStream()
67{
e90c1d2a 68 if (m_Archive)
d1af991f 69 {
5526e819
VS
70 if (m_Size != 0)
71 unzCloseCurrentFile((unzFile)m_Archive);
72 unzClose((unzFile)m_Archive);
73 }
74}
75
f6bcfd97
BP
76bool wxZipInputStream::Eof() const
77{
78 wxASSERT_MSG( m_Pos <= (off_t)m_Size,
79 _T("wxZipInputStream: invalid current position") );
80
81 return m_Pos >= (off_t)m_Size;
82}
5526e819
VS
83
84
85size_t wxZipInputStream::OnSysRead(void *buffer, size_t bufsize)
86{
f6bcfd97
BP
87 wxASSERT_MSG( m_Pos <= (off_t)m_Size,
88 _T("wxZipInputStream: invalid current position") );
89
90 if ( m_Pos >= (off_t)m_Size )
91 {
92 m_lasterror = wxStream_EOF;
93 return 0;
94 }
95
96 if (m_Pos + bufsize > m_Size)
97 bufsize = m_Size - m_Pos;
98
5526e819
VS
99 unzReadCurrentFile((unzFile)m_Archive, buffer, bufsize);
100 m_Pos += bufsize;
f6bcfd97 101
5526e819
VS
102 return bufsize;
103}
104
105
106
107off_t wxZipInputStream::OnSysSeek(off_t seek, wxSeekMode mode)
108{
109 off_t nextpos;
110 void *buf;
111
e90c1d2a 112 switch (mode)
d1af991f 113 {
5526e819
VS
114 case wxFromCurrent : nextpos = seek + m_Pos; break;
115 case wxFromStart : nextpos = seek; break;
116 case wxFromEnd : nextpos = m_Size - 1 + seek; break;
117 default : nextpos = m_Pos; break; /* just to fool compiler, never happens */
118 }
119
120 // cheated seeking :
e90c1d2a 121 if (nextpos > m_Pos)
d1af991f 122 {
5526e819
VS
123 buf = malloc(nextpos - m_Pos);
124 unzReadCurrentFile((unzFile)m_Archive, buf, nextpos - m_Pos);
125 free(buf);
126 }
127 else if (nextpos < m_Pos) {
128 unzCloseCurrentFile((unzFile)m_Archive);
e90c1d2a
VZ
129 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK)
130 {
5526e819
VS
131 m_lasterror = wxStream_READ_ERR;
132 return m_Pos;
133 }
134 buf = malloc(nextpos);
135 unzReadCurrentFile((unzFile)m_Archive, buf, nextpos);
136 free(buf);
137 }
138
139 m_Pos = nextpos;
140 return m_Pos;
141}
142
d78b3d64 143#endif
e90c1d2a 144 // wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB