1 |
/*************************************************************************** |
2 |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
5 |
* Copyright (C) 2003-2014 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
7 |
* * |
8 |
* This program is part of libgig. * |
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 Free Software * |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
23 |
* MA 02111-1307 USA * |
24 |
***************************************************************************/ |
25 |
|
26 |
#ifdef HAVE_CONFIG_H |
27 |
#include <config.h> |
28 |
#endif |
29 |
|
30 |
#include <stdio.h> |
31 |
#include <iostream> |
32 |
#include <string> |
33 |
#include <cstdlib> |
34 |
|
35 |
#include "RIFF.h" |
36 |
|
37 |
using namespace std; |
38 |
|
39 |
string Revision(); |
40 |
void PrintVersion(); |
41 |
void PrintUsage(); |
42 |
void PrintChunkList(RIFF::List* list, bool PrintSize); |
43 |
static uint32_t strToChunkID(string s); |
44 |
|
45 |
int main(int argc, char *argv[]) |
46 |
{ |
47 |
bool bPrintSize = false; |
48 |
RIFF::endian_t endian = RIFF::endian_native; |
49 |
RIFF::layout_t layout = RIFF::layout_standard; |
50 |
bool bExpectFirstChunkID = false; |
51 |
uint32_t uiExpectedFirstChunkID; |
52 |
|
53 |
// validate & parse arguments provided to this program |
54 |
if (argc <= 1) { |
55 |
PrintUsage(); |
56 |
return EXIT_FAILURE; |
57 |
} |
58 |
int iArg; |
59 |
for (iArg = 1; iArg < argc; ++iArg) { |
60 |
const string opt = argv[iArg]; |
61 |
if (opt == "--") { // common for all command line tools: separator between initial option arguments and i.e. subsequent file arguments |
62 |
iArg++; |
63 |
break; |
64 |
} |
65 |
if (opt.substr(0, 1) != "-") break; |
66 |
|
67 |
if (opt == "-v") { |
68 |
PrintVersion(); |
69 |
return EXIT_SUCCESS; |
70 |
} else if (opt == "-s") { |
71 |
bPrintSize = true; |
72 |
} else if (opt == "--big-endian") { |
73 |
endian = RIFF::endian_big; |
74 |
} else if (opt == "--little-endian") { |
75 |
endian = RIFF::endian_little; |
76 |
} else if (opt == "--flat") { |
77 |
layout = RIFF::layout_flat; |
78 |
} else if (opt == "--first-chunk-id") { |
79 |
iArg++; |
80 |
if (iArg >= argc) { |
81 |
PrintVersion(); |
82 |
return EXIT_SUCCESS; |
83 |
} |
84 |
bExpectFirstChunkID = true; |
85 |
uiExpectedFirstChunkID = strToChunkID(argv[iArg]); |
86 |
} else { |
87 |
cerr << "Unknown option '" << opt << "'" << endl; |
88 |
cerr << endl; |
89 |
PrintUsage(); |
90 |
return EXIT_FAILURE; |
91 |
} |
92 |
} |
93 |
|
94 |
const int nFileArguments = argc - iArg; |
95 |
if (nFileArguments != 1) { |
96 |
cerr << "You must provide a file argument." << endl; |
97 |
return EXIT_FAILURE; |
98 |
} |
99 |
const int FileArgIndex = iArg; |
100 |
|
101 |
FILE* hFile = fopen(argv[FileArgIndex], "r"); |
102 |
if (!hFile) { |
103 |
cerr << "Invalid file argument!" << endl; |
104 |
return EXIT_FAILURE; |
105 |
} |
106 |
fclose(hFile); |
107 |
try { |
108 |
RIFF::File* riff = NULL; |
109 |
switch (layout) { |
110 |
case RIFF::layout_standard: |
111 |
riff = new RIFF::File(argv[FileArgIndex]); |
112 |
cout << "RIFF(" << riff->GetListTypeString() << ")->"; |
113 |
break; |
114 |
case RIFF::layout_flat: |
115 |
if (!bExpectFirstChunkID) { |
116 |
cerr << "If you are using '--flat' then you must also use '--first-chunk-id'." << endl; |
117 |
return EXIT_FAILURE; |
118 |
} |
119 |
riff = new RIFF::File( |
120 |
argv[FileArgIndex], uiExpectedFirstChunkID, endian, layout |
121 |
); |
122 |
cout << "Flat RIFF-alike file"; |
123 |
break; |
124 |
} |
125 |
if (bPrintSize) cout << " (" << riff->GetSize() << " Bytes)"; |
126 |
cout << endl; |
127 |
PrintChunkList(riff, bPrintSize); |
128 |
delete riff; |
129 |
} |
130 |
catch (RIFF::Exception e) { |
131 |
e.PrintMessage(); |
132 |
return EXIT_FAILURE; |
133 |
} |
134 |
catch (...) { |
135 |
cerr << "Unknown exception while trying to parse file." << endl; |
136 |
return EXIT_FAILURE; |
137 |
} |
138 |
|
139 |
return EXIT_SUCCESS; |
140 |
} |
141 |
|
142 |
void PrintChunkList(RIFF::List* list, bool PrintSize) { |
143 |
RIFF::Chunk* ck = list->GetFirstSubChunk(); |
144 |
while (ck != NULL) { |
145 |
RIFF::Chunk* ckParent = ck; |
146 |
while (ckParent->GetParent() != NULL) { |
147 |
cout << " "; // e.g. 'LIST(INFO)->' |
148 |
ckParent = ckParent->GetParent(); |
149 |
} |
150 |
cout << ck->GetChunkIDString(); |
151 |
switch (ck->GetChunkID()) { |
152 |
case CHUNK_ID_LIST: case CHUNK_ID_RIFF: |
153 |
{ |
154 |
RIFF::List* l = (RIFF::List*) ck; |
155 |
cout << "(" << l->GetListTypeString() << ")->"; |
156 |
if (PrintSize) cout << " (" << l->GetSize() << " Bytes)"; |
157 |
cout << endl; |
158 |
PrintChunkList(l, PrintSize); |
159 |
break; |
160 |
} |
161 |
default: |
162 |
cout << ";"; |
163 |
if (PrintSize) cout << " (" << ck->GetSize() << " Bytes)"; |
164 |
cout << endl; |
165 |
} |
166 |
ck = list->GetNextSubChunk(); |
167 |
} |
168 |
} |
169 |
|
170 |
static uint32_t strToChunkID(string s) { |
171 |
if (s.size() != 4) { |
172 |
cerr << "Argument after '--first-chunk-id' must be exactly 4 characters long." << endl; |
173 |
exit(-1); |
174 |
} |
175 |
uint32_t result = 0; |
176 |
for (int i = 0; i < 4; ++i) { |
177 |
uint32_t byte = s[i]; |
178 |
result |= byte << i*8; |
179 |
} |
180 |
return result; |
181 |
} |
182 |
|
183 |
string Revision() { |
184 |
string s = "$Revision$"; |
185 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
186 |
} |
187 |
|
188 |
void PrintVersion() { |
189 |
cout << "rifftree revision " << Revision() << endl; |
190 |
cout << "using " << RIFF::libraryName() << " " << RIFF::libraryVersion() << endl; |
191 |
} |
192 |
|
193 |
void PrintUsage() { |
194 |
cout << "rifftree - parses an arbitrary RIFF file and prints out the RIFF tree structure." << endl; |
195 |
cout << endl; |
196 |
cout << "Usage: rifftree [OPTIONS] FILE" << endl; |
197 |
cout << endl; |
198 |
cout << "Options:" << endl; |
199 |
cout << endl; |
200 |
cout << " -v Print version and exit." << endl; |
201 |
cout << endl; |
202 |
cout << " -s Print the size of each chunk." << endl; |
203 |
cout << endl; |
204 |
cout << " --flat" << endl; |
205 |
cout << " First chunk of file is not a list (container) chunk." << endl; |
206 |
cout << endl; |
207 |
cout << " --first-chunk-id CKID" << endl; |
208 |
cout << " 32 bit chunk ID of very first chunk in file." << endl; |
209 |
cout << endl; |
210 |
cout << " --big-endian" << endl; |
211 |
cout << " File is in big endian format." << endl; |
212 |
cout << endl; |
213 |
cout << " --little-endian" << endl; |
214 |
cout << " File is in little endian format." << endl; |
215 |
cout << endl; |
216 |
} |