rifftree.cpp

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

Generated on Wed Dec 6 19:25:57 2006 for libgig by  doxygen 1.5.1