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 <xmlccwrap/xmlccwrap.h>
9 #include <dvbsi++/service_description_section.h>
10 #include <dvbsi++/descriptor_tag.h>
11 #include <dvbsi++/service_descriptor.h>
12 #include <dvbsi++/satellite_delivery_system_descriptor.h>
14 DEFINE_REF(eDVBService);
16 RESULT eBouquet::addService(const eServiceReference &ref, eServiceReference before)
19 std::find(m_services.begin(), m_services.end(), ref);
20 if ( it != m_services.end() )
24 it = std::find(m_services.begin(), m_services.end(), before);
25 m_services.insert(it, ref);
28 m_services.push_back(ref);
32 RESULT eBouquet::removeService(const eServiceReference &ref)
35 std::find(m_services.begin(), m_services.end(), ref);
36 if ( it == m_services.end() )
42 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
44 if ( pos < 0 || pos >= m_services.size() )
47 list::iterator source=m_services.end();
48 list::iterator dest=m_services.end();
50 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
52 if (dest == m_services.end() && !--pos)
59 if (dest != m_services.end() && source != m_services.end())
62 if (dest == m_services.end() || source == m_services.end() || source == dest)
64 while (source != dest)
67 std::iter_swap(source++, source);
69 std::iter_swap(source--, source);
74 RESULT eBouquet::flushChanges()
76 FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
79 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
81 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
83 eServiceReference tmp = *i;
84 std::string str = tmp.path;
85 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
87 if ( i->name.length() )
88 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
95 eDebug("couldn't write file %s", m_filename.c_str());
99 RESULT eBouquet::setListName(const std::string &name)
101 m_bouquet_name = name;
105 eDVBService::eDVBService()
106 :m_cache(0), m_flags(0)
110 eDVBService::~eDVBService()
115 eDVBService &eDVBService::operator=(const eDVBService &s)
117 m_service_name = s.m_service_name;
118 m_service_name_sort = s.m_service_name_sort;
119 m_provider_name = s.m_provider_name;
122 copyCache(s.m_cache);
126 void eDVBService::genSortName()
128 m_service_name_sort = removeDVBChars(m_service_name);
129 makeUpper(m_service_name_sort);
130 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
131 m_service_name_sort.erase(0, 1);
133 /* put unnamed services at the end, not at the beginning. */
134 if (m_service_name_sort.empty())
135 m_service_name_sort = "\xFF";
138 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
140 if (!ref.name.empty())
141 name = ref.name; // use renamed service name..
142 else if (!m_service_name.empty())
143 name = m_service_name;
149 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
151 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
154 int eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
156 ePtr<eDVBResourceManager> res_mgr;
157 if ( eDVBResourceManager::getInstance( res_mgr ) )
158 eDebug("isPlayble... no res manager!!");
161 eDVBChannelID chid, chid_ignore;
162 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
163 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
164 return res_mgr->canAllocateChannel(chid, chid_ignore);
169 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
172 switch (query.m_type)
174 case eDVBChannelQuery::tName:
175 res = m_service_name_sort.find(query.m_string) != std::string::npos;
177 case eDVBChannelQuery::tProvider:
178 res = m_provider_name.find(query.m_string) != std::string::npos;
180 case eDVBChannelQuery::tType:
181 res = ref.getServiceType() == query.m_int;
183 case eDVBChannelQuery::tBouquet:
186 case eDVBChannelQuery::tSatellitePosition:
187 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
189 case eDVBChannelQuery::tFlags:
190 res = (m_flags & query.m_int) == query.m_int;
192 case eDVBChannelQuery::tChannelID:
195 ref.getChannelID(chid);
196 res = chid == query.m_channelid;
199 case eDVBChannelQuery::tAND:
200 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
202 case eDVBChannelQuery::tOR:
203 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
205 case eDVBChannelQuery::tAny:
216 bool eDVBService::cacheEmpty()
219 for (int i=0; i < cacheMax; ++i)
220 if (m_cache[i] != -1)
225 void eDVBService::initCache()
227 m_cache = new int[cacheMax];
228 memset(m_cache, -1, sizeof(int) * cacheMax);
231 void eDVBService::copyCache(int *source)
236 m_cache = new int[cacheMax];
237 memcpy(m_cache, source, cacheMax * sizeof(int));
246 int eDVBService::getCacheEntry(cacheID id)
248 if (id >= cacheMax || !m_cache)
253 void eDVBService::setCacheEntry(cacheID id, int pid)
263 void eDVBDB::reloadServicelist()
265 loadServicelist(CONFIGDIR"/enigma2/lamedb");
268 /* THIS CODE IS BAD. it should be replaced by somethine better. */
269 void eDVBDB::loadServicelist(const char *file)
271 eDebug("---- opening lame channel db");
272 FILE *f=fopen(file, "rt");
273 if (!f && strcmp(file, CONFIGDIR"/enigma2/lamedb") == 0)
276 if ( !stat("lamedb", &s) )
278 if ( !stat(CONFIGDIR"/enigma2", &s) )
280 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
287 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
289 eDebug("not a servicefile");
293 eDebug("reading services");
294 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
296 eDebug("services invalid, no transponders");
301 // clear all transponders
305 if (!fgets(line, 256, f))
307 if (!strcmp(line, "end\n"))
309 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
310 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
311 if (original_network_id == -1)
313 eDVBChannelID channelid = eDVBChannelID(
314 eDVBNamespace(dvb_namespace),
315 eTransportStreamID(transport_stream_id),
316 eOriginalNetworkID(original_network_id));
318 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
322 if (!strcmp(line, "/\n"))
326 eDVBFrontendParametersSatellite sat;
327 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
328 system=eDVBFrontendParametersSatellite::System::DVB_S,
329 modulation=eDVBFrontendParametersSatellite::Modulation::QPSK,
330 rolloff=eDVBFrontendParametersSatellite::RollOff::alpha_0_35,
331 pilot=eDVBFrontendParametersSatellite::Pilot::Unknown;
332 sscanf(line+3, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion, &system, &modulation, &rolloff, &pilot);
333 sat.frequency = frequency;
334 sat.symbol_rate = symbol_rate;
335 sat.polarisation = polarisation;
337 sat.orbital_position =
338 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
339 sat.inversion = inversion;
341 sat.modulation = modulation;
342 sat.rolloff = rolloff;
344 feparm->setDVBS(sat);
345 } else if (line[1]=='t')
347 eDVBFrontendParametersTerrestrial ter;
348 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
349 sscanf(line+3, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &bandwidth, &code_rate_HP, &code_rate_LP, &modulation, &transmission_mode, &guard_interval, &hierarchy, &inversion);
350 ter.frequency = frequency;
351 ter.bandwidth = bandwidth;
352 ter.code_rate_HP = code_rate_HP;
353 ter.code_rate_LP = code_rate_LP;
354 ter.modulation = modulation;
355 ter.transmission_mode = transmission_mode;
356 ter.guard_interval = guard_interval;
357 ter.hierarchy = hierarchy;
358 ter.inversion = inversion;
359 feparm->setDVBT(ter);
360 } else if (line[1]=='c')
362 eDVBFrontendParametersCable cab;
363 int frequency, symbol_rate,
364 inversion=eDVBFrontendParametersCable::Inversion::Unknown,
365 modulation=eDVBFrontendParametersCable::Modulation::Auto,
366 fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
367 sscanf(line+3, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
368 cab.frequency = frequency;
369 cab.fec_inner = fec_inner;
370 cab.inversion = inversion;
371 cab.symbol_rate = symbol_rate;
372 cab.modulation = modulation;
373 feparm->setDVBC(cab);
376 addChannelToList(channelid, feparm);
379 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
381 eDebug("services invalid, no services");
385 // clear all services
391 if (!fgets(line, 256, f))
393 if (!strcmp(line, "end\n"))
396 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
397 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
398 if (service_number == -1)
400 ePtr<eDVBService> s = new eDVBService;
401 eServiceReferenceDVB ref =
402 eServiceReferenceDVB(
403 eDVBNamespace(dvb_namespace),
404 eTransportStreamID(transport_stream_id),
405 eOriginalNetworkID(original_network_id),
406 eServiceID(service_id),
411 line[strlen(line)-1]=0;
413 s->m_service_name = line;
418 line[strlen(line)-1]=0;
419 std::string str=line;
421 if (str[1]!=':') // old ... (only service_provider)
423 s->m_provider_name=line;
425 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
427 size_t c=str.find(',');
430 if (c == std::string::npos)
436 v=str.substr(2, c-2);
439 // eDebug("%c ... %s", p, v.c_str());
441 s->m_provider_name=v;
444 sscanf(v.c_str(), "%x", &s->m_flags);
448 sscanf(v.c_str(), "%02d%x", &cid, &val);
449 s->setCacheEntry((eDVBService::cacheID)cid,val);
453 sscanf(v.c_str(), "%04x", &val);
454 s->m_ca.push_front((uint16_t)val);
460 eDebug("loaded %d services", count);
465 void eDVBDB::saveServicelist(const char *file)
467 eDebug("---- saving lame channel db");
468 FILE *f=fopen(file, "w");
469 int channels=0, services=0;
471 eFatal("couldn't save lame channel db!");
472 fprintf(f, "eDVB services /3/\n");
473 fprintf(f, "transponders\n");
474 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
475 i != m_channels.end(); ++i)
477 const eDVBChannelID &chid = i->first;
478 const channel &ch = i->second;
480 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
481 chid.transport_stream_id.get(), chid.original_network_id.get());
482 eDVBFrontendParametersSatellite sat;
483 eDVBFrontendParametersTerrestrial ter;
484 eDVBFrontendParametersCable cab;
485 if (!ch.m_frontendParameters->getDVBS(sat))
487 if (sat.system == eDVBFrontendParametersSatellite::System::DVB_S2)
489 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d",
490 sat.frequency, sat.symbol_rate,
491 sat.polarisation, sat.fec,
492 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
497 if (sat.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
498 fprintf(f, ":%d\n", sat.pilot);
504 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
505 sat.frequency, sat.symbol_rate,
506 sat.polarisation, sat.fec,
507 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
511 else if (!ch.m_frontendParameters->getDVBT(ter))
513 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
514 ter.frequency, ter.bandwidth, ter.code_rate_HP,
515 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
516 ter.guard_interval, ter.hierarchy, ter.inversion);
518 else if (!ch.m_frontendParameters->getDVBC(cab))
520 fprintf(f, "\tc %d:%d:%d:%d:%d\n",
521 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
526 fprintf(f, "end\nservices\n");
528 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
529 i != m_services.end(); ++i)
531 const eServiceReferenceDVB &s = i->first;
532 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
533 s.getServiceID().get(), s.getDVBNamespace().get(),
534 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
538 fprintf(f, "%s\n", i->second->m_service_name.c_str());
540 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
543 for (int x=0; x < eDVBService::cacheMax; ++x)
545 int entry = i->second->getCacheEntry((eDVBService::cacheID)x);
547 fprintf(f, ",c:%02d%04x", x, entry);
550 // write cached ca pids
551 for (CAID_LIST::const_iterator ca(i->second->m_ca.begin());
552 ca != i->second->m_ca.end(); ++ca)
553 fprintf(f, ",C:%04x", *ca);
555 if (i->second->m_flags)
556 fprintf(f, ",f:%x", i->second->m_flags);
561 fprintf(f, "end\nHave a lot of bugs!\n");
562 eDebug("saved %d channels and %d services!", channels, services);
566 void eDVBDB::saveServicelist()
568 saveServicelist(CONFIGDIR"/enigma2/lamedb");
571 void eDVBDB::loadBouquet(const char *path)
573 std::string bouquet_name = path;
574 if (!bouquet_name.length())
576 eDebug("Bouquet load failed.. no path given..");
579 size_t pos = bouquet_name.rfind('/');
580 if ( pos != std::string::npos )
581 bouquet_name.erase(0, pos+1);
582 if (bouquet_name.empty())
584 eDebug("Bouquet load failed.. no filename given..");
587 eBouquet &bouquet = m_bouquets[bouquet_name];
588 bouquet.m_filename = bouquet_name;
589 std::list<eServiceReference> &list = bouquet.m_services;
592 std::string p = CONFIGDIR"/enigma2/";
594 eDebug("loading bouquet... %s", p.c_str());
595 FILE *fp=fopen(p.c_str(), "rt");
600 if ( !stat(path, &s) )
602 rename(path, p.c_str() );
606 eDebug("failed to open.");
607 if ( strstr(path, "bouquets.tv") )
609 eDebug("recreate bouquets.tv");
610 bouquet.m_bouquet_name="Bouquets (TV)";
611 bouquet.flushChanges();
613 else if ( strstr(path, "bouquets.radio") )
615 eDebug("recreate bouquets.radio");
616 bouquet.m_bouquet_name="Bouquets (Radio)";
617 bouquet.flushChanges();
622 bool read_descr=false;
623 eServiceReference *e = NULL;
626 if (!fgets(line, 256, fp))
628 line[strlen(line)-1]=0;
629 if (strlen(line) && line[strlen(line)-1]=='\r')
630 line[strlen(line)-1]=0;
635 if (!strncmp(line, "#SERVICE", 8))
637 int offs = line[8] == ':' ? 10 : 9;
638 eServiceReference tmp(line+offs);
639 if (tmp.type != eServiceReference::idDVB)
641 eDebug("only DVB Bouquets supported");
644 if ( tmp.flags&eServiceReference::canDescent )
646 size_t pos = tmp.path.rfind('/');
648 std::string path = tmp.path;
649 if ( pos != std::string::npos )
650 path.erase(0, pos+1);
653 eDebug("Bouquet load failed.. no filename given..");
656 pos = path.find("FROM BOUQUET ");
657 if (pos != std::string::npos)
659 char endchr = path[pos+13];
662 eDebug("ignore invalid bouquet '%s' (only \" are allowed)",
663 tmp.toString().c_str());
666 char *beg = &path[pos+14];
667 char *end = strchr(beg, endchr);
668 path.assign(beg, end - beg);
672 snprintf(buf, 256, "FROM BOUQUET \"%s\" ORDER BY bouquet", path.c_str());
675 loadBouquet(path.c_str());
682 else if (read_descr && !strncmp(line, "#DESCRIPTION", 12))
684 int offs = line[12] == ':' ? 14 : 13;
688 else if (!strncmp(line, "#NAME ", 6))
689 bouquet.m_bouquet_name=line+6;
694 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
697 void eDVBDB::reloadBouquets()
700 loadBouquet("bouquets.tv");
701 loadBouquet("bouquets.radio");
702 // create default bouquets when missing
703 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
705 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
706 b.m_filename = "userbouquet.favourites.tv";
707 b.m_bouquet_name = "Favourites (TV)";
709 eServiceReference ref;
710 memset(ref.data, 0, sizeof(ref.data));
714 ref.path="FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
715 eBouquet &parent = m_bouquets["bouquets.tv"];
716 parent.m_services.push_back(ref);
717 parent.flushChanges();
719 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
721 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
722 b.m_filename = "userbouquet.favourites.radio";
723 b.m_bouquet_name = "Favourites (Radio)";
725 eServiceReference ref;
726 memset(ref.data, 0, sizeof(ref.data));
730 ref.path="FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
731 eBouquet &parent = m_bouquets["bouquets.radio"];
732 parent.m_services.push_back(ref);
733 parent.flushChanges();
737 eDVBDB *eDVBDB::instance;
739 using namespace xmlcc;
747 PyObject *eDVBDB::readSatellites(ePyObject sat_list, ePyObject sat_dict, ePyObject tp_dict)
749 if (!PyDict_Check(tp_dict)) {
750 PyErr_SetString(PyExc_StandardError,
752 eDebug("arg 2 is not a python dict");
755 else if (!PyDict_Check(sat_dict))
757 PyErr_SetString(PyExc_StandardError,
759 eDebug("arg 1 is not a python dict");
762 else if (!PyList_Check(sat_list))
764 PyErr_SetString(PyExc_StandardError,
766 eDebug("arg 0 is not a python list");
770 tree.setFilename("/etc/tuxbox/satellites.xml");
772 Element *root = tree.getRoot();
775 eDebug("couldn't open /etc/tuxbox/satellites.xml!!");
779 int tmp, *dest = NULL,
780 modulation, system, freq, sr, pol, fec, inv, pilot, rolloff;
784 const ElementList &root_elements = root->getElementList();
785 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
787 // eDebug("element: %s", (*it)->name().c_str());
788 const Element *el = *it;
789 const ElementList &sat_elements = el->getElementList();
790 const AttributeList &sat_attributes = el->getAttributeList();
794 for (AttributeConstIterator it(sat_attributes.begin()); it != sat_attributes.end(); ++it)
796 // eDebug("\tattr: %s", at->name().c_str());
800 sat_name = PyString_FromString(at->value().c_str());
801 else if (name == "flags")
803 tmp = strtol(at->value().c_str(), &end_ptr, 10);
805 sat_flags = PyInt_FromLong(tmp);
807 else if (name == "position")
809 tmp = strtol(at->value().c_str(), &end_ptr, 10);
814 sat_pos = PyInt_FromLong(tmp);
818 if (sat_pos && sat_name)
820 ePyObject tplist = PyList_New(0);
821 ePyObject tuple = PyTuple_New(3);
823 sat_flags = PyInt_FromLong(0);
824 PyTuple_SET_ITEM(tuple, 0, sat_pos);
825 PyTuple_SET_ITEM(tuple, 1, sat_name);
826 PyTuple_SET_ITEM(tuple, 2, sat_flags);
827 PyList_Append(sat_list, tuple);
829 PyDict_SetItem(sat_dict, sat_pos, sat_name);
830 PyDict_SetItem(tp_dict, sat_pos, tplist);
831 for (ElementConstIterator it(sat_elements.begin()); it != sat_elements.end(); ++it)
833 // eDebug("\telement: %s", (*it)->name().c_str());
834 const AttributeList &tp_attributes = (*it)->getAttributeList();
835 AttributeConstIterator end = tp_attributes.end();
836 modulation = 1; // QPSK default
837 system = 0; // DVB-S default
841 fec = 0; // AUTO default
842 inv = 2; // AUTO default
843 pilot = 2; // AUTO default
844 rolloff = 0; // alpha 0.35
845 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
847 // eDebug("\t\tattr: %s", at->name().c_str());
850 if (name == "modulation") dest = &modulation;
851 else if (name == "system") dest = &system;
852 else if (name == "frequency") dest = &freq;
853 else if (name == "symbol_rate") dest = &sr;
854 else if (name == "polarization") dest = &pol;
855 else if (name == "fec_inner") dest = &fec;
856 else if (name == "inversion") dest = &inv;
857 else if (name == "rolloff") dest = &rolloff;
858 else if (name == "pilot") dest = &pilot;
861 tmp = strtol(at->value().c_str(), &end_ptr, 10);
866 if (freq && sr && pol != -1)
868 tuple = PyTuple_New(10);
869 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0));
870 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
871 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
872 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(pol));
873 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
874 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(system));
875 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(modulation));
876 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(inv));
877 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(rolloff));
878 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(pilot));
879 PyList_Append(tplist, tuple);
892 Py_DECREF(sat_flags);
899 PyObject *eDVBDB::readCables(ePyObject cab_list, ePyObject tp_dict)
901 if (!PyDict_Check(tp_dict)) {
902 PyErr_SetString(PyExc_StandardError,
904 eDebug("arg 1 is not a python dict");
907 else if (!PyList_Check(cab_list))
909 PyErr_SetString(PyExc_StandardError,
911 eDebug("arg 0 is not a python list");
915 tree.setFilename("/etc/tuxbox/cables.xml");
917 Element *root = tree.getRoot();
920 eDebug("couldn't open /etc/tuxbox/cables.xml!!");
926 modulation, fec, freq, sr;
929 const ElementList &root_elements = root->getElementList();
930 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
932 // eDebug("element: %s", el->name().c_str());
933 const Element *el = *it;
934 const ElementList &cab_elements = el->getElementList();
935 const AttributeList &cab_attributes = el->getAttributeList();
938 for (AttributeConstIterator it(cab_attributes.begin()); it != cab_attributes.end(); ++it)
940 // eDebug("\tattr: %s", at->name().c_str());
944 cab_name = PyString_FromString(at->value().c_str());
945 else if (name == "flags")
947 tmp = strtol(at->value().c_str(), &end_ptr, 10);
949 cab_flags = PyInt_FromLong(tmp);
954 ePyObject tplist = PyList_New(0);
955 ePyObject tuple = PyTuple_New(2);
957 cab_flags = PyInt_FromLong(0);
958 PyTuple_SET_ITEM(tuple, 0, cab_name);
959 PyTuple_SET_ITEM(tuple, 1, cab_flags);
960 PyList_Append(cab_list, tuple);
962 PyDict_SetItem(tp_dict, cab_name, tplist);
963 for (ElementConstIterator it(cab_elements.begin()); it != cab_elements.end(); ++it)
965 // eDebug("\telement: %s", (*it)->name().c_str());
966 const AttributeList &tp_attributes = (*it)->getAttributeList();
967 AttributeConstIterator end = tp_attributes.end();
968 modulation = 3; // QAM64 default
969 fec = 0; // AUTO default
972 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
974 // eDebug("\t\tattr: %s", at->name().c_str());
978 if (name == "modulation") dest = &modulation;
979 else if (name == "frequency") dest = &freq;
980 else if (name == "symbol_rate") dest = &sr;
981 else if (name == "fec_inner") dest = &fec;
984 tmp = strtol(at->value().c_str(), &end_ptr, 10);
991 while (freq > 999999)
993 tuple = PyTuple_New(5);
994 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
995 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
996 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
997 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(modulation));
998 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
999 PyList_Append(tplist, tuple);
1006 Py_DECREF(cab_flags);
1012 PyObject *eDVBDB::readTerrestrials(ePyObject ter_list, ePyObject tp_dict)
1014 if (!PyDict_Check(tp_dict)) {
1015 PyErr_SetString(PyExc_StandardError,
1017 eDebug("arg 1 is not a python dict");
1020 else if (!PyList_Check(ter_list))
1022 PyErr_SetString(PyExc_StandardError,
1024 eDebug("arg 0 is not a python list");
1028 tree.setFilename("/etc/tuxbox/terrestrial.xml");
1030 Element *root = tree.getRoot();
1033 eDebug("couldn't open /etc/tuxbox/terrestrial.xml!!");
1034 Py_INCREF(Py_False);
1037 const Attribute *at;
1040 freq, bw, constellation, crh = 5, crl = 5, guard = 4, transm, hierarchy, inv = 2;
1042 const ElementList &root_elements = root->getElementList();
1043 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
1045 // eDebug("element: %s", el->name().c_str());
1046 const Element *el = *it;
1047 const ElementList &ter_elements = el->getElementList();
1048 const AttributeList &ter_attributes = el->getAttributeList();
1050 ePyObject ter_flags;
1051 for (AttributeConstIterator it(ter_attributes.begin()); it != ter_attributes.end(); ++it)
1053 // eDebug("\tattr: %s", at->name().c_str());
1057 ter_name = PyString_FromString(at->value().c_str());
1058 else if (name == "flags")
1060 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1062 ter_flags = PyInt_FromLong(tmp);
1067 ePyObject tplist = PyList_New(0);
1068 ePyObject tuple = PyTuple_New(2);
1070 ter_flags = PyInt_FromLong(0);
1071 PyTuple_SET_ITEM(tuple, 0, ter_name);
1072 PyTuple_SET_ITEM(tuple, 1, ter_flags);
1073 PyList_Append(ter_list, tuple);
1075 PyDict_SetItem(tp_dict, ter_name, tplist);
1076 for (ElementConstIterator it(ter_elements.begin()); it != ter_elements.end(); ++it)
1078 // eDebug("\telement: %s", (*it)->name().c_str());
1079 const AttributeList &tp_attributes = (*it)->getAttributeList();
1080 AttributeConstIterator end = tp_attributes.end();
1083 constellation = 1; // AUTO
1088 hierarchy = 4; // AUTO
1090 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
1092 // eDebug("\t\tattr: %s", at->name().c_str());
1096 if (name == "centre_frequency") dest = &freq;
1097 else if (name == "bandwidth") dest = &bw;
1098 else if (name == "constellation") dest = &constellation;
1099 else if (name == "code_rate_hp") dest = &crh;
1100 else if (name == "code_rate_lp") dest = &crl;
1101 else if (name == "guard_interval") dest = &guard;
1102 else if (name == "transmission_mode") dest = &transm;
1103 else if (name == "hierarchy_information") dest = &hierarchy;
1104 else if (name == "inversion") dest = &inv;
1107 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1114 if (crh > 5) // our terrestrial.xml is buggy... 6 for AUTO
1116 if (crl > 5) // our terrestrial.xml is buggy... 6 for AUTO
1118 tuple = PyTuple_New(10);
1119 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1120 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1121 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(bw));
1122 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(constellation));
1123 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(crh));
1124 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(crl));
1125 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(guard));
1126 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(transm));
1127 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(hierarchy));
1128 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(inv));
1129 PyList_Append(tplist, tuple);
1136 Py_DECREF(ter_flags);
1147 RESULT eDVBDB::removeService(const eServiceReference &ref)
1149 if (ref.type == eServiceReference::idDVB)
1151 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1152 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1153 if (it != m_services.end())
1155 m_services.erase(it);
1162 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1164 return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1167 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
1171 eTransportStreamID eTsid;
1172 eOriginalNetworkID eOnid;
1173 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1174 std::set<eDVBChannelID> removed_chids;
1175 while (it != m_channels.end())
1177 const eDVBChannelID &ch = it->first;
1180 it->second.m_frontendParameters->getSystem(system);
1181 if ( system == iDVBFrontend::feSatellite )
1183 eDVBFrontendParametersSatellite sat;
1184 it->second.m_frontendParameters->getDVBS(sat);
1185 if ((unsigned int)sat.orbital_position != orbpos)
1188 if ( remove && chid.dvbnamespace != eNs )
1190 if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1192 else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1194 else if ( chid.dvbnamespace != ch.dvbnamespace )
1197 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1199 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1203 eDebug("remove %08x %04x %04x",
1204 ch.dvbnamespace.get(),
1205 ch.original_network_id.get(),
1206 ch.transport_stream_id.get());
1207 removed_chids.insert(it->first);
1208 m_channels.erase(it++);
1213 if (!removed_chids.empty())
1215 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1216 while(service != m_services.end())
1219 service->first.getChannelID(chid);
1220 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1221 if (it != removed_chids.end())
1222 m_services.erase(service++);
1231 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
1233 if (ref.type == eServiceReference::idDVB)
1235 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1236 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1237 if (it != m_services.end())
1238 it->second->m_flags |= ~flagmask;
1244 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
1246 if (ref.type == eServiceReference::idDVB)
1248 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1249 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1250 if (it != m_services.end())
1251 it->second->m_flags &= ~flagmask;
1257 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1259 return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1262 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
1265 eTransportStreamID eTsid;
1266 eOriginalNetworkID eOnid;
1267 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1268 std::set<eDVBChannelID> removed_chids;
1269 while (it != m_channels.end())
1271 const eDVBChannelID &ch = it->first;
1274 it->second.m_frontendParameters->getSystem(system);
1275 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
1277 eDVBFrontendParametersSatellite sat;
1278 it->second.m_frontendParameters->getDVBS(sat);
1279 if ((unsigned int)sat.orbital_position != orbpos)
1282 if ( remove && chid.dvbnamespace != eNs )
1284 if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1286 else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1288 else if ( chid.dvbnamespace != ch.dvbnamespace )
1291 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1293 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1296 removed_chids.insert(it->first);
1299 if (!removed_chids.empty())
1301 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1302 while(service != m_services.end())
1305 service->first.getChannelID(chid);
1306 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1307 if (it != removed_chids.end())
1308 service->second->m_flags &= ~flagmask;
1315 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
1319 ch.m_frontendParameters = feparm;
1320 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
1324 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
1326 m_channels.erase(id);
1330 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
1332 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
1333 if (i == m_channels.end())
1338 parm = i->second.m_frontendParameters;
1342 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
1344 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
1345 if (it == m_services.end())
1346 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
1350 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
1352 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
1353 i = m_services.find(reference);
1354 if (i == m_services.end())
1359 service = i->second;
1363 RESULT eDVBDB::flush()
1369 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
1371 std::string str = ref.path;
1374 eDebug("getBouquet failed.. no path given!");
1377 size_t pos = str.find("FROM BOUQUET \"");
1378 if ( pos != std::string::npos )
1380 str.erase(0, pos+14);
1381 pos = str.find('"');
1382 if ( pos != std::string::npos )
1389 eDebug("getBouquet failed.. couldn't parse bouquet name");
1392 std::map<std::string, eBouquet>::iterator i =
1393 m_bouquets.find(str);
1394 if (i == m_bouquets.end())
1399 bouquet = &i->second;
1403 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
1405 if ( source.path.find("FROM") != std::string::npos )
1407 if ( source.path.find("BOUQUET") != std::string::npos )
1408 query = new eDVBDBBouquetQuery(this, source, q);
1409 else if ( source.path.find("SATELLITES") != std::string::npos )
1410 query = new eDVBDBSatellitesQuery(this, source, q);
1411 else if ( source.path.find("PROVIDERS") != std::string::npos )
1412 query = new eDVBDBProvidersQuery(this, source, q);
1414 eFatal("invalid query %s", source.toString().c_str());
1417 query = new eDVBDBQuery(this, source, q);
1421 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
1423 eServiceID Sid(sid);
1424 eTransportStreamID Tsid(tsid);
1425 eOriginalNetworkID Onid(onid);
1426 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
1427 sit != m_services.end(); ++sit)
1429 if (sit->first.getTransportStreamID() == Tsid &&
1430 sit->first.getOriginalNetworkID() == Onid &&
1431 sit->first.getServiceID() == Sid)
1434 return eServiceReference();
1437 DEFINE_REF(eDVBDBQueryBase);
1439 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1440 :m_db(db), m_query(query), m_source(source)
1444 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1446 ePtr<eDVBService> a_service, b_service;
1447 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1449 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1451 if (a.name.empty() && m_db->getService(a, a_service))
1453 if (b.name.empty() && m_db->getService(b, b_service))
1459 case eDVBChannelQuery::tName:
1463 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1466 std::string str = b.name;
1468 return a_service->m_service_name_sort < str;
1473 std::string str = a.name;
1475 return str < b_service->m_service_name_sort;
1479 std::string aa = a.name, bb = b.name;
1484 case eDVBChannelQuery::tProvider:
1485 return a_service->m_provider_name < b_service->m_provider_name;
1486 case eDVBChannelQuery::tType:
1487 return a.getServiceType() < b.getServiceType();
1488 case eDVBChannelQuery::tBouquet:
1490 case eDVBChannelQuery::tSatellitePosition:
1491 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1498 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1499 :eDVBDBQueryBase(db, source, query)
1501 m_cursor = m_db->m_services.begin();
1504 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1506 while (m_cursor != m_db->m_services.end())
1508 ePtr<eDVBService> service = m_cursor->second;
1509 if (service->isHidden())
1513 ref = m_cursor->first;
1515 int res = (!m_query) || service->checkFilter(ref, *m_query);
1524 ref.type = eServiceReference::idInvalid;
1529 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1530 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1534 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1536 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1537 std::list<eServiceReference> &list = bouquet.m_services;
1538 while (m_cursor != list.end())
1540 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1542 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1543 m_db->m_services.find(ref);
1545 int res = (!m_query) || it == m_db->m_services.end() || !(it->second->isHidden() && it->second->checkFilter(ref, *m_query));
1553 ref.type = eServiceReference::idInvalid;
1558 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1559 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1563 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1565 if (m_cursor != m_list.end())
1570 ref.type = eServiceReference::idInvalid;
1574 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1576 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1578 int x = (a.getDVBNamespace().get() >> 16);
1579 int y = (b.getDVBNamespace().get() >> 16);
1586 std::string aa = a.name, bb = b.name;
1592 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1593 :eDVBDBListQuery(db, source, query)
1595 std::set<unsigned int> found;
1596 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1597 it != m_db->m_services.end(); ++it)
1599 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1602 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1603 if (found.find(dvbnamespace) == found.end())
1605 found.insert(dvbnamespace);
1606 eServiceReferenceDVB ref;
1607 ref.setDVBNamespace(dvbnamespace);
1608 ref.flags=eServiceReference::flagDirectory;
1610 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1612 ref.path=buf+source.path;
1613 unsigned int pos=ref.path.find("FROM");
1614 ref.path.erase(pos);
1615 ref.path+="ORDER BY name";
1616 // eDebug("ref.path now %s", ref.path.c_str());
1617 m_list.push_back(ref);
1619 ref.path=buf+source.path;
1620 pos=ref.path.find("FROM");
1621 ref.path.erase(pos+5);
1622 ref.path+="PROVIDERS ORDER BY name";
1623 // eDebug("ref.path now %s", ref.path.c_str());
1624 m_list.push_back(ref);
1626 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1627 ref.path=buf+source.path;
1628 pos=ref.path.find("FROM");
1629 ref.path.erase(pos);
1630 ref.path+="ORDER BY name";
1631 // eDebug("ref.path now %s", ref.path.c_str());
1632 m_list.push_back(ref);
1636 m_cursor=m_list.begin();
1639 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1640 :eDVBDBListQuery(db, source, query)
1642 std::set<std::string> found;
1643 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1644 it != m_db->m_services.end(); ++it)
1646 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1649 const char *provider_name = it->second->m_provider_name.length() ?
1650 it->second->m_provider_name.c_str() :
1652 if (found.find(std::string(provider_name)) == found.end())
1654 found.insert(std::string(provider_name));
1655 eServiceReferenceDVB ref;
1657 ref.name=provider_name;
1658 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1659 ref.path=buf+source.path;
1660 unsigned int pos = ref.path.find("FROM");
1661 ref.flags=eServiceReference::flagDirectory;
1662 ref.path.erase(pos);
1663 ref.path+="ORDER BY name";
1664 // eDebug("ref.path now %s", ref.path.c_str());
1665 m_list.push_back(ref);
1669 m_cursor=m_list.begin();
1672 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1674 static int decodeType(const std::string &type)
1677 return eDVBChannelQuery::tName;
1678 else if (type == "provider")
1679 return eDVBChannelQuery::tProvider;
1680 else if (type == "type")
1681 return eDVBChannelQuery::tType;
1682 else if (type == "bouquet")
1683 return eDVBChannelQuery::tBouquet;
1684 else if (type == "satellitePosition")
1685 return eDVBChannelQuery::tSatellitePosition;
1686 else if (type == "channelID")
1687 return eDVBChannelQuery::tChannelID;
1688 else if (type == "flags")
1689 return eDVBChannelQuery::tFlags;
1694 /* never, NEVER write a parser in C++! */
1695 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1697 std::list<std::string>::const_iterator end_of_exp;
1705 while (end_of_exp != end)
1706 if (*end_of_exp == ")")
1711 if (end_of_exp == end)
1713 eDebug("expression parse: end of expression while searching for closing brace");
1718 // begin..end_of_exp
1719 int r = parseExpression(res, begin, end_of_exp);
1724 /* we had only one sub expression */
1725 if (end_of_exp == end)
1727 // eDebug("only one sub expression");
1731 /* otherwise we have an operator here.. */
1733 ePtr<eDVBChannelQuery> r2 = res;
1734 res = new eDVBChannelQuery();
1740 if (*end_of_exp == "||")
1741 res->m_type = eDVBChannelQuery::tOR;
1742 else if (*end_of_exp == "&&")
1743 res->m_type = eDVBChannelQuery::tAND;
1746 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1753 return parseExpression(res->m_p2, end_of_exp, end);
1756 // "begin" <op> "end"
1757 std::string type, op, val;
1759 res = new eDVBChannelQuery();
1763 while (begin != end)
1777 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1786 eDebug("malformed query: missing stuff");
1791 res->m_type = decodeType(type);
1793 if (res->m_type == -1)
1795 eDebug("malformed query: invalid type %s", type.c_str());
1802 else if (op == "!=")
1806 eDebug("invalid operator %s", op.c_str());
1811 res->m_string = val;
1813 if (res->m_type == eDVBChannelQuery::tChannelID)
1816 if (sscanf(val.c_str(), "%08x%04x%04x", &ns, &tsid, &onid) == 3)
1817 res->m_channelid = eDVBChannelID(eDVBNamespace(ns), eTransportStreamID(tsid), eOriginalNetworkID(onid));
1819 eDebug("couldn't parse channelid !! format should be hex NNNNNNNNTTTTOOOO (namespace, tsid, onid)");
1822 res->m_int = atoi(val.c_str());
1827 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1829 std::list<std::string> tokens;
1831 std::string current_token;
1832 std::string bouquet_name;
1834 // eDebug("splitting %s....", query.c_str());
1836 const char *splitchars="()";
1837 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1838 while (i <= query.size())
1840 int c = (i < query.size()) ? query[i] : ' ';
1843 int issplit = !!strchr(splitchars, c);
1844 int isaln = isalnum(c);
1845 int iswhite = c == ' ';
1846 int isquot = c == '\"';
1850 iswhite = issplit = 0;
1854 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1856 if (current_token.size())
1857 tokens.push_back(current_token);
1858 current_token.clear();
1861 if (!(iswhite || isquot))
1865 quotemode = !quotemode;
1866 lastsplit = issplit;
1870 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1872 // printf("%s\n", a->c_str());
1875 int sort = eDVBChannelQuery::tName;
1876 /* check for "ORDER BY ..." */
1878 std::list<std::string>::iterator it = tokens.begin();
1879 while (it != tokens.end())
1884 if (it != tokens.end() && *it == "BY")
1887 sort = decodeType(*it);
1892 else if (*it == "FROM")
1895 if (it != tokens.end() && *it == "BOUQUET")
1901 else if (it != tokens.end() && *it == "SATELLITES")
1903 else if (it != tokens.end() && *it == "PROVIDERS")
1907 eDebug("FROM unknown %s", (*it).c_str());
1917 eWarning("ORDER BY .. string invalid.");
1922 // eDebug("sort by %d", sort);
1924 /* now we recursivly parse that. */
1925 int r = parseExpression(res, tokens.begin(), tokens.end());
1927 /* we have an empty (but valid!) expression */
1930 res = new eDVBChannelQuery();
1932 res->m_type = eDVBChannelQuery::tAny;
1938 res->m_bouquet_name = bouquet_name;
1941 // eDebug("return: %d", r);
1945 DEFINE_REF(eDVBChannelQuery);