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