File: dump.cc

package info (click to toggle)
wreport 2.14-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,240 kB
  • ctags: 1,425
  • sloc: sh: 11,322; cpp: 9,823; makefile: 242; python: 207
file content (128 lines) | stat: -rw-r--r-- 3,141 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * dump - Example simple BUFR/CREX dump program
 *
 * Copyright (C) 2010  ARPA-SIM <urpsim@smr.arpa.emr.it>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * Author: Enrico Zini <enrico@enricozini.com>
 */

// Messages are read in a Bulletin class
#include <wreport/bulletin.h>
#include <string>
#include <cstdio>
#include <strings.h>

using namespace std;

/**
 * Little enum we use to represent the choice of encoding from the command line
 */
enum Encoding { BUFR, CREX };

/**
 * Dump a BUFR or CREX file.
 *
 * We use a single template for both bulletin types, as they have exactly the
 * same API.
 */
template<typename TYPE>
void dump(FILE* in, const char* fname)
{
	try {
		// Bulletin to use to store the decoded data
		TYPE bulletin;

		// Each bulletin type has a read function that reads the next
		// message from any input stream into a string buffer
		string buf;
		while (TYPE::read(in, buf, fname))
		{
			// Decode the raw data filling in the bulletin
			bulletin.decode(buf);
			// Dump it all to stdout
			bulletin.print(stdout);
		}
	} catch (std::exception& e) {
		// Errors are reported through exceptions. All exceptions used
		// by wreport are descendents of std::exception
		fprintf(stderr, "Error reading %s: %s\n", fname, e.what());
	}
}

/// Select the right bulletin type according to the requested format
void dump(Encoding type, FILE* in, const char* fname)
{
	switch (type)
	{
		case BUFR: dump<wreport::BufrBulletin>(in, fname); break;
		case CREX: dump<wreport::CrexBulletin>(in, fname); break;
	}
}

const char* argv0 = "dump";

/// Print program usage
void usage()
{
	fprintf(stderr, "Usage: %s {BUFR|CREX} [file1 [file2...]]\n", argv0);
	fprintf(stderr, "Dumps bufr or crex file contents to standard output\n");
}

int main(int argc, const char* argv[])
{
	argv0 = argv[0];

	if (argc == 1)
	{
		usage();
		return 1;
	}

	// Detect what encoding type is requested
	Encoding type;
	if (strcasecmp(argv[1], "BUFR") == 0)
		type = BUFR;
	else if (strcasecmp(argv[1], "CREX") == 0)
		type = CREX;
	else
	{
		usage();
		return 1;
	}

	if (argc == 2)
	{
		// Dump stdin if no input file was given
		printf("Dumping standard input...\n");
		dump(type, stdin, "standard input");
	} else {
		// Else dump all input files
		for (int i = 2; i < argc; ++i)
		{
			printf("Dumping file %s...\n", argv[i]);
			FILE* in = fopen(argv[i], "rb");
			if (in == NULL) 
			{
				perror("cannot open file");
				continue;
			}
			dump(type, in, argv[i]);
			fclose(in);
		}
	}

	return 0;
}