1 |
/*************************************************************************** |
2 |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
5 |
* Copyright (C) 2003-2006 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 |
|
44 |
int main(int argc, char *argv[]) |
45 |
{ |
46 |
int FileArgIndex = 1; |
47 |
bool bPrintSize = false; |
48 |
|
49 |
if (argc <= 1) { |
50 |
PrintUsage(); |
51 |
return EXIT_FAILURE; |
52 |
} |
53 |
if (argv[1][0] == '-') { |
54 |
switch (argv[1][1]) { |
55 |
case 's': |
56 |
bPrintSize = true; |
57 |
break; |
58 |
case 'v': |
59 |
PrintVersion(); |
60 |
return EXIT_SUCCESS; |
61 |
default: |
62 |
cerr << "Unknown option -" << argv[1][1] << endl; |
63 |
cerr << endl; |
64 |
PrintUsage(); |
65 |
return EXIT_FAILURE; |
66 |
} |
67 |
FileArgIndex++; |
68 |
} |
69 |
if (FileArgIndex >= argc) { |
70 |
PrintUsage(); |
71 |
return EXIT_FAILURE; |
72 |
} |
73 |
FILE* hFile = fopen(argv[FileArgIndex], "r"); |
74 |
if (!hFile) { |
75 |
cout << "Invalid file argument!" << endl; |
76 |
return EXIT_FAILURE; |
77 |
} |
78 |
fclose(hFile); |
79 |
try { |
80 |
RIFF::File* riff = new RIFF::File(argv[FileArgIndex]); |
81 |
cout << "RIFF(" << riff->GetListTypeString() << ")->"; |
82 |
if (bPrintSize) cout << " (" << riff->GetSize() << " Bytes)"; |
83 |
cout << endl; |
84 |
PrintChunkList(riff, bPrintSize); |
85 |
delete riff; |
86 |
} |
87 |
catch (RIFF::Exception e) { |
88 |
e.PrintMessage(); |
89 |
return EXIT_FAILURE; |
90 |
} |
91 |
catch (...) { |
92 |
cout << "Unknown exception while trying to parse file." << endl; |
93 |
return EXIT_FAILURE; |
94 |
} |
95 |
|
96 |
return EXIT_SUCCESS; |
97 |
} |
98 |
|
99 |
void PrintChunkList(RIFF::List* list, bool PrintSize) { |
100 |
RIFF::Chunk* ck = list->GetFirstSubChunk(); |
101 |
while (ck != NULL) { |
102 |
RIFF::Chunk* ckParent = ck; |
103 |
while (ckParent->GetParent() != NULL) { |
104 |
cout << " "; // e.g. 'LIST(INFO)->' |
105 |
ckParent = ckParent->GetParent(); |
106 |
} |
107 |
cout << ck->GetChunkIDString(); |
108 |
switch (ck->GetChunkID()) { |
109 |
case CHUNK_ID_LIST: case CHUNK_ID_RIFF: |
110 |
{ |
111 |
RIFF::List* l = (RIFF::List*) ck; |
112 |
cout << "(" << l->GetListTypeString() << ")->"; |
113 |
if (PrintSize) cout << " (" << l->GetSize() << " Bytes)"; |
114 |
cout << endl; |
115 |
PrintChunkList(l, PrintSize); |
116 |
break; |
117 |
} |
118 |
default: |
119 |
cout << ";"; |
120 |
if (PrintSize) cout << " (" << ck->GetSize() << " Bytes)"; |
121 |
cout << endl; |
122 |
} |
123 |
ck = list->GetNextSubChunk(); |
124 |
} |
125 |
} |
126 |
|
127 |
string Revision() { |
128 |
string s = "$Revision: 1.4 $"; |
129 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
130 |
} |
131 |
|
132 |
void PrintVersion() { |
133 |
cout << "rifftree revision " << Revision() << endl; |
134 |
cout << "using " << RIFF::libraryName() << " " << RIFF::libraryVersion() << endl; |
135 |
} |
136 |
|
137 |
void PrintUsage() { |
138 |
cout << "rifftree - parses an arbitrary RIFF file and prints out the RIFF tree structure." << endl; |
139 |
cout << endl; |
140 |
cout << "Usage: rifftree [-s|-v] FILE" << endl; |
141 |
cout << endl; |
142 |
cout << " -s Print the size of each chunk." << endl; |
143 |
cout << " -v Print version and exit." << endl; |
144 |
cout << endl; |
145 |
} |