numberzap accepts a maximum of 4 digits
[enigma2.git] / lib / dvb / db.cpp
1 #include <errno.h>
2 #include <lib/dvb/db.h>
3 #include <lib/dvb/frontend.h>
4 #include <lib/base/eerror.h>
5 #include <lib/base/estring.h>
6 #include <dvbsi++/service_description_section.h>
7 #include <dvbsi++/descriptor_tag.h>
8 #include <dvbsi++/service_descriptor.h>
9 #include <dvbsi++/satellite_delivery_system_descriptor.h>
10
11 DEFINE_REF(eDVBService);
12
13 eDVBService::eDVBService()
14 {
15 }
16
17 eDVBService::~eDVBService()
18 {
19 }
20
21 eDVBService &eDVBService::operator=(const eDVBService &s)
22 {
23         m_service_name = s.m_service_name;
24         m_service_name_sort = s.m_service_name_sort;
25         m_provider_name = s.m_provider_name;
26         m_flags = s.m_flags;
27         m_ca = s.m_ca;
28         m_cache = s.m_cache;
29         return *this;
30 }
31
32 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
33 {
34         if (!ref.name.empty())
35                 name = ref.name;
36         else if (!m_service_name.empty())
37                 name = m_service_name;
38         else
39                 name = "(...)";
40         return 0;
41 }
42
43 int eDVBService::getLength(const eServiceReference &ref)
44 {
45         return -1;
46 }
47
48 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
49 {
50         int res = 0;
51         switch (query.m_type)
52         {
53         case eDVBChannelQuery::tName:
54                 res = m_service_name_sort.find(query.m_string) != std::string::npos;
55                 break;
56         case eDVBChannelQuery::tProvider:
57                 res = m_provider_name.find(query.m_string) != std::string::npos;
58                 break;
59         case eDVBChannelQuery::tType:
60                 res = ref.getServiceType() == query.m_int;
61                 break;
62         case eDVBChannelQuery::tBouquet:
63                 res = 0;
64                 break;
65         case eDVBChannelQuery::tSatellitePosition:
66                 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
67                 break;
68         case eDVBChannelQuery::tChannelID:
69         {
70                 eDVBChannelID chid;
71                 ref.getChannelID(chid);
72                 res = chid == query.m_channelid;
73                 break;
74         }
75         case eDVBChannelQuery::tAND:
76                 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
77                 break;
78         case eDVBChannelQuery::tOR:
79                 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
80                 break;
81         }
82
83         if (query.m_inverse)
84                 return !res;
85         else
86                 return res;
87 }
88
89 int eDVBService::getCachePID(cacheID id)
90 {
91         std::map<int, int>::iterator it = m_cache.find(id);
92         if ( it != m_cache.end() )
93                 return it->second;
94         return -1;
95 }
96
97 void eDVBService::setCachePID(cacheID id, int pid)
98 {
99         m_cache[id] = pid;
100 }
101
102 DEFINE_REF(eDVBDB);
103
104         /* THIS CODE IS BAD. it should be replaced by somethine better. */
105 void eDVBDB::load()
106 {
107         eDebug("---- opening lame channel db");
108         FILE *f=fopen("lamedb", "rt");
109         if (!f)
110                 return;
111         char line[256];
112         if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
113         {
114                 eDebug("not a servicefile");
115                 fclose(f);
116                 return;
117         }
118         eDebug("reading services");
119         if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
120         {
121                 eDebug("services invalid, no transponders");
122                 fclose(f);
123                 return;
124         }
125
126         // clear all transponders
127
128         while (!feof(f))
129         {
130                 if (!fgets(line, 256, f))
131                         break;
132                 if (!strcmp(line, "end\n"))
133                         break;
134                 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
135                 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
136                 if (original_network_id == -1)
137                         continue;
138                 eDVBChannelID channelid = eDVBChannelID(
139                         eDVBNamespace(dvb_namespace),
140                         eTransportStreamID(transport_stream_id),
141                         eOriginalNetworkID(original_network_id));
142
143                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
144                 while (!feof(f))
145                 {
146                         fgets(line, 256, f);
147                         if (!strcmp(line, "/\n"))
148                                 break;
149                         if (line[1]=='s')
150                         {
151                                 eDVBFrontendParametersSatellite sat;
152                                 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
153                                 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
154                                 sat.frequency = frequency;
155                                 sat.symbol_rate = symbol_rate;
156                                 sat.polarisation = polarisation;
157                                 sat.fec = fec;
158                                 sat.orbital_position = orbital_position;
159                                 sat.inversion = inversion;
160                                 feparm->setDVBS(sat);
161                         } else if (line[1]=='t')
162                         {
163                                 eDVBFrontendParametersTerrestrial ter;
164                                 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
165                                 sscanf(line+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &bandwidth, &code_rate_HP, &code_rate_LP, &modulation, &transmission_mode, &guard_interval, &hierarchy, &inversion);
166                                 ter.frequency = frequency;
167                                 ter.bandwidth = bandwidth;
168                                 ter.code_rate_HP = code_rate_HP;
169                                 ter.code_rate_LP = code_rate_LP;
170                                 ter.modulation = modulation;
171                                 ter.transmission_mode = transmission_mode;
172                                 ter.guard_interval = guard_interval;
173                                 ter.hierarchy = hierarchy;
174                                 ter.inversion = inversion;
175                                 
176                                 feparm->setDVBT(ter);
177                         } else if (line[1]=='c')
178                         {
179                                 int frequency, symbol_rate, inversion=0, modulation=3;
180                                 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
181 //                              t.setCable(frequency, symbol_rate, inversion, modulation);
182                         }
183                 }
184                 addChannelToList(channelid, feparm);
185         }
186
187         if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
188         {
189                 eDebug("services invalid, no services");
190                 return;
191         }
192
193         // clear all services
194
195         int count=0;
196
197         while (!feof(f))
198         {
199                 if (!fgets(line, 256, f))
200                         break;
201                 if (!strcmp(line, "end\n"))
202                         break;
203
204                 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
205                 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
206                 if (service_number == -1)
207                         continue;
208                 ePtr<eDVBService> s = new eDVBService;
209                 eServiceReferenceDVB ref =
210                                                 eServiceReferenceDVB(
211                                                 eDVBNamespace(dvb_namespace),
212                                                 eTransportStreamID(transport_stream_id),
213                                                 eOriginalNetworkID(original_network_id),
214                                                 eServiceID(service_id),
215                                                 service_type);
216                 count++;
217                 fgets(line, 256, f);
218                 if (strlen(line))
219                         line[strlen(line)-1]=0;
220
221                 s->m_service_name = line;
222                 s->m_service_name_sort = removeDVBChars(line);
223                 makeUpper(s->m_service_name_sort);
224                 while ((!s->m_service_name_sort.empty()) && s->m_service_name_sort[0] == ' ')
225                         s->m_service_name_sort.erase(0, 1);
226                 
227                         /* put unnamed services at the end, not at the beginning. */
228                 if (s->m_service_name_sort.empty())
229                         s->m_service_name_sort = "\xFF";
230                 
231                 fgets(line, 256, f);
232                 if (strlen(line))
233                         line[strlen(line)-1]=0;
234
235                 std::string str=line;
236
237                 if (str[1]!=':')        // old ... (only service_provider)
238                 {
239                         s->m_provider_name=line;
240                 } else
241                         while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
242                         {
243                                 unsigned int c=str.find(',');
244                                 char p=str[0];
245                                 std::string v;
246                                 if (c == std::string::npos)
247                                 {
248                                         v=str.substr(2);
249                                         str="";
250                                 } else
251                                 {
252                                         v=str.substr(2, c-2);
253                                         str=str.substr(c+1);
254                                 }
255 //                              eDebug("%c ... %s", p, v.c_str());
256                                 if (p == 'p')
257                                         s->m_provider_name=v;
258                                 else if (p == 'f')
259                                 {
260                                         sscanf(v.c_str(), "%x", &s->m_flags);
261                                 } else if (p == 'c')
262                                 {
263                                         int cid, val;
264                                         sscanf(v.c_str(), "%02d%04x", &cid, &val);
265                                         s->m_cache[cid]=val;
266                                 } else if (p == 'C')
267                                 {
268                                         int val;
269                                         sscanf(v.c_str(), "%04x", &val);
270                                         s->m_ca.insert(val);
271                                 }
272                         }
273                 addService(ref, s);
274         }
275
276         eDebug("loaded %d services", count);
277
278         fclose(f);
279 }
280
281 void eDVBDB::save()
282 {
283         eDebug("---- saving lame channel db");
284         FILE *f=fopen("lamedb", "wt");
285         int channels=0, services=0;
286         if (!f)
287                 eFatal("couldn't save lame channel db!");
288         fprintf(f, "eDVB services /3/\n");
289         fprintf(f, "transponders\n");
290         for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
291                         i != m_channels.end(); ++i)
292         {
293                 const eDVBChannelID &chid = i->first;
294                 const channel &ch = i->second;
295
296                 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
297                                 chid.transport_stream_id.get(), chid.original_network_id.get());
298                 eDVBFrontendParametersSatellite sat;
299                 eDVBFrontendParametersTerrestrial ter;
300                 if (!ch.m_frontendParameters->getDVBS(sat))
301                 {
302                         fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
303                                 sat.frequency, sat.symbol_rate,
304                                 sat.polarisation, sat.fec, sat.orbital_position,
305                                 sat.inversion);
306                 }
307                 if (!ch.m_frontendParameters->getDVBT(ter))
308                 {
309                         fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
310                                 ter.frequency, ter.bandwidth, ter.code_rate_HP,
311                                 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
312                                 ter.guard_interval, ter.hierarchy, ter.inversion);
313                 }
314                 fprintf(f, "/\n");
315                 channels++;
316         }
317         fprintf(f, "end\nservices\n");
318
319         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
320                 i != m_services.end(); ++i)
321         {
322                 const eServiceReferenceDVB &s = i->first;
323                 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
324                                 s.getServiceID().get(), s.getDVBNamespace().get(),
325                                 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
326                                 s.getServiceType(),
327                                 0);
328
329                 fprintf(f, "%s\n", i->second->m_service_name.c_str());
330                 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
331
332                 // write cached pids
333                 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
334                         ca != i->second->m_cache.end(); ++ca)
335                         fprintf(f, ",c:%02d%04x", ca->first, ca->second);
336
337                 // write cached ca pids
338                 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
339                         ca != i->second->m_ca.end(); ++ca)
340                         fprintf(f, ",C:%04x", *ca);
341
342                 fprintf(f, "\n");
343                 services++;
344         }
345         fprintf(f, "end\nHave a lot of bugs!\n");
346         eDebug("saved %d channels and %d services!", channels, services);
347         fclose(f);
348 }
349
350 void eDVBDB::loadBouquet(const char *path)
351 {
352         std::string bouquet_name = path;
353         if (!bouquet_name.length())
354         {
355                 eDebug("Bouquet load failed.. no path given..");
356                 return;
357         }
358         unsigned int pos = bouquet_name.rfind('/');
359         if ( pos != std::string::npos )
360                 bouquet_name.erase(0, pos+1);
361         if (bouquet_name.empty())
362         {
363                 eDebug("Bouquet load failed.. no filename given..");
364                 return;
365         }
366         eBouquet &bouquet = m_bouquets[bouquet_name];
367         bouquet.m_path = path;
368         std::list<eServiceReference> &list = bouquet.m_services;
369         list.clear();
370
371         eDebug("loading bouquet... %s", path);
372         FILE *fp=fopen(path, "rt");
373         int entries=0;
374         if (!fp)
375         {
376                 eDebug("failed to open.");
377                 return;
378         }
379         char line[256];
380         bool read_descr=false;
381         eServiceReference *e = NULL;
382         while (1)
383         {
384                 if (!fgets(line, 256, fp))
385                         break;
386                 line[strlen(line)-1]=0;
387                 if (strlen(line) && line[strlen(line)-1]=='\r')
388                         line[strlen(line)-1]=0;
389                 if (!line[0])
390                         break;
391                 if (line[0]=='#')
392                 {
393                         if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
394                         {
395                                 int offs = line[8] == ':' ? 10 : 9;
396                                 eServiceReference tmp(line+offs);
397                                 if (tmp.type != eServiceReference::idDVB)
398                                 {
399                                         eDebug("only DVB Bouquets supported");
400                                         continue;
401                                 }
402                                 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
403                                 {
404                                         std::string str = tmp.path;
405                                         unsigned int pos = str.rfind('/');
406                                         if ( pos != std::string::npos )
407                                                 str.erase(0, pos+1);
408                                         if (str.empty())
409                                         {
410                                                 eDebug("Bouquet load failed.. no filename given..");
411                                                 continue;
412                                         }
413                                         loadBouquet(tmp.path.c_str());
414                                         char buf[256];
415                                         snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], str.c_str());
416                                         tmp.path = buf;
417                                 }
418                                 list.push_back(tmp);
419                                 e = &list.back();
420                                 read_descr=true;
421                                 ++entries;
422                         }
423                         else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
424                         {
425                                 e->name = line+13;
426                                 read_descr=false;
427                         }
428                         else if (!strncmp(line, "#NAME ", 6))
429                                 bouquet.m_bouquet_name=line+6;
430                         continue;
431                 }
432         }
433         fclose(fp);
434         eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
435 }
436
437 void eDVBDB::saveBouquet(const char *path)
438 {
439
440 }
441
442 void eDVBDB::loadBouquets()
443 {
444         loadBouquet("bouquets.tv");
445         loadBouquet("bouquets.radio");
446 }
447
448 void eDVBDB::saveBouquets()
449 {
450
451 }
452
453 eDVBDB::eDVBDB()
454 {
455         load();
456         loadBouquets();
457 }
458
459 eDVBDB::~eDVBDB()
460 {
461 //      save();
462 }
463
464 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
465 {
466         channel ch;
467         assert(feparm);
468         ch.m_frontendParameters = feparm;
469         m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
470         return 0;
471 }
472
473 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
474 {
475         m_channels.erase(id);
476         return 0;
477 }
478
479 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
480 {
481         std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
482         if (i == m_channels.end())
483         {
484                 parm = 0;
485                 return -ENOENT;
486         }
487         parm = i->second.m_frontendParameters;
488         return 0;
489 }
490
491 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
492 {
493         m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
494         return 0;
495 }
496
497 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
498 {
499         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
500         i = m_services.find(reference);
501         if (i == m_services.end())
502         {
503                 service = 0;
504                 return -ENOENT;
505         }
506         service = i->second;
507         return 0;
508 }
509
510 RESULT eDVBDB::getBouquet(const eServiceReference &ref, const eBouquet* &bouquet)
511 {
512         std::string str = ref.path;
513         if (str.empty())
514         {
515                 eDebug("getBouquet failed.. no path given!");
516                 return -1;
517         }
518         unsigned int pos = str.find("FROM BOUQUET \"");
519         if ( pos != std::string::npos )
520         {
521                 str.erase(0, pos+14);
522                 pos = str.find('"');
523                 if ( pos != std::string::npos )
524                         str.erase(pos);
525                 else
526                         str.clear();
527         }
528         if (str.empty())
529         {
530                 eDebug("getBouquet failed.. couldn't parse bouquet name");
531                 return -1;
532         }
533         std::map<std::string, eBouquet>::iterator i =
534                 m_bouquets.find(str);
535         if (i == m_bouquets.end())
536         {
537                 bouquet = 0;
538                 return -ENOENT;
539         }
540         bouquet = &i->second;
541         return 0;
542 }
543
544 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
545 {
546         if ( q && q->m_bouquet_name.length() )
547                 query = new eDVBDBBouquetQuery(this, source, q);
548         else
549                 query = new eDVBDBQuery(this, source, q);
550         return 0;
551 }
552
553 DEFINE_REF(eDVBDBQueryBase);
554
555 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
556         :m_db(db), m_query(query), m_source(source)
557 {
558 }
559
560 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
561 {
562         ePtr<eDVBService> a_service, b_service;
563         
564         int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
565         
566         if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
567         {
568                 if (m_db->getService(a, a_service))
569                         return 1;
570                 if (m_db->getService(b, b_service))
571                         return 1;
572         }
573         
574         switch (sortmode)
575         {
576         case eDVBChannelQuery::tName:
577                 return a_service->m_service_name_sort < b_service->m_service_name_sort;
578         case eDVBChannelQuery::tProvider:
579                 return a_service->m_provider_name < b_service->m_provider_name;
580         case eDVBChannelQuery::tType:
581                 return a.getServiceType() < b.getServiceType();
582         case eDVBChannelQuery::tBouquet:
583                 return 0;
584         case eDVBChannelQuery::tSatellitePosition:
585                 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
586         default:
587                 return 1;
588         }
589         return 0;
590 }
591
592 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
593         :eDVBDBQueryBase(db, source, query)
594 {
595         m_cursor = m_db->m_services.begin();
596 }
597
598 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
599 {
600         while (m_cursor != m_db->m_services.end())
601         {
602                 ref = m_cursor->first;
603
604                 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
605
606                 ++m_cursor;
607
608                 if (res)
609                         return 0;
610         }
611
612         ref = eServiceReferenceDVB();
613         return 1;
614 }
615
616 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
617         :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
618 {
619 }
620
621 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
622 {
623         eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
624         std::list<eServiceReference> &list = bouquet.m_services;
625         while (m_cursor != list.end())
626         {
627                 ref = *((eServiceReferenceDVB*)&(*m_cursor));
628
629                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
630                         m_db->m_services.find(ref);
631
632                 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
633
634                 ++m_cursor;
635
636                 if (res)
637                         return 0;
638         }
639
640         ref = eServiceReferenceDVB();
641
642         return 1;
643 }
644
645 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
646
647 static int decodeType(const std::string &type)
648 {
649         if (type == "name")
650                 return eDVBChannelQuery::tName;
651         else if (type == "provider")
652                 return eDVBChannelQuery::tProvider;
653         else if (type == "type")
654                 return eDVBChannelQuery::tType;
655         else if (type == "bouquet")
656                 return eDVBChannelQuery::tBouquet;
657         else if (type == "satellitePosition")
658                 return eDVBChannelQuery::tSatellitePosition;
659         else if (type == "channelID")
660                 return eDVBChannelQuery::tChannelID;
661         else
662                 return -1;
663 }
664
665         /* never, NEVER write a parser in C++! */
666 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
667 {
668         std::list<std::string>::const_iterator end_of_exp;
669         if (*begin == "(")
670         {
671                 end_of_exp = begin;
672                 while (end_of_exp != end)
673                         if (*end_of_exp == ")")
674                                 break;
675                         else
676                                 ++end_of_exp;
677         
678                 if (end_of_exp == end)
679                 {
680                         eDebug("expression parse: end of expression while searching for closing brace");
681                         return -1;
682                 }
683                 
684                 ++begin;
685                 // begin..end_of_exp
686                 int r = parseExpression(res, begin, end_of_exp);
687                 if (r)
688                         return r;
689                 ++end_of_exp;
690                 
691                         /* we had only one sub expression */
692                 if (end_of_exp == end)
693                 {
694 //                      eDebug("only one sub expression");
695                         return 0;
696                 }
697                 
698                         /* otherwise we have an operator here.. */
699                 
700                 ePtr<eDVBChannelQuery> r2 = res;
701                 res = new eDVBChannelQuery();
702                 res->m_sort = 0;
703                 res->m_p1 = r2;
704                 res->m_inverse = 0;
705                 r2 = 0;
706                 
707                 if (*end_of_exp == "||")
708                         res->m_type = eDVBChannelQuery::tOR;
709                 else if (*end_of_exp == "&&")
710                         res->m_type = eDVBChannelQuery::tAND;
711                 else
712                 {
713                         eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
714                         res = 0;
715                         return 1;
716                 }
717                 
718                 ++end_of_exp;
719                 
720                 return parseExpression(res->m_p2, end_of_exp, end);
721         }
722         
723         // "begin" <op> "end"
724         std::string type, op, val;
725         
726         res = new eDVBChannelQuery();
727         res->m_sort = 0;
728         
729         int cnt = 0;
730         while (begin != end)
731         {
732                 switch (cnt)
733                 {
734                 case 0:
735                         type = *begin;
736                         break;
737                 case 1:
738                         op = *begin;
739                         break;
740                 case 2:
741                         val = *begin;
742                         break;
743                 case 3:
744                         eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
745                         return 1;
746                 }
747                 ++begin;
748                 ++cnt;
749         }
750         
751         if (cnt != 3)
752         {
753                 eDebug("malformed query: missing stuff");
754                 res = 0;
755                 return 1;
756         }
757         
758         res->m_type = decodeType(type);
759         
760         if (res->m_type == -1)
761         {
762                 eDebug("malformed query: invalid type %s", type.c_str());
763                 res = 0;
764                 return 1;
765         }
766         
767         if (op == "==")
768                 res->m_inverse = 0;
769         else if (op == "!=")
770                 res->m_inverse = 1;
771         else
772         {
773                 eDebug("invalid operator %s", op.c_str());
774                 res = 0;
775                 return 1;
776         }
777         
778         res->m_string = val;
779         res->m_int = atoi(val.c_str());
780 //      res->m_channelid = eDVBChannelID(val);
781         
782         return 0;
783 }
784
785 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
786 {
787         std::list<std::string> tokens;
788         
789         std::string current_token;
790         std::string bouquet_name;
791
792 //      eDebug("splitting %s....", query.c_str());
793         unsigned int i = 0;
794         const char *splitchars="()";
795         int quotemode = 0, lastsplit = 0, lastalnum = 0;
796         while (i <= query.size())
797         {
798                 int c = (i < query.size()) ? query[i] : ' ';
799                 ++i;
800                 
801                 int issplit = !!strchr(splitchars, c);
802                 int isaln = isalnum(c);
803                 int iswhite = c == ' ';
804                 int isquot = c == '\"';
805                 
806                 if (quotemode)
807                 {
808                         iswhite = issplit = 0;
809                         isaln = lastalnum;
810                 }
811                 
812                 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
813                 {
814                         if (current_token.size())
815                                 tokens.push_back(current_token);
816                         current_token.clear();
817                 }
818                 
819                 if (!(iswhite || isquot))
820                         current_token += c;
821                 
822                 if (isquot)
823                         quotemode = !quotemode;
824                 lastsplit = issplit;
825                 lastalnum = isaln;
826         }
827         
828 //      for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
829 //      {
830 //              printf("%s\n", a->c_str());
831 //      }
832
833         int sort = eDVBChannelQuery::tName;
834                 /* check for "ORDER BY ..." */
835
836         while (tokens.size() > 2)
837         {
838                 std::list<std::string>::iterator it = tokens.end();
839                 --it; --it; --it;
840                 if (*it == "ORDER")
841                 {
842                         ++it;
843                         if (*it == "BY")
844                         {
845                                 ++it;
846                                 sort = decodeType(*it);
847                                 tokens.pop_back(); // ...
848                                 tokens.pop_back(); // BY
849                                 tokens.pop_back(); // ORDER
850                         } else
851                                 sort = -1;
852                 }
853                 else if (*it == "FROM")
854                 {
855                         ++it;
856                         if (*it == "BOUQUET")
857                         {
858                                 ++it;
859                                 bouquet_name = *it;
860                                 tokens.pop_back(); // ...
861                                 tokens.pop_back(); // FROM
862                                 tokens.pop_back(); // BOUQUET
863                         }
864                 }
865                 else
866                         break;
867         }
868
869         if (sort == -1)
870         {
871                 eWarning("ORDER BY .. string invalid.");
872                 res = 0;
873                 return -1;
874         }
875         
876 //      eDebug("sort by %d", sort);
877         
878                 /* now we recursivly parse that. */
879         int r = parseExpression(res, tokens.begin(), tokens.end());
880         
881         if (res)
882         {
883                 res->m_sort = sort;
884                 res->m_bouquet_name = bouquet_name;
885         }
886
887 //      eDebug("return: %d", r);
888         return r;
889 }
890
891 DEFINE_REF(eDVBChannelQuery);