Skip to content

Commit

Permalink
reindent
Browse files Browse the repository at this point in the history
  • Loading branch information
hrkfdn committed Nov 6, 2017
1 parent 64b775c commit 3270046
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 540 deletions.
460 changes: 230 additions & 230 deletions audioscrobbler.cpp

Large diffs are not rendered by default.

78 changes: 39 additions & 39 deletions audioscrobbler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@

class CAudioScrobbler
{
public:
CAudioScrobbler(CConfig *cfg);
~CAudioScrobbler();

void Handshake();
std::string CreateScrobbleMessage(int index, const CacheEntry& entry);
bool Scrobble(const CacheEntry& entry);
void ReportResponse(char* buf, size_t size);
bool LoveTrack(const Song& song, bool unlove = false);
bool SendNowPlaying(const Song& song);
void Failure();
private:
std::string GetServiceURL();
void OpenURL(std::string url, const char* postfields, char* errbuf);
bool CheckFailure(std::string response);

CConfig *_cfg;
CURL* _handle;

std::string _password;
std::string _response;

std::string _sessionid;

bool _authed;
int _failcount;
public:
CAudioScrobbler(CConfig *cfg);
~CAudioScrobbler();

void Handshake();
std::string CreateScrobbleMessage(int index, const CacheEntry& entry);
bool Scrobble(const CacheEntry& entry);
void ReportResponse(char* buf, size_t size);
bool LoveTrack(const Song& song, bool unlove = false);
bool SendNowPlaying(const Song& song);
void Failure();
private:
std::string GetServiceURL();
void OpenURL(std::string url, const char* postfields, char* errbuf);
bool CheckFailure(std::string response);

CConfig *_cfg;
CURL* _handle;

std::string _password;
std::string _response;

std::string _sessionid;

bool _authed;
int _failcount;
};

class CLastFMMessage
{
public:
CLastFMMessage(CURL *curl_handle) { this->curl_handle = curl_handle; }
void AddField(std::string name, std::string value) { valueMap[name] = value; }
void AddField(std::string name, int value) {
std::ostringstream str;
str << value;
AddField(name, str.str());
}
std::string GetMessage();
private:
std::map<std::string, std::string> valueMap;
std::string GetSignatureHash();
CURL *curl_handle;
public:
CLastFMMessage(CURL *curl_handle) { this->curl_handle = curl_handle; }
void AddField(std::string name, std::string value) { valueMap[name] = value; }
void AddField(std::string name, int value) {
std::ostringstream str;
str << value;
AddField(name, str.str());
}
std::string GetMessage();
private:
std::map<std::string, std::string> valueMap;
std::string GetSignatureHash();
CURL *curl_handle;
};

extern CAudioScrobbler* AudioScrobbler;
Expand Down
154 changes: 77 additions & 77 deletions cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,111 +4,111 @@ 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++) {
CacheEntry* entry = _entries[i];
ofs << *entry;

if(i+1 == _entries.size())
ofs.flush();
else
ofs << std::endl;
}
ofs.close();
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++) {
CacheEntry* entry = _entries[i];
ofs << *entry;

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);
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);
ifs.seekg (0, std::ios::end);
length = ifs.tellg();
ifs.seekg (0, std::ios::beg);


while(ifs.good()) {
if(length == ifs.tellg())
break;
while(ifs.good()) {
if(length == ifs.tellg())
break;

CacheEntry *entry = new CacheEntry();
ifs >> *entry;
_entries.push_back(entry);
}
CacheEntry *entry = new CacheEntry();
ifs >> *entry;
_entries.push_back(entry);
}

ifs.close();
remove(path.c_str());
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())) {
delete _entries.front();
_entries.erase(_entries.begin());
}
else {
eprintf("%s", "Error scrobbling. Trying again in 5 minutes.");
_failtime = time(NULL);
AudioScrobbler->Failure();
break;
}
sleep(1);
}
SaveCache();
if(_failtime && time(NULL) - _failtime < 300) {
return;
}
_failtime = 0;
while(_entries.size()) {
if(AudioScrobbler->Scrobble(*_entries.front())) {
delete _entries.front();
_entries.erase(_entries.begin());
}
else {
eprintf("%s", "Error scrobbling. Trying again in 5 minutes.");
_failtime = time(NULL);
AudioScrobbler->Failure();
break;
}
sleep(1);
}
SaveCache();
}

