summaryrefslogtreecommitdiff
path: root/tools.h
blob: 259712fce302d79d6ddceddd5fc1dc18daca641f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * 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 <vdr/epg.h>
#include <vdr/tools.h>
#include <unistd.h>
#include <stdio.h>

#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 LISTITEM, class PARAMETER> class cEpgfixerList : public cList<LISTITEM>
{
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<LISTITEM>::Last()->SetFromString(s, true);
                   if (cList<LISTITEM>::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<LISTITEM>::Clear(); }
  bool ReloadConfigFile(bool AllowComments = true)
  {
    Clear();
    return LoadConfigFile(AllowComments);
  }
  bool Apply(PARAMETER *Parameter)
  {
    int res = false;
    LISTITEM *item = (LISTITEM *)(cList<LISTITEM>::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_