3 Mnemonic: rdc_extract.c
4 Abstract: Read a raw data capture file from the mc-listener
5 and tease out one message type.
7 0000000 40 52 44 43 << delim
8 30 30 31 30 30 35 30 2a << mtype
11 40 4d 43 4c 30 30 30 30 30 34 36 00 << raw message
15 This is a very quick and dirty thing, so it might be flakey.
17 Parms from command line are file to read, and the msg type to extract.
18 If mtype given is 0, then message type of each record is written to
19 stdout (can be sorted -u for a list of messages in the file).
21 For capture mode, a file MT_<mtype> is created for the extracted
25 Author: E. Scott Daniels
35 int main( int argc, char** argv ) {
38 int wfd = 1; // write file des; default to stdout
49 fprintf( stderr, "bad args.\nUsage: %s file mtype [output-file]\n", argv[0] );
53 fd = open( argv[1], O_RDONLY );
55 fprintf( stderr, "bad open: %s\n", strerror(errno) );
59 desired = atoi( argv[2] );
62 wfd = open( argv[4], O_WRONLY | O_CREAT | O_TRUNC, 0644 );
65 remain = read( fd, rbuf, sizeof( rbuf ) );
68 if( remain < 20 ) { // not enough stuff
69 memcpy( rbuf, nxt, remain ); // copy remaining up front
71 remain += read( fd, nxt + remain, sizeof( rbuf ) - remain );
74 if( remain < 20 ) { // truncated or a record > rbuf
75 fprintf( stderr, "abort: @header check, truncated file, or record > read buffer size\n" );
79 if( strncmp( nxt, "@RDC", 4 ) == 0 ) {
80 mtype = atoi( nxt+4 );
81 mlen = atoi( nxt+12 );
85 if( remain < mlen ) { // not enough stuff
86 memcpy( rbuf, nxt, remain ); // copy remaining up front
88 remain += read( fd, nxt + remain, sizeof( rbuf ) - remain );
91 if( remain < mlen ) { // truncated or a record > rbuf
92 fprintf( stderr, "abort: truncated file, or record > read buffer size\n" );
96 if( desired == 0 ) { // just dump mtypes
98 fprintf( stdout, "%d\n", mtype );
101 if( mtype == desired ) {
103 write( wfd, nxt, mlen );
111 fprintf( stderr, "didn't find rdc header!?! @ %ld\n", (long) (nxt - rbuf) );
116 fprintf( stderr, "done, captured %d messages (%d bytes)\n", captured, wlen );