#include #include #include #include #include #include #include "helpers.h" #include cPlugin *GetScraperPlugin(void) { static cPlugin *pScraper = cPluginManager::GetPlugin("scraper2vdr"); if( !pScraper ) // if it doesn't exit, try tvscraper pScraper = cPluginManager::GetPlugin("tvscraper"); return pScraper; } cSize ScaleToFit(int widthMax, int heightMax, int widthOriginal, int heightOriginal) { int width = 1; int height = 1; if ((widthMax == 0)||(heightMax==0)||(widthOriginal==0)||(heightOriginal==0)) return cSize(width, height); if ((widthOriginal <= widthMax) && (heightOriginal <= heightMax)) { width = widthOriginal; height = heightOriginal; } else if ((widthOriginal > widthMax) && (heightOriginal <= heightMax)) { width = widthMax; height = (double)width/(double)widthOriginal * heightOriginal; } else if ((widthOriginal <= widthMax) && (heightOriginal > heightMax)) { height = heightMax; width = (double)height/(double)heightOriginal * widthOriginal; } else { width = widthMax; height = (double)width/(double)widthOriginal * heightOriginal; if (height > heightMax) { height = heightMax; width = (double)height/(double)heightOriginal * widthOriginal; } } return cSize(width, height); } int Minimum(int a, int b, int c, int d, int e, int f) { int min = a; if (b < min) min = b; if (c < min) min = c; if (d < min) min = d; if (e < min) min = e; if (f < min) min = f; return min; } string StrToLowerCase(string str) { string lowerCase = str; const int length = lowerCase.length(); for(int i=0; i < length; ++i) { lowerCase[i] = std::tolower(lowerCase[i]); } return lowerCase; } bool isNumber(const string& s) { string::const_iterator it = s.begin(); while (it != s.end() && std::isdigit(*it)) ++it; return !s.empty() && it == s.end(); } bool IsToken(const string& token) { if ((token.find("{") == 0) && (token.find("}") == (token.size()-1))) return true; return false; } bool FileExists(const string &fullpath) { struct stat buffer; if (stat (fullpath.c_str(), &buffer) == 0) { return true; } if (config.debugImageLoading) { dsyslog("skindesigner: did not find %s", fullpath.c_str()); } return false; } bool FileExists(const string &path, const string &name, const string &ext) { stringstream fileName; fileName << path << name << "." << ext; struct stat buffer; if (stat (fileName.str().c_str(), &buffer) == 0) { return true; } if (config.debugImageLoading) { dsyslog("skindesigner: did not find %s", fileName.str().c_str()); } return false; } bool FolderExists(const string &path) { struct stat buffer; return stat(path.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode); } bool FirstFileInFolder(string &path, string &extension, string &fileName) { DIR *folder = NULL; struct dirent *file; folder = opendir(path.c_str()); if (!folder) return false; while (file = readdir(folder)) { if (endswith(file->d_name, extension.c_str())) { string currentFileName = file->d_name; int strlength = currentFileName.size(); if (strlength < 8) continue; fileName = currentFileName; return true; } } return false; } void CreateFolder(string &path) { cString command = cString::sprintf("mkdir -p %s", path.c_str()); int ok = system(*command); if (!ok) {} } // trim from start string <rim(string &s) { s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun(isspace)))); return s; } // trim from end string &rtrim(string &s) { s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), s.end()); return s; } // trim from both ends string &trim(string &s) { return ltrim(rtrim(s)); } // split: receives a char delimiter; returns a vector of strings // By default ignores repeated delimiters, unless argument rep == 1. vector& splitstring::split(char delim, int rep) { if (!flds.empty()) flds.clear(); // empty vector if necessary string work = data(); string buf = ""; int i = 0; while (i < (int)work.length()) { if (work[i] != delim) buf += work[i]; else if (rep == 1) { flds.push_back(buf); buf = ""; } else if (buf.length() > 0) { flds.push_back(buf); buf = ""; } i++; } if (!buf.empty()) flds.push_back(buf); return flds; } cStopWatch::cStopWatch(const char* message) { start = cTimeMs::Now(); last = start; if (message) { dsyslog("skindesigner: Starting StopWatch %s", message); } } void cStopWatch::Report(const char* message) { dsyslog("skindesigner: %s - needed %d ms", message, (int)(cTimeMs::Now() - last)); last = cTimeMs::Now(); } void cStopWatch::Stop(const char* message) { dsyslog("skindesigner: %s - needed %d ms", message, (int)(cTimeMs::Now() - start)); } string GetTimeString(int seconds) { time_t sec(seconds); tm *p = gmtime(&sec); int hours = p->tm_hour; int mins = p->tm_min; int secs = p->tm_sec; if (hours > 0) { return *cString::sprintf("%d:%02d:%02d", hours, mins, secs); } return *cString::sprintf("%02d:%02d", mins, secs);; } //View Helpers string GetScreenResolutionString(int width, int height, bool *isHD, bool *isUHD) { // TODO: try to get more information from information sources about interlace/progressive // cDevice::PrimaryDevice()->GetVideoSize is NOT providing enough information *isHD = false; // default *isUHD = false; // default string name = ""; switch (height) { case 4320: // 7680 x 4320 = 8K UHD name = "uhd4320p"; *isHD = true; *isUHD = true; break; case 2160: // 3840 x 2160 = 4K UHD name = "uhd2160p"; *isHD = true; *isUHD = true; break; case 1440: // 2560 x 1440 = QHD name = "hd1440p"; *isHD = true; break; case 1080: name = "hd1080i"; // 'i' is default, 'p' can't be detected currently *isHD = true; break; case 720: name = "hd720p"; // 'i' is not defined in standards *isHD = true; break; case 576: name = "sd576i"; // assumed 'i' break; case 480: name = "sd480i"; // assumed 'i' break; default: name = "unknown"; break; } return name; } string GetScreenAspectString(double aspect, bool *isWideScreen) { string name = ""; *isWideScreen = false; if (aspect == 4.0/3.0) { name = "4:3"; *isWideScreen = false; } else if (aspect == 16.0/9.0) { name = "16:9"; *isWideScreen = true; } else if (aspect == 2.21) { name = "21:9"; *isWideScreen = true; } return name; }