summaryrefslogtreecommitdiff
path: root/src/klvwalk.cpp
diff options
context:
space:
mode:
authortmccolm <tmccolm@cinecert.com>2006-06-23 19:48:03 +0000
committertmccolm <>2006-06-23 19:48:03 +0000
commitc589ee9d47d9f00aa4be32c5832a44ce466f014d (patch)
treed285585caea07384dc75c5cb2a697df821437f72 /src/klvwalk.cpp
parent5e91ca52284adc91a42d6fe389c9cc70a33126a6 (diff)
2006/06/23 tmccolm
Diffstat (limited to 'src/klvwalk.cpp')
-rwxr-xr-xsrc/klvwalk.cpp233
1 files changed, 178 insertions, 55 deletions
diff --git a/src/klvwalk.cpp b/src/klvwalk.cpp
index 395aa54..467eff9 100755
--- a/src/klvwalk.cpp
+++ b/src/klvwalk.cpp
@@ -42,94 +42,217 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using namespace ASDCP;
using Kumu::DefaultLogSink;
-const char* PACKAGE = "klvwalk";
-
//------------------------------------------------------------------------------------------
//
+// command line option parser class
+static const char* PACKAGE = "klvwalk"; // program name for messages
+typedef std::list<std::string> FileList_t;
-int
-main(int argc, char** argv)
+// 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)
{
- Result_t result = RESULT_OK;
- bool read_mxf = false;
- int arg_i = 1;
+ fprintf(stream, "\n\
+%s (asdcplib %s)\n\n\
+Copyright (c) 2005-2006 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",
+ PACKAGE, ASDCP::Version(), PACKAGE, PACKAGE);
+}
- if ( argc > arg_i && strcmp(argv[1], "-r") == 0 )
- {
- read_mxf = true;
- arg_i++;
- }
+//
+void
+usage(FILE* stream = stdout)
+{
+ fprintf(stream, "\
+USAGE: %s [-r] [-v] <input-file> [<input-file2> ...]\n\
+\n\
+ %s [-h|-help] [-V]\n\
+\n\
+ -h | -help - Show help\n\
+ -r - When KLV data is an OPAtom file, additionally\n\
+ 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", PACKAGE, PACKAGE);
+}
- if ( argc - arg_i != 1 )
- {
- fprintf(stderr, "usage: %s [-r] <infile>\n", PACKAGE);
- return 1;
- }
+//
+//
+ class CommandOptions
+ {
+ CommandOptions();
- fprintf(stderr, "Opening file %s\n", argv[arg_i]);
+ 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
- if ( read_mxf )
- {
- Kumu::FileReader Reader;
- ASDCP::MXF::OPAtomHeader Header;
+ 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++ )
+ {
- result = Reader.OpenRead(argv[arg_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] )
+ {
- if ( ASDCP_SUCCESS(result) )
- result = Header.InitFromFile(Reader);
+ 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;
- Header.Dump(stdout);
+ default:
+ fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
+ return;
+ }
+ }
+ else
+ {
+ if ( argv[i][0] != '-' )
+ inFileList.push_back(argv[i]);
- if ( ASDCP_SUCCESS(result) )
- {
- ASDCP::MXF::OPAtomIndexFooter Index;
- result = Reader.Seek(Header.FooterPartition);
+ else
+ {
+ fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
+ return;
+ }
+ }
+ }
- if ( ASDCP_SUCCESS(result) )
- {
- Index.m_Lookup = &Header.m_Primer;
- result = Index.InitFromFile(Reader);
- }
+ if ( help_flag || version_flag )
+ return;
+
+ if ( inFileList.empty() )
+ {
+ fputs("Input filename(s) required.\n", stderr);
+ return;
+ }
+
+ error_flag = false;
+ }
+ };
- if ( ASDCP_SUCCESS(result) )
- Index.Dump(stdout);
- }
- }
- else // dump klv
- {
- Kumu::FileReader Reader;
- KLVFilePacket KP;
- result = Reader.OpenRead(argv[arg_i]);
+//---------------------------------------------------------------------------------------------------
+//
+
+int
+main(int argc, const char** argv)
+{
+ CommandOptions Options(argc, argv);
- if ( ASDCP_SUCCESS(result) )
- result = KP.InitFromFile(Reader);
+ if ( Options.version_flag )
+ banner();
- while ( ASDCP_SUCCESS(result) )
+ if ( Options.help_flag )
+ usage();
+
+ if ( Options.version_flag || Options.help_flag )
+ return 0;
+
+ if ( Options.error_flag )
+ {
+ fprintf(stderr, "There was a problem. Type %s -h for help.\n", PACKAGE);
+ return 3;
+ }
+
+ FileList_t::iterator fi;
+ Result_t result = RESULT_OK;
+
+ for ( fi = Options.inFileList.begin(); ASDCP_SUCCESS(result) && fi != Options.inFileList.end(); fi++ )
+ {
+ if (Options.verbose_flag)
+ fprintf(stderr, "Opening file %s\n", ((*fi).c_str()));
+
+ if ( Options.read_mxf_flag )
{
- KP.Dump(stdout, true);
- result = KP.InitFromFile(Reader);
+ Kumu::FileReader Reader;
+ ASDCP::MXF::OPAtomHeader Header;
+
+ result = Reader.OpenRead((*fi).c_str());
+
+ if ( ASDCP_SUCCESS(result) )
+ result = Header.InitFromFile(Reader);
+
+ Header.Dump(stdout);
+
+ if ( ASDCP_SUCCESS(result) )
+ {
+ 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);
+ }
+ }
+ else // dump klv
+ {
+ 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(result, stderr);
fputc('\n', stderr);
}
-
+
return 1;
}
-
+
return 0;
}