-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiledatemanager.c
156 lines (138 loc) · 5.06 KB
/
filedatemanager.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sstream>
#include <vdr/recording.h>
#include "tools.h"
#include "lib/common.h"
#include "filedatemanager.h"
cFileDateManager::cFileDateManager(void) {
curPath = "";
}
cFileDateManager::~cFileDateManager(void) {
files.clear();
}
// add new entry to files map
void cFileDateManager::AddFileValue(string fileName, long lastUpdated, bool used) {
sFileValue v;
v.lastupdated = lastUpdated;
v.used = used;
// search for existing entry
map<string, sFileValue>::iterator hit = files.find(fileName);
if (hit != files.end())
files.erase(hit); // delete existing entry for this filename
files.insert(pair<string, sFileValue>(fileName, v));
}
// Fill list with values from information file
bool cFileDateManager::LoadFileDateList(string path, bool used) {
files.clear(); // clear old values
curPath = path;
// build filename
stringstream fPath("");
fPath << path << "/" << LASTUPDATEDFILENAME;
string fileName = fPath.str();
bool fExists = FileExists(fileName,false);
if (!fExists) {
// tell(1,"no %s found in %s",LASTUPDATEDFILENAME,path.c_str());
return true; // file not available
}
// file available, read all non empty lines
vector<string> lastupdatedLines;
FILE *f = fopen(fileName.c_str(), "r");
if (!f) {
tell(0, "failed to read %s", fileName.c_str());
return false;
}
cReadLine ReadLine;
char *line;
while ((line = ReadLine.Read(f)) != NULL) {
lastupdatedLines.push_back(line);
}
fclose(f);
string newFileName = "", newLastUpdated = "";
int numLines = lastupdatedLines.size();
for (int line=0; line < numLines; line++) {
lastupdatedLines[line] = trim(lastupdatedLines[line]);
// split at =
splitstring s(lastupdatedLines[line].c_str());
vector<string> flds = s.split('=');
if (flds.size() == 2) {
AddFileValue(trim(flds[0]),atoi(trim(flds[1]).c_str()),used); // use given value as default
} else {
tell(0, "invalid value in %s", fileName.c_str());
return false;
}
}
return true;
}
// Save list to information file in curPath
bool cFileDateManager::SaveFileDateList(void) {
// build filename
stringstream fPath("");
fPath << curPath << "/" << LASTUPDATEDFILENAME;
string fileName = fPath.str();
FILE *f = fopen(fileName.c_str(), "w"); // overwrite current file
if (!f) {
tell(0, "failed to write to %s", fileName.c_str());
return false;
}
for (map<string, sFileValue>::iterator it = files.begin(); it != files.end(); it++) {
sFileValue v = it->second;
if (v.used) {
// only save used values to new file
fPath.str("");
fPath << it->first << " = " << v.lastupdated << "\n";
fputs(fPath.str().c_str(),f);
}
}
fclose(f);
return true;
}
// result = true if image not exist in files, or new lastUpdated > saved lastUpdated
// function add filename to list if is new, try to read lastUpdated from filedate if available
bool cFileDateManager::CheckImageNeedRefresh(string fileName, long lastUpdated) {
// build filename
stringstream fPath("");
fPath << curPath << "/" << fileName;
string fullFileName = fPath.str();
bool fExists = FileExists(fullFileName,true);
long fileLastUpdated = 0;
// search for existing entry
map<string, sFileValue>::iterator hit = files.find(fileName);
if (hit != files.end()) {
// filename exists in list, check lastUpdated
hit->second.used = true;
return (!fExists) || (hit->second.lastupdated < lastUpdated);
} else {
// new file, check if we can read filedate of existing file
if (fExists)
fileLastUpdated = fileModTime(fullFileName.c_str());
AddFileValue(fileName,fileLastUpdated,true); // add with lastupdated from file (if available)
return fileLastUpdated < lastUpdated;
}
}
// same as above, but check if thumbfile exist
bool cFileDateManager::CheckImageNeedRefreshThumb(string fileName, string thumbfilename, long lastUpdated) {
// build thumb filename
stringstream fPath("");
fPath << curPath << "/" << thumbfilename;
string fullFileName = fPath.str();
bool fExists = FileExists(fullFileName,true);
return (CheckImageNeedRefresh(fileName, lastUpdated) || (!fExists)); // thumb file not exist, or real file need update
}
// set lastUpdated for image (create new entry in list if is new)
void cFileDateManager::SetLastUpdated(string fileName, long lastUpdated) {
// search for existing entry
map<string, sFileValue>::iterator hit = files.find(fileName);
if (hit != files.end()) {
hit->second.used = true;
hit->second.lastupdated = lastUpdated;
} else {
// new file, add to list
AddFileValue(fileName,lastUpdated,true);
}
}
// delete image from list
void cFileDateManager::DeleteImage(string fileName) {
map<string, sFileValue>::iterator hit = files.find(fileName);
if (hit != files.end()) {
files.erase(hit);
}
}