Child: [9d3075] (diff)

Download this file

cache.cpp    125 lines (112 with data), 2.7 kB

  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
#include "mpdas.h"
CCache* Cache = 0;
void
CCache::SaveCache()
{
std::string path = getenv("HOME");
path.append("/.mpdascache");
remove(path.c_str());
std::ofstream ofs(path.c_str());
if(!_entries.size()) {
remove(path.c_str());
return;
}
for(unsigned int i = 0; i < _entries.size(); i++) {
centry_t* entry = _entries[i];
if(entry->album)
ofs << true << "\n";
ofs << entry->artist << "\n";
ofs << entry->title << "\n";
ofs << entry->time << "\n";
ofs << entry->starttime;
if(entry->album)
ofs << "\n" << entry->album;
if(i+1 == _entries.size())
ofs.flush();
else
ofs << std::endl;
}
ofs.close();
}
void
CCache::LoadCache()
{
int length;
std::string path = getenv("HOME");
path.append("/.mpdascache");
std::ifstream ifs(path.c_str(), std::ios::in|std::ios::binary);
ifs.seekg (0, std::ios::end);
length = ifs.tellg();
ifs.seekg (0, std::ios::beg);
while(ifs.good()) {
if(length == ifs.tellg())
break;
std::string artist, album, title;
bool gotalbum = false;
int time;
time_t starttime;
ifs >> gotalbum;
ifs.ignore(1);
ifs >> artist;
ifs >> title;
ifs >> time;
ifs.ignore(1);
ifs >> starttime;
ifs.ignore(1);
if(gotalbum)
getline(ifs, album);
AddToCache(time, artist, title, album, starttime, true);
}
ifs.close();
remove(path.c_str());
}
void
CCache::WorkCache()
{
if(_failtime && time(NULL) - _failtime < 300) {
return;
}
_failtime = 0;
while(_entries.size()) {
if(AudioScrobbler->Scrobble(_entries.front())) {
curl_free((void*)_entries.front()->artist);
curl_free((void*)_entries.front()->title);
if(_entries.front()->album)
curl_free((void*)_entries.front()->album);
delete _entries.front();
_entries.erase(_entries.begin());
}
else {
eprintf("%s", "Error scrobbling. Trying again in 5 minutes.");
_failtime = time(NULL);
break;
}
sleep(1);
}
SaveCache();
}
void
CCache::AddToCache(int time, std::string artist, std::string title, std::string album, time_t starttime, bool escaped = false)
{
centry_t* entry = new centry_t;
bzero(entry, sizeof(centry_t));
entry->time = time;
if(!escaped) {
entry->artist = (char*)curl_escape(artist.c_str(), 0);
entry->title = (char*)curl_escape(title.c_str(), 0);
if(album.size())
entry->album = (char*)curl_escape(album.c_str(), 0);
} else {
entry->artist = new char[artist.size()+1];
strcpy(entry->artist, artist.c_str());
entry->title = new char[title.size()+1];
strcpy(entry->title, title.c_str());
if(album.size()) {
entry->album = new char[album.size()+1];
strcpy(entry->album, album.c_str());
}
}
entry->starttime = starttime;
_entries.push_back(entry);
SaveCache();
}