1 |
/*************************************************************************** |
2 |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
7 |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#ifdef HAVE_CONFIG_H |
25 |
#include <config.h> |
26 |
#endif |
27 |
|
28 |
#include <stdio.h> |
29 |
#include <iostream> |
30 |
#include <string> |
31 |
#include <cstdlib> |
32 |
|
33 |
#include "RIFF.h" |
34 |
|
35 |
using namespace std; |
36 |
|
37 |
string Revision(); |
38 |
void PrintVersion(); |
39 |
void PrintUsage(); |
40 |
void PrintChunkList(RIFF::List* list, bool PrintSize); |
41 |
|
42 |
int main(int argc, char *argv[]) |
43 |
{ |
44 |
int FileArgIndex = 1; |
45 |
bool bPrintSize = false; |
46 |
|
47 |
if (argc <= 1) { |
48 |
PrintUsage(); |
49 |
return EXIT_FAILURE; |
50 |
} |
51 |
if (argv[1][0] == '-') { |
52 |
switch (argv[1][1]) { |
53 |
case 's': |
54 |
bPrintSize = true; |
55 |
break; |
56 |
case 'v': |
57 |
PrintVersion(); |
58 |
return EXIT_SUCCESS; |
59 |
default: |
60 |
cerr << "Unknown option -" << argv[1][1] << endl; |
61 |
cerr << endl; |
62 |
PrintUsage(); |
63 |
return EXIT_FAILURE; |
64 |
} |
65 |
FileArgIndex++; |
66 |
} |
67 |
if (FileArgIndex >= argc) { |
68 |
PrintUsage(); |
69 |
return EXIT_FAILURE; |
70 |
} |
71 |
FILE* hFile = fopen(argv[FileArgIndex], "r"); |
72 |
if (!hFile) { |
73 |
cout << "Invalid file argument!" << endl; |
74 |
return EXIT_FAILURE; |
75 |
} |
76 |
fclose(hFile); |
77 |
try { |
78 |
RIFF::File* riff = new RIFF::File(argv[FileArgIndex]); |
79 |
cout << "RIFF(" << riff->GetListTypeString() << ")->"; |
80 |
if (bPrintSize) cout << " (" << riff->GetSize() << " Bytes)"; |
81 |
cout << endl; |
82 |
PrintChunkList(riff, bPrintSize); |
83 |
delete riff; |
84 |
} |
85 |
catch (RIFF::Exception e) { |
86 |
e.PrintMessage(); |
87 |
return EXIT_FAILURE; |
88 |
} |
89 |
catch (...) { |
90 |
cout << "Unknown exception while trying to parse file." << endl; |
91 |
return EXIT_FAILURE; |
92 |
} |
93 |
|
94 |
return EXIT_SUCCESS; |
95 |
} |
96 |
|
97 |
void PrintChunkList(RIFF::List* list, bool PrintSize) { |
98 |
RIFF::Chunk* ck = list->GetFirstSubChunk(); |
99 |
while (ck != NULL) { |
100 |
RIFF::Chunk* ckParent = ck; |
101 |
while (ckParent->GetParent() != NULL) { |
102 |
cout << " "; // e.g. 'LIST(INFO)->' |
103 |
ckParent = ckParent->GetParent(); |
104 |
} |
105 |
cout << ck->GetChunkIDString(); |
106 |
switch (ck->GetChunkID()) { |
107 |
case CHUNK_ID_LIST: case CHUNK_ID_RIFF: |
108 |
{ |
109 |
RIFF::List* l = (RIFF::List*) ck; |
110 |
cout << "(" << l->GetListTypeString() << ")->"; |
111 |
if (PrintSize) cout << " (" << l->GetSize() << " Bytes)"; |
112 |
cout << endl; |
113 |
PrintChunkList(l, PrintSize); |
114 |
break; |
115 |
} |
116 |
default: |
117 |
cout << ";"; |
118 |
if (PrintSize) cout << " (" << ck->GetSize() << " Bytes)"; |
119 |
cout << endl; |
120 |
} |
121 |
ck = list->GetNextSubChunk(); |
122 |
} |
123 |
} |
124 |
|
125 |
string Revision() { |
126 |
string s = "$Revision: 1.3 $"; |
127 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
128 |
} |
129 |
|
130 |
void PrintVersion() { |
131 |
cout << "rifftree revision " << Revision() << endl; |
132 |
cout << "using " << RIFF::libraryName() << " " << RIFF::libraryVersion() << endl; |
133 |
} |
134 |
|
135 |
void PrintUsage() { |
136 |
cout << "rifftree - parses an arbitrary RIFF file and prints out the RIFF tree structure." << endl; |
137 |
cout << endl; |
138 |
cout << "Usage: rifftree [-s|-v] FILE" << endl; |
139 |
cout << endl; |
140 |
cout << " -s Print the size of each chunk." << endl; |
141 |
cout << " -v Print version and exit." << endl; |
142 |
cout << endl; |
143 |
} |