Merge branch 'master' of fraxinas@git.opendreambox.org:/git/enigma2
[vuplus_dvbapp] / 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                 case 7:
71                         m_service_data = line;
72                         break;
73                 default:
74                         break;
75                 }
76                 ++linecnt;
77         }
78         fclose(f);
79         m_data_ok = 1;
80         return 0;
81 }
82
83 int eDVBMetaParser::parseRecordings(const std::string &filename)
84 {
85         std::string::size_type slash = filename.rfind('/');
86         if (slash == std::string::npos)
87                 return -1;
88         
89         std::string recordings = filename.substr(0, slash) + "/recordings.epl";
90         
91         FILE *f = fopen(recordings.c_str(), "r");
92         if (!f)
93         {
94 //              eDebug("no recordings.epl found: %s: %m", recordings.c_str());
95                 return -1;
96         }
97         
98         std::string description;
99         eServiceReferenceDVB ref;
100         
101 //      eDebug("parsing recordings.epl..");
102         
103         while (1)
104         {
105                 char line[1024];
106                 if (!fgets(line, 1024, f))
107                         break;
108                 
109                 if (strlen(line))
110                         line[strlen(line)-1] = 0;
111                 
112                 if (strlen(line) && line[strlen(line)-1] == '\r')
113                         line[strlen(line)-1] = 0;
114                 
115                 if (!strncmp(line, "#SERVICE: ", 10))
116                         ref = eServiceReferenceDVB(line + 10);
117                 if (!strncmp(line, "#DESCRIPTION: ", 14))
118                         description = line + 14;
119                 if ((line[0] == '/') && (ref.path.substr(ref.path.find_last_of('/')) == filename.substr(filename.find_last_of('/'))))
120                 {
121 //                      eDebug("hit! ref %s descr %s", m_ref.toString().c_str(), m_name.c_str());
122                         m_ref = ref;
123                         m_name = description;
124                         m_description = "";
125                         m_time_create = 0;
126                         m_length = 0;
127                         m_filesize = 0;
128                                                 
129                         m_data_ok = 1;
130                         fclose(f);
131                         updateMeta(filename.c_str());
132                         return 0;
133                 }
134         }
135         fclose(f);
136         return -1;
137 }
138
139 int eDVBMetaParser::updateMeta(const std::string &tsname)
140 {
141         /* write meta file only if we have valid data. Note that we might convert recordings.epl data to .meta, which is fine. */
142         if (!m_data_ok)
143                 return -1;
144         std::string filename = tsname + ".meta";
145         eServiceReference ref = m_ref;
146         ref.path = "";
147
148         FILE *f = fopen(filename.c_str(), "w");
149         if (!f)
150                 return -ENOENT;
151         fprintf(f, "%s\n%s\n%s\n%d\n%s\n%d\n%lld\n%s\n", ref.toString().c_str(), m_name.c_str(), m_description.c_str(), m_time_create, m_tags.c_str(), m_length, m_filesize, m_service_data.c_str() );
152         fclose(f);
153         return 0;
154 }