2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2009 by Christian Schoenebeck * |
* Copyright (C) 2003-2014 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This program is part of libgig. * |
* This program is part of libgig. * |
40 |
void PrintVersion(); |
void PrintVersion(); |
41 |
void PrintUsage(); |
void PrintUsage(); |
42 |
void PrintChunkList(RIFF::List* list, bool PrintSize); |
void PrintChunkList(RIFF::List* list, bool PrintSize); |
43 |
|
static uint32_t strToChunkID(string s); |
44 |
|
|
45 |
int main(int argc, char *argv[]) |
int main(int argc, char *argv[]) |
46 |
{ |
{ |
47 |
int FileArgIndex = 1; |
bool bPrintSize = false; |
48 |
bool bPrintSize = false; |
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) { |
if (argc <= 1) { |
55 |
PrintUsage(); |
PrintUsage(); |
56 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
57 |
} |
} |
58 |
if (argv[1][0] == '-') { |
int iArg; |
59 |
switch (argv[1][1]) { |
for (iArg = 1; iArg < argc; ++iArg) { |
60 |
case 's': |
const string opt = argv[iArg]; |
61 |
bPrintSize = true; |
if (opt == "--") { // common for all command line tools: separator between initial option arguments and i.e. subsequent file arguments |
62 |
break; |
iArg++; |
63 |
case 'v': |
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(); |
PrintVersion(); |
82 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
83 |
default: |
} |
84 |
cerr << "Unknown option -" << argv[1][1] << endl; |
bExpectFirstChunkID = true; |
85 |
cerr << endl; |
uiExpectedFirstChunkID = strToChunkID(argv[iArg]); |
86 |
PrintUsage(); |
} else { |
87 |
return EXIT_FAILURE; |
cerr << "Unknown option '" << opt << "'" << endl; |
88 |
|
cerr << endl; |
89 |
|
PrintUsage(); |
90 |
|
return EXIT_FAILURE; |
91 |
} |
} |
|
FileArgIndex++; |
|
92 |
} |
} |
93 |
if (FileArgIndex >= argc) { |
|
94 |
PrintUsage(); |
const int nFileArguments = argc - iArg; |
95 |
|
if (nFileArguments != 1) { |
96 |
|
cerr << "You must provide a file argument." << endl; |
97 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
98 |
} |
} |
99 |
|
const int FileArgIndex = iArg; |
100 |
|
|
101 |
FILE* hFile = fopen(argv[FileArgIndex], "r"); |
FILE* hFile = fopen(argv[FileArgIndex], "r"); |
102 |
if (!hFile) { |
if (!hFile) { |
103 |
cout << "Invalid file argument!" << endl; |
cerr << "Invalid file argument!" << endl; |
104 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
105 |
} |
} |
106 |
fclose(hFile); |
fclose(hFile); |
107 |
try { |
try { |
108 |
RIFF::File* riff = new RIFF::File(argv[FileArgIndex]); |
RIFF::File* riff = NULL; |
109 |
cout << "RIFF(" << riff->GetListTypeString() << ")->"; |
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)"; |
if (bPrintSize) cout << " (" << riff->GetSize() << " Bytes)"; |
126 |
cout << endl; |
cout << endl; |
127 |
PrintChunkList(riff, bPrintSize); |
PrintChunkList(riff, bPrintSize); |
132 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
133 |
} |
} |
134 |
catch (...) { |
catch (...) { |
135 |
cout << "Unknown exception while trying to parse file." << endl; |
cerr << "Unknown exception while trying to parse file." << endl; |
136 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
137 |
} |
} |
138 |
|
|
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() { |
string Revision() { |
184 |
string s = "$Revision$"; |
string s = "$Revision$"; |
185 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
193 |
void PrintUsage() { |
void PrintUsage() { |
194 |
cout << "rifftree - parses an arbitrary RIFF file and prints out the RIFF tree structure." << endl; |
cout << "rifftree - parses an arbitrary RIFF file and prints out the RIFF tree structure." << endl; |
195 |
cout << endl; |
cout << endl; |
196 |
cout << "Usage: rifftree [-s|-v] FILE" << endl; |
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; |
cout << endl; |
213 |
cout << " -s Print the size of each chunk." << endl; |
cout << " --little-endian" << endl; |
214 |
cout << " -v Print version and exit." << endl; |
cout << " File is in little endian format." << endl; |
215 |
cout << endl; |
cout << endl; |
216 |
} |
} |