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()
120 eDVBService::~eDVBService()
124 eDVBService &eDVBService::operator=(const eDVBService &s)
126 m_service_name = s.m_service_name;
127 m_service_name_sort = s.m_service_name_sort;
128 m_provider_name = s.m_provider_name;
135 void eDVBService::genSortName()
137 m_service_name_sort = removeDVBChars(m_service_name);
138 makeUpper(m_service_name_sort);
139 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
140 m_service_name_sort.erase(0, 1);
142 /* put unnamed services at the end, not at the beginning. */
143 if (m_service_name_sort.empty())
144 m_service_name_sort = "\xFF";
147 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
149 if (!ref.name.empty())
150 name = ref.name; // use renamed service name..
151 else if (!m_service_name.empty())
152 name = m_service_name;
158 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
160 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
163 bool eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
165 ePtr<eDVBResourceManager> res_mgr;
166 if ( eDVBResourceManager::getInstance( res_mgr ) )
167 eDebug("isPlayble... no res manager!!");
170 eDVBChannelID chid, chid_ignore;
171 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
172 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
173 return res_mgr->canAllocateChannel(chid, chid_ignore);
178 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
181 switch (query.m_type)
183 case eDVBChannelQuery::tName:
184 res = m_service_name_sort.find(query.m_string) != std::string::npos;
186 case eDVBChannelQuery::tProvider:
187 res = m_provider_name.find(query.m_string) != std::string::npos;
189 case eDVBChannelQuery::tType:
190 res = ref.getServiceType() == query.m_int;
192 case eDVBChannelQuery::tBouquet:
195 case eDVBChannelQuery::tSatellitePosition:
196 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
198 case eDVBChannelQuery::tChannelID:
201 ref.getChannelID(chid);
202 res = chid == query.m_channelid;
205 case eDVBChannelQuery::tAND:
206 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
208 case eDVBChannelQuery::tOR:
209 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
219 int eDVBService::getCachePID(cacheID id)
221 std::map<int, int>::iterator it = m_cache.find(id);
222 if ( it != m_cache.end() )
227 void eDVBService::setCachePID(cacheID id, int pid)
237 /* THIS CODE IS BAD. it should be replaced by somethine better. */
238 void eDVBDB::reloadServicelist()
240 eDebug("---- opening lame channel db");
241 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
245 if ( !stat("lamedb", &s) )
247 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
253 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
255 eDebug("not a servicefile");
259 eDebug("reading services");
260 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
262 eDebug("services invalid, no transponders");
267 // clear all transponders
271 if (!fgets(line, 256, f))
273 if (!strcmp(line, "end\n"))
275 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
276 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
277 if (original_network_id == -1)
279 eDVBChannelID channelid = eDVBChannelID(
280 eDVBNamespace(dvb_namespace),
281 eTransportStreamID(transport_stream_id),
282 eOriginalNetworkID(original_network_id));
284 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
288 if (!strcmp(line, "/\n"))
292 eDVBFrontendParametersSatellite sat;
293 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
294 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
295 sat.frequency = frequency;
296 sat.symbol_rate = symbol_rate;
297 sat.polarisation = polarisation;
299 sat.orbital_position =
300 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
301 sat.inversion = inversion;
302 feparm->setDVBS(sat);
303 } else if (line[1]=='t')
305 eDVBFrontendParametersTerrestrial ter;
306 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
307 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);
308 ter.frequency = frequency;
309 ter.bandwidth = bandwidth;
310 ter.code_rate_HP = code_rate_HP;
311 ter.code_rate_LP = code_rate_LP;
312 ter.modulation = modulation;
313 ter.transmission_mode = transmission_mode;
314 ter.guard_interval = guard_interval;
315 ter.hierarchy = hierarchy;
316 ter.inversion = inversion;
318 feparm->setDVBT(ter);
319 } else if (line[1]=='c')
321 int frequency, symbol_rate, inversion=0, modulation=3;
322 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
323 // t.setCable(frequency, symbol_rate, inversion, modulation);
326 addChannelToList(channelid, feparm);
329 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
331 eDebug("services invalid, no services");
335 // clear all services
341 if (!fgets(line, 256, f))
343 if (!strcmp(line, "end\n"))
346 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
347 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
348 if (service_number == -1)
350 ePtr<eDVBService> s = new eDVBService;
351 eServiceReferenceDVB ref =
352 eServiceReferenceDVB(
353 eDVBNamespace(dvb_namespace),
354 eTransportStreamID(transport_stream_id),
355 eOriginalNetworkID(original_network_id),
356 eServiceID(service_id),
361 line[strlen(line)-1]=0;
363 s->m_service_name = line;
368 line[strlen(line)-1]=0;
369 std::string str=line;
371 if (str[1]!=':') // old ... (only service_provider)
373 s->m_provider_name=line;
375 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
377 unsigned int c=str.find(',');
380 if (c == std::string::npos)
386 v=str.substr(2, c-2);
389 // eDebug("%c ... %s", p, v.c_str());
391 s->m_provider_name=v;
394 sscanf(v.c_str(), "%x", &s->m_flags);
398 sscanf(v.c_str(), "%02d%04x", &cid, &val);
403 sscanf(v.c_str(), "%04x", &val);
410 eDebug("loaded %d services", count);
415 void eDVBDB::saveServicelist()
417 eDebug("---- saving lame channel db");
418 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
419 int channels=0, services=0;
421 eFatal("couldn't save lame channel db!");
422 fprintf(f, "eDVB services /3/\n");
423 fprintf(f, "transponders\n");
424 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
425 i != m_channels.end(); ++i)
427 const eDVBChannelID &chid = i->first;
428 const channel &ch = i->second;
430 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
431 chid.transport_stream_id.get(), chid.original_network_id.get());
432 eDVBFrontendParametersSatellite sat;
433 eDVBFrontendParametersTerrestrial ter;
434 if (!ch.m_frontendParameters->getDVBS(sat))
436 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
437 sat.frequency, sat.symbol_rate,
438 sat.polarisation, sat.fec,
439 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
442 if (!ch.m_frontendParameters->getDVBT(ter))
444 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
445 ter.frequency, ter.bandwidth, ter.code_rate_HP,
446 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
447 ter.guard_interval, ter.hierarchy, ter.inversion);
452 fprintf(f, "end\nservices\n");
454 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
455 i != m_services.end(); ++i)
457 const eServiceReferenceDVB &s = i->first;
458 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
459 s.getServiceID().get(), s.getDVBNamespace().get(),
460 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
464 fprintf(f, "%s\n", i->second->m_service_name.c_str());
465 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
468 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
469 ca != i->second->m_cache.end(); ++ca)
470 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
472 // write cached ca pids
473 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
474 ca != i->second->m_ca.end(); ++ca)
475 fprintf(f, ",C:%04x", *ca);
480 fprintf(f, "end\nHave a lot of bugs!\n");
481 eDebug("saved %d channels and %d services!", channels, services);
485 void eDVBDB::loadBouquet(const char *path)
487 std::string bouquet_name = path;
488 if (!bouquet_name.length())
490 eDebug("Bouquet load failed.. no path given..");
493 unsigned int pos = bouquet_name.rfind('/');
494 if ( pos != std::string::npos )
495 bouquet_name.erase(0, pos+1);
496 if (bouquet_name.empty())
498 eDebug("Bouquet load failed.. no filename given..");
501 eBouquet &bouquet = m_bouquets[bouquet_name];
502 bouquet.m_filename = bouquet_name;
503 std::list<eServiceReference> &list = bouquet.m_services;
506 std::string p = CONFIGDIR"/enigma2/";
508 eDebug("loading bouquet... %s", p.c_str());
509 FILE *fp=fopen(p.c_str(), "rt");
514 if ( !stat(path, &s) )
516 rename(path, p.c_str() );
520 eDebug("failed to open.");
521 if ( strstr(path, "bouquets.tv") )
523 eDebug("recreate bouquets.tv");
524 bouquet.m_bouquet_name="Bouquets (TV)";
525 bouquet.flushChanges();
527 else if ( strstr(path, "bouquets.radio") )
529 eDebug("recreate bouquets.radio");
530 bouquet.m_bouquet_name="Bouquets (Radio)";
531 bouquet.flushChanges();
536 bool read_descr=false;
537 eServiceReference *e = NULL;
540 if (!fgets(line, 256, fp))
542 line[strlen(line)-1]=0;
543 if (strlen(line) && line[strlen(line)-1]=='\r')
544 line[strlen(line)-1]=0;
549 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
551 int offs = line[8] == ':' ? 10 : 9;
552 eServiceReference tmp(line+offs);
553 if (tmp.type != eServiceReference::idDVB)
555 eDebug("only DVB Bouquets supported");
558 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
560 unsigned int pos = tmp.path.rfind('/');
561 if ( pos != std::string::npos )
562 tmp.path.erase(0, pos+1);
563 if (tmp.path.empty())
565 eDebug("Bouquet load failed.. no filename given..");
568 loadBouquet(tmp.path.c_str());
570 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], tmp.path.c_str());
578 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
583 else if (!strncmp(line, "#NAME ", 6))
584 bouquet.m_bouquet_name=line+6;
589 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
592 void eDVBDB::reloadBouquets()
595 loadBouquet("bouquets.tv");
596 loadBouquet("bouquets.radio");
597 // create default bouquets when missing
598 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
600 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
601 b.m_filename = "userbouquet.favourites.tv";
602 b.m_bouquet_name = "Favourites (TV)";
604 eServiceReference ref;
605 memset(ref.data, 0, sizeof(ref.data));
609 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
610 eBouquet &parent = m_bouquets["bouquets.tv"];
611 parent.m_services.push_back(ref);
612 parent.flushChanges();
614 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
616 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
617 b.m_filename = "userbouquet.favourites.radio";
618 b.m_bouquet_name = "Favourites (Radio)";
620 eServiceReference ref;
621 memset(ref.data, 0, sizeof(ref.data));
625 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
626 eBouquet &parent = m_bouquets["bouquets.radio"];
627 parent.m_services.push_back(ref);
628 parent.flushChanges();
632 eDVBDB *eDVBDB::instance;
645 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
649 ch.m_frontendParameters = feparm;
650 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
654 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
656 m_channels.erase(id);
660 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
662 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
663 if (i == m_channels.end())
668 parm = i->second.m_frontendParameters;
672 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
674 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
678 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
680 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
681 i = m_services.find(reference);
682 if (i == m_services.end())
691 RESULT eDVBDB::flush()
697 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
699 std::string str = ref.path;
702 eDebug("getBouquet failed.. no path given!");
705 unsigned int pos = str.find("FROM BOUQUET \"");
706 if ( pos != std::string::npos )
708 str.erase(0, pos+14);
710 if ( pos != std::string::npos )
717 eDebug("getBouquet failed.. couldn't parse bouquet name");
720 std::map<std::string, eBouquet>::iterator i =
721 m_bouquets.find(str);
722 if (i == m_bouquets.end())
727 bouquet = &i->second;
731 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
733 if ( source.path.find("FROM") != std::string::npos )
735 if ( source.path.find("BOUQUET") != std::string::npos )
736 query = new eDVBDBBouquetQuery(this, source, q);
737 else if ( source.path.find("SATELLITES") != std::string::npos )
738 query = new eDVBDBSatellitesQuery(this, source, q);
739 else if ( source.path.find("PROVIDERS") != std::string::npos )
740 query = new eDVBDBProvidersQuery(this, source, q);
742 eFatal("invalid query %s", source.toString().c_str());
745 query = new eDVBDBQuery(this, source, q);
749 DEFINE_REF(eDVBDBQueryBase);
751 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
752 :m_db(db), m_query(query), m_source(source)
756 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
758 ePtr<eDVBService> a_service, b_service;
760 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
762 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
764 if (m_db->getService(a, a_service))
766 if (m_db->getService(b, b_service))
772 case eDVBChannelQuery::tName:
773 return a_service->m_service_name_sort < b_service->m_service_name_sort;
774 case eDVBChannelQuery::tProvider:
775 return a_service->m_provider_name < b_service->m_provider_name;
776 case eDVBChannelQuery::tType:
777 return a.getServiceType() < b.getServiceType();
778 case eDVBChannelQuery::tBouquet:
780 case eDVBChannelQuery::tSatellitePosition:
781 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
788 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
789 :eDVBDBQueryBase(db, source, query)
791 m_cursor = m_db->m_services.begin();
794 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
796 while (m_cursor != m_db->m_services.end())
798 ref = m_cursor->first;
800 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
808 ref.type = eServiceReference::idInvalid;
813 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
814 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
818 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
820 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
821 std::list<eServiceReference> &list = bouquet.m_services;
822 while (m_cursor != list.end())
824 ref = *((eServiceReferenceDVB*)&(*m_cursor));
826 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
827 m_db->m_services.find(ref);
829 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
837 ref.type = eServiceReference::idInvalid;
842 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
843 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
847 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
849 if (m_cursor != m_list.end())
854 ref.type = eServiceReference::idInvalid;
858 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
860 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
862 int x = (a.getDVBNamespace().get() >> 16);
863 int y = (b.getDVBNamespace().get() >> 16);
870 return a.name < b.name;
873 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
874 :eDVBDBListQuery(db, source, query)
876 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
877 it != m_db->m_services.end(); ++it)
879 int res = it->second->checkFilter(it->first, *query);
882 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
884 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
885 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
892 eServiceReferenceDVB ref;
893 ref.setDVBNamespace(dvbnamespace);
894 ref.flags=eServiceReference::flagDirectory;
896 snprintf(buf, 64, "(satellitePosition == %d) && ", dvbnamespace>>16);
898 ref.path=buf+source.path;
899 unsigned int pos=ref.path.find("FROM");
901 ref.path+="ORDER BY name";
902 // eDebug("ref.path now %s", ref.path.c_str());
903 m_list.push_back(ref);
905 ref.path=buf+source.path;
906 pos=ref.path.find("FROM");
907 ref.path.erase(pos+5);
908 ref.path+="PROVIDERS ORDER BY name";
909 // eDebug("ref.path now %s", ref.path.c_str());
910 m_list.push_back(ref);
914 m_cursor=m_list.begin();
917 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
918 :eDVBDBListQuery(db, source, query)
920 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
921 it != m_db->m_services.end(); ++it)
923 int res = it->second->checkFilter(it->first, *query);
928 const char *provider_name = it->second->m_provider_name.length() ?
929 it->second->m_provider_name.c_str() :
932 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
933 if (i->name == provider_name)
940 eServiceReferenceDVB ref;
942 ref.name=provider_name;
943 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
944 ref.path=buf+source.path;
945 unsigned int pos = ref.path.find("FROM");
946 ref.flags=eServiceReference::flagDirectory;
948 ref.path+="ORDER BY name";
949 // eDebug("ref.path now %s", ref.path.c_str());
950 m_list.push_back(ref);
954 m_cursor=m_list.begin();
957 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
959 static int decodeType(const std::string &type)
962 return eDVBChannelQuery::tName;
963 else if (type == "provider")
964 return eDVBChannelQuery::tProvider;
965 else if (type == "type")
966 return eDVBChannelQuery::tType;
967 else if (type == "bouquet")
968 return eDVBChannelQuery::tBouquet;
969 else if (type == "satellitePosition")
970 return eDVBChannelQuery::tSatellitePosition;
971 else if (type == "channelID")
972 return eDVBChannelQuery::tChannelID;
977 /* never, NEVER write a parser in C++! */
978 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
980 std::list<std::string>::const_iterator end_of_exp;
984 while (end_of_exp != end)
985 if (*end_of_exp == ")")
990 if (end_of_exp == end)
992 eDebug("expression parse: end of expression while searching for closing brace");
998 int r = parseExpression(res, begin, end_of_exp);
1003 /* we had only one sub expression */
1004 if (end_of_exp == end)
1006 // eDebug("only one sub expression");
1010 /* otherwise we have an operator here.. */
1012 ePtr<eDVBChannelQuery> r2 = res;
1013 res = new eDVBChannelQuery();
1019 if (*end_of_exp == "||")
1020 res->m_type = eDVBChannelQuery::tOR;
1021 else if (*end_of_exp == "&&")
1022 res->m_type = eDVBChannelQuery::tAND;
1025 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1032 return parseExpression(res->m_p2, end_of_exp, end);
1035 // "begin" <op> "end"
1036 std::string type, op, val;
1038 res = new eDVBChannelQuery();
1042 while (begin != end)
1056 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1065 eDebug("malformed query: missing stuff");
1070 res->m_type = decodeType(type);
1072 if (res->m_type == -1)
1074 eDebug("malformed query: invalid type %s", type.c_str());
1081 else if (op == "!=")
1085 eDebug("invalid operator %s", op.c_str());
1090 res->m_string = val;
1091 res->m_int = atoi(val.c_str());
1092 // res->m_channelid = eDVBChannelID(val);
1097 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1099 std::list<std::string> tokens;
1101 std::string current_token;
1102 std::string bouquet_name;
1104 // eDebug("splitting %s....", query.c_str());
1106 const char *splitchars="()";
1107 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1108 while (i <= query.size())
1110 int c = (i < query.size()) ? query[i] : ' ';
1113 int issplit = !!strchr(splitchars, c);
1114 int isaln = isalnum(c);
1115 int iswhite = c == ' ';
1116 int isquot = c == '\"';
1120 iswhite = issplit = 0;
1124 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1126 if (current_token.size())
1127 tokens.push_back(current_token);
1128 current_token.clear();
1131 if (!(iswhite || isquot))
1135 quotemode = !quotemode;
1136 lastsplit = issplit;
1140 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1142 // printf("%s\n", a->c_str());
1145 int sort = eDVBChannelQuery::tName;
1146 /* check for "ORDER BY ..." */
1148 std::list<std::string>::iterator it = tokens.begin();
1149 while (it != tokens.end())
1154 if (it != tokens.end() && *it == "BY")
1157 sort = decodeType(*it);
1162 else if (*it == "FROM")
1165 if (it != tokens.end() && *it == "BOUQUET")
1171 else if (it != tokens.end() && *it == "SATELLITES")
1173 else if (it != tokens.end() && *it == "PROVIDERS")
1177 eDebug("FROM unknown %s", (*it).c_str());
1187 eWarning("ORDER BY .. string invalid.");
1192 // eDebug("sort by %d", sort);
1194 /* now we recursivly parse that. */
1195 int r = parseExpression(res, tokens.begin(), tokens.end());
1200 res->m_bouquet_name = bouquet_name;
1203 // eDebug("return: %d", r);
1207 DEFINE_REF(eDVBChannelQuery);