void CCache::AddToCache(const Song& song, time_t starttime)
{
CacheEntry *entry = new CacheEntry(song, starttime);
CacheEntry *entry = new CacheEntry(song, starttime);

_entries.push_back(entry);
SaveCache();
_entries.push_back(entry);
SaveCache();
}

std::ofstream& operator <<(std::ofstream& outstream, const CacheEntry& inobj)
{
Song song = inobj.getSong();
outstream << song.getArtist() << std::endl
<< song.getTitle() << std::endl
<< song.getAlbum() << std::endl
<< song.getDuration() << std::endl
<< inobj.getStartTime();

return outstream;
Song song = inobj.getSong();
outstream << song.getArtist() << std::endl
<< song.getTitle() << std::endl
<< song.getAlbum() << std::endl
<< song.getDuration() << std::endl
<< inobj.getStartTime();

return outstream;
}

std::ifstream& operator >>(std::ifstream& instream, CacheEntry& outobj)
{
std::string artist, title, album;
int duration;
time_t starttime;
std::string artist, title, album;
int duration;
time_t starttime;

getline(instream, artist);
getline(instream, title);
getline(instream, album);
getline(instream, artist);
getline(instream, title);
getline(instream, album);

instream >> duration;
instream.ignore(1);
instream >> starttime;
instream.ignore(1);
instream >> duration;
instream.ignore(1);
instream >> starttime;
instream.ignore(1);

Song song(artist, title, album, duration);
outobj = CacheEntry(song, starttime);
Song song(artist, title, album, duration);
outobj = CacheEntry(song, starttime);

return instream;
return instream;
}
46 changes: 23 additions & 23 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@

class CacheEntry
{
public:
CacheEntry() {};
CacheEntry(const Song& song, time_t starttime) {
this->song = song;
this->starttime = starttime;
}
public:
CacheEntry() {};
CacheEntry(const Song& song, time_t starttime) {
this->song = song;
this->starttime = starttime;
}

// stream serialization
friend std::ofstream& operator <<(std::ofstream& outstream, const CacheEntry& inobj);
friend std::ifstream& operator >>(std::ifstream& instream, CacheEntry& outobj);
// stream serialization
friend std::ofstream& operator <<(std::ofstream& outstream, const CacheEntry& inobj);
friend std::ifstream& operator >>(std::ifstream& instream, CacheEntry& outobj);

Song getSong() const { return song; }
time_t getStartTime() const { return starttime; }
private:
Song song;
time_t starttime;
Song getSong() const { return song; }
time_t getStartTime() const { return starttime; }
private:
Song song;
time_t starttime;
};

class CCache
{
public:
CCache() { _failtime = 0; }
void AddToCache(const Song& song, time_t starttime);
void WorkCache();
void SaveCache();
void LoadCache();
private:
time_t _failtime;
std::vector<CacheEntry*> _entries;
public:
CCache() { _failtime = 0; }
void AddToCache(const Song& song, time_t starttime);
void WorkCache();
void SaveCache();
void LoadCache();
private:
time_t _failtime;
std::vector<CacheEntry*> _entries;
};

extern CCache* Cache;
Expand Down
16 changes: 8 additions & 8 deletions config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ int IniHandler(void* param, const char* section, const char* name, const char* v

// strip quotes if they exist to allow passwords to begin with a whitespace
if(val.length() >= 2 && val[0] == '\"' && val[val.length()-1] == '\"') {
val.erase(0, 1);
val.erase(val.length() - 1);
val.erase(0, 1);
val.erase(val.length() - 1);
}

config->Set(name, val);
Expand All @@ -19,14 +19,14 @@ int IniHandler(void* param, const char* section, const char* name, const char* v
void CConfig::LoadConfig(std::string path)
{
if(ini_parse(path.c_str(), &IniHandler, this) < 0) {
iprintf("Cannot parse config file (%s).", path.c_str());
return;
iprintf("Cannot parse config file (%s).", path.c_str());
return;
}
}
std::string CConfig::Get(std::string name)
{
if(_configuration.find(name) == _configuration.end()) {
return "";
return "";
}

return _configuration.find(name)->second;
Expand Down Expand Up @@ -59,11 +59,11 @@ CConfig::CConfig(char* cfg)
std::string path = "";

if(!cfg) {
path = CONFDIR;
path.append("/mpdasrc");
path = CONFDIR;
path.append("/mpdasrc");
}
else {
path = cfg;
path = cfg;
}

LoadConfig(path);
Expand Down
Loading

0 comments on commit 3270046

Please sign in to comment.