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