summaryrefslogtreecommitdiff
path: root/extensions/helpers.c
blob: 5e06d9535ccfc6ca467e853a2cdf8191e905ed1d (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <string>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include "helpers.h"
#include <vdr/skins.h>

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 &ltrim(string &s) {
    s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
    return s;
}

// trim from end
string &rtrim(string &s) {
    s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(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<string>& 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;
}