massive dictionary re-factoring
[asdcplib.git] / src / klvwalk.cpp
1 /*
2 Copyright (c) 2005-2009, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    klvwalk.cpp
28     \version $Id$
29     \brief   KLV+MXF test
30 */
31
32 #include "AS_DCP.h"
33 #include "MXF.h"
34 #include <KM_log.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <assert.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 using namespace ASDCP;
43 using Kumu::DefaultLogSink;
44
45
46 //------------------------------------------------------------------------------------------
47 //
48 // command line option parser class
49
50 static const char* PROGRAM_NAME = "klvwalk";    // program name for messages
51 typedef std::list<std::string> FileList_t;
52
53 // Increment the iterator, test for an additional non-option command line argument.
54 // Causes the caller to return if there are no remaining arguments or if the next
55 // argument begins with '-'.
56 #define TEST_EXTRA_ARG(i,c)    if ( ++i >= argc || argv[(i)][0] == '-' ) \
57                                  { \
58                                    fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
59                                    return; \
60                                  }
61
62 //
63 void
64 banner(FILE* stream = stdout)
65 {
66   fprintf(stream, "\n\
67 %s (asdcplib %s)\n\n\
68 Copyright (c) 2005-2009 John Hurst\n\
69 %s is part of the asdcplib DCP tools package.\n\
70 asdcplib may be copied only under the terms of the license found at\n\
71 the top of every file in the asdcplib distribution kit.\n\n\
72 Specify the -h (help) option for further information about %s\n\n",
73           PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME, PROGRAM_NAME);
74 }
75
76 //
77 void
78 usage(FILE* stream = stdout)
79 {
80   fprintf(stream, "\
81 USAGE: %s [-r] [-v] <input-file> [<input-file2> ...]\n\
82 \n\
83        %s [-h|-help] [-V]\n\
84 \n\
85   -h | -help   - Show help\n\
86   -r           - When KLV data is an MXF OPAtom file, display OPAtom headers\n\
87   -v           - Verbose. Prints informative messages to stderr\n\
88   -V           - Show version information\n\
89 \n\
90   NOTES: o There is no option grouping, all options must be distinct arguments.\n\
91          o All option arguments must be separated from the option by whitespace.\n\
92 \n", PROGRAM_NAME, PROGRAM_NAME);
93 }
94
95 //
96 //
97  class CommandOptions
98  {
99    CommandOptions();
100
101  public:
102    bool   error_flag;               // true if the given options are in error or not complete
103    bool   version_flag;             // true if the version display option was selected
104    bool   help_flag;                // true if the help display option was selected
105    bool   verbose_flag;             // true if the informative messages option was selected
106    bool   read_mxf_flag;            // true if the -r option was selected
107    FileList_t inFileList;           // File to operate on
108
109    CommandOptions(int argc, const char** argv) :
110      error_flag(true), version_flag(false), help_flag(false), verbose_flag(false), read_mxf_flag(false)
111    {
112      for ( int i = 1; i < argc; i++ )
113        {
114
115          if ( (strcmp( argv[i], "-help") == 0) )
116            {
117              help_flag = true;
118              continue;
119            }
120          
121          if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
122            {
123              switch ( argv[i][1] )
124                {
125                case 'h': help_flag = true; break;
126                case 'r': read_mxf_flag = true; break;
127                case 'V': version_flag = true; break;
128                case 'v': verbose_flag = true; break;
129
130                default:
131                  fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
132                  return;
133                }
134            }
135          else
136            {
137              if ( argv[i][0] != '-' )
138                inFileList.push_back(argv[i]);
139
140              else
141                {
142                  fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
143                  return;
144                }
145            }
146        }
147
148      if ( help_flag || version_flag )
149        return;
150      
151      if ( inFileList.empty() )
152        {
153          fputs("Input filename(s) required.\n", stderr);
154          return;
155        }
156      
157      error_flag = false;
158    }
159  };
160
161
162 //---------------------------------------------------------------------------------------------------
163 //
164
165 int
166 main(int argc, const char** argv)
167 {
168   CommandOptions Options(argc, argv);
169
170   if ( Options.version_flag )
171     banner();
172
173   if ( Options.help_flag )
174     usage();
175
176   if ( Options.version_flag || Options.help_flag )
177     return 0;
178
179   if ( Options.error_flag )
180     {
181       fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME);
182       return 3;
183     }
184
185   FileList_t::iterator fi;
186   Result_t result = RESULT_OK;
187
188   for ( fi = Options.inFileList.begin(); ASDCP_SUCCESS(result) && fi != Options.inFileList.end(); fi++ )
189     {
190       if (Options.verbose_flag)
191         fprintf(stderr, "Opening file %s\n", ((*fi).c_str()));
192       
193       if ( Options.read_mxf_flag ) // dump MXF
194         {
195           Kumu::FileReader        Reader;
196           ASDCP::MXF::OPAtomHeader Header(DefaultCompositeDict());
197           
198           result = Reader.OpenRead((*fi).c_str());
199           
200           if ( ASDCP_SUCCESS(result) )
201             result = Header.InitFromFile(Reader);
202           
203           if ( ASDCP_SUCCESS(result) )
204             Header.Dump(stdout);
205           
206           if ( ASDCP_SUCCESS(result) && Header.m_RIP.PairArray.size() > 2 )
207             {
208               MXF::Array<MXF::RIP::Pair>::const_iterator pi = Header.m_RIP.PairArray.begin();
209
210               for ( pi++; pi != Header.m_RIP.PairArray.end() && ASDCP_SUCCESS(result); pi++ )
211                 {
212                   result = Reader.Seek((*pi).ByteOffset);
213
214                   if ( ASDCP_SUCCESS(result) )
215                     {
216                       MXF::Partition TmpPart(DefaultCompositeDict());
217                       result = TmpPart.InitFromFile(Reader);
218
219                       if ( ASDCP_SUCCESS(result) && TmpPart.BodySID > 0 )
220                         TmpPart.Dump(stdout);
221                     }
222                 }
223             }
224
225           if ( ASDCP_SUCCESS(result) )
226             {
227               ASDCP::MXF::OPAtomIndexFooter Index(DefaultCompositeDict());
228               result = Reader.Seek(Header.FooterPartition);
229               
230               if ( ASDCP_SUCCESS(result) )
231                 {
232                   Index.m_Lookup = &Header.m_Primer;
233                   result = Index.InitFromFile(Reader);
234                 }
235               
236               if ( ASDCP_SUCCESS(result) )
237                 Index.Dump(stdout);
238             }
239
240           if ( ASDCP_SUCCESS(result) )
241             Header.m_RIP.Dump(stdout);
242         }
243       else // dump klv
244         {
245           Kumu::FileReader Reader;
246           KLVFilePacket KP;
247           
248           result = Reader.OpenRead((*fi).c_str());
249           
250           if ( ASDCP_SUCCESS(result) )
251             result = KP.InitFromFile(Reader);
252           
253           while ( ASDCP_SUCCESS(result) )
254             {
255               KP.Dump(stdout, DefaultCompositeDict(), true);
256               result = KP.InitFromFile(Reader);
257             }
258           
259           if( result == RESULT_ENDOFFILE )
260             result = RESULT_OK;
261         }
262     }
263
264   if ( ASDCP_FAILURE(result) )
265     {
266       fputs("Program stopped on error.\n", stderr);
267       
268       if ( result != RESULT_FAIL )
269         {
270           fputs(result, stderr);
271           fputc('\n', stderr);
272         }
273       
274       return 1;
275     }
276   
277   return 0;
278 }
279
280
281 //
282 // end klvwalk.cpp
283 //