File: dvb-format-convert.c

package info (click to toggle)
v4l-utils 0.8.8-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,492 kB
  • sloc: ansic: 33,068; cpp: 10,882; perl: 4,294; makefile: 470; php: 119; sh: 24
file content (136 lines) | stat: -rw-r--r-- 3,884 bytes parent folder | download
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
129
130
131
132
133
134
135
136
/*
 * Copyright (c) 2011-2012 - Mauro Carvalho Chehab <mchehab@redhat.com>
 *
 * 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 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
 * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <argp.h>

#include "dvb-file.h"
#include "dvb-demux.h"
#include "libscan.h"

#define PROGRAM_NAME	"dvb-format-convert"

struct arguments {
	char *input_file, *output_file;
	enum file_formats input_format, output_format;
	int delsys;
};

static const struct argp_option options[] = {
	{"input-format",	'I',	"format",	0, "Input format: ZAP, CHANNEL, DVBV5", 0},
	{"output-format",	'O',	"format",	0, "Input format: ZAP, CHANNEL, DVBV5", 0},
	{"delsys",		's',	"system",	0, "Delivery system type. Needed if input or output format is ZAP", 0},
	{ 0, 0, 0, 0, 0, 0 }
};

const char *argp_program_version = PROGRAM_NAME " version " V4L_UTILS_VERSION;
const char *argp_program_bug_address = "Mauro Carvalho Chehab <mchehab@redhat.com>";

static error_t parse_opt(int k, char *optarg, struct argp_state *state)
{
	struct arguments *args = state->input;
	switch (k) {
	case 'I':
		args->input_format = parse_format(optarg);
		break;
	case 'O':
		args->output_format = parse_format(optarg);
		break;
	case 's':
		args->delsys = parse_delsys(optarg);
		break;
	default:
		return ARGP_ERR_UNKNOWN;
	};
	return 0;
}

static int convert_file(struct arguments *args)
{
	struct dvb_file *dvb_file = NULL;
	int ret;

	printf("Reading file %s\n", args->input_file);

	dvb_file = read_file_format(args->input_file, args->delsys,
				    args->input_format);
	if (!dvb_file) {
		fprintf(stderr, "Error reading file %s\n", args->input_file);
		return -1;
	}

	printf("Writing file %s\n", args->output_file);
	ret = write_file_format(args->output_file, dvb_file,
				args->delsys, args->output_format);

	return ret;
}

int main(int argc, char **argv)
{
	struct arguments args;
	int idx = -1, missing = 0;
	const struct argp argp = {
		.options = options,
		.parser = parse_opt,
		.doc = "scan DVB services using the channel file",
		.args_doc = "<input file> <output file>",
	};

	memset(&args, 0, sizeof(args));
	argp_parse(&argp, argc, argv, 0, &idx, &args);

	if (idx + 1 < argc) {
		args.input_file = argv[idx];
		args.output_file = argv[idx + 1];
	}

	if (args.input_format == FILE_UNKNOWN) {
		fprintf(stderr, "ERROR: Please specify a valid input format\n");
		missing = 1;
	} else if (!args.input_file) {
		fprintf(stderr, "ERROR: Please specify a valid input file\n");
		missing = 1;
	} else if (args.output_format == FILE_UNKNOWN) {
		fprintf(stderr, "ERROR: Please specify a valid output format\n");
		missing = 1;
	} else if (!args.output_file) {
		fprintf(stderr, "ERROR: Please specify a valid output file\n");
		missing = 1;
	} else if (((args.input_format == FILE_ZAP) ||
		   (args.output_format == FILE_ZAP)) && args.delsys <= 0) {
		fprintf(stderr, "ERROR: Please specify a valid delivery system for ZAP format\n");
		missing = 1;
	}
	if (missing) {
		argp_help(&argp, stderr, ARGP_HELP_STD_HELP, PROGRAM_NAME);
		return -1;
	}

	return convert_file(&args);
}