fix: fd leaked when reading recordings.epl
[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                 default:
59                         break;
60                 }
61                 ++linecnt;
62         }
63         fclose(f);
64         return 0;
65 }
66
67 int eDVBMetaParser::parseRecordings(const std::string &filename)
68 {
69         std::string::size_type slash = filename.rfind('/');
70         if (slash == std::string::npos)
71                 return -1;
72         
73         std::string recordings = filename.substr(0, slash) + "/recordings.epl";
74         
75         FILE *f = fopen(recordings.c_str(), "r");
76         if (!f)
77         {
78 //              eDebug("no recordings.epl found: %s: %m", recordings.c_str());
79                 return -1;
80         }
81         
82         std::string description;
83         eServiceReferenceDVB ref;
84         
85 //      eDebug("parsing recordings.epl..");
86         
87         while (1)
88         {
89                 char line[1024];
90                 if (!fgets(line, 1024, f))
91                         break;
92                 
93                 if (strlen(line))
94                         line[strlen(line)-1] = 0;
95                 
96                 if (strlen(line) && line[strlen(line)-1] == '\r')
97                         line[strlen(line)-1] = 0;
98                 
99                 if (!strncmp(line, "#SERVICE: ", 10))
100                         ref = eServiceReferenceDVB(line + 10);
101                 if (!strncmp(line, "#DESCRIPTION: ", 14))
102                         description = line + 14;
103
104                 if ((line[0] == '/') && (ref.path == filename))
105                 {
106 //                      eDebug("hit! ref %s descr %s", m_ref.toString().c_str(), m_name.c_str());
107                         m_ref = ref;
108                         m_name = description;
109                         m_description = "";
110                         m_time_create = 0;
111                         fclose(f);
112                         return 0;
113                 }
114         }
115         fclose(f);
116         return -1;
117 }