2 #include <lib/dvb/db.h>
3 #include <lib/dvb/frontend.h>
4 #include <lib/base/eerror.h>
5 #include <lib/base/estring.h>
6 #include <dvbsi++/service_description_section.h>
7 #include <dvbsi++/descriptor_tag.h>
8 #include <dvbsi++/service_descriptor.h>
9 #include <dvbsi++/satellite_delivery_system_descriptor.h>
11 DEFINE_REF(eDVBService);
13 RESULT eBouquet::addService(const eServiceReference &ref)
16 std::find(m_services.begin(), m_services.end(), ref);
17 if ( it != m_services.end() )
19 m_services.push_back(ref);
23 RESULT eBouquet::removeService(const eServiceReference &ref)
26 std::find(m_services.begin(), m_services.end(), ref);
27 if ( it == m_services.end() )
33 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
35 if ( pos < 0 || pos >= m_services.size() )
38 list::iterator source=m_services.end();
39 list::iterator dest=m_services.end();
41 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
43 if (dest == m_services.end() && !--pos)
50 if (dest != m_services.end() && source != m_services.end())
53 if (dest == m_services.end() || source == m_services.end() || source == dest)
55 while (source != dest)
58 std::iter_swap(source++, source);
60 std::iter_swap(source--, source);
65 RESULT eBouquet::flushChanges()
67 FILE *f=fopen(m_path.c_str(), "wt");
70 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
72 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
74 eServiceReference tmp = *i;
75 std::string str = tmp.path;
76 if ( (i->flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
78 unsigned int p1 = str.find("FROM BOUQUET \"");
79 if (p1 == std::string::npos)
81 eDebug("doof... kaputt");
86 if (p1 == std::string::npos)
88 eDebug("doof2... kaputt");
94 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
96 if ( i->name.length() )
97 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
104 eDebug("couldn't write file %s", m_path.c_str());
108 eDVBService::eDVBService()
112 eDVBService::~eDVBService()
116 eDVBService &eDVBService::operator=(const eDVBService &s)
118 m_service_name = s.m_service_name;
119 m_service_name_sort = s.m_service_name_sort;
120 m_provider_name = s.m_provider_name;
127 void eDVBService::genSortName()
129 m_service_name_sort = removeDVBChars(m_service_name);
130 makeUpper(m_service_name_sort);
131 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
132 m_service_name_sort.erase(0, 1);
134 /* put unnamed services at the end, not at the beginning. */
135 if (m_service_name_sort.empty())
136 m_service_name_sort = "\xFF";
139 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
141 if (!ref.name.empty())
143 else if (!m_service_name.empty())
144 name = m_service_name;
150 int eDVBService::getLength(const eServiceReference &ref)
155 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
158 switch (query.m_type)
160 case eDVBChannelQuery::tName:
161 res = m_service_name_sort.find(query.m_string) != std::string::npos;
163 case eDVBChannelQuery::tProvider:
164 res = m_provider_name.find(query.m_string) != std::string::npos;
166 case eDVBChannelQuery::tType:
167 res = ref.getServiceType() == query.m_int;
169 case eDVBChannelQuery::tBouquet:
172 case eDVBChannelQuery::tSatellitePosition:
173 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
175 case eDVBChannelQuery::tChannelID:
178 ref.getChannelID(chid);
179 res = chid == query.m_channelid;
182 case eDVBChannelQuery::tAND:
183 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
185 case eDVBChannelQuery::tOR:
186 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
196 int eDVBService::getCachePID(cacheID id)
198 std::map<int, int>::iterator it = m_cache.find(id);
199 if ( it != m_cache.end() )
204 void eDVBService::setCachePID(cacheID id, int pid)
211 /* THIS CODE IS BAD. it should be replaced by somethine better. */
214 eDebug("---- opening lame channel db");
215 FILE *f=fopen("lamedb", "rt");
219 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
221 eDebug("not a servicefile");
225 eDebug("reading services");
226 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
228 eDebug("services invalid, no transponders");
233 // clear all transponders
237 if (!fgets(line, 256, f))
239 if (!strcmp(line, "end\n"))
241 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
242 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
243 if (original_network_id == -1)
245 eDVBChannelID channelid = eDVBChannelID(
246 eDVBNamespace(dvb_namespace),
247 eTransportStreamID(transport_stream_id),
248 eOriginalNetworkID(original_network_id));
250 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
254 if (!strcmp(line, "/\n"))
258 eDVBFrontendParametersSatellite sat;
259 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
260 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
261 sat.frequency = frequency;
262 sat.symbol_rate = symbol_rate;
263 sat.polarisation = polarisation;
265 sat.orbital_position = orbital_position;
266 sat.inversion = inversion;
267 feparm->setDVBS(sat);
268 } else if (line[1]=='t')
270 eDVBFrontendParametersTerrestrial ter;
271 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
272 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);
273 ter.frequency = frequency;
274 ter.bandwidth = bandwidth;
275 ter.code_rate_HP = code_rate_HP;
276 ter.code_rate_LP = code_rate_LP;
277 ter.modulation = modulation;
278 ter.transmission_mode = transmission_mode;
279 ter.guard_interval = guard_interval;
280 ter.hierarchy = hierarchy;
281 ter.inversion = inversion;
283 feparm->setDVBT(ter);
284 } else if (line[1]=='c')
286 int frequency, symbol_rate, inversion=0, modulation=3;
287 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
288 // t.setCable(frequency, symbol_rate, inversion, modulation);
291 addChannelToList(channelid, feparm);
294 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
296 eDebug("services invalid, no services");
300 // clear all services
306 if (!fgets(line, 256, f))
308 if (!strcmp(line, "end\n"))
311 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
312 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
313 if (service_number == -1)
315 ePtr<eDVBService> s = new eDVBService;
316 eServiceReferenceDVB ref =
317 eServiceReferenceDVB(
318 eDVBNamespace(dvb_namespace),
319 eTransportStreamID(transport_stream_id),
320 eOriginalNetworkID(original_network_id),
321 eServiceID(service_id),
326 line[strlen(line)-1]=0;
328 s->m_service_name = line;
333 line[strlen(line)-1]=0;
335 std::string str=line;
337 if (str[1]!=':') // old ... (only service_provider)
339 s->m_provider_name=line;
341 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
343 unsigned int c=str.find(',');
346 if (c == std::string::npos)
352 v=str.substr(2, c-2);
355 // eDebug("%c ... %s", p, v.c_str());
357 s->m_provider_name=v;
360 sscanf(v.c_str(), "%x", &s->m_flags);
364 sscanf(v.c_str(), "%02d%04x", &cid, &val);
369 sscanf(v.c_str(), "%04x", &val);
376 eDebug("loaded %d services", count);
383 eDebug("---- saving lame channel db");
384 FILE *f=fopen("lamedb", "wt");
385 int channels=0, services=0;
387 eFatal("couldn't save lame channel db!");
388 fprintf(f, "eDVB services /3/\n");
389 fprintf(f, "transponders\n");
390 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
391 i != m_channels.end(); ++i)
393 const eDVBChannelID &chid = i->first;
394 const channel &ch = i->second;
396 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
397 chid.transport_stream_id.get(), chid.original_network_id.get());
398 eDVBFrontendParametersSatellite sat;
399 eDVBFrontendParametersTerrestrial ter;
400 if (!ch.m_frontendParameters->getDVBS(sat))
402 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
403 sat.frequency, sat.symbol_rate,
404 sat.polarisation, sat.fec, sat.orbital_position,
407 if (!ch.m_frontendParameters->getDVBT(ter))
409 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
410 ter.frequency, ter.bandwidth, ter.code_rate_HP,
411 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
412 ter.guard_interval, ter.hierarchy, ter.inversion);
417 fprintf(f, "end\nservices\n");
419 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
420 i != m_services.end(); ++i)
422 const eServiceReferenceDVB &s = i->first;
423 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
424 s.getServiceID().get(), s.getDVBNamespace().get(),
425 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
429 fprintf(f, "%s\n", i->second->m_service_name.c_str());
430 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
433 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
434 ca != i->second->m_cache.end(); ++ca)
435 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
437 // write cached ca pids
438 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
439 ca != i->second->m_ca.end(); ++ca)
440 fprintf(f, ",C:%04x", *ca);
445 fprintf(f, "end\nHave a lot of bugs!\n");
446 eDebug("saved %d channels and %d services!", channels, services);
450 void eDVBDB::loadBouquet(const char *path)
452 std::string bouquet_name = path;
453 if (!bouquet_name.length())
455 eDebug("Bouquet load failed.. no path given..");
458 unsigned int pos = bouquet_name.rfind('/');
459 if ( pos != std::string::npos )
460 bouquet_name.erase(0, pos+1);
461 if (bouquet_name.empty())
463 eDebug("Bouquet load failed.. no filename given..");
466 eBouquet &bouquet = m_bouquets[bouquet_name];
467 bouquet.m_path = path;
468 std::list<eServiceReference> &list = bouquet.m_services;
471 eDebug("loading bouquet... %s", path);
472 FILE *fp=fopen(path, "rt");
476 eDebug("failed to open.");
477 if ( strstr(path, "bouquets.tv") )
479 eDebug("recreate bouquets.tv");
480 bouquet.m_bouquet_name="Bouquets (TV)";
481 bouquet.flushChanges();
483 else if ( strstr(path, "bouquets.radio") )
485 eDebug("recreate bouquets.radio");
486 bouquet.m_bouquet_name="Bouquets (Radio)";
487 bouquet.flushChanges();
492 bool read_descr=false;
493 eServiceReference *e = NULL;
496 if (!fgets(line, 256, fp))
498 line[strlen(line)-1]=0;
499 if (strlen(line) && line[strlen(line)-1]=='\r')
500 line[strlen(line)-1]=0;
505 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
507 int offs = line[8] == ':' ? 10 : 9;
508 eServiceReference tmp(line+offs);
509 if (tmp.type != eServiceReference::idDVB)
511 eDebug("only DVB Bouquets supported");
514 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
516 std::string str = tmp.path;
517 unsigned int pos = str.rfind('/');
518 if ( pos != std::string::npos )
522 eDebug("Bouquet load failed.. no filename given..");
525 loadBouquet(tmp.path.c_str());
527 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], str.c_str());
535 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
540 else if (!strncmp(line, "#NAME ", 6))
541 bouquet.m_bouquet_name=line+6;
546 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
549 void eDVBDB::loadBouquets()
551 loadBouquet("bouquets.tv");
552 loadBouquet("bouquets.radio");
553 // create default bouquets when missing
554 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
556 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
557 b.m_path = "userbouquet.favourites.tv";
558 b.m_bouquet_name = "Favourites (TV)";
560 eServiceReference ref;
561 memset(ref.data, 0, sizeof(ref.data));
565 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
566 eBouquet &parent = m_bouquets["bouquets.tv"];
567 parent.m_services.push_back(ref);
568 parent.flushChanges();
570 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
572 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
573 b.m_path = "userbouquet.favourites.radio";
574 b.m_bouquet_name = "Favourites (Radio)";
576 eServiceReference ref;
577 memset(ref.data, 0, sizeof(ref.data));
581 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
582 eBouquet &parent = m_bouquets["bouquets.radio"];
583 parent.m_services.push_back(ref);
584 parent.flushChanges();
599 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
603 ch.m_frontendParameters = feparm;
604 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
608 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
610 m_channels.erase(id);
614 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
616 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
617 if (i == m_channels.end())
622 parm = i->second.m_frontendParameters;
626 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
628 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
632 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
634 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
635 i = m_services.find(reference);
636 if (i == m_services.end())
645 RESULT eDVBDB::flush()
651 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
653 std::string str = ref.path;
656 eDebug("getBouquet failed.. no path given!");
659 unsigned int pos = str.find("FROM BOUQUET \"");
660 if ( pos != std::string::npos )
662 str.erase(0, pos+14);
664 if ( pos != std::string::npos )
671 eDebug("getBouquet failed.. couldn't parse bouquet name");
674 std::map<std::string, eBouquet>::iterator i =
675 m_bouquets.find(str);
676 if (i == m_bouquets.end())
681 bouquet = &i->second;
685 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
687 if ( source.path.find("FROM") != std::string::npos )
689 if ( source.path.find("BOUQUET") != std::string::npos )
690 query = new eDVBDBBouquetQuery(this, source, q);
691 else if ( source.path.find("SATELLITES") != std::string::npos )
692 query = new eDVBDBSatellitesQuery(this, source, q);
693 else if ( source.path.find("PROVIDERS") != std::string::npos )
694 query = new eDVBDBProvidersQuery(this, source, q);
696 eFatal("invalid query %s", source.toString().c_str());
699 query = new eDVBDBQuery(this, source, q);
703 DEFINE_REF(eDVBDBQueryBase);
705 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
706 :m_db(db), m_query(query), m_source(source)
710 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
712 ePtr<eDVBService> a_service, b_service;
714 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
716 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
718 if (m_db->getService(a, a_service))
720 if (m_db->getService(b, b_service))
726 case eDVBChannelQuery::tName:
727 return a_service->m_service_name_sort < b_service->m_service_name_sort;
728 case eDVBChannelQuery::tProvider:
729 return a_service->m_provider_name < b_service->m_provider_name;
730 case eDVBChannelQuery::tType:
731 return a.getServiceType() < b.getServiceType();
732 case eDVBChannelQuery::tBouquet:
734 case eDVBChannelQuery::tSatellitePosition:
735 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
742 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
743 :eDVBDBQueryBase(db, source, query)
745 m_cursor = m_db->m_services.begin();
748 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
750 while (m_cursor != m_db->m_services.end())
752 ref = m_cursor->first;
754 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
762 ref.type = eServiceReference::idInvalid;
767 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
768 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
772 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
774 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
775 std::list<eServiceReference> &list = bouquet.m_services;
776 while (m_cursor != list.end())
778 ref = *((eServiceReferenceDVB*)&(*m_cursor));
780 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
781 m_db->m_services.find(ref);
783 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
791 ref.type = eServiceReference::idInvalid;
796 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
797 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
801 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
803 if (m_cursor != m_list.end())
808 ref.type = eServiceReference::idInvalid;
812 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
814 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
815 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
816 return a.name < b.name;
819 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
820 :eDVBDBListQuery(db, source, query)
822 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
823 it != m_db->m_services.end(); ++it)
825 int res = it->second->checkFilter(it->first, *query);
828 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
830 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
831 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
838 eServiceReferenceDVB ref;
839 ref.setDVBNamespace(dvbnamespace);
841 // TODO get real satellite name..
842 // but i dont like to parse the satellites.xml here.. and in the python part
843 snprintf(buf, 64, "Services - %d", dvbnamespace>>16);
845 snprintf(buf, 64, "(satellitePosition == %d) && ", dvbnamespace>>16);
846 ref.path=buf+source.path;
847 unsigned int pos=ref.path.find("FROM");
848 ref.flags=eServiceReference::flagDirectory;
850 ref.path+="ORDER BY name";
851 // eDebug("ref.path now %s", ref.path.c_str());
852 m_list.push_back(ref);
854 ref.path=buf+source.path;
855 pos=ref.path.find("FROM");
856 ref.path.erase(pos+5);
857 ref.path+="PROVIDERS ORDER BY name";
858 // eDebug("ref.path now %s", ref.path.c_str());
859 snprintf(buf, 64, "Providers - %d", dvbnamespace>>16);
861 m_list.push_back(ref);
865 m_cursor=m_list.begin();
868 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
869 :eDVBDBListQuery(db, source, query)
871 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
872 it != m_db->m_services.end(); ++it)
874 int res = it->second->checkFilter(it->first, *query);
879 const char *provider_name = it->second->m_provider_name.length() ?
880 it->second->m_provider_name.c_str() :
883 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
884 if (i->name == provider_name)
891 eServiceReferenceDVB ref;
893 ref.name=provider_name;
894 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
895 ref.path=buf+source.path;
896 unsigned int pos = ref.path.find("FROM");
897 ref.flags=eServiceReference::flagDirectory;
899 ref.path+="ORDER BY name";
900 // eDebug("ref.path now %s", ref.path.c_str());
901 m_list.push_back(ref);
905 m_cursor=m_list.begin();
908 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
910 static int decodeType(const std::string &type)
913 return eDVBChannelQuery::tName;
914 else if (type == "provider")
915 return eDVBChannelQuery::tProvider;
916 else if (type == "type")
917 return eDVBChannelQuery::tType;
918 else if (type == "bouquet")
919 return eDVBChannelQuery::tBouquet;
920 else if (type == "satellitePosition")
921 return eDVBChannelQuery::tSatellitePosition;
922 else if (type == "channelID")
923 return eDVBChannelQuery::tChannelID;
928 /* never, NEVER write a parser in C++! */
929 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
931 std::list<std::string>::const_iterator end_of_exp;
935 while (end_of_exp != end)
936 if (*end_of_exp == ")")
941 if (end_of_exp == end)
943 eDebug("expression parse: end of expression while searching for closing brace");
949 int r = parseExpression(res, begin, end_of_exp);
954 /* we had only one sub expression */
955 if (end_of_exp == end)
957 // eDebug("only one sub expression");
961 /* otherwise we have an operator here.. */
963 ePtr<eDVBChannelQuery> r2 = res;
964 res = new eDVBChannelQuery();
970 if (*end_of_exp == "||")
971 res->m_type = eDVBChannelQuery::tOR;
972 else if (*end_of_exp == "&&")
973 res->m_type = eDVBChannelQuery::tAND;
976 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
983 return parseExpression(res->m_p2, end_of_exp, end);
986 // "begin" <op> "end"
987 std::string type, op, val;
989 res = new eDVBChannelQuery();
1007 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1016 eDebug("malformed query: missing stuff");
1021 res->m_type = decodeType(type);
1023 if (res->m_type == -1)
1025 eDebug("malformed query: invalid type %s", type.c_str());
1032 else if (op == "!=")
1036 eDebug("invalid operator %s", op.c_str());
1041 res->m_string = val;
1042 res->m_int = atoi(val.c_str());
1043 // res->m_channelid = eDVBChannelID(val);
1048 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1050 std::list<std::string> tokens;
1052 std::string current_token;
1053 std::string bouquet_name;
1055 // eDebug("splitting %s....", query.c_str());
1057 const char *splitchars="()";
1058 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1059 while (i <= query.size())
1061 int c = (i < query.size()) ? query[i] : ' ';
1064 int issplit = !!strchr(splitchars, c);
1065 int isaln = isalnum(c);
1066 int iswhite = c == ' ';
1067 int isquot = c == '\"';
1071 iswhite = issplit = 0;
1075 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1077 if (current_token.size())
1078 tokens.push_back(current_token);
1079 current_token.clear();
1082 if (!(iswhite || isquot))
1086 quotemode = !quotemode;
1087 lastsplit = issplit;
1091 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1093 // printf("%s\n", a->c_str());
1096 int sort = eDVBChannelQuery::tName;
1097 /* check for "ORDER BY ..." */
1099 std::list<std::string>::iterator it = tokens.begin();
1100 while (it != tokens.end())
1105 if (it != tokens.end() && *it == "BY")
1108 sort = decodeType(*it);
1113 else if (*it == "FROM")
1116 if (it != tokens.end() && *it == "BOUQUET")
1122 else if (it != tokens.end() && *it == "SATELLITES")
1124 else if (it != tokens.end() && *it == "PROVIDERS")
1128 eDebug("FROM unknown %s", (*it).c_str());
1138 eWarning("ORDER BY .. string invalid.");
1143 // eDebug("sort by %d", sort);
1145 /* now we recursivly parse that. */
1146 int r = parseExpression(res, tokens.begin(), tokens.end());
1151 res->m_bouquet_name = bouquet_name;
1154 // eDebug("return: %d", r);
1158 DEFINE_REF(eDVBChannelQuery);