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 = (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);
220 int eDVBService::getCachePID(cacheID id)
222 std::map<int, int>::iterator it = m_cache.find(id);
223 if ( it != m_cache.end() )
228 void eDVBService::setCachePID(cacheID id, int pid)
238 /* THIS CODE IS BAD. it should be replaced by somethine better. */
239 void eDVBDB::reloadServicelist()
241 eDebug("---- opening lame channel db");
242 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
246 if ( !stat("lamedb", &s) )
248 if ( !stat(CONFIGDIR"/enigma2", &s) )
250 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
257 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
259 eDebug("not a servicefile");
263 eDebug("reading services");
264 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
266 eDebug("services invalid, no transponders");
271 // clear all transponders
275 if (!fgets(line, 256, f))
277 if (!strcmp(line, "end\n"))
279 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
280 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
281 if (original_network_id == -1)
283 eDVBChannelID channelid = eDVBChannelID(
284 eDVBNamespace(dvb_namespace),
285 eTransportStreamID(transport_stream_id),
286 eOriginalNetworkID(original_network_id));
288 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
292 if (!strcmp(line, "/\n"))
296 eDVBFrontendParametersSatellite sat;
297 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
298 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
299 sat.frequency = frequency;
300 sat.symbol_rate = symbol_rate;
301 sat.polarisation = polarisation;
303 sat.orbital_position =
304 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
305 sat.inversion = inversion;
306 feparm->setDVBS(sat);
307 } else if (line[1]=='t')
309 eDVBFrontendParametersTerrestrial ter;
310 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
311 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);
312 ter.frequency = frequency;
313 ter.bandwidth = bandwidth;
314 ter.code_rate_HP = code_rate_HP;
315 ter.code_rate_LP = code_rate_LP;
316 ter.modulation = modulation;
317 ter.transmission_mode = transmission_mode;
318 ter.guard_interval = guard_interval;
319 ter.hierarchy = hierarchy;
320 ter.inversion = inversion;
321 feparm->setDVBT(ter);
322 } else if (line[1]=='c')
324 eDVBFrontendParametersCable cab;
325 int frequency, symbol_rate,
326 inversion=eDVBFrontendParametersCable::Inversion::Unknown,
327 modulation=eDVBFrontendParametersCable::Modulation::Auto,
328 fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
329 sscanf(line+2, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
330 cab.frequency = frequency;
331 cab.fec_inner = fec_inner;
332 cab.inversion = inversion;
333 cab.symbol_rate = symbol_rate;
334 cab.modulation = modulation;
335 feparm->setDVBC(cab);
338 addChannelToList(channelid, feparm);
341 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
343 eDebug("services invalid, no services");
347 // clear all services
353 if (!fgets(line, 256, f))
355 if (!strcmp(line, "end\n"))
358 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
359 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
360 if (service_number == -1)
362 ePtr<eDVBService> s = new eDVBService;
363 eServiceReferenceDVB ref =
364 eServiceReferenceDVB(
365 eDVBNamespace(dvb_namespace),
366 eTransportStreamID(transport_stream_id),
367 eOriginalNetworkID(original_network_id),
368 eServiceID(service_id),
373 line[strlen(line)-1]=0;
375 s->m_service_name = line;
380 line[strlen(line)-1]=0;
381 std::string str=line;
383 if (str[1]!=':') // old ... (only service_provider)
385 s->m_provider_name=line;
387 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
389 unsigned int c=str.find(',');
392 if (c == std::string::npos)
398 v=str.substr(2, c-2);
401 // eDebug("%c ... %s", p, v.c_str());
403 s->m_provider_name=v;
406 sscanf(v.c_str(), "%x", &s->m_flags);
410 sscanf(v.c_str(), "%02d%04x", &cid, &val);
415 sscanf(v.c_str(), "%04x", &val);
422 eDebug("loaded %d services", count);
427 void eDVBDB::saveServicelist()
429 eDebug("---- saving lame channel db");
430 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
431 int channels=0, services=0;
433 eFatal("couldn't save lame channel db!");
434 fprintf(f, "eDVB services /3/\n");
435 fprintf(f, "transponders\n");
436 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
437 i != m_channels.end(); ++i)
439 const eDVBChannelID &chid = i->first;
440 const channel &ch = i->second;
442 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
443 chid.transport_stream_id.get(), chid.original_network_id.get());
444 eDVBFrontendParametersSatellite sat;
445 eDVBFrontendParametersTerrestrial ter;
446 eDVBFrontendParametersCable cab;
447 if (!ch.m_frontendParameters->getDVBS(sat))
449 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
450 sat.frequency, sat.symbol_rate,
451 sat.polarisation, sat.fec,
452 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
455 if (!ch.m_frontendParameters->getDVBT(ter))
457 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
458 ter.frequency, ter.bandwidth, ter.code_rate_HP,
459 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
460 ter.guard_interval, ter.hierarchy, ter.inversion);
462 if (!ch.m_frontendParameters->getDVBC(cab))
464 fprintf(f, "\tc %d:%d:%d:%d:%d\n",
465 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
470 fprintf(f, "end\nservices\n");
472 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
473 i != m_services.end(); ++i)
475 const eServiceReferenceDVB &s = i->first;
476 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
477 s.getServiceID().get(), s.getDVBNamespace().get(),
478 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
482 fprintf(f, "%s\n", i->second->m_service_name.c_str());
484 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
487 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
488 ca != i->second->m_cache.end(); ++ca)
489 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
491 // write cached ca pids
492 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
493 ca != i->second->m_ca.end(); ++ca)
494 fprintf(f, ",C:%04x", *ca);
496 if (i->second->m_flags)
497 fprintf(f, ",f:%x", i->second->m_flags);
502 fprintf(f, "end\nHave a lot of bugs!\n");
503 eDebug("saved %d channels and %d services!", channels, services);
507 void eDVBDB::loadBouquet(const char *path)
509 std::string bouquet_name = path;
510 if (!bouquet_name.length())
512 eDebug("Bouquet load failed.. no path given..");
515 unsigned int pos = bouquet_name.rfind('/');
516 if ( pos != std::string::npos )
517 bouquet_name.erase(0, pos+1);
518 if (bouquet_name.empty())
520 eDebug("Bouquet load failed.. no filename given..");
523 eBouquet &bouquet = m_bouquets[bouquet_name];
524 bouquet.m_filename = bouquet_name;
525 std::list<eServiceReference> &list = bouquet.m_services;
528 std::string p = CONFIGDIR"/enigma2/";
530 eDebug("loading bouquet... %s", p.c_str());
531 FILE *fp=fopen(p.c_str(), "rt");
536 if ( !stat(path, &s) )
538 rename(path, p.c_str() );
542 eDebug("failed to open.");
543 if ( strstr(path, "bouquets.tv") )
545 eDebug("recreate bouquets.tv");
546 bouquet.m_bouquet_name="Bouquets (TV)";
547 bouquet.flushChanges();
549 else if ( strstr(path, "bouquets.radio") )
551 eDebug("recreate bouquets.radio");
552 bouquet.m_bouquet_name="Bouquets (Radio)";
553 bouquet.flushChanges();
558 bool read_descr=false;
559 eServiceReference *e = NULL;
562 if (!fgets(line, 256, fp))
564 line[strlen(line)-1]=0;
565 if (strlen(line) && line[strlen(line)-1]=='\r')
566 line[strlen(line)-1]=0;
571 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
573 int offs = line[8] == ':' ? 10 : 9;
574 eServiceReference tmp(line+offs);
575 if (tmp.type != eServiceReference::idDVB)
577 eDebug("only DVB Bouquets supported");
580 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
582 unsigned int pos = tmp.path.rfind('/');
583 if ( pos != std::string::npos )
584 tmp.path.erase(0, pos+1);
585 if (tmp.path.empty())
587 eDebug("Bouquet load failed.. no filename given..");
590 loadBouquet(tmp.path.c_str());
592 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], tmp.path.c_str());
600 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
605 else if (!strncmp(line, "#NAME ", 6))
606 bouquet.m_bouquet_name=line+6;
611 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
614 void eDVBDB::reloadBouquets()
617 loadBouquet("bouquets.tv");
618 loadBouquet("bouquets.radio");
619 // create default bouquets when missing
620 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
622 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
623 b.m_filename = "userbouquet.favourites.tv";
624 b.m_bouquet_name = "Favourites (TV)";
626 eServiceReference ref;
627 memset(ref.data, 0, sizeof(ref.data));
631 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
632 eBouquet &parent = m_bouquets["bouquets.tv"];
633 parent.m_services.push_back(ref);
634 parent.flushChanges();
636 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
638 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
639 b.m_filename = "userbouquet.favourites.radio";
640 b.m_bouquet_name = "Favourites (Radio)";
642 eServiceReference ref;
643 memset(ref.data, 0, sizeof(ref.data));
647 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
648 eBouquet &parent = m_bouquets["bouquets.radio"];
649 parent.m_services.push_back(ref);
650 parent.flushChanges();
654 eDVBDB *eDVBDB::instance;
667 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
671 ch.m_frontendParameters = feparm;
672 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
676 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
678 m_channels.erase(id);
682 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
684 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
685 if (i == m_channels.end())
690 parm = i->second.m_frontendParameters;
694 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
696 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
700 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
702 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
703 i = m_services.find(reference);
704 if (i == m_services.end())
713 RESULT eDVBDB::flush()
719 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
721 std::string str = ref.path;
724 eDebug("getBouquet failed.. no path given!");
727 unsigned int pos = str.find("FROM BOUQUET \"");
728 if ( pos != std::string::npos )
730 str.erase(0, pos+14);
732 if ( pos != std::string::npos )
739 eDebug("getBouquet failed.. couldn't parse bouquet name");
742 std::map<std::string, eBouquet>::iterator i =
743 m_bouquets.find(str);
744 if (i == m_bouquets.end())
749 bouquet = &i->second;
753 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
755 if ( source.path.find("FROM") != std::string::npos )
757 if ( source.path.find("BOUQUET") != std::string::npos )
758 query = new eDVBDBBouquetQuery(this, source, q);
759 else if ( source.path.find("SATELLITES") != std::string::npos )
760 query = new eDVBDBSatellitesQuery(this, source, q);
761 else if ( source.path.find("PROVIDERS") != std::string::npos )
762 query = new eDVBDBProvidersQuery(this, source, q);
764 eFatal("invalid query %s", source.toString().c_str());
767 query = new eDVBDBQuery(this, source, q);
771 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
774 eTransportStreamID Tsid(tsid);
775 eOriginalNetworkID Onid(onid);
776 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
777 sit != m_services.end(); ++sit)
779 if (sit->first.getTransportStreamID() == Tsid &&
780 sit->first.getOriginalNetworkID() == Onid &&
781 sit->first.getServiceID() == Sid)
784 return eServiceReference();
787 DEFINE_REF(eDVBDBQueryBase);
789 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
790 :m_db(db), m_query(query), m_source(source)
794 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
796 ePtr<eDVBService> a_service, b_service;
798 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
800 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
802 if (m_db->getService(a, a_service))
804 if (m_db->getService(b, b_service))
810 case eDVBChannelQuery::tName:
811 return a_service->m_service_name_sort < b_service->m_service_name_sort;
812 case eDVBChannelQuery::tProvider:
813 return a_service->m_provider_name < b_service->m_provider_name;
814 case eDVBChannelQuery::tType:
815 return a.getServiceType() < b.getServiceType();
816 case eDVBChannelQuery::tBouquet:
818 case eDVBChannelQuery::tSatellitePosition:
819 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
826 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
827 :eDVBDBQueryBase(db, source, query)
829 m_cursor = m_db->m_services.begin();
832 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
834 while (m_cursor != m_db->m_services.end())
836 ref = m_cursor->first;
838 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
846 ref.type = eServiceReference::idInvalid;
851 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
852 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
856 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
858 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
859 std::list<eServiceReference> &list = bouquet.m_services;
860 while (m_cursor != list.end())
862 ref = *((eServiceReferenceDVB*)&(*m_cursor));
864 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
865 m_db->m_services.find(ref);
867 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
875 ref.type = eServiceReference::idInvalid;
880 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
881 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
885 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
887 if (m_cursor != m_list.end())
892 ref.type = eServiceReference::idInvalid;
896 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
898 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
900 int x = (a.getDVBNamespace().get() >> 16);
901 int y = (b.getDVBNamespace().get() >> 16);
908 return a.name < b.name;
911 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
912 :eDVBDBListQuery(db, source, query)
914 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
915 it != m_db->m_services.end(); ++it)
917 int res = it->second->checkFilter(it->first, *query);
920 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
922 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
923 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
930 eServiceReferenceDVB ref;
931 ref.setDVBNamespace(dvbnamespace);
932 ref.flags=eServiceReference::flagDirectory;
934 snprintf(buf, 64, "(satellitePosition == %d) && ", dvbnamespace>>16);
936 ref.path=buf+source.path;
937 unsigned int pos=ref.path.find("FROM");
939 ref.path+="ORDER BY name";
940 // eDebug("ref.path now %s", ref.path.c_str());
941 m_list.push_back(ref);
943 ref.path=buf+source.path;
944 pos=ref.path.find("FROM");
945 ref.path.erase(pos+5);
946 ref.path+="PROVIDERS ORDER BY name";
947 // eDebug("ref.path now %s", ref.path.c_str());
948 m_list.push_back(ref);
952 m_cursor=m_list.begin();
955 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
956 :eDVBDBListQuery(db, source, query)
958 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
959 it != m_db->m_services.end(); ++it)
961 int res = it->second->checkFilter(it->first, *query);
966 const char *provider_name = it->second->m_provider_name.length() ?
967 it->second->m_provider_name.c_str() :
970 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
971 if (i->name == provider_name)
978 eServiceReferenceDVB ref;
980 ref.name=provider_name;
981 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
982 ref.path=buf+source.path;
983 unsigned int pos = ref.path.find("FROM");
984 ref.flags=eServiceReference::flagDirectory;
986 ref.path+="ORDER BY name";
987 // eDebug("ref.path now %s", ref.path.c_str());
988 m_list.push_back(ref);
992 m_cursor=m_list.begin();
995 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
997 static int decodeType(const std::string &type)
1000 return eDVBChannelQuery::tName;
1001 else if (type == "provider")
1002 return eDVBChannelQuery::tProvider;
1003 else if (type == "type")
1004 return eDVBChannelQuery::tType;
1005 else if (type == "bouquet")
1006 return eDVBChannelQuery::tBouquet;
1007 else if (type == "satellitePosition")
1008 return eDVBChannelQuery::tSatellitePosition;
1009 else if (type == "channelID")
1010 return eDVBChannelQuery::tChannelID;
1015 /* never, NEVER write a parser in C++! */
1016 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1018 std::list<std::string>::const_iterator end_of_exp;
1022 while (end_of_exp != end)
1023 if (*end_of_exp == ")")
1028 if (end_of_exp == end)
1030 eDebug("expression parse: end of expression while searching for closing brace");
1035 // begin..end_of_exp
1036 int r = parseExpression(res, begin, end_of_exp);
1041 /* we had only one sub expression */
1042 if (end_of_exp == end)
1044 // eDebug("only one sub expression");
1048 /* otherwise we have an operator here.. */
1050 ePtr<eDVBChannelQuery> r2 = res;
1051 res = new eDVBChannelQuery();
1057 if (*end_of_exp == "||")
1058 res->m_type = eDVBChannelQuery::tOR;
1059 else if (*end_of_exp == "&&")
1060 res->m_type = eDVBChannelQuery::tAND;
1063 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1070 return parseExpression(res->m_p2, end_of_exp, end);
1073 // "begin" <op> "end"
1074 std::string type, op, val;
1076 res = new eDVBChannelQuery();
1080 while (begin != end)
1094 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1103 eDebug("malformed query: missing stuff");
1108 res->m_type = decodeType(type);
1110 if (res->m_type == -1)
1112 eDebug("malformed query: invalid type %s", type.c_str());
1119 else if (op == "!=")
1123 eDebug("invalid operator %s", op.c_str());
1128 res->m_string = val;
1129 res->m_int = atoi(val.c_str());
1130 // res->m_channelid = eDVBChannelID(val);
1135 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1137 std::list<std::string> tokens;
1139 std::string current_token;
1140 std::string bouquet_name;
1142 // eDebug("splitting %s....", query.c_str());
1144 const char *splitchars="()";
1145 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1146 while (i <= query.size())
1148 int c = (i < query.size()) ? query[i] : ' ';
1151 int issplit = !!strchr(splitchars, c);
1152 int isaln = isalnum(c);
1153 int iswhite = c == ' ';
1154 int isquot = c == '\"';
1158 iswhite = issplit = 0;
1162 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1164 if (current_token.size())
1165 tokens.push_back(current_token);
1166 current_token.clear();
1169 if (!(iswhite || isquot))
1173 quotemode = !quotemode;
1174 lastsplit = issplit;
1178 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1180 // printf("%s\n", a->c_str());
1183 int sort = eDVBChannelQuery::tName;
1184 /* check for "ORDER BY ..." */
1186 std::list<std::string>::iterator it = tokens.begin();
1187 while (it != tokens.end())
1192 if (it != tokens.end() && *it == "BY")
1195 sort = decodeType(*it);
1200 else if (*it == "FROM")
1203 if (it != tokens.end() && *it == "BOUQUET")
1209 else if (it != tokens.end() && *it == "SATELLITES")
1211 else if (it != tokens.end() && *it == "PROVIDERS")
1215 eDebug("FROM unknown %s", (*it).c_str());
1225 eWarning("ORDER BY .. string invalid.");
1230 // eDebug("sort by %d", sort);
1232 /* now we recursivly parse that. */
1233 int r = parseExpression(res, tokens.begin(), tokens.end());
1238 res->m_bouquet_name = bouquet_name;
1241 // eDebug("return: %d", r);
1245 DEFINE_REF(eDVBChannelQuery);