]> git.saurik.com Git - wxWidgets.git/blame - utils/wxMMedia/mmriff.cpp
Added test for sprintf and vsnprintf to fix string.cpp for non-GNU systems.
[wxWidgets.git] / utils / wxMMedia / mmriff.cpp
CommitLineData
4d6306eb
GL
1////////////////////////////////////////////////////////////////////////////////
2// Name: mmriff.cpp
3// Purpose: wxMMedia
4// Author: Guilhem Lavaux
5// Created: 1997
6// Updated: 1998
7// Copyright: (C) 1997, 1998, Guilhem Lavaux
8// License: wxWindows license
9////////////////////////////////////////////////////////////////////////////////
10#ifdef __GNUG__
11#pragma implementation "mmriff.h"
12#endif
13
14#ifdef WX_PRECOMP
15#include "wx_prec.h"
16#else
17#include "wx/wx.h"
18#endif
19#include "mmfile.h"
20#include "mmriff.h"
21
22#ifdef __BORLANDC__
23#pragma hdrstop
24#endif
25
26wxRiffCodec::wxRiffCodec() :
27 riff_i_stream(NULL), riff_o_stream(NULL), chunk_length(INVALID_CHUNK_LEN)
28{
29}
30
31wxRiffCodec::wxRiffCodec(wxInputStream& s) :
32 riff_i_stream(&s), riff_o_stream(NULL), chunk_length(INVALID_CHUNK_LEN)
33{
34}
35
36wxRiffCodec::wxRiffCodec(wxOutputStream& s) :
37 riff_i_stream(NULL), riff_o_stream(&s), chunk_length(INVALID_CHUNK_LEN)
38{
39}
40
41wxRiffCodec::~wxRiffCodec()
42{
43}
44
45bool wxRiffCodec::RiffReset(wxUint8 mode)
46{
47 switch (mode) {
48 case RIFF_READ:
49 if (!riff_i_stream)
50 return FALSE;
51 riff_i_stream->SeekI(0);
52 chunk_length = INVALID_CHUNK_LEN;
53 return TRUE;
54 case RIFF_WRITE:
55 if (!riff_o_stream)
56 return FALSE;
57 riff_o_stream->SeekO(0);
58 chunk_length = INVALID_CHUNK_LEN;
59 return TRUE;
60 }
61 return FALSE;
62}
63
64bool wxRiffCodec::ReadData(void *data, wxUint32 size)
65{
66 if (!riff_i_stream)
67 return FALSE;
68
69 if (chunk_length != INVALID_CHUNK_LEN && (wxUint32)chunk_length < size) {
70 riff_error = wxMMFILE_EOF;
71 return FALSE;
72 }
73 if (chunk_length != INVALID_CHUNK_LEN)
74 chunk_length -= size;
75
76 bool ret = (riff_i_stream->Read((char *)data, size).LastRead() == size);
77
78 return ret;
79}
80
81bool wxRiffCodec::WriteData(void *data, wxUint32 size)
82{
83 if (!riff_o_stream)
84 return FALSE;
85
86 if (chunk_length < size) {
87 riff_error = wxMMFILE_EOF;
88 return FALSE;
89 }
90 chunk_length -= size;
91
92 riff_o_stream->Write(data, size);
93
94 return TRUE;
95}
96
97bool wxRiffCodec::Read32(wxUint32& i32)
98{
99 wxUint8 i8[4];
100
101 if (!ReadData(i8, 4))
102 return FALSE;
103
104 i32 = i8[0];
105 i32 |= ((wxUint32)i8[1]) << 8;
106 i32 |= ((wxUint32)i8[2]) << 16;
107 i32 |= ((wxUint32)i8[3]) << 24;
108
109 return TRUE;
110}
111
112bool wxRiffCodec::Write32(wxUint32 i32)
113{
114 wxUint8 i8[4];
115
116 i8[0] = i32 & 0xff;
117 i8[1] = (i32 >> 8) & 0xff;
118 i8[2] = (i32 >> 16) & 0xff;
119 i8[3] = (i32 >> 24) & 0xff;
120
121 if (!WriteData(i8, 4))
122 return FALSE;
123
124 return TRUE;
125}
126
127bool wxRiffCodec::Read16(wxUint16& i16)
128{
129 wxUint8 i8[2];
130
131 if (!ReadData(i8, 2))
132 return FALSE;
133
134 i16 = i8[0];
135 i16 |= ((wxUint16)i8[1]) << 8;
136
137 return TRUE;
138}
139
140bool wxRiffCodec::Write16(wxUint16 i16)
141{
142 wxUint8 i8[2];
143
144 i8[0] = i16 & 0xff;
145 i8[1] = (i16 >> 8) & 0xff;
146
147 if (!WriteData(i8, 2))
148 return FALSE;
149
150 return TRUE;
151}
152
153bool wxRiffCodec::Skip(wxUint32 skip)
154{
155 if (!riff_i_stream || (chunk_length != INVALID_CHUNK_LEN && (wxInt32)skip > chunk_length))
156 return FALSE;
157
158 if (chunk_length != INVALID_CHUNK_LEN)
159 chunk_length -= skip;
160 riff_i_stream->SeekI(skip, wxFromCurrent);
161
162 return TRUE;
163}
164
165bool wxRiffCodec::CreateChunk(const wxString& name, wxUint32 size)
166{
167 if (!riff_o_stream || name.Length() != 4)
168 return FALSE;
169
170 if (riff_o_stream->Write(name.GetData(), 4).LastError()) {
171 riff_error = wxMMFILE_EOF;
172 return FALSE;
173 }
174
175 chunk_length = size+4;
176
177 return Write32(size);
178}
179
180bool wxRiffCodec::FindChunk(const wxString& name, bool from_here)
181{
182 char buf[5];
183 wxString str2;
184
185 if (!riff_i_stream)
186 return FALSE;
187
188 if (chunk_length != INVALID_CHUNK_LEN && !from_here)
189 Skip(chunk_length);
190 while (1) {
191 if (riff_i_stream->Read(buf, 4).LastError()) {
192 riff_error = wxMMFILE_EOF;
193 return FALSE;
194 }
195
196 chunk_length = INVALID_CHUNK_LEN;
197 if (!Read32(chunk_length)) {
198 riff_error = wxMMFILE_EOF;
199 return FALSE;
200 }
201
202 buf[4] = 0;
203 str2 = buf;
204 if ((!name.IsNull()) && str2 != name) {
205 Skip(chunk_length);
206 continue;
207 }
208
209 m_chunk = str2;
210
211 return TRUE;
212 }
213
214 return TRUE;
215}