/* * Copyright (C) 2006-2009 Alex Lasnier * * This file is part of Radio Info * * 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, see . */ #include #include #include "cache.h" #include "tools.h" #define PID_KEY(NID,TSID,SID) ((((uint64_t) NID) << 32) | (((uint64_t) TSID) << 16) | ((uint64_t) SID)) /////////////////////////////////////////////////////////////////////////////// PidCache::PidCache(void) { fileName = NULL; } //---------------------------------------------------------------------------- PidCache::~PidCache() { Save(); free(fileName); } //---------------------------------------------------------------------------- int PidCache::Pid(uint16_t nid, uint16_t tid, uint16_t sid) { std::map::const_iterator itr = pids.find(PID_KEY(nid, tid, sid)); return (itr != pids.end()) ? (*itr).second : -1; } //---------------------------------------------------------------------------- void PidCache::SetPid(uint16_t nid, uint16_t tid, uint16_t sid, uint16_t pid) { pids[PID_KEY(nid, tid, sid)] = pid; } //---------------------------------------------------------------------------- void PidCache::Load(void) { if (fileName) { free(fileName); fileName = NULL; } asprintf(&fileName, "%s/radioinfo.cache", cPlugin::ConfigDirectory()); if (!ReadFile()) dprint(L_ERR, "Failed to load PID cache"); } //---------------------------------------------------------------------------- void PidCache::Save(void) { FILE* file = NULL; file = fopen(fileName, "w"); if (file == NULL) { dprint(L_ERR, "Failed to write PIDs to cache."); return; } std::map::const_iterator itr = pids.begin(); for ( ; itr != pids.end(); ++itr) { int nid = ((*itr).first >> 32) & 0xFFFF; int tid = ((*itr).first >> 16) & 0xFFFF; int sid = (*itr).first & 0xFFFF; int pid = (*itr).second; fprintf(file, "%d:%d:%d:%d\n", sid ,nid ,tid, pid); } fclose(file); } //---------------------------------------------------------------------------- bool PidCache::ReadFile(void) { FILE* file = NULL; file = fopen(fileName, "r"); if (file == NULL) { // Create file if it doesn't exist return false; } char* s; int line = 0; cReadLine ReadLine; bool result = true; while ((s = ReadLine.Read(file)) != NULL) { line++; char *p = strchr(s, '#'); if (p) *p = 0; stripspace(s); if (!isempty(s)) { if (!Parse(s)) { dprint(L_ERR, "ERROR: error in %s, line %d", fileName, line); result = false; break; } } } fclose(file); return result; } //---------------------------------------------------------------------------- bool PidCache::Parse(const char* line) { int nid = 0; int sid = 0; int tid = 0; int pid = 0; if (4 == sscanf(line, "%d :%d :%d :%d", &sid ,&nid ,&tid, &pid) && (sid && nid && tid && pid)) { pids[PID_KEY(nid, tid, sid)] = pid; return true; } return false; } ///////////////////////////////////////////////////////////////////////////////