add 'tags' interface (4th line in .meta files are a space-seperated list of tags...
[enigma2.git] / lib / dvb / metaparser.cpp
1 #include <lib/dvb/metaparser.h>
2 #include <lib/base/eerror.h>
3 #include <errno.h>
4
5 eDVBMetaParser::eDVBMetaParser()
6 {
7         m_time_create = 0;
8 }
9
10 int eDVBMetaParser::parseFile(const std::string &basename)
11 {
12                 /* first, try parsing the .meta file */
13         if (!parseMeta(basename))
14                 return 0;
15         
16                 /* otherwise, use recordings.epl */
17         return parseRecordings(basename);
18 }
19
20 int eDVBMetaParser::parseMeta(const std::string &tsname)
21 {
22                 /* if it's a PVR channel, recover service id. */
23         std::string filename = tsname + ".meta";
24                 
25         FILE *f = fopen(filename.c_str(), "r");
26         if (!f)
27                 return -ENOENT;
28
29         int linecnt = 0;
30         
31         m_time_create = 0;
32         
33         while (1)
34         {
35                 char line[1024];
36                 if (!fgets(line, 1024, f))
37                         break;
38                 if (*line && line[strlen(line)-1] == '\n')
39                         line[strlen(line)-1] = 0;
40
41                 if (*line && line[strlen(line)-1] == '\r')
42                         line[strlen(line)-1] = 0;
43
44                 switch (linecnt)
45                 {
46                 case 0:
47                         m_ref = eServiceReferenceDVB(line);
48                         break;
49                 case 1:
50                         m_name = line;
51                         break;
52                 case 2:
53                         m_description = line;
54                         break;
55                 case 3:
56                         m_time_create = atoi(line);
57                         break;
58                 case 4:
59                         m_tags = line;
60                         break;
61                 default:
62                         break;
63                 }
64                 ++linecnt;
65         }
66         fclose(f);
67         return 0;
68 }
69
70 int eDVBMetaParser::parseRecordings(const std::string &filename)
71 {
72         std::string::size_type slash = filename.rfind('/');
73         if (slash == std::string::npos)
74                 return -1;
75         
76         std::string recordings = filename.substr(0, slash) + "/recordings.epl";
77         
78         FILE *f = fopen(recordings.c_str(), "r");
79         if (!f)
80         {
81 //              eDebug("no recordings.epl found: %s: %m", recordings.c_str());
82                 return -1;
83         }
84         
85         std::string description;
86         eServiceReferenceDVB ref;
87         
88 //      eDebug("parsing recordings.epl..");
89         
90         while (1)
91         {
92                 char line[1024];
93                 if (!fgets(line, 1024, f))
94                         break;
95                 
96                 if (strlen(line))
97                         line[strlen(line)-1] = 0;
98                 
99                 if (strlen(line) && line[strlen(line)-1] == '\r')
100                         line[strlen(line)-1] = 0;
101                 
102                 if (!strncmp(line, "#SERVICE: ", 10))
103                         ref = eServiceReferenceDVB(line + 10);
104                 if (!strncmp(line, "#DESCRIPTION: ", 14))
105                         description = line + 14;
106
107                 if ((line[0] == '/') && (ref.path == filename))
108                 {
109 //                      eDebug("hit! ref %s descr %s", m_ref.toString().c_str(), m_name.c_str());
110                         m_ref = ref;
111                         m_name = description;
112                         m_description = "";
113                         m_time_create = 0;
114                         fclose(f);
115                         return 0;
116                 }
117         }
118         fclose(f);
119         return -1;
120 }