summaryrefslogtreecommitdiff
path: root/channel.c
blob: 8c32a961d9b98d84c640d460de9ce6f9d7512da2 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "channel.h"

struct channel_t *channels;
int channel_count;

void channel_init() {
	channels = NULL;
	channel_count = 0;
}

struct channel_t *channel_add(const char *name, const char *xmlpath) {
	channels = realloc(channels, ++channel_count * sizeof(struct channel_t));
	if(!channels) {
		char *error = strerror(errno);
		fprintf(stderr, "Could not (re)allocate memory for channels: %s\n", error);
		return NULL;
	}
	struct channel_t *channel = &channels[channel_count-1];
	channel->name = strdup(name);
	channel->xmlpath = strdup(xmlpath);
	channel->files = NULL;
	memset(channel->hours, 0, 24*4 * sizeof(unsigned long));
	return channel;
}

struct channel_file_t *channel_file_add(struct channel_t *channel, const char *path, int rs_index) {
	struct channel_file_t *file = malloc(sizeof(struct channel_file_t));
	struct channel_file_t *last = channel->files;
	if(last) {
		while(last->next) last = last->next;
		last->next = file;
	} else
		channel->files = file;
	file->path = strdup(path);
	file->rs = rs_get(rs_index);
	file->next = NULL;
	if(!file->rs)
		return NULL;
	return file;
}

int channel_get_count() {
	return channel_count;
}

struct channel_t *channel_get(int index) {
	return (index < channel_count ? &channels[index] : NULL);
}

void channel_free() {
	for(int i = 0; i < channel_count; i++) {
		free(channels[i].name);
		free(channels[i].xmlpath);
		struct channel_file_t *file = channels[i].files;
		while(file) {
			struct channel_file_t *next = file->next;
			free(file->path);
			free(file);
			file = next;
		}
	}
	free(channels);
	channels = NULL;
}