2 #include <lib/dvb/db.h>
3 #include <lib/dvb/dvb.h>
4 #include <lib/dvb/frontend.h>
5 #include <lib/dvb/epgcache.h>
6 #include <lib/base/eerror.h>
7 #include <lib/base/estring.h>
8 #include <dvbsi++/service_description_section.h>
9 #include <dvbsi++/descriptor_tag.h>
10 #include <dvbsi++/service_descriptor.h>
11 #include <dvbsi++/satellite_delivery_system_descriptor.h>
13 DEFINE_REF(eDVBService);
15 RESULT eBouquet::addService(const eServiceReference &ref)
18 std::find(m_services.begin(), m_services.end(), ref);
19 if ( it != m_services.end() )
21 m_services.push_back(ref);
25 RESULT eBouquet::removeService(const eServiceReference &ref)
28 std::find(m_services.begin(), m_services.end(), ref);
29 if ( it == m_services.end() )
35 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
37 if ( pos < 0 || pos >= m_services.size() )
40 list::iterator source=m_services.end();
41 list::iterator dest=m_services.end();
43 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
45 if (dest == m_services.end() && !--pos)
52 if (dest != m_services.end() && source != m_services.end())
55 if (dest == m_services.end() || source == m_services.end() || source == dest)
57 while (source != dest)
60 std::iter_swap(source++, source);
62 std::iter_swap(source--, source);
67 RESULT eBouquet::flushChanges()
69 FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
72 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
74 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
76 eServiceReference tmp = *i;
77 std::string str = tmp.path;
78 if ( (i->flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
80 unsigned int p1 = str.find("FROM BOUQUET \"");
81 if (p1 == std::string::npos)
83 eDebug("doof... kaputt");
88 if (p1 == std::string::npos)
90 eDebug("doof2... kaputt");
96 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
98 if ( i->name.length() )
99 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
106 eDebug("couldn't write file %s", m_filename.c_str());
110 RESULT eBouquet::setListName(const std::string &name)
112 m_bouquet_name = name;
116 eDVBService::eDVBService()
121 eDVBService::~eDVBService()
125 eDVBService &eDVBService::operator=(const eDVBService &s)
127 m_service_name = s.m_service_name;
128 m_service_name_sort = s.m_service_name_sort;
129 m_provider_name = s.m_provider_name;
136 void eDVBService::genSortName()
138 m_service_name_sort = removeDVBChars(m_service_name);
139 makeUpper(m_service_name_sort);
140 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
141 m_service_name_sort.erase(0, 1);
143 /* put unnamed services at the end, not at the beginning. */
144 if (m_service_name_sort.empty())
145 m_service_name_sort = "\xFF";
148 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
150 if (!ref.name.empty())
151 name = ref.name; // use renamed service name..
152 else if (!m_service_name.empty())
153 name = m_service_name;
159 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
161 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
164 bool eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
166 ePtr<eDVBResourceManager> res_mgr;
167 if ( eDVBResourceManager::getInstance( res_mgr ) )
168 eDebug("isPlayble... no res manager!!");
171 eDVBChannelID chid, chid_ignore;
172 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
173 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
174 return res_mgr->canAllocateChannel(chid, chid_ignore);
179 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
182 switch (query.m_type)
184 case eDVBChannelQuery::tName:
185 res = m_service_name_sort.find(query.m_string) != std::string::npos;
187 case eDVBChannelQuery::tProvider:
188 res = m_provider_name.find(query.m_string) != std::string::npos;
190 case eDVBChannelQuery::tType:
191 res = ref.getServiceType() == query.m_int;
193 case eDVBChannelQuery::tBouquet:
196 case eDVBChannelQuery::tSatellitePosition:
197 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == query.m_int;
199 case eDVBChannelQuery::tChannelID:
202 ref.getChannelID(chid);
203 res = chid == query.m_channelid;
206 case eDVBChannelQuery::tAND:
207 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
209 case eDVBChannelQuery::tOR:
210 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
212 case eDVBChannelQuery::tAny:
223 int eDVBService::getCachePID(cacheID id)
225 std::map<int, int>::iterator it = m_cache.find(id);
226 if ( it != m_cache.end() )
231 void eDVBService::setCachePID(cacheID id, int pid)
241 /* THIS CODE IS BAD. it should be replaced by somethine better. */
242 void eDVBDB::reloadServicelist()
244 eDebug("---- opening lame channel db");
245 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
249 if ( !stat("lamedb", &s) )
251 if ( !stat(CONFIGDIR"/enigma2", &s) )
253 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
260 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
262 eDebug("not a servicefile");
266 eDebug("reading services");
267 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
269 eDebug("services invalid, no transponders");
274 // clear all transponders
278 if (!fgets(line, 256, f))
280 if (!strcmp(line, "end\n"))
282 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
283 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
284 if (original_network_id == -1)
286 eDVBChannelID channelid = eDVBChannelID(
287 eDVBNamespace(dvb_namespace),
288 eTransportStreamID(transport_stream_id),
289 eOriginalNetworkID(original_network_id));
291 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
295 if (!strcmp(line, "/\n"))
299 eDVBFrontendParametersSatellite sat;
300 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
301 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
302 sat.frequency = frequency;
303 sat.symbol_rate = symbol_rate;
304 sat.polarisation = polarisation;
306 sat.orbital_position =
307 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
308 sat.inversion = inversion;
309 feparm->setDVBS(sat);
310 } else if (line[1]=='t')
312 eDVBFrontendParametersTerrestrial ter;
313 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
314 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);
315 ter.frequency = frequency;
316 ter.bandwidth = bandwidth;
317 ter.code_rate_HP = code_rate_HP;
318 ter.code_rate_LP = code_rate_LP;
319 ter.modulation = modulation;
320 ter.transmission_mode = transmission_mode;
321 ter.guard_interval = guard_interval;
322 ter.hierarchy = hierarchy;
323 ter.inversion = inversion;
324 feparm->setDVBT(ter);
325 } else if (line[1]=='c')
327 eDVBFrontendParametersCable cab;
328 int frequency, symbol_rate,
329 inversion=eDVBFrontendParametersCable::Inversion::Unknown,
330 modulation=eDVBFrontendParametersCable::Modulation::Auto,
331 fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
332 sscanf(line+2, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
333 cab.frequency = frequency;
334 cab.fec_inner = fec_inner;
335 cab.inversion = inversion;
336 cab.symbol_rate = symbol_rate;
337 cab.modulation = modulation;
338 feparm->setDVBC(cab);
341 addChannelToList(channelid, feparm);
344 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
346 eDebug("services invalid, no services");
350 // clear all services
356 if (!fgets(line, 256, f))
358 if (!strcmp(line, "end\n"))
361 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
362 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
363 if (service_number == -1)
365 ePtr<eDVBService> s = new eDVBService;
366 eServiceReferenceDVB ref =
367 eServiceReferenceDVB(
368 eDVBNamespace(dvb_namespace),
369 eTransportStreamID(transport_stream_id),
370 eOriginalNetworkID(original_network_id),
371 eServiceID(service_id),
376 line[strlen(line)-1]=0;
378 s->m_service_name = line;
383 line[strlen(line)-1]=0;
384 std::string str=line;
386 if (str[1]!=':') // old ... (only service_provider)
388 s->m_provider_name=line;
390 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
392 unsigned int c=str.find(',');
395 if (c == std::string::npos)
401 v=str.substr(2, c-2);
404 // eDebug("%c ... %s", p, v.c_str());
406 s->m_provider_name=v;
409 sscanf(v.c_str(), "%x", &s->m_flags);
413 sscanf(v.c_str(), "%02d%04x", &cid, &val);
418 sscanf(v.c_str(), "%04x", &val);
425 eDebug("loaded %d services", count);
430 void eDVBDB::saveServicelist()
432 eDebug("---- saving lame channel db");
433 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
434 int channels=0, services=0;
436 eFatal("couldn't save lame channel db!");
437 fprintf(f, "eDVB services /3/\n");
438 fprintf(f, "transponders\n");
439 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
440 i != m_channels.end(); ++i)
442 const eDVBChannelID &chid = i->first;
443 const channel &ch = i->second;
445 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
446 chid.transport_stream_id.get(), chid.original_network_id.get());
447 eDVBFrontendParametersSatellite sat;
448 eDVBFrontendParametersTerrestrial ter;
449 eDVBFrontendParametersCable cab;
450 if (!ch.m_frontendParameters->getDVBS(sat))
452 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
453 sat.frequency, sat.symbol_rate,
454 sat.polarisation, sat.fec,
455 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
458 if (!ch.m_frontendParameters->getDVBT(ter))
460 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
461 ter.frequency, ter.bandwidth, ter.code_rate_HP,
462 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
463 ter.guard_interval, ter.hierarchy, ter.inversion);
465 if (!ch.m_frontendParameters->getDVBC(cab))
467 fprintf(f, "\tc %d:%d:%d:%d:%d\n",
468 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
473 fprintf(f, "end\nservices\n");
475 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
476 i != m_services.end(); ++i)
478 const eServiceReferenceDVB &s = i->first;
479 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
480 s.getServiceID().get(), s.getDVBNamespace().get(),
481 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
485 fprintf(f, "%s\n", i->second->m_service_name.c_str());
487 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
490 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
491 ca != i->second->m_cache.end(); ++ca)
492 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
494 // write cached ca pids
495 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
496 ca != i->second->m_ca.end(); ++ca)
497 fprintf(f, ",C:%04x", *ca);
499 if (i->second->m_flags)
500 fprintf(f, ",f:%x", i->second->m_flags);
505 fprintf(f, "end\nHave a lot of bugs!\n");
506 eDebug("saved %d channels and %d services!", channels, services);
510 void eDVBDB::loadBouquet(const char *path)
512 std::string bouquet_name = path;
513 if (!bouquet_name.length())
515 eDebug("Bouquet load failed.. no path given..");
518 unsigned int pos = bouquet_name.rfind('/');
519 if ( pos != std::string::npos )
520 bouquet_name.erase(0, pos+1);
521 if (bouquet_name.empty())
523 eDebug("Bouquet load failed.. no filename given..");
526 eBouquet &bouquet = m_bouquets[bouquet_name];
527 bouquet.m_filename = bouquet_name;
528 std::list<eServiceReference> &list = bouquet.m_services;
531 std::string p = CONFIGDIR"/enigma2/";
533 eDebug("loading bouquet... %s", p.c_str());
534 FILE *fp=fopen(p.c_str(), "rt");
539 if ( !stat(path, &s) )
541 rename(path, p.c_str() );
545 eDebug("failed to open.");
546 if ( strstr(path, "bouquets.tv") )
548 eDebug("recreate bouquets.tv");
549 bouquet.m_bouquet_name="Bouquets (TV)";
550 bouquet.flushChanges();
552 else if ( strstr(path, "bouquets.radio") )
554 eDebug("recreate bouquets.radio");
555 bouquet.m_bouquet_name="Bouquets (Radio)";
556 bouquet.flushChanges();
561 bool read_descr=false;
562 eServiceReference *e = NULL;
565 if (!fgets(line, 256, fp))
567 line[strlen(line)-1]=0;
568 if (strlen(line) && line[strlen(line)-1]=='\r')
569 line[strlen(line)-1]=0;
574 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
576 int offs = line[8] == ':' ? 10 : 9;
577 eServiceReference tmp(line+offs);
578 if (tmp.type != eServiceReference::idDVB)
580 eDebug("only DVB Bouquets supported");
583 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
585 unsigned int pos = tmp.path.rfind('/');
586 if ( pos != std::string::npos )
587 tmp.path.erase(0, pos+1);
588 if (tmp.path.empty())
590 eDebug("Bouquet load failed.. no filename given..");
593 loadBouquet(tmp.path.c_str());
595 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], tmp.path.c_str());
603 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
608 else if (!strncmp(line, "#NAME ", 6))
609 bouquet.m_bouquet_name=line+6;
614 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
617 void eDVBDB::reloadBouquets()
620 loadBouquet("bouquets.tv");
621 loadBouquet("bouquets.radio");
622 // create default bouquets when missing
623 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
625 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
626 b.m_filename = "userbouquet.favourites.tv";
627 b.m_bouquet_name = "Favourites (TV)";
629 eServiceReference ref;
630 memset(ref.data, 0, sizeof(ref.data));
634 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
635 eBouquet &parent = m_bouquets["bouquets.tv"];
636 parent.m_services.push_back(ref);
637 parent.flushChanges();
639 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
641 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
642 b.m_filename = "userbouquet.favourites.radio";
643 b.m_bouquet_name = "Favourites (Radio)";
645 eServiceReference ref;
646 memset(ref.data, 0, sizeof(ref.data));
650 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
651 eBouquet &parent = m_bouquets["bouquets.radio"];
652 parent.m_services.push_back(ref);
653 parent.flushChanges();
657 eDVBDB *eDVBDB::instance;
670 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
674 ch.m_frontendParameters = feparm;
675 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
679 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
681 m_channels.erase(id);
685 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
687 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
688 if (i == m_channels.end())
693 parm = i->second.m_frontendParameters;
697 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
699 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
703 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
705 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
706 i = m_services.find(reference);
707 if (i == m_services.end())
716 RESULT eDVBDB::flush()
722 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
724 std::string str = ref.path;
727 eDebug("getBouquet failed.. no path given!");
730 unsigned int pos = str.find("FROM BOUQUET \"");
731 if ( pos != std::string::npos )
733 str.erase(0, pos+14);
735 if ( pos != std::string::npos )
742 eDebug("getBouquet failed.. couldn't parse bouquet name");
745 std::map<std::string, eBouquet>::iterator i =
746 m_bouquets.find(str);
747 if (i == m_bouquets.end())
752 bouquet = &i->second;
756 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
758 if ( source.path.find("FROM") != std::string::npos )
760 if ( source.path.find("BOUQUET") != std::string::npos )
761 query = new eDVBDBBouquetQuery(this, source, q);
762 else if ( source.path.find("SATELLITES") != std::string::npos )
763 query = new eDVBDBSatellitesQuery(this, source, q);
764 else if ( source.path.find("PROVIDERS") != std::string::npos )
765 query = new eDVBDBProvidersQuery(this, source, q);
767 eFatal("invalid query %s", source.toString().c_str());
770 query = new eDVBDBQuery(this, source, q);
774 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
777 eTransportStreamID Tsid(tsid);
778 eOriginalNetworkID Onid(onid);
779 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
780 sit != m_services.end(); ++sit)
782 if (sit->first.getTransportStreamID() == Tsid &&
783 sit->first.getOriginalNetworkID() == Onid &&
784 sit->first.getServiceID() == Sid)
787 return eServiceReference();
790 DEFINE_REF(eDVBDBQueryBase);
792 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
793 :m_db(db), m_query(query), m_source(source)
797 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
799 ePtr<eDVBService> a_service, b_service;
801 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
803 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
805 if (m_db->getService(a, a_service))
807 if (m_db->getService(b, b_service))
813 case eDVBChannelQuery::tName:
814 return a_service->m_service_name_sort < b_service->m_service_name_sort;
815 case eDVBChannelQuery::tProvider:
816 return a_service->m_provider_name < b_service->m_provider_name;
817 case eDVBChannelQuery::tType:
818 return a.getServiceType() < b.getServiceType();
819 case eDVBChannelQuery::tBouquet:
821 case eDVBChannelQuery::tSatellitePosition:
822 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
829 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
830 :eDVBDBQueryBase(db, source, query)
832 m_cursor = m_db->m_services.begin();
835 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
837 while (m_cursor != m_db->m_services.end())
839 ref = m_cursor->first;
841 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
849 ref.type = eServiceReference::idInvalid;
854 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
855 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
859 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
861 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
862 std::list<eServiceReference> &list = bouquet.m_services;
863 while (m_cursor != list.end())
865 ref = *((eServiceReferenceDVB*)&(*m_cursor));
867 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
868 m_db->m_services.find(ref);
870 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
878 ref.type = eServiceReference::idInvalid;
883 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
884 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
888 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
890 if (m_cursor != m_list.end())
895 ref.type = eServiceReference::idInvalid;
899 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
901 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
903 int x = (a.getDVBNamespace().get() >> 16);
904 int y = (b.getDVBNamespace().get() >> 16);
911 return a.name < b.name;
914 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
915 :eDVBDBListQuery(db, source, query)
917 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
918 it != m_db->m_services.end(); ++it)
920 int res = it->second->checkFilter(it->first, *query);
923 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
925 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
926 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
933 eServiceReferenceDVB ref;
934 ref.setDVBNamespace(dvbnamespace);
935 ref.flags=eServiceReference::flagDirectory;
937 snprintf(buf, 64, "(satellitePosition == %d) && ", dvbnamespace>>16);
939 ref.path=buf+source.path;
940 unsigned int pos=ref.path.find("FROM");
942 ref.path+="ORDER BY name";
943 // eDebug("ref.path now %s", ref.path.c_str());
944 m_list.push_back(ref);
946 ref.path=buf+source.path;
947 pos=ref.path.find("FROM");
948 ref.path.erase(pos+5);
949 ref.path+="PROVIDERS ORDER BY name";
950 // eDebug("ref.path now %s", ref.path.c_str());
951 m_list.push_back(ref);
955 m_cursor=m_list.begin();
958 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
959 :eDVBDBListQuery(db, source, query)
961 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
962 it != m_db->m_services.end(); ++it)
964 int res = it->second->checkFilter(it->first, *query);
969 const char *provider_name = it->second->m_provider_name.length() ?
970 it->second->m_provider_name.c_str() :
973 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
974 if (i->name == provider_name)
981 eServiceReferenceDVB ref;
983 ref.name=provider_name;
984 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
985 ref.path=buf+source.path;
986 unsigned int pos = ref.path.find("FROM");
987 ref.flags=eServiceReference::flagDirectory;
989 ref.path+="ORDER BY name";
990 // eDebug("ref.path now %s", ref.path.c_str());
991 m_list.push_back(ref);
995 m_cursor=m_list.begin();
998 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1000 static int decodeType(const std::string &type)
1003 return eDVBChannelQuery::tName;
1004 else if (type == "provider")
1005 return eDVBChannelQuery::tProvider;
1006 else if (type == "type")
1007 return eDVBChannelQuery::tType;
1008 else if (type == "bouquet")
1009 return eDVBChannelQuery::tBouquet;
1010 else if (type == "satellitePosition")
1011 return eDVBChannelQuery::tSatellitePosition;
1012 else if (type == "channelID")
1013 return eDVBChannelQuery::tChannelID;
1018 /* never, NEVER write a parser in C++! */
1019 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1021 std::list<std::string>::const_iterator end_of_exp;
1025 eDebug("empty expression!");
1032 while (end_of_exp != end)
1033 if (*end_of_exp == ")")
1038 if (end_of_exp == end)
1040 eDebug("expression parse: end of expression while searching for closing brace");
1045 // begin..end_of_exp
1046 int r = parseExpression(res, begin, end_of_exp);
1051 /* we had only one sub expression */
1052 if (end_of_exp == end)
1054 // eDebug("only one sub expression");
1058 /* otherwise we have an operator here.. */
1060 ePtr<eDVBChannelQuery> r2 = res;
1061 res = new eDVBChannelQuery();
1067 if (*end_of_exp == "||")
1068 res->m_type = eDVBChannelQuery::tOR;
1069 else if (*end_of_exp == "&&")
1070 res->m_type = eDVBChannelQuery::tAND;
1073 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1080 return parseExpression(res->m_p2, end_of_exp, end);
1083 // "begin" <op> "end"
1084 std::string type, op, val;
1086 res = new eDVBChannelQuery();
1090 while (begin != end)
1104 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1113 eDebug("malformed query: missing stuff");
1118 res->m_type = decodeType(type);
1120 if (res->m_type == -1)
1122 eDebug("malformed query: invalid type %s", type.c_str());
1129 else if (op == "!=")
1133 eDebug("invalid operator %s", op.c_str());
1138 res->m_string = val;
1139 res->m_int = atoi(val.c_str());
1140 // res->m_channelid = eDVBChannelID(val);
1145 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1147 std::list<std::string> tokens;
1149 std::string current_token;
1150 std::string bouquet_name;
1152 // eDebug("splitting %s....", query.c_str());
1154 const char *splitchars="()";
1155 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1156 while (i <= query.size())
1158 int c = (i < query.size()) ? query[i] : ' ';
1161 int issplit = !!strchr(splitchars, c);
1162 int isaln = isalnum(c);
1163 int iswhite = c == ' ';
1164 int isquot = c == '\"';
1168 iswhite = issplit = 0;
1172 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1174 if (current_token.size())
1175 tokens.push_back(current_token);
1176 current_token.clear();
1179 if (!(iswhite || isquot))
1183 quotemode = !quotemode;
1184 lastsplit = issplit;
1188 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1190 // printf("%s\n", a->c_str());
1193 int sort = eDVBChannelQuery::tName;
1194 /* check for "ORDER BY ..." */
1196 std::list<std::string>::iterator it = tokens.begin();
1197 while (it != tokens.end())
1202 if (it != tokens.end() && *it == "BY")
1205 sort = decodeType(*it);
1210 else if (*it == "FROM")
1213 if (it != tokens.end() && *it == "BOUQUET")
1219 else if (it != tokens.end() && *it == "SATELLITES")
1221 else if (it != tokens.end() && *it == "PROVIDERS")
1225 eDebug("FROM unknown %s", (*it).c_str());
1235 eWarning("ORDER BY .. string invalid.");
1240 // eDebug("sort by %d", sort);
1242 /* now we recursivly parse that. */
1243 int r = parseExpression(res, tokens.begin(), tokens.end());
1245 /* we have an empty (but valid!) expression */
1248 res = new eDVBChannelQuery();
1249 res->m_type = eDVBChannelQuery::tAny;
1255 res->m_bouquet_name = bouquet_name;
1258 // eDebug("return: %d", r);
1262 DEFINE_REF(eDVBChannelQuery);