/[svn]/libgig/trunk/src/tools/gigdump.cpp
ViewVC logotype

Diff of /libgig/trunk/src/tools/gigdump.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2984 by schoenebeck, Tue Sep 20 15:13:58 2016 UTC revision 2989 by schoenebeck, Sat Sep 24 14:00:46 2016 UTC
# Line 2  Line 2 
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-2014 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2016 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.                                       *
# Line 45  void PrintInstruments(gig::File* gig); Line 45  void PrintInstruments(gig::File* gig);
45  void PrintRegions(gig::Instrument* instr);  void PrintRegions(gig::Instrument* instr);
46  void PrintUsage();  void PrintUsage();
47  void PrintDimensionRegions(gig::Region* rgn);  void PrintDimensionRegions(gig::Region* rgn);
48    bool VerifyFile(gig::File* gig);
49    void RebuildChecksumTable(gig::File* gig);
50    
51  class PubSample : public gig::Sample {  class PubSample : public gig::Sample {
52  public:  public:
53      using DLS::Sample::pCkData;      using DLS::Sample::pCkData;
54  };  };
55    
56    class PubFile : public gig::File {
57    public:
58        using gig::File::VerifySampleChecksumTable;
59        using gig::File::RebuildSampleChecksumTable;
60    };
61    
62    
63  int main(int argc, char *argv[])  int main(int argc, char *argv[])
64  {  {
65        bool bVerify = false;
66        bool bRebuildChecksums = false;
67    
68      if (argc <= 1) {      if (argc <= 1) {
69          PrintUsage();          PrintUsage();
70          return EXIT_FAILURE;          return EXIT_FAILURE;
71      }      }
72      if (argv[1][0] == '-') {  
73          switch (argv[1][1]) {      int iArg;
74              case 'v':      for (iArg = 1; iArg < argc; ++iArg) {
75                  PrintVersion();          const string opt = argv[iArg];
76                  return EXIT_SUCCESS;          if (opt == "--") { // common for all command line tools: separator between initial option arguments and i.e. subsequent file arguments
77                iArg++;
78                break;
79            }
80            if (opt.substr(0, 1) != "-") break;
81    
82            if (opt == "-v") {
83                PrintVersion();
84                return EXIT_SUCCESS;
85            } else if (opt == "--verify") {
86                bVerify = true;
87            } else if (opt == "--rebuild-checksums") {
88                bRebuildChecksums = true;
89            } else {
90                cerr << "Unknown option '" << opt << "'" << endl;
91                cerr << endl;
92                PrintUsage();
93                return EXIT_FAILURE;
94          }          }
95      }      }
96      FILE* hFile = fopen(argv[1], "r");      if (iArg >= argc) {
97            cout << "No file name provided!" << endl;
98            return EXIT_FAILURE;
99        }
100        const char* filename = argv[iArg];
101    
102        FILE* hFile = fopen(filename, "r");
103      if (!hFile) {      if (!hFile) {
104          cout << "Invalid file argument!" << endl;          cout << "Invalid file argument!" << endl;
105          return EXIT_FAILURE;          return EXIT_FAILURE;
106      }      }
107      fclose(hFile);      fclose(hFile);
108      try {      try {
109          RIFF::File* riff = new RIFF::File(argv[1]);          RIFF::File* riff = new RIFF::File(filename);
110          gig::File*  gig  = new gig::File(riff);          gig::File*  gig  = new gig::File(riff);
111          PrintFileInformations(gig);  
112          cout << endl;          if (bRebuildChecksums) {
113          PrintGroups(gig);              RebuildChecksumTable(gig);
114          cout << endl;          } else if (bVerify) {
115          PrintSamples(gig);              bool OK = VerifyFile(gig);
116          cout << endl;              if (OK) cout << "All checks passed successfully! :-)\n";
117          PrintScripts(gig);              return (OK) ? EXIT_SUCCESS : EXIT_FAILURE;
118          cout << endl;          } else {
119          PrintInstruments(gig);              PrintFileInformations(gig);
120                cout << endl;
121                PrintGroups(gig);
122                cout << endl;
123                PrintSamples(gig);
124                cout << endl;
125                PrintScripts(gig);
126                cout << endl;
127                PrintInstruments(gig);
128            }
129          delete gig;          delete gig;
130          delete riff;          delete riff;
131      }      }
# Line 184  void PrintSamples(gig::File* gig) { Line 228  void PrintSamples(gig::File* gig) {
228              cout << ", LoopFraction=" << pSample->LoopFraction << ", Start=" << pSample->LoopStart << ", End=" << pSample->LoopEnd;              cout << ", LoopFraction=" << pSample->LoopFraction << ", Start=" << pSample->LoopStart << ", End=" << pSample->LoopEnd;
229              cout << ", LoopPlayCount=" << pSample->LoopPlayCount;              cout << ", LoopPlayCount=" << pSample->LoopPlayCount;
230          }          }
231            cout << flush;
232            printf(", crc=%x", pSample->GetWaveDataCRC32Checksum());
233            fflush(stdout);
234          cout << ", Length=" << pSample->SamplesTotal << " Compressed=" << ((pSample->Compressed) ? "true" : "false")          cout << ", Length=" << pSample->SamplesTotal << " Compressed=" << ((pSample->Compressed) ? "true" : "false")
235               << " foffset=" << pSample->pCkData->GetFilePos()               << " foffset=" << pSample->pCkData->GetFilePos()
236               << " fsz=" << pSample->pCkData->GetSize()               << " fsz=" << pSample->pCkData->GetSize()
# Line 467  void PrintDimensionRegions(gig::Region* Line 514  void PrintDimensionRegions(gig::Region*
514      }      }
515  }  }
516    
517    struct _FailedSample {
518        gig::Sample* sample;
519        uint32_t calculatedCRC;
520    };
521    
522    bool VerifyFile(gig::File* _gig) {
523        PubFile* gig = (PubFile*) _gig;
524    
525        cout << "Verifying sample checksum table ... " << flush;
526        if (!gig->VerifySampleChecksumTable()) {
527            cout << "DAMAGED\n";
528            cout << "You may use --rebuild-checksums to repair the sample checksum table.\n";
529            return false;
530        }
531        cout << "OK\n" << flush;
532    
533        cout << "Verifying samples ... " << flush;
534        std::map<int,_FailedSample> failedSamples;
535        int iTotal = 0;
536        for (gig::Sample* pSample = gig->GetFirstSample(); pSample; pSample = gig->GetNextSample(), ++iTotal) {
537            uint32_t crc; // will be set to the actually now calculated checksum
538            if (!pSample->VerifyWaveData(&crc)) {
539                _FailedSample failed;
540                failed.sample = pSample;
541                failed.calculatedCRC = crc;
542                failedSamples[iTotal] = failed;
543            }
544        }
545        if (failedSamples.empty()) {
546            cout << "ALL OK\n";
547            return true;
548        } else {
549            cout << failedSamples.size() << " of " << iTotal << " Samples DAMAGED:\n";
550            for (std::map<int,_FailedSample>::iterator it = failedSamples.begin(); it != failedSamples.end(); ++it) {
551                const int i = it->first;
552                gig::Sample* pSample = it->second.sample;
553    
554                string name = pSample->pInfo->Name;
555                if (name == "") name = "<NO NAME>";
556                else            name = '\"' + name + '\"';
557    
558                cout << "Damaged Sample " << (i+1) << ") " << name << flush;
559                printf(" expectedCRC=%x calculatedCRC=%x\n", pSample->GetWaveDataCRC32Checksum(), it->second.calculatedCRC);
560            }
561            return false;
562        }
563    }
564    
565    void RebuildChecksumTable(gig::File* _gig) {
566        PubFile* gig = (PubFile*) _gig;
567    
568        cout << "Recalculating checksums of all samples ... " << flush;
569        bool bSaveRequired = gig->RebuildSampleChecksumTable();
570        cout << "OK\n";
571        if (bSaveRequired) {
572            cout << "WARNING: File structure change required, rebuilding entire file now ..." << endl;
573            gig->Save();
574            cout << "DONE\n";
575            cout << "NOTE: Since the entire file was rebuilt, you may need to manually check all samples in this particular case now!\n";
576        }
577    }
578    
579  string Revision() {  string Revision() {
580      string s = "$Revision$";      string s = "$Revision$";
581      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
# Line 480  void PrintVersion() { Line 589  void PrintVersion() {
589  void PrintUsage() {  void PrintUsage() {
590      cout << "gigdump - parses Gigasampler files and prints out the content." << endl;      cout << "gigdump - parses Gigasampler files and prints out the content." << endl;
591      cout << endl;      cout << endl;
592      cout << "Usage: gigdump [-v] FILE" << endl;      cout << "Usage: gigdump [-v | --verify | --rebuild-checksums] FILE" << endl;
593        cout << endl;
594        cout << "   --rebuild-checksums  Rebuild checksum table for all samples." << endl;
595        cout << endl;
596        cout << "   -v                   Print version and exit." << endl;
597      cout << endl;      cout << endl;
598      cout << "   -v  Print version and exit." << endl;      cout << "   --verify             Checks raw wave data integrity of all samples." << endl;
599      cout << endl;      cout << endl;
600  }  }

Legend:
Removed from v.2984  
changed lines
  Added in v.2989

  ViewVC Help
Powered by ViewVC