summaryrefslogtreecommitdiff
path: root/mngurls.c
blob: e9f18d0cecf88a8b4c144080cd7dc53cf3406198 (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
/*
 * mgnurls.c: VDR on Smart TV plugin
 *
 * Copyright (C) 2012, 2013 T. Lohmar
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 */

#include "mngurls.h"


cManageUrls::cManageUrls(string dir): mLog(), mFile(NULL), mEntries() {
  mLog = Log::getInstance();

  loadEntries(dir);
  mFile = new ofstream((dir +"/urls.txt").c_str(), ios::out | ios::app);  
  mFile->seekp(ios_base::end);
};

cManageUrls::~cManageUrls() {
  if (mFile != NULL) {
    mFile->close();
    delete mFile;
  }
  //TODO: delete entries
};

//called from outside to add an entry
void cManageUrls::appendEntry(string type, string url) {
  // iter through entries
  *(mLog->log()) << " cManageUrls::appendEntry: type= " << type << "url= " << url << endl;

  bool found = false;
  if (type.compare("YT") !=0) {
    return;
  }
  for (int i = 0; i < mEntries.size(); i ++) {
    if (url.compare(mEntries[i]->mEntry) == 0) {
      found = true;
      break;
    }
  }
  if (!found) {
    *(mLog->log()) << " cManageUrls::appendEntry: Appending... " << endl;
    mEntries.push_back (new sUrlEntry (type, url));
    appendToFile(type+"|"+url);
  }
}

size_t cManageUrls::size() { 
  return mEntries.size(); 
} 

sUrlEntry* cManageUrls::getEntry( int index)  { 
  return mEntries[index]; 
};

void cManageUrls::loadEntries(string dir) {
  ifstream myfile ((dir +"/urls.txt").c_str());
  string line;

  string type;

  while ( myfile.good() ) {
    getline (myfile, line);

    if ((line == "") or (line[0] == '#'))
      continue;

    size_t pos = line.find('|');
    string type = line.substr(0, pos);
    string value = line.substr(pos+1);

    //    sUrlEntry* entry = new sUrlEntry(type, value);
    mEntries.push_back(new sUrlEntry(type, value));
  }
  myfile.close();
};

void cManageUrls::appendToFile(string s_line) {
  if (mFile == NULL) {
    *(mLog->log()) << " ERROR in cManageUrls::appendToFile: no file open... " << endl;
    return;
  }
  *(mLog->log()) << " cManageUrls::appendToFile: writing  " << s_line << endl;
  *mFile << s_line;
  //  mFile->write(s_line.c_str(), s_line.size());

  mFile->flush();

}