aboutsummaryrefslogtreecommitdiff
path: root/lib/dvb/metaparser.cpp
blob: 175c7cdb055af9a97289cdf71049d56550db6add (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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <lib/dvb/metaparser.h>
#include <lib/base/eerror.h>
#include <errno.h>

eDVBMetaParser::eDVBMetaParser()
{
	m_time_create = 0;
	m_data_ok = 0;
	m_length = 0;
	m_filesize = 0;
}

int eDVBMetaParser::parseFile(const std::string &basename)
{
		/* first, try parsing the .meta file */
	if (!parseMeta(basename))
		return 0;
	
		/* otherwise, use recordings.epl */
	return parseRecordings(basename);
}

int eDVBMetaParser::parseMeta(const std::string &tsname)
{
		/* if it's a PVR channel, recover service id. */
	std::string filename = tsname + ".meta";
		
	FILE *f = fopen(filename.c_str(), "r");
	if (!f)
		return -ENOENT;

	int linecnt = 0;
	
	m_time_create = 0;
	
	while (1)
	{
		char line[1024];
		if (!fgets(line, 1024, f))
			break;
		if (*line && line[strlen(line)-1] == '\n')
			line[strlen(line)-1] = 0;

 		if (*line && line[strlen(line)-1] == '\r')
			line[strlen(line)-1] = 0;

		switch (linecnt)
		{
		case 0:
			m_ref = eServiceReferenceDVB(line);
			break;
		case 1:
			m_name = line;
			break;
		case 2:
			m_description = line;
			break;
		case 3:
			m_time_create = atoi(line);
			break;
		case 4:
			m_tags = line;
			break;
		case 5:
			m_length = atoi(line);  //movielength in pts
			break;
		case 6:
			m_filesize = atoll(line);
			break;
		default:
			break;
		}
		++linecnt;
	}
	fclose(f);
	m_data_ok = 1;
	return 0;
}

int eDVBMetaParser::parseRecordings(const std::string &filename)
{
	std::string::size_type slash = filename.rfind('/');
	if (slash == std::string::npos)
		return -1;
	
	std::string recordings = filename.substr(0, slash) + "/recordings.epl";
	
	FILE *f = fopen(recordings.c_str(), "r");
	if (!f)
	{
//		eDebug("no recordings.epl found: %s: %m", recordings.c_str());
		return -1;
	}
	
	std::string description;
	eServiceReferenceDVB ref;
	
//	eDebug("parsing recordings.epl..");
	
	while (1)
	{
		char line[1024];
		if (!fgets(line, 1024, f))
			break;
		
		if (strlen(line))
			line[strlen(line)-1] = 0;
		
		if (strlen(line) && line[strlen(line)-1] == '\r')
			line[strlen(line)-1] = 0;
		
		if (!strncmp(line, "#SERVICE: ", 10))
			ref = eServiceReferenceDVB(line + 10);
		if (!strncmp(line, "#DESCRIPTION: ", 14))
			description = line + 14;
		if ((line[0] == '/') && (ref.path.substr(ref.path.find_last_of('/')) == filename.substr(filename.find_last_of('/'))))
		{
//			eDebug("hit! ref %s descr %s", m_ref.toString().c_str(), m_name.c_str());
			m_ref = ref;
			m_name = description;
			m_description = "";
			m_time_create = 0;
			m_length = 0;
			m_filesize = 0;
						
			m_data_ok = 1;
			fclose(f);
			updateMeta(filename.c_str());
			return 0;
		}
	}
	fclose(f);
	return -1;
}

int eDVBMetaParser::updateMeta(const std::string &tsname)
{
	/* write meta file only if we have valid data. Note that we might convert recordings.epl data to .meta, which is fine. */
	if (!m_data_ok)
		return -1;
	std::string filename = tsname + ".meta";
	eServiceReference ref = m_ref;
	ref.path = "";

	FILE *f = fopen(filename.c_str(), "w");
	if (!f)
		return -ENOENT;
	fprintf(f, "%s\n%s\n%s\n%d\n%s\n%d\n%lld\n", ref.toString().c_str(), m_name.c_str(), m_description.c_str(), m_time_create, m_tags.c_str(), m_length, m_filesize );
	fclose(f);
	return 0;
}