rifftree.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   libgig - C++ cross-platform Gigasampler format file loader library    *
00004  *                                                                         *
00005  *   Copyright (C) 2003-2005 by Christian Schoenebeck                      *
00006  *                              <cuse@users.sourceforge.net>               *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the Free Software           *
00020  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00021  *   MA  02111-1307  USA                                                   *
00022  ***************************************************************************/
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <stdio.h>
00029 #include <iostream>
00030 #include <string>
00031 #include <cstdlib>
00032 
00033 #include "RIFF.h"
00034 
00035 using namespace std;
00036 
00037 string Revision();
00038 void PrintVersion();
00039 void PrintUsage();
00040 void PrintChunkList(RIFF::List* list, bool PrintSize);
00041 
00042 int main(int argc, char *argv[])
00043 {
00044     int  FileArgIndex = 1;
00045     bool bPrintSize   = false;
00046 
00047     if (argc <= 1) {
00048         PrintUsage();
00049         return EXIT_FAILURE;
00050     }
00051     if (argv[1][0] == '-') {
00052         switch (argv[1][1]) {
00053             case 's':
00054                 bPrintSize = true;
00055                 break;
00056             case 'v':
00057                 PrintVersion();
00058                 return EXIT_SUCCESS;
00059             default:
00060                 cerr << "Unknown option -" << argv[1][1] << endl;
00061                 cerr << endl;
00062                 PrintUsage();
00063                 return EXIT_FAILURE;
00064         }
00065         FileArgIndex++;
00066     }
00067     if (FileArgIndex >= argc) {
00068         PrintUsage();
00069         return EXIT_FAILURE;
00070     }
00071     FILE* hFile = fopen(argv[FileArgIndex], "r");
00072     if (!hFile) {
00073         cout << "Invalid file argument!" << endl;
00074         return EXIT_FAILURE;
00075     }
00076     fclose(hFile);
00077     try {
00078         RIFF::File* riff = new RIFF::File(argv[FileArgIndex]);
00079         cout << "RIFF(" << riff->GetListTypeString() << ")->";
00080         if (bPrintSize) cout << " (" << riff->GetSize() << " Bytes)";
00081         cout << endl;
00082         PrintChunkList(riff, bPrintSize);
00083         delete riff;
00084     }
00085     catch (RIFF::Exception e) {
00086         e.PrintMessage();
00087         return EXIT_FAILURE;
00088     }
00089     catch (...) {
00090         cout << "Unknown exception while trying to parse file." << endl;
00091         return EXIT_FAILURE;
00092     }
00093 
00094     return EXIT_SUCCESS;
00095 }
00096 
00097 void PrintChunkList(RIFF::List* list, bool PrintSize) {
00098     RIFF::Chunk* ck = list->GetFirstSubChunk();
00099     while (ck != NULL) {
00100         RIFF::Chunk* ckParent = ck;
00101         while (ckParent->GetParent() != NULL) {
00102             cout << "            "; // e.g. 'LIST(INFO)->'
00103             ckParent = ckParent->GetParent();
00104         }
00105         cout << ck->GetChunkIDString();
00106         switch (ck->GetChunkID()) {
00107             case CHUNK_ID_LIST: case CHUNK_ID_RIFF:
00108               {
00109                 RIFF::List* l = (RIFF::List*) ck;
00110                 cout << "(" << l->GetListTypeString() << ")->";
00111                 if (PrintSize) cout << " (" << l->GetSize() << " Bytes)";
00112                 cout << endl;
00113                 PrintChunkList(l, PrintSize);
00114                 break;
00115               }
00116             default:
00117                 cout << ";";
00118                 if (PrintSize) cout << " (" << ck->GetSize() << " Bytes)";
00119                 cout << endl;
00120         }
00121         ck = list->GetNextSubChunk();
00122     }
00123 }
00124 
00125 string Revision() {
00126     string s = "$Revision: 1.3 $";
00127     return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
00128 }
00129 
00130 void PrintVersion() {
00131     cout << "rifftree revision " << Revision() << endl;
00132     cout << "using " << RIFF::libraryName() << " " << RIFF::libraryVersion() << endl;
00133 }
00134 
00135 void PrintUsage() {
00136     cout << "rifftree - parses an arbitrary RIFF file and prints out the RIFF tree structure." << endl;
00137     cout << endl;
00138     cout << "Usage: rifftree [-s|-v] FILE" << endl;
00139     cout << endl;
00140     cout << "   -s  Print the size of each chunk." << endl;
00141     cout << "   -v  Print version and exit." << endl;
00142     cout << endl;
00143 }

Generated on Wed Nov 23 16:02:31 2005 for libgig by  doxygen 1.4.5