JPC-RR r11.7
[jpcrr.git] / streamtools / cutdump.cpp
blob741d80566f0b624da4d2a1138eb061c8ea57667a
1 #include "newpacket.hpp"
2 #include "timeparse.hpp"
3 #include <iostream>
4 #include <sstream>
5 #include <list>
6 #include <map>
7 #include <vector>
8 #include <cstdio>
9 #include <cstring>
10 #include <stdexcept>
11 #include <algorithm>
13 int real_main(int argc, char** argv)
15 std::map<std::string, uint16_t> channel_assignments;
16 std::vector<struct channel> chans;
17 uint16_t dummy_channel = 0;
18 if(argc != 5) {
19 std::cerr << "syntax: muxdump.exe <input> <start> <end> <output>" << std::endl;
20 exit(1);
23 read_channel rchan(argv[1]);
24 write_channel wchan(argv[4]);
25 uint64_t low = parse_timespec(argv[2]);
26 uint64_t high = parse_timespec(argv[3]);
27 if(low > high) {
28 std::cerr << "Start of region must be before end." << std::endl;
29 exit(1);
32 //Create dummy channel.
33 channel_assignments["<DUMMY>"] = dummy_channel = (uint16_t)chans.size();
34 struct channel c;
35 c.c_channel = (uint16_t)chans.size();
36 c.c_type = 3;
37 c.c_channel_name = "<DUMMY>";
38 chans.push_back(c);
39 wchan.start_segment(chans);
41 packet* p;
42 while((p = rchan.read())) {
43 if(p->rp_timestamp < low) {
44 delete p;
45 continue;
47 if(p->rp_timestamp > high) {
48 delete p;
49 break;
51 if(!channel_assignments.count(p->rp_channel_name)) {
52 //No channel yet, create.
53 channel_assignments[p->rp_channel_name] = (uint16_t)chans.size();
54 struct channel c;
55 c.c_channel = (uint16_t)chans.size();
56 c.c_type = p->rp_major;
57 c.c_channel_name = p->rp_channel_name;
58 chans.push_back(c);
59 wchan.start_segment(chans);
61 uint16_t chan = channel_assignments[p->rp_channel_name];
62 if(chans[chan].c_type != p->rp_major) {
63 //Change type.
64 chans[chan].c_type = p->rp_major;
65 wchan.start_segment(chans);
67 p->rp_channel = chan;
68 p->rp_timestamp -= low;
69 wchan.write(*p);
70 delete p;
73 struct packet p2;
74 p2.rp_channel = dummy_channel;
75 p2.rp_timestamp = high - low;
76 p2.rp_minor = 0;
77 wchan.write(p2);
79 return 0;