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(m_path.c_str(), "wt");
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_path.c_str());
110 eDVBService::eDVBService()
114 eDVBService::~eDVBService()
118 eDVBService &eDVBService::operator=(const eDVBService &s)
120 m_service_name = s.m_service_name;
121 m_service_name_sort = s.m_service_name_sort;
122 m_provider_name = s.m_provider_name;
129 void eDVBService::genSortName()
131 m_service_name_sort = removeDVBChars(m_service_name);
132 makeUpper(m_service_name_sort);
133 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
134 m_service_name_sort.erase(0, 1);
136 /* put unnamed services at the end, not at the beginning. */
137 if (m_service_name_sort.empty())
138 m_service_name_sort = "\xFF";
141 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
143 if (!ref.name.empty())
144 name = ref.name; // use renamed service name..
145 else if (!m_service_name.empty())
146 name = m_service_name;
152 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr)
155 return eEPGCache::getInstance()->lookupEventTime(ref, t, ptr);
158 bool eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
160 ePtr<eDVBResourceManager> res_mgr;
161 if ( eDVBResourceManager::getInstance( res_mgr ) )
162 eDebug("isPlayble... no res manager!!");
165 eDVBChannelID chid, chid_ignore;
166 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
167 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
168 return res_mgr->canAllocateChannel(chid, chid_ignore);
173 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
176 switch (query.m_type)
178 case eDVBChannelQuery::tName:
179 res = m_service_name_sort.find(query.m_string) != std::string::npos;
181 case eDVBChannelQuery::tProvider:
182 res = m_provider_name.find(query.m_string) != std::string::npos;
184 case eDVBChannelQuery::tType:
185 res = ref.getServiceType() == query.m_int;
187 case eDVBChannelQuery::tBouquet:
190 case eDVBChannelQuery::tSatellitePosition:
191 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
193 case eDVBChannelQuery::tChannelID:
196 ref.getChannelID(chid);
197 res = chid == query.m_channelid;
200 case eDVBChannelQuery::tAND:
201 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
203 case eDVBChannelQuery::tOR:
204 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
214 int eDVBService::getCachePID(cacheID id)
216 std::map<int, int>::iterator it = m_cache.find(id);
217 if ( it != m_cache.end() )
222 void eDVBService::setCachePID(cacheID id, int pid)
229 /* THIS CODE IS BAD. it should be replaced by somethine better. */
232 eDebug("---- opening lame channel db");
233 FILE *f=fopen("lamedb", "rt");
237 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
239 eDebug("not a servicefile");
243 eDebug("reading services");
244 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
246 eDebug("services invalid, no transponders");
251 // clear all transponders
255 if (!fgets(line, 256, f))
257 if (!strcmp(line, "end\n"))
259 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
260 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
261 if (original_network_id == -1)
263 eDVBChannelID channelid = eDVBChannelID(
264 eDVBNamespace(dvb_namespace),
265 eTransportStreamID(transport_stream_id),
266 eOriginalNetworkID(original_network_id));
268 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
272 if (!strcmp(line, "/\n"))
276 eDVBFrontendParametersSatellite sat;
277 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
278 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
279 sat.frequency = frequency;
280 sat.symbol_rate = symbol_rate;
281 sat.polarisation = polarisation;
283 sat.orbital_position = orbital_position;
284 sat.inversion = inversion;
285 feparm->setDVBS(sat);
286 } else if (line[1]=='t')
288 eDVBFrontendParametersTerrestrial ter;
289 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
290 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);
291 ter.frequency = frequency;
292 ter.bandwidth = bandwidth;
293 ter.code_rate_HP = code_rate_HP;
294 ter.code_rate_LP = code_rate_LP;
295 ter.modulation = modulation;
296 ter.transmission_mode = transmission_mode;
297 ter.guard_interval = guard_interval;
298 ter.hierarchy = hierarchy;
299 ter.inversion = inversion;
301 feparm->setDVBT(ter);
302 } else if (line[1]=='c')
304 int frequency, symbol_rate, inversion=0, modulation=3;
305 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
306 // t.setCable(frequency, symbol_rate, inversion, modulation);
309 addChannelToList(channelid, feparm);
312 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
314 eDebug("services invalid, no services");
318 // clear all services
324 if (!fgets(line, 256, f))
326 if (!strcmp(line, "end\n"))
329 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
330 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
331 if (service_number == -1)
333 ePtr<eDVBService> s = new eDVBService;
334 eServiceReferenceDVB ref =
335 eServiceReferenceDVB(
336 eDVBNamespace(dvb_namespace),
337 eTransportStreamID(transport_stream_id),
338 eOriginalNetworkID(original_network_id),
339 eServiceID(service_id),
344 line[strlen(line)-1]=0;
346 s->m_service_name = line;
351 line[strlen(line)-1]=0;
353 std::string str=line;
355 if (str[1]!=':') // old ... (only service_provider)
357 s->m_provider_name=line;
359 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
361 unsigned int c=str.find(',');
364 if (c == std::string::npos)
370 v=str.substr(2, c-2);
373 // eDebug("%c ... %s", p, v.c_str());
375 s->m_provider_name=v;
378 sscanf(v.c_str(), "%x", &s->m_flags);
382 sscanf(v.c_str(), "%02d%04x", &cid, &val);
387 sscanf(v.c_str(), "%04x", &val);
394 eDebug("loaded %d services", count);
401 eDebug("---- saving lame channel db");
402 FILE *f=fopen("lamedb", "wt");
403 int channels=0, services=0;
405 eFatal("couldn't save lame channel db!");
406 fprintf(f, "eDVB services /3/\n");
407 fprintf(f, "transponders\n");
408 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
409 i != m_channels.end(); ++i)
411 const eDVBChannelID &chid = i->first;
412 const channel &ch = i->second;
414 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
415 chid.transport_stream_id.get(), chid.original_network_id.get());
416 eDVBFrontendParametersSatellite sat;
417 eDVBFrontendParametersTerrestrial ter;
418 if (!ch.m_frontendParameters->getDVBS(sat))
420 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
421 sat.frequency, sat.symbol_rate,
422 sat.polarisation, sat.fec, sat.orbital_position,
425 if (!ch.m_frontendParameters->getDVBT(ter))
427 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
428 ter.frequency, ter.bandwidth, ter.code_rate_HP,
429 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
430 ter.guard_interval, ter.hierarchy, ter.inversion);
435 fprintf(f, "end\nservices\n");
437 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
438 i != m_services.end(); ++i)
440 const eServiceReferenceDVB &s = i->first;
441 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
442 s.getServiceID().get(), s.getDVBNamespace().get(),
443 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
447 fprintf(f, "%s\n", i->second->m_service_name.c_str());
448 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
451 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
452 ca != i->second->m_cache.end(); ++ca)
453 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
455 // write cached ca pids
456 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
457 ca != i->second->m_ca.end(); ++ca)
458 fprintf(f, ",C:%04x", *ca);
463 fprintf(f, "end\nHave a lot of bugs!\n");
464 eDebug("saved %d channels and %d services!", channels, services);
468 void eDVBDB::loadBouquet(const char *path)
470 std::string bouquet_name = path;
471 if (!bouquet_name.length())
473 eDebug("Bouquet load failed.. no path given..");
476 unsigned int pos = bouquet_name.rfind('/');
477 if ( pos != std::string::npos )
478 bouquet_name.erase(0, pos+1);
479 if (bouquet_name.empty())
481 eDebug("Bouquet load failed.. no filename given..");
484 eBouquet &bouquet = m_bouquets[bouquet_name];
485 bouquet.m_path = path;
486 std::list<eServiceReference> &list = bouquet.m_services;
489 eDebug("loading bouquet... %s", path);
490 FILE *fp=fopen(path, "rt");
494 eDebug("failed to open.");
495 if ( strstr(path, "bouquets.tv") )
497 eDebug("recreate bouquets.tv");
498 bouquet.m_bouquet_name="Bouquets (TV)";
499 bouquet.flushChanges();
501 else if ( strstr(path, "bouquets.radio") )
503 eDebug("recreate bouquets.radio");
504 bouquet.m_bouquet_name="Bouquets (Radio)";
505 bouquet.flushChanges();
510 bool read_descr=false;
511 eServiceReference *e = NULL;
514 if (!fgets(line, 256, fp))
516 line[strlen(line)-1]=0;
517 if (strlen(line) && line[strlen(line)-1]=='\r')
518 line[strlen(line)-1]=0;
523 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
525 int offs = line[8] == ':' ? 10 : 9;
526 eServiceReference tmp(line+offs);
527 if (tmp.type != eServiceReference::idDVB)
529 eDebug("only DVB Bouquets supported");
532 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
534 std::string str = tmp.path;
535 unsigned int pos = str.rfind('/');
536 if ( pos != std::string::npos )
540 eDebug("Bouquet load failed.. no filename given..");
543 loadBouquet(tmp.path.c_str());
545 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], str.c_str());
553 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
558 else if (!strncmp(line, "#NAME ", 6))
559 bouquet.m_bouquet_name=line+6;
564 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
567 void eDVBDB::loadBouquets()
569 loadBouquet("bouquets.tv");
570 loadBouquet("bouquets.radio");
571 // create default bouquets when missing
572 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
574 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
575 b.m_path = "userbouquet.favourites.tv";
576 b.m_bouquet_name = "Favourites (TV)";
578 eServiceReference ref;
579 memset(ref.data, 0, sizeof(ref.data));
583 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
584 eBouquet &parent = m_bouquets["bouquets.tv"];
585 parent.m_services.push_back(ref);
586 parent.flushChanges();
588 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
590 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
591 b.m_path = "userbouquet.favourites.radio";
592 b.m_bouquet_name = "Favourites (Radio)";
594 eServiceReference ref;
595 memset(ref.data, 0, sizeof(ref.data));
599 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
600 eBouquet &parent = m_bouquets["bouquets.radio"];
601 parent.m_services.push_back(ref);
602 parent.flushChanges();
617 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
621 ch.m_frontendParameters = feparm;
622 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
626 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
628 m_channels.erase(id);
632 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
634 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
635 if (i == m_channels.end())
640 parm = i->second.m_frontendParameters;
644 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
646 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
650 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
652 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
653 i = m_services.find(reference);
654 if (i == m_services.end())
663 RESULT eDVBDB::flush()
669 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
671 std::string str = ref.path;
674 eDebug("getBouquet failed.. no path given!");
677 unsigned int pos = str.find("FROM BOUQUET \"");
678 if ( pos != std::string::npos )
680 str.erase(0, pos+14);
682 if ( pos != std::string::npos )
689 eDebug("getBouquet failed.. couldn't parse bouquet name");
692 std::map<std::string, eBouquet>::iterator i =
693 m_bouquets.find(str);
694 if (i == m_bouquets.end())
699 bouquet = &i->second;
703 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
705 if ( source.path.find("FROM") != std::string::npos )
707 if ( source.path.find("BOUQUET") != std::string::npos )
708 query = new eDVBDBBouquetQuery(this, source, q);
709 else if ( source.path.find("SATELLITES") != std::string::npos )
710 query = new eDVBDBSatellitesQuery(this, source, q);
711 else if ( source.path.find("PROVIDERS") != std::string::npos )
712 query = new eDVBDBProvidersQuery(this, source, q);
714 eFatal("invalid query %s", source.toString().c_str());
717 query = new eDVBDBQuery(this, source, q);
721 DEFINE_REF(eDVBDBQueryBase);
723 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
724 :m_db(db), m_query(query), m_source(source)
728 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
730 ePtr<eDVBService> a_service, b_service;
732 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
734 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
736 if (m_db->getService(a, a_service))
738 if (m_db->getService(b, b_service))
744 case eDVBChannelQuery::tName:
745 return a_service->m_service_name_sort < b_service->m_service_name_sort;
746 case eDVBChannelQuery::tProvider:
747 return a_service->m_provider_name < b_service->m_provider_name;
748 case eDVBChannelQuery::tType:
749 return a.getServiceType() < b.getServiceType();
750 case eDVBChannelQuery::tBouquet:
752 case eDVBChannelQuery::tSatellitePosition:
753 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
760 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
761 :eDVBDBQueryBase(db, source, query)
763 m_cursor = m_db->m_services.begin();
766 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
768 while (m_cursor != m_db->m_services.end())
770 ref = m_cursor->first;
772 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
780 ref.type = eServiceReference::idInvalid;
785 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
786 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
790 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
792 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
793 std::list<eServiceReference> &list = bouquet.m_services;
794 while (m_cursor != list.end())
796 ref = *((eServiceReferenceDVB*)&(*m_cursor));
798 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
799 m_db->m_services.find(ref);
801 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
809 ref.type = eServiceReference::idInvalid;
814 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
815 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
819 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
821 if (m_cursor != m_list.end())
826 ref.type = eServiceReference::idInvalid;
830 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
832 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
833 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
834 return a.name < b.name;
837 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
838 :eDVBDBListQuery(db, source, query)
840 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
841 it != m_db->m_services.end(); ++it)
843 int res = it->second->checkFilter(it->first, *query);
846 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
848 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
849 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
856 eServiceReferenceDVB ref;
857 ref.setDVBNamespace(dvbnamespace);
859 // TODO get real satellite name..
860 // but i dont like to parse the satellites.xml here.. and in the python part
861 snprintf(buf, 64, "Services - %d", dvbnamespace>>16);
863 snprintf(buf, 64, "(satellitePosition == %d) && ", dvbnamespace>>16);
864 ref.path=buf+source.path;
865 unsigned int pos=ref.path.find("FROM");
866 ref.flags=eServiceReference::flagDirectory;
868 ref.path+="ORDER BY name";
869 // eDebug("ref.path now %s", ref.path.c_str());
870 m_list.push_back(ref);
872 ref.path=buf+source.path;
873 pos=ref.path.find("FROM");
874 ref.path.erase(pos+5);
875 ref.path+="PROVIDERS ORDER BY name";
876 // eDebug("ref.path now %s", ref.path.c_str());
877 snprintf(buf, 64, "Providers - %d", dvbnamespace>>16);
879 m_list.push_back(ref);
883 m_cursor=m_list.begin();
886 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
887 :eDVBDBListQuery(db, source, query)
889 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
890 it != m_db->m_services.end(); ++it)
892 int res = it->second->checkFilter(it->first, *query);
897 const char *provider_name = it->second->m_provider_name.length() ?
898 it->second->m_provider_name.c_str() :
901 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
902 if (i->name == provider_name)
909 eServiceReferenceDVB ref;
911 ref.name=provider_name;
912 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
913 ref.path=buf+source.path;
914 unsigned int pos = ref.path.find("FROM");
915 ref.flags=eServiceReference::flagDirectory;
917 ref.path+="ORDER BY name";
918 // eDebug("ref.path now %s", ref.path.c_str());
919 m_list.push_back(ref);
923 m_cursor=m_list.begin();
926 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
928 static int decodeType(const std::string &type)
931 return eDVBChannelQuery::tName;
932 else if (type == "provider")
933 return eDVBChannelQuery::tProvider;
934 else if (type == "type")
935 return eDVBChannelQuery::tType;
936 else if (type == "bouquet")
937 return eDVBChannelQuery::tBouquet;
938 else if (type == "satellitePosition")
939 return eDVBChannelQuery::tSatellitePosition;
940 else if (type == "channelID")
941 return eDVBChannelQuery::tChannelID;
946 /* never, NEVER write a parser in C++! */
947 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
949 std::list<std::string>::const_iterator end_of_exp;
953 while (end_of_exp != end)
954 if (*end_of_exp == ")")
959 if (end_of_exp == end)
961 eDebug("expression parse: end of expression while searching for closing brace");
967 int r = parseExpression(res, begin, end_of_exp);
972 /* we had only one sub expression */
973 if (end_of_exp == end)
975 // eDebug("only one sub expression");
979 /* otherwise we have an operator here.. */
981 ePtr<eDVBChannelQuery> r2 = res;
982 res = new eDVBChannelQuery();
988 if (*end_of_exp == "||")
989 res->m_type = eDVBChannelQuery::tOR;
990 else if (*end_of_exp == "&&")
991 res->m_type = eDVBChannelQuery::tAND;
994 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1001 return parseExpression(res->m_p2, end_of_exp, end);
1004 // "begin" <op> "end"
1005 std::string type, op, val;
1007 res = new eDVBChannelQuery();
1011 while (begin != end)
1025 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1034 eDebug("malformed query: missing stuff");
1039 res->m_type = decodeType(type);
1041 if (res->m_type == -1)
1043 eDebug("malformed query: invalid type %s", type.c_str());
1050 else if (op == "!=")
1054 eDebug("invalid operator %s", op.c_str());
1059 res->m_string = val;
1060 res->m_int = atoi(val.c_str());
1061 // res->m_channelid = eDVBChannelID(val);
1066 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1068 std::list<std::string> tokens;
1070 std::string current_token;
1071 std::string bouquet_name;
1073 // eDebug("splitting %s....", query.c_str());
1075 const char *splitchars="()";
1076 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1077 while (i <= query.size())
1079 int c = (i < query.size()) ? query[i] : ' ';
1082 int issplit = !!strchr(splitchars, c);
1083 int isaln = isalnum(c);
1084 int iswhite = c == ' ';
1085 int isquot = c == '\"';
1089 iswhite = issplit = 0;
1093 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1095 if (current_token.size())
1096 tokens.push_back(current_token);
1097 current_token.clear();
1100 if (!(iswhite || isquot))
1104 quotemode = !quotemode;
1105 lastsplit = issplit;
1109 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1111 // printf("%s\n", a->c_str());
1114 int sort = eDVBChannelQuery::tName;
1115 /* check for "ORDER BY ..." */
1117 std::list<std::string>::iterator it = tokens.begin();
1118 while (it != tokens.end())
1123 if (it != tokens.end() && *it == "BY")
1126 sort = decodeType(*it);
1131 else if (*it == "FROM")
1134 if (it != tokens.end() && *it == "BOUQUET")
1140 else if (it != tokens.end() && *it == "SATELLITES")
1142 else if (it != tokens.end() && *it == "PROVIDERS")
1146 eDebug("FROM unknown %s", (*it).c_str());
1156 eWarning("ORDER BY .. string invalid.");
1161 // eDebug("sort by %d", sort);
1163 /* now we recursivly parse that. */
1164 int r = parseExpression(res, tokens.begin(), tokens.end());
1169 res->m_bouquet_name = bouquet_name;
1172 // eDebug("return: %d", r);
1176 DEFINE_REF(eDVBChannelQuery);