+RESULT eDVBDB::flush()
+{
+ saveServicelist();
+ return 0;
+}
+
+RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
+{
+ std::string str = ref.path;
+ if (str.empty())
+ {
+ eDebug("getBouquet failed.. no path given!");
+ return -1;
+ }
+ unsigned int pos = str.find("FROM BOUQUET \"");
+ if ( pos != std::string::npos )
+ {
+ str.erase(0, pos+14);
+ pos = str.find('"');
+ if ( pos != std::string::npos )
+ str.erase(pos);
+ else
+ str.clear();
+ }
+ if (str.empty())
+ {
+ eDebug("getBouquet failed.. couldn't parse bouquet name");
+ return -1;
+ }
+ std::map<std::string, eBouquet>::iterator i =
+ m_bouquets.find(str);
+ if (i == m_bouquets.end())
+ {
+ bouquet = 0;
+ return -ENOENT;
+ }
+ bouquet = &i->second;
+ return 0;
+}
+
+RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
+{
+ if ( source.path.find("FROM") != std::string::npos )
+ {
+ if ( source.path.find("BOUQUET") != std::string::npos )
+ query = new eDVBDBBouquetQuery(this, source, q);
+ else if ( source.path.find("SATELLITES") != std::string::npos )
+ query = new eDVBDBSatellitesQuery(this, source, q);
+ else if ( source.path.find("PROVIDERS") != std::string::npos )
+ query = new eDVBDBProvidersQuery(this, source, q);
+ else
+ eFatal("invalid query %s", source.toString().c_str());
+ }
+ else
+ query = new eDVBDBQuery(this, source, q);
+ return 0;
+}
+
+DEFINE_REF(eDVBDBQueryBase);
+
+eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+ :m_db(db), m_query(query), m_source(source)
+{
+}
+
+int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
+{
+ ePtr<eDVBService> a_service, b_service;
+
+ int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
+
+ if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
+ {
+ if (m_db->getService(a, a_service))
+ return 1;
+ if (m_db->getService(b, b_service))
+ return 1;
+ }
+
+ switch (sortmode)
+ {
+ case eDVBChannelQuery::tName:
+ return a_service->m_service_name_sort < b_service->m_service_name_sort;
+ case eDVBChannelQuery::tProvider:
+ return a_service->m_provider_name < b_service->m_provider_name;
+ case eDVBChannelQuery::tType:
+ return a.getServiceType() < b.getServiceType();
+ case eDVBChannelQuery::tBouquet:
+ return 0;
+ case eDVBChannelQuery::tSatellitePosition:
+ return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+ :eDVBDBQueryBase(db, source, query)
+{
+ m_cursor = m_db->m_services.begin();
+}
+
+RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
+{
+ while (m_cursor != m_db->m_services.end())
+ {
+ ref = m_cursor->first;
+
+ int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
+
+ ++m_cursor;
+
+ if (res)
+ return 0;
+ }
+
+ ref.type = eServiceReference::idInvalid;
+
+ return 1;
+}
+
+eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+ :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
+{
+}
+
+RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
+{
+ eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
+ std::list<eServiceReference> &list = bouquet.m_services;
+ while (m_cursor != list.end())
+ {
+ ref = *((eServiceReferenceDVB*)&(*m_cursor));
+
+ std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
+ m_db->m_services.find(ref);
+
+ int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
+
+ ++m_cursor;
+
+ if (res)
+ return 0;
+ }
+
+ ref.type = eServiceReference::idInvalid;
+
+ return 1;
+}
+
+eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+ :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
+{
+}
+
+RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
+{
+ if (m_cursor != m_list.end())
+ {
+ ref = *m_cursor++;
+ return 0;
+ }
+ ref.type = eServiceReference::idInvalid;
+ return 1;
+}
+
+int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
+{
+ if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
+ {
+ int x = (a.getDVBNamespace().get() >> 16);
+ int y = (b.getDVBNamespace().get() >> 16);
+ if ( x > 1800 )
+ x -= 3600;
+ if ( y > 1800 )
+ y -= 3600;
+ return x < y;
+ }
+ return a.name < b.name;
+}
+
+eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+ :eDVBDBListQuery(db, source, query)
+{
+ for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
+ it != m_db->m_services.end(); ++it)
+ {
+ int res = it->second->checkFilter(it->first, *query);
+ if (res)
+ {
+ unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
+ bool found=0;
+ for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
+ if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
+ {
+ found=true;
+ break;
+ }
+ if (!found)
+ {
+ eServiceReferenceDVB ref;
+ ref.setDVBNamespace(dvbnamespace);
+ ref.flags=eServiceReference::flagDirectory;
+ char buf[64];
+ snprintf(buf, 64, "(satellitePosition == %d) && ", dvbnamespace>>16);
+
+ ref.path=buf+source.path;
+ unsigned int pos=ref.path.find("FROM");
+ ref.path.erase(pos);
+ ref.path+="ORDER BY name";
+// eDebug("ref.path now %s", ref.path.c_str());
+ m_list.push_back(ref);
+
+ ref.path=buf+source.path;
+ pos=ref.path.find("FROM");
+ ref.path.erase(pos+5);
+ ref.path+="PROVIDERS ORDER BY name";
+// eDebug("ref.path now %s", ref.path.c_str());
+ m_list.push_back(ref);
+ }
+ }
+ }
+ m_cursor=m_list.begin();
+}
+
+eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+ :eDVBDBListQuery(db, source, query)
+{
+ for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
+ it != m_db->m_services.end(); ++it)
+ {
+ int res = it->second->checkFilter(it->first, *query);
+ if (res)
+ {
+ bool found=0;
+
+ const char *provider_name = it->second->m_provider_name.length() ?
+ it->second->m_provider_name.c_str() :
+ "Unknown";
+
+ for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
+ if (i->name == provider_name)
+ {
+ found=true;
+ break;
+ }
+ if (!found)
+ {
+ eServiceReferenceDVB ref;
+ char buf[64];
+ ref.name=provider_name;
+ snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
+ ref.path=buf+source.path;
+ unsigned int pos = ref.path.find("FROM");
+ ref.flags=eServiceReference::flagDirectory;
+ ref.path.erase(pos);
+ ref.path+="ORDER BY name";
+// eDebug("ref.path now %s", ref.path.c_str());
+ m_list.push_back(ref);
+ }
+ }
+ }
+ m_cursor=m_list.begin();
+}
+
+/* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
+
+static int decodeType(const std::string &type)
+{
+ if (type == "name")
+ return eDVBChannelQuery::tName;
+ else if (type == "provider")
+ return eDVBChannelQuery::tProvider;
+ else if (type == "type")
+ return eDVBChannelQuery::tType;
+ else if (type == "bouquet")
+ return eDVBChannelQuery::tBouquet;
+ else if (type == "satellitePosition")
+ return eDVBChannelQuery::tSatellitePosition;
+ else if (type == "channelID")
+ return eDVBChannelQuery::tChannelID;
+ else
+ return -1;
+}
+
+ /* never, NEVER write a parser in C++! */
+RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
+{
+ std::list<std::string>::const_iterator end_of_exp;
+ if (*begin == "(")
+ {
+ end_of_exp = begin;
+ while (end_of_exp != end)
+ if (*end_of_exp == ")")
+ break;
+ else
+ ++end_of_exp;
+
+ if (end_of_exp == end)
+ {
+ eDebug("expression parse: end of expression while searching for closing brace");
+ return -1;
+ }
+
+ ++begin;
+ // begin..end_of_exp
+ int r = parseExpression(res, begin, end_of_exp);
+ if (r)
+ return r;
+ ++end_of_exp;
+
+ /* we had only one sub expression */
+ if (end_of_exp == end)
+ {
+// eDebug("only one sub expression");
+ return 0;
+ }
+
+ /* otherwise we have an operator here.. */
+
+ ePtr<eDVBChannelQuery> r2 = res;
+ res = new eDVBChannelQuery();
+ res->m_sort = 0;
+ res->m_p1 = r2;
+ res->m_inverse = 0;
+ r2 = 0;
+
+ if (*end_of_exp == "||")
+ res->m_type = eDVBChannelQuery::tOR;
+ else if (*end_of_exp == "&&")
+ res->m_type = eDVBChannelQuery::tAND;
+ else
+ {
+ eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
+ res = 0;
+ return 1;
+ }
+
+ ++end_of_exp;
+
+ return parseExpression(res->m_p2, end_of_exp, end);
+ }
+
+ // "begin" <op> "end"
+ std::string type, op, val;
+
+ res = new eDVBChannelQuery();
+ res->m_sort = 0;
+
+ int cnt = 0;
+ while (begin != end)
+ {
+ switch (cnt)
+ {
+ case 0:
+ type = *begin;
+ break;
+ case 1:
+ op = *begin;
+ break;
+ case 2:
+ val = *begin;
+ break;
+ case 3:
+ eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
+ return 1;
+ }
+ ++begin;
+ ++cnt;
+ }
+
+ if (cnt != 3)
+ {
+ eDebug("malformed query: missing stuff");
+ res = 0;
+ return 1;
+ }
+
+ res->m_type = decodeType(type);
+
+ if (res->m_type == -1)
+ {
+ eDebug("malformed query: invalid type %s", type.c_str());
+ res = 0;
+ return 1;
+ }
+
+ if (op == "==")
+ res->m_inverse = 0;
+ else if (op == "!=")
+ res->m_inverse = 1;
+ else
+ {
+ eDebug("invalid operator %s", op.c_str());
+ res = 0;
+ return 1;
+ }
+
+ res->m_string = val;
+ res->m_int = atoi(val.c_str());
+// res->m_channelid = eDVBChannelID(val);
+
+ return 0;
+}
+
+RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
+{
+ std::list<std::string> tokens;
+
+ std::string current_token;
+ std::string bouquet_name;
+
+// eDebug("splitting %s....", query.c_str());
+ unsigned int i = 0;
+ const char *splitchars="()";
+ int quotemode = 0, lastsplit = 0, lastalnum = 0;
+ while (i <= query.size())
+ {
+ int c = (i < query.size()) ? query[i] : ' ';
+ ++i;
+
+ int issplit = !!strchr(splitchars, c);
+ int isaln = isalnum(c);
+ int iswhite = c == ' ';
+ int isquot = c == '\"';
+
+ if (quotemode)
+ {
+ iswhite = issplit = 0;
+ isaln = lastalnum;
+ }
+
+ if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
+ {
+ if (current_token.size())
+ tokens.push_back(current_token);
+ current_token.clear();
+ }
+
+ if (!(iswhite || isquot))
+ current_token += c;
+
+ if (isquot)
+ quotemode = !quotemode;
+ lastsplit = issplit;
+ lastalnum = isaln;
+ }
+
+// for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
+// {
+// printf("%s\n", a->c_str());
+// }
+
+ int sort = eDVBChannelQuery::tName;
+ /* check for "ORDER BY ..." */
+
+ std::list<std::string>::iterator it = tokens.begin();
+ while (it != tokens.end())
+ {
+ if (*it == "ORDER")
+ {
+ tokens.erase(it++);
+ if (it != tokens.end() && *it == "BY")
+ {
+ tokens.erase(it++);
+ sort = decodeType(*it);
+ tokens.erase(it++);
+ } else
+ sort = -1;
+ }
+ else if (*it == "FROM")
+ {
+ tokens.erase(it++);
+ if (it != tokens.end() && *it == "BOUQUET")
+ {
+ tokens.erase(it++);
+ bouquet_name = *it;
+ tokens.erase(it++);
+ }
+ else if (it != tokens.end() && *it == "SATELLITES")
+ tokens.erase(it++);
+ else if (it != tokens.end() && *it == "PROVIDERS")
+ tokens.erase(it++);
+ else
+ {
+ eDebug("FROM unknown %s", (*it).c_str());
+ tokens.erase(it++);
+ }
+ }
+ else
+ ++it;
+ }
+
+ if (sort == -1)
+ {
+ eWarning("ORDER BY .. string invalid.");
+ res = 0;
+ return -1;
+ }
+
+// eDebug("sort by %d", sort);
+
+ /* now we recursivly parse that. */
+ int r = parseExpression(res, tokens.begin(), tokens.end());
+
+ if (res)
+ {
+ res->m_sort = sort;
+ res->m_bouquet_name = bouquet_name;
+ }
+
+// eDebug("return: %d", r);
+ return r;
+}
+
+DEFINE_REF(eDVBChannelQuery);