/* * tools.h: Tools for handling configure files and strings * * See the README file for copyright information and how to reach the author. * */ #ifndef __EPGFIXER_TOOLS_H_ #define __EPGFIXER_TOOLS_H_ #include #include #include #include #define error(x...) esyslog("EPGFixer: " x); #define FREE(x) { free(x); x = NULL; } void AddEvent(cEvent *event, tChannelID ChannelID); char *striphtml(char *str); class cListItem : public cListObject { protected: bool enabled; char *string; int *channels_num; tChannelID *channels_id; int numchannels; void Free(); tChannelID *GetChannelID(int index); int GetChannelNum(int index); int LoadChannelsFromString(const char *string); bool IsActive(tChannelID ChannelID); public: cListItem(); virtual ~cListItem(); virtual bool Apply(cChannel *Channel) { return 0; } virtual bool Apply(cEvent *Event) { return 0; } void SetFromString(char *string, bool Enabled); const char *GetString() { return string; } bool Enabled(void) { return enabled; } void ToggleEnabled(void); void PrintConfigLineToFile(FILE *f); }; template class cEpgfixerList : public cList { protected: char *fileName; bool LoadConfigFile(bool AllowComments = true) { bool result = false; if (fileName && access(fileName, F_OK) == 0) { FILE *f = fopen(fileName, "r"); if (f) { char *s; int line = 0; int count = 0; cReadLine ReadLine; cString logmsg(""); logmsg = cString::sprintf("%s%s loaded. Active lines:", *logmsg, fileName); while ((s = ReadLine.Read(f)) != NULL) { ++line; if (!isempty(s)) { this->Add(new LISTITEM()); cList::Last()->SetFromString(s, true); if (cList::Last()->Enabled()) { ++count; logmsg = cString::sprintf("%s%s%i", *logmsg, count == 1 ? " " : ",", line); } } } fclose(f); if (count == 0) logmsg = cString::sprintf("%s none", *logmsg); isyslog("%s", *logmsg); } else { LOG_ERROR_STR(fileName); result = false; } } ; return result; } public: cEpgfixerList() { fileName = NULL; } ~cEpgfixerList() { free(fileName); } void Clear(void) { cList::Clear(); } bool ReloadConfigFile(bool AllowComments = true) { Clear(); return LoadConfigFile(AllowComments); } bool Apply(PARAMETER *Parameter) { int res = false; LISTITEM *item = (LISTITEM *)(cList::First()); while (item) { if (item->Enabled()) { int ret = item->LISTITEM::Apply(Parameter); if (ret && !res) res = true; } item = (LISTITEM *)(item->Next()); } return res; } void SetConfigFile(const char *FileName) { fileName = strdup(FileName); } const char *GetConfigFile() { return fileName; } }; #endif //__EPGFIXER_TOOLS_H_