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