wheee!
[asdcplib.git] / src / klvwalk.cpp
1 //
2 // klvwalk.cpp
3 //
4
5 #include <AS_DCP.h>
6 #include <MXF.h>
7 #include <hex_utils.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <assert.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14
15 using namespace ASDCP;
16
17
18 //------------------------------------------------------------------------------------------
19 //
20
21 // There is no header file thet defines this function.
22 // You just have to know it's there...
23 void set_debug_mode(bool info_mode, bool debug_mode);
24
25
26 int
27 main(int argc, char** argv)
28 {
29   Result_t result = RESULT_OK;
30   bool read_mxf = false;
31   bool rewrite_mxf = false;
32   int arg_i = 1;
33   set_debug_mode(true, true);
34
35   if ( strcmp(argv[1], "-r") == 0 )
36     {
37       read_mxf = true;
38       arg_i++;
39     }
40   else if ( strcmp(argv[1], "-w") == 0 )
41     {
42       rewrite_mxf = true;
43       arg_i++;
44       assert(argc - arg_i == 2);
45     }
46
47   fprintf(stderr, "Opening file %s\n", argv[arg_i]);
48
49   if ( read_mxf )
50     {
51       ASDCP::FileReader        Reader;
52       ASDCP::MXF::OPAtomHeader Header;
53
54       result = Reader.OpenRead(argv[arg_i]);
55
56       if ( ASDCP_SUCCESS(result) )
57         result = Header.InitFromFile(Reader);
58
59       //      if ( ASDCP_SUCCESS(result) )
60       Header.Dump();
61
62       if ( ASDCP_SUCCESS(result) )
63         {
64           ASDCP::MXF::OPAtomIndexFooter Index;
65           result = Reader.Seek(Header.FooterPartition);
66
67           if ( ASDCP_SUCCESS(result) )
68             {
69               Index.m_Lookup = &Header.m_Primer;
70               result = Index.InitFromFile(Reader);
71             }
72
73           if ( ASDCP_SUCCESS(result) )
74             Index.Dump();
75         }
76     }
77   else if ( rewrite_mxf )
78     {
79       ASDCP::FileReader        Reader;
80       ASDCP::FileWriter        Writer;
81       ASDCP::MXF::OPAtomHeader Header;
82       ASDCP::MXF::OPAtomIndexFooter Index;
83
84       result = Reader.OpenRead(argv[arg_i++]);
85
86       if ( ASDCP_SUCCESS(result) )
87         result = Header.InitFromFile(Reader);
88
89       if ( ASDCP_SUCCESS(result) )
90         result = Reader.Seek(Header.FooterPartition);
91
92       if ( ASDCP_SUCCESS(result) )
93         result = Index.InitFromFile(Reader);
94
95       Header.m_Primer.ClearTagList();
96
97       if ( ASDCP_SUCCESS(result) )
98         result = Writer.OpenWrite(argv[arg_i]);
99
100       if ( ASDCP_SUCCESS(result) )
101         result = Header.WriteToFile(Writer);
102
103 //      if ( ASDCP_SUCCESS(result) )
104 //      result = Index.WriteToFile(Writer);
105
106       // essence packets
107
108       // index
109
110       // RIP
111     }
112   else // dump klv
113     {
114       ASDCP::FileReader Reader;
115       KLVFilePacket KP;
116
117       result = Reader.OpenRead(argv[arg_i]);
118
119       if ( ASDCP_SUCCESS(result) )
120         result = KP.InitFromFile(Reader);
121
122       while ( ASDCP_SUCCESS(result) )
123         {
124           KP.Dump(stderr, true);
125           result = KP.InitFromFile(Reader);
126         }
127
128       if( result == RESULT_ENDOFFILE )
129         result = RESULT_OK;
130     }
131
132   if ( result != RESULT_OK )
133     {
134       fputs("Program stopped on error.\n", stderr);
135
136       if ( result != RESULT_FAIL )
137         {
138           fputs(GetResultString(result), stderr);
139           fputc('\n', stderr);
140         }
141
142       return 1;
143     }
144
145   return 0;
146 }
147
148
149 //
150 // end klvwalk.cpp
151 //
152
153
154
155
156
157
158
159
160
161