X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fklvwalk.cpp;h=9a815b76ff92423ec73c0858fc4845f7a43ba2cc;hb=3bb652da6ea2765b1538d0713cefa284238c8bdb;hp=b5b3cea21a876f8cd1a9f00ea14f3f4c122d6f62;hpb=fdf31e0105bf8272a6b7fa9c4039941dff37a271;p=asdcplib.git diff --git a/src/klvwalk.cpp b/src/klvwalk.cpp index b5b3cea..9a815b7 100755 --- a/src/klvwalk.cpp +++ b/src/klvwalk.cpp @@ -1,5 +1,5 @@ /* -Copyright (c) 2005-2006, John Hurst +Copyright (c) 2005-2008, John Hurst All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,7 +31,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "AS_DCP.h" #include "MXF.h" -#include "hex_utils.h" +#include #include #include #include @@ -40,92 +40,240 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include using namespace ASDCP; +using Kumu::DefaultLogSink; //------------------------------------------------------------------------------------------ // +// command line option parser class -// There is no header file thet defines this function. -// You just have to know it's there... -void set_debug_mode(bool info_mode, bool debug_mode); +static const char* PROGRAM_NAME = "klvwalk"; // program name for messages +typedef std::list FileList_t; +// Increment the iterator, test for an additional non-option command line argument. +// Causes the caller to return if there are no remaining arguments or if the next +// argument begins with '-'. +#define TEST_EXTRA_ARG(i,c) if ( ++i >= argc || argv[(i)][0] == '-' ) \ + { \ + fprintf(stderr, "Argument not found for option -%c.\n", (c)); \ + return; \ + } + +// +void +banner(FILE* stream = stdout) +{ + fprintf(stream, "\n\ +%s (asdcplib %s)\n\n\ +Copyright (c) 2005-2008 John Hurst\n\ +%s is part of the asdcplib DCP tools package.\n\ +asdcplib may be copied only under the terms of the license found at\n\ +the top of every file in the asdcplib distribution kit.\n\n\ +Specify the -h (help) option for further information about %s\n\n", + PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME, PROGRAM_NAME); +} + +// +void +usage(FILE* stream = stdout) +{ + fprintf(stream, "\ +USAGE: %s [-r] [-v] [ ...]\n\ +\n\ + %s [-h|-help] [-V]\n\ +\n\ + -h | -help - Show help\n\ + -r - When KLV data is an MXF OPAtom file, display OPAtom headers\n\ + -v - Verbose. Prints informative messages to stderr\n\ + -V - Show version information\n\ +\n\ + NOTES: o There is no option grouping, all options must be distinct arguments.\n\ + o All option arguments must be separated from the option by whitespace.\n\ +\n", PROGRAM_NAME, PROGRAM_NAME); +} + +// +// + class CommandOptions + { + CommandOptions(); + + public: + bool error_flag; // true if the given options are in error or not complete + bool version_flag; // true if the version display option was selected + bool help_flag; // true if the help display option was selected + bool verbose_flag; // true if the informative messages option was selected + bool read_mxf_flag; // true if the -r option was selected + FileList_t inFileList; // File to operate on + + CommandOptions(int argc, const char** argv) : + error_flag(true), version_flag(false), help_flag(false), verbose_flag(false), read_mxf_flag(false) + { + for ( int i = 1; i < argc; i++ ) + { + + if ( (strcmp( argv[i], "-help") == 0) ) + { + help_flag = true; + continue; + } + + if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 ) + { + switch ( argv[i][1] ) + { + case 'h': help_flag = true; break; + case 'r': read_mxf_flag = true; break; + case 'V': version_flag = true; break; + case 'v': verbose_flag = true; break; + + default: + fprintf(stderr, "Unrecognized option: %s\n", argv[i]); + return; + } + } + else + { + if ( argv[i][0] != '-' ) + inFileList.push_back(argv[i]); + + else + { + fprintf(stderr, "Unrecognized option: %s\n", argv[i]); + return; + } + } + } + + if ( help_flag || version_flag ) + return; + + if ( inFileList.empty() ) + { + fputs("Input filename(s) required.\n", stderr); + return; + } + + error_flag = false; + } + }; + + +//--------------------------------------------------------------------------------------------------- +// int -main(int argc, char** argv) +main(int argc, const char** argv) { - Result_t result = RESULT_OK; - bool read_mxf = false; - int arg_i = 1; - set_debug_mode(true, true); + CommandOptions Options(argc, argv); + + if ( Options.version_flag ) + banner(); - if ( strcmp(argv[1], "-r") == 0 ) + if ( Options.help_flag ) + usage(); + + if ( Options.version_flag || Options.help_flag ) + return 0; + + if ( Options.error_flag ) { - read_mxf = true; - arg_i++; + fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME); + return 3; } - fprintf(stderr, "Opening file %s\n", argv[arg_i]); + FileList_t::iterator fi; + Result_t result = RESULT_OK; - if ( read_mxf ) + for ( fi = Options.inFileList.begin(); ASDCP_SUCCESS(result) && fi != Options.inFileList.end(); fi++ ) { - ASDCP::FileReader Reader; - ASDCP::MXF::OPAtomHeader Header; - - result = Reader.OpenRead(argv[arg_i]); + if (Options.verbose_flag) + fprintf(stderr, "Opening file %s\n", ((*fi).c_str())); + + if ( Options.read_mxf_flag ) // dump MXF + { + Kumu::FileReader Reader; + ASDCP::MXF::OPAtomHeader Header; + + result = Reader.OpenRead((*fi).c_str()); + + if ( ASDCP_SUCCESS(result) ) + result = Header.InitFromFile(Reader); + + if ( ASDCP_SUCCESS(result) ) + Header.Dump(stdout); + + if ( ASDCP_SUCCESS(result) && Header.m_RIP.PairArray.size() > 2 ) + { + MXF::Array::const_iterator pi = Header.m_RIP.PairArray.begin(); - if ( ASDCP_SUCCESS(result) ) - result = Header.InitFromFile(Reader); + for ( pi++; pi != Header.m_RIP.PairArray.end() && ASDCP_SUCCESS(result); pi++ ) + { + result = Reader.Seek((*pi).ByteOffset); - Header.Dump(stdout); + if ( ASDCP_SUCCESS(result) ) + { + MXF::Partition TmpPart; + result = TmpPart.InitFromFile(Reader); - if ( ASDCP_SUCCESS(result) ) - { - ASDCP::MXF::OPAtomIndexFooter Index; - result = Reader.Seek(Header.FooterPartition); + if ( ASDCP_SUCCESS(result) && TmpPart.BodySID > 0 ) + TmpPart.Dump(stdout); + } + } + } if ( ASDCP_SUCCESS(result) ) { - Index.m_Lookup = &Header.m_Primer; - result = Index.InitFromFile(Reader); + ASDCP::MXF::OPAtomIndexFooter Index; + result = Reader.Seek(Header.FooterPartition); + + if ( ASDCP_SUCCESS(result) ) + { + Index.m_Lookup = &Header.m_Primer; + result = Index.InitFromFile(Reader); + } + + if ( ASDCP_SUCCESS(result) ) + Index.Dump(stdout); } if ( ASDCP_SUCCESS(result) ) - Index.Dump(stdout); + Header.m_RIP.Dump(stdout); } - } - else // dump klv - { - ASDCP::FileReader Reader; - KLVFilePacket KP; - - result = Reader.OpenRead(argv[arg_i]); - - if ( ASDCP_SUCCESS(result) ) - result = KP.InitFromFile(Reader); - - while ( ASDCP_SUCCESS(result) ) + else // dump klv { - KP.Dump(stdout, true); - result = KP.InitFromFile(Reader); + Kumu::FileReader Reader; + KLVFilePacket KP; + + result = Reader.OpenRead((*fi).c_str()); + + if ( ASDCP_SUCCESS(result) ) + result = KP.InitFromFile(Reader); + + while ( ASDCP_SUCCESS(result) ) + { + KP.Dump(stdout, true); + result = KP.InitFromFile(Reader); + } + + if( result == RESULT_ENDOFFILE ) + result = RESULT_OK; } - - if( result == RESULT_ENDOFFILE ) - result = RESULT_OK; } if ( ASDCP_FAILURE(result) ) { fputs("Program stopped on error.\n", stderr); - + if ( result != RESULT_FAIL ) { - fputs(GetResultString(result), stderr); + fputs(result, stderr); fputc('\n', stderr); } - + return 1; } - + return 0; }