1 |
schoenebeck |
1405 |
// This little test application demonstrates how to create and modify |
2 |
|
|
// arbitrary RIFF files with libgig 3.0.0. |
3 |
|
|
// |
4 |
|
|
// Date: 2006-04-29 |
5 |
|
|
// |
6 |
|
|
// Compile with: 'g++ -lgig -o riffwritedemo riffwritedemo.cpp' |
7 |
|
|
|
8 |
|
|
#include <RIFF.h> |
9 |
|
|
|
10 |
|
|
#include <stdlib.h> |
11 |
|
|
#include <stdio.h> |
12 |
|
|
#include <string.h> |
13 |
|
|
|
14 |
|
|
#define LIST_TYPE_FOOX 0x584F4F46 |
15 |
|
|
#define LIST_TYPE_GOOX 0x584F4F47 |
16 |
|
|
#define CHUNK_ID_AAAA 0x41414141 |
17 |
|
|
#define CHUNK_ID_BBBB 0x42424242 |
18 |
|
|
#define CHUNK_ID_CCCC 0x43434343 |
19 |
|
|
|
20 |
|
|
int main() { |
21 |
|
|
try { |
22 |
|
|
// Create a RIFF file with one list chunk <FOOX> having three |
23 |
|
|
// subchunks (<AAA>, <BBBB> and <CCCC>) and save it as |
24 |
|
|
// "footext.riff". Data for the three chunks will be directly |
25 |
|
|
// written to disk. |
26 |
|
|
{ |
27 |
|
|
// create empty RIFF file with RIFF file type <GOOX> |
28 |
|
|
RIFF::File file(LIST_TYPE_GOOX); |
29 |
|
|
// add a sublist chunk with type <FOOX> |
30 |
|
|
RIFF::List* pList = file.AddSubList(LIST_TYPE_FOOX); |
31 |
|
|
// add three subchunks to the sublist chunk, each having a data |
32 |
|
|
// body size of one byte |
33 |
|
|
RIFF::Chunk* pChunkA = pList->AddSubChunk(CHUNK_ID_AAAA, 1); |
34 |
|
|
RIFF::Chunk* pChunkB = pList->AddSubChunk(CHUNK_ID_BBBB, 1); |
35 |
|
|
RIFF::Chunk* pChunkC = pList->AddSubChunk(CHUNK_ID_CCCC, 1); |
36 |
|
|
// write that new RIFF file |
37 |
|
|
file.Save("footext.riff"); |
38 |
|
|
|
39 |
|
|
// write an arbitrary byte to the chunks |
40 |
|
|
// (by directly writing to disk) |
41 |
|
|
uint8_t c = '1'; |
42 |
|
|
pChunkA->WriteUint8(&c); c = '2'; |
43 |
|
|
pChunkB->WriteUint8(&c); c = '3'; |
44 |
|
|
pChunkC->WriteUint8(&c); |
45 |
|
|
|
46 |
|
|
// resize (enlarge) chunk A and B to five bytes each |
47 |
|
|
pChunkA->Resize(5); |
48 |
|
|
pChunkB->Resize(5); |
49 |
|
|
// actually apply the resize operations to the file |
50 |
|
|
file.Save(); |
51 |
|
|
|
52 |
|
|
// now that the file and thus the chunks are physically resized, |
53 |
|
|
// we can write more data to them |
54 |
|
|
c = 'V'; |
55 |
|
|
pChunkA->WriteUint8(&c); |
56 |
|
|
c = 'E'; |
57 |
|
|
pChunkA->WriteUint8(&c); |
58 |
|
|
c = 'R'; |
59 |
|
|
pChunkA->WriteUint8(&c); |
60 |
|
|
c = 'A'; |
61 |
|
|
pChunkA->WriteUint8(&c); |
62 |
|
|
c = '6'; |
63 |
|
|
pChunkA->WriteUint8(&c); |
64 |
|
|
|
65 |
|
|
c = 'A'; |
66 |
|
|
pChunkB->WriteUint8(&c); |
67 |
|
|
c = 'R'; |
68 |
|
|
pChunkB->WriteUint8(&c); |
69 |
|
|
c = 'E'; |
70 |
|
|
pChunkB->WriteUint8(&c); |
71 |
|
|
c = 'V'; |
72 |
|
|
pChunkB->WriteUint8(&c); |
73 |
|
|
c = '7'; |
74 |
|
|
pChunkB->WriteUint8(&c); |
75 |
|
|
|
76 |
|
|
// resize (shrink) chunk A to one byte |
77 |
|
|
pChunkA->Resize(1); |
78 |
|
|
file.Save(); |
79 |
|
|
|
80 |
|
|
// write an arbitrary new byte to chunk A |
81 |
|
|
c = 'Q'; |
82 |
|
|
pChunkA->WriteUint8(&c); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
// Create yet another RIFF file with one list chunk <FOOX> having |
86 |
|
|
// three subchunks (<AAA>, <BBBB> and <CCCC>) and save it as |
87 |
|
|
// "footextRAM.riff". Data for the three chunks will this time be |
88 |
|
|
// 'written' to RAM and afterwards actually be written to disk by |
89 |
|
|
// the File::Save() call. |
90 |
|
|
{ |
91 |
|
|
// create empty RIFF file with RIFF file type <GOOX> |
92 |
|
|
RIFF::File file(LIST_TYPE_GOOX); |
93 |
|
|
// add a sublist chunk with type <FOOX> |
94 |
|
|
RIFF::List* pList = file.AddSubList(LIST_TYPE_FOOX); |
95 |
|
|
// add three subchunks to the sublist chunk, each having a data |
96 |
|
|
// body size of one byte |
97 |
|
|
RIFF::Chunk* pChunkA = pList->AddSubChunk(CHUNK_ID_AAAA, 1); |
98 |
|
|
RIFF::Chunk* pChunkB = pList->AddSubChunk(CHUNK_ID_BBBB, 1); |
99 |
|
|
RIFF::Chunk* pChunkC = pList->AddSubChunk(CHUNK_ID_CCCC, 1); |
100 |
|
|
// write that new RIFF file |
101 |
|
|
file.Save("footextRAM.riff"); |
102 |
|
|
// get the buffer to copy the chunks' data to |
103 |
|
|
char* pAData = (char*) pChunkA->LoadChunkData(); |
104 |
|
|
char* pBData = (char*) pChunkB->LoadChunkData(); |
105 |
|
|
char* pCData = (char*) pChunkC->LoadChunkData(); |
106 |
|
|
// copy the chunks' data to the retrieved buffer |
107 |
|
|
pAData[0] = '1'; |
108 |
|
|
pBData[0] = '2'; |
109 |
|
|
pCData[0] = '3'; |
110 |
|
|
|
111 |
|
|
// we already schedule to enlarge chunk A and B to a size of |
112 |
|
|
// five bytes |
113 |
|
|
pChunkA->Resize(5); |
114 |
|
|
pChunkB->Resize(5); |
115 |
|
|
// write the buffered chunk data to disk and apply the resize |
116 |
|
|
// operations in one rush |
117 |
|
|
file.Save(); |
118 |
|
|
|
119 |
|
|
char vera6[] = "vera6"; |
120 |
|
|
char arev7[] = "arev7"; |
121 |
|
|
// we have to re-retrieve the buffers, as we resized the chunks |
122 |
|
|
pAData = (char*) pChunkA->LoadChunkData(); |
123 |
|
|
pBData = (char*) pChunkB->LoadChunkData(); |
124 |
|
|
pCData = (char*) pChunkC->LoadChunkData(); |
125 |
|
|
// write arbitrary five bytes as data to the chunk's buffers |
126 |
|
|
memcpy(pAData, vera6, 5); |
127 |
|
|
memcpy(pBData, arev7, 5); |
128 |
|
|
// actually write that data to disk |
129 |
|
|
file.Save(); |
130 |
|
|
|
131 |
|
|
// shrink chunk A to a data body size of one byte |
132 |
|
|
pChunkA->Resize(1); |
133 |
|
|
// perform shrink operation |
134 |
|
|
file.Save(); |
135 |
|
|
|
136 |
|
|
// finally write another arbitrary byte to the shrinked chunk |
137 |
|
|
pAData = (char*) pChunkA->LoadChunkData(); |
138 |
|
|
pAData[0] = 'Q'; |
139 |
|
|
file.Save(); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
// In this example we create a RIFF file as in the previous example, |
143 |
|
|
// but this time we delete chunks afterwards. |
144 |
|
|
{ |
145 |
|
|
// create empty RIFF file with RIFF file type <GOOX> |
146 |
|
|
RIFF::File file(LIST_TYPE_GOOX); |
147 |
|
|
// add a sublist chunk with type <FOOX> |
148 |
|
|
RIFF::List* pList = file.AddSubList(LIST_TYPE_FOOX); |
149 |
|
|
// add three subchunks to the sublist chunk, each having a data |
150 |
|
|
// body size of one byte |
151 |
|
|
RIFF::Chunk* pChunkA = pList->AddSubChunk(CHUNK_ID_AAAA, 1); |
152 |
|
|
RIFF::Chunk* pChunkB = pList->AddSubChunk(CHUNK_ID_BBBB, 1); |
153 |
|
|
RIFF::Chunk* pChunkC = pList->AddSubChunk(CHUNK_ID_CCCC, 1); |
154 |
|
|
// write that new RIFF file |
155 |
|
|
file.Save("footext_delete.riff"); |
156 |
|
|
|
157 |
|
|
// write some arbitrary data to the chunks |
158 |
|
|
uint8_t c = '1'; |
159 |
|
|
pChunkA->WriteUint8(&c); c = '2'; |
160 |
|
|
pChunkB->WriteUint8(&c); c = '3'; |
161 |
|
|
pChunkC->WriteUint8(&c); |
162 |
|
|
|
163 |
|
|
// schedule to enlarge chunk A and B to five bytes |
164 |
|
|
pChunkA->Resize(5); |
165 |
|
|
pChunkB->Resize(5); |
166 |
|
|
// perform scheduled enlarge operations |
167 |
|
|
file.Save(); |
168 |
|
|
|
169 |
|
|
// write some more data to the enlarged chunks |
170 |
|
|
c = 'V'; |
171 |
|
|
pChunkA->WriteUint8(&c); |
172 |
|
|
c = 'E'; |
173 |
|
|
pChunkA->WriteUint8(&c); |
174 |
|
|
c = 'R'; |
175 |
|
|
pChunkA->WriteUint8(&c); |
176 |
|
|
c = 'A'; |
177 |
|
|
pChunkA->WriteUint8(&c); |
178 |
|
|
c = '6'; |
179 |
|
|
pChunkA->WriteUint8(&c); |
180 |
|
|
|
181 |
|
|
c = 'A'; |
182 |
|
|
pChunkB->WriteUint8(&c); |
183 |
|
|
c = 'R'; |
184 |
|
|
pChunkB->WriteUint8(&c); |
185 |
|
|
c = 'E'; |
186 |
|
|
pChunkB->WriteUint8(&c); |
187 |
|
|
c = 'V'; |
188 |
|
|
pChunkB->WriteUint8(&c); |
189 |
|
|
c = '7'; |
190 |
|
|
pChunkB->WriteUint8(&c); |
191 |
|
|
|
192 |
|
|
// schedule to delete chunk A |
193 |
|
|
pList->DeleteSubChunk(pChunkA); |
194 |
|
|
// apply 'delete' operation to file |
195 |
|
|
file.Save(); |
196 |
|
|
} |
197 |
|
|
} catch (RIFF::Exception e) { |
198 |
|
|
e.PrintMessage(); |
199 |
|
|
return -1; |
200 |
|
|
} |
201 |
|
|
return 0; |
202 |
|
|
} |