summaryrefslogtreecommitdiff
path: root/libtemplate/parameter.c
blob: 1675a8de71aa272dbf25b4c7c2d3398d6a33a2d7 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "../config.h"
#include "parameter.h"

using namespace std;

// --- cNumericParameter -------------------------------------------------------------

cNumericParameter::cNumericParameter(string value) { 
    this->value = value;
    globals = NULL;
    isValid = false;
    width = 0;
    height = 0;
    columnWidth = -1;
    rowHeight = -1;
    hor = true;
    defaultValue = 0;
}

cNumericParameter::~cNumericParameter(void) {
}

void cNumericParameter::SetAreaSize(int w, int h) {
    width = w;
    height = h;
}

int cNumericParameter::Parse(string &parsedValue) {
    int retVal = defaultValue;

    if (IsNumber(value)) {
        isValid = true;
        retVal = atoi(value.c_str());
        return retVal;
    }

    //checking for percent value
    bool isPercentValue = CheckPercentValue(retVal);
    if (isPercentValue) {
        isValid = true;
        return retVal;
    }

    //checking for expression
    bool isValidExpression = CheckExpression(retVal, parsedValue);
    if (isValidExpression) {
        isValid = true;
        return retVal;
    }

    return retVal;
}

bool cNumericParameter::IsNumber(const string& s) {
    string::const_iterator it = s.begin();
    while (it != s.end() && isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}

bool cNumericParameter::CheckPercentValue(int &val) {
    bool ok = false;
    size_t posPercent = value.find('%');
    if (posPercent != string::npos) {
        string strPerc = value.substr(0, posPercent);
        if (!IsNumber(strPerc)) {
            return ok;
        }
        int perc = atoi(strPerc.c_str());
        if (hor) {
            val = width * perc / 100;
        } else {
            val = height * perc / 100;          
        }
        ok = true;
    }
    return ok;
}

bool cNumericParameter::CheckExpression(int &val, string &parsedVal) {
    bool ok = false;
    string parsedValue = value;
    //remove white spaces
    parsedValue.erase( std::remove_if( parsedValue.begin(), parsedValue.end(), ::isspace ), parsedValue.end() );

    //check and replace {areawidth} and {areaheight} tokens
    string tokenWidth = "{areawidth}";
    string tokenHeight = "{areaheight}";

    stringstream sw;
    sw << width;
    string strWidth = sw.str();
    stringstream sh;
    sh << height;
    string strHeight = sh.str();

    bool foundToken = true;
    while(foundToken) {
        size_t foundTokenWidth = parsedValue.find(tokenWidth);
        if (foundTokenWidth != string::npos) {
            parsedValue = parsedValue.replace(foundTokenWidth, tokenWidth.size(), strWidth);
        } else {
            foundToken = false;
        }
    }

    foundToken = true;
    while(foundToken) {
        size_t foundTokenHeight = parsedValue.find(tokenHeight);
        if (foundTokenHeight != string::npos) {
            parsedValue = parsedValue.replace(foundTokenHeight, tokenHeight.size(), strHeight);
        } else {
            foundToken = false;
        }
    }

    //check and replace {columnwidth} and {rowheight} tokens for loop functions
    if (columnWidth > 0 || rowHeight > 0) {
        tokenWidth = "{columnwidth}";
        tokenHeight = "{rowheight}";
        stringstream cw;
        cw << columnWidth;
        strWidth = cw.str();
        stringstream rh;
        rh << rowHeight;
        strHeight = rh.str();

        foundToken = true;
        while(foundToken) {
            size_t foundTokenWidth = parsedValue.find(tokenWidth);
            if (foundTokenWidth != string::npos) {
                parsedValue = parsedValue.replace(foundTokenWidth, tokenWidth.size(), strWidth);
            } else {
                foundToken = false;
            }
        }

        foundToken = true;
        while(foundToken) {
            size_t foundTokenHeight = parsedValue.find(tokenHeight);
            if (foundTokenHeight != string::npos) {
                parsedValue = parsedValue.replace(foundTokenHeight, tokenHeight.size(), strHeight);
            } else {
                foundToken = false;
            }
        }
    }

    if (globals) {
        globals->ReplaceIntVars(parsedValue);
        globals->ReplaceDoubleVars(parsedValue);
    }

    if (IsNumber(parsedValue)) {
        ok = true;
        val = atoi(parsedValue.c_str());
        return ok;
    }

    if (!ValidNumericExpression(parsedValue)) {
        parsedVal = parsedValue;
        return ok;
    }
    ok = true;
    char * expression = new char[parsedValue.size() + 1];
    std::copy(parsedValue.begin(), parsedValue.end(), expression);
    expression[parsedValue.size()] = '\0';
    int expRes = EvaluateTheExpression(expression);
    val = expRes;
    delete[] expression;
    return ok;
}

bool cNumericParameter::ValidNumericExpression(string &parsedValue) {
    string::const_iterator it = parsedValue.begin();
    while (it != parsedValue.end() && (isdigit(*it) || *it == '.' || *it == ',' || *it == '+' || *it == '-' || *it == '*' || *it == '/')) ++it;
    return !parsedValue.empty() && it == parsedValue.end();
}

int cNumericParameter::EvaluateTheExpression(char* expr) {
    return round(ParseSummands(expr));
}

double cNumericParameter::ParseAtom(char*& expr) {
    // Read the number from string
    char* end_ptr;
    double res = strtod(expr, &end_ptr);
    // Advance the pointer and return the result
    expr = end_ptr;
    return res;
}

// Parse multiplication and division
double cNumericParameter::ParseFactors(char*& expr) {
    double num1 = ParseAtom(expr);
    for(;;) {
        // Save the operation
        char op = *expr;
        if(op != '/' && op != '*')
            return num1;
        expr++;
        double num2 = ParseAtom(expr);
        // Perform the saved operation
        if(op == '/') {
            if (num2 != 0) {
                num1 /= num2;
            }
        } else
            num1 *= num2;
    }
}

// Parse addition and subtraction
double cNumericParameter::ParseSummands(char*& expr) {
    double num1 = ParseFactors(expr);
    for(;;) {
        char op = *expr;
        if(op != '-' && op != '+')
            return num1;
        expr++;
        double num2 = ParseFactors(expr);
        if(op == '-')
            num1 -= num2;
        else
            num1 += num2;
    }
}

// --- cConditionalParameter -------------------------------------------------------------

cConditionalParameter::cConditionalParameter(cGlobals *globals, string value) { 
    this->globals = globals;
    isTrue = false;
    this->value = value;
    type = cpNone;
}

cConditionalParameter::~cConditionalParameter(void) {
}

void cConditionalParameter::Tokenize(void) {
    size_t posAnd = value.find("++");
    if (posAnd != string::npos) {
        type = cpAnd;
        TokenizeValue("++");
    } else {
        size_t posOr = value.find("||");
        if (posOr != string::npos) {
            type = cpOr;
            TokenizeValue("||");
        }
    }
    if (type == cpNone) {
        InsertCondition(value);
    }
}

void cConditionalParameter::Evaluate(map < string, int > *intTokens, map < string, string > *stringTokens) {
    isTrue = false;
    bool first = true;
    for (vector<sCondition>::iterator cond = conditions.begin(); cond != conditions.end(); cond++) {
        bool tokenTrue = false;
        
        if (cond->type == ctStringSet) {
            if (stringTokens) {
                map < string, string >::iterator hit = stringTokens->find(cond->tokenName);
                if (hit != stringTokens->end()) {
                    string value = hit->second;
                    if (value.size() > 0)
                        tokenTrue = true;
                }
            }
        } else if (cond->type == ctStringEmpty) {
            if (stringTokens) {
                map < string, string >::iterator hit = stringTokens->find(cond->tokenName);
                if (hit != stringTokens->end()) {
                    string value = hit->second;
                    if (value.size() == 0)
                        tokenTrue = true;
                } else {
                    tokenTrue = true;
                }
            } else {
                tokenTrue = true;
            }
        } else {
            int tokenValue = EvaluateParameter(cond->tokenName, intTokens, stringTokens);
            if (cond->type == ctBool) {
                tokenTrue = tokenValue;
            } else if (cond->type == ctGreater) {
                tokenTrue = (tokenValue > cond->compareValue) ? true : false;
            } else if (cond->type == ctLower) {
                tokenTrue = (tokenValue < cond->compareValue) ? true : false;
            } else if (cond->type == ctEquals) {
                tokenTrue = (tokenValue == cond->compareValue) ? true : false;
            }
        }

        if (cond->isNegated)
            tokenTrue = !tokenTrue;
        if (type == cpAnd) {
            if (first)
                isTrue = tokenTrue;
            else
                isTrue = isTrue && tokenTrue;
        } else if (type == cpOr) {
            isTrue = isTrue || tokenTrue;           
        } else {
            isTrue = tokenTrue;
        }
        first = false;
    }
}

int cConditionalParameter::EvaluateParameter(string token, map < string, int > *intTokens, map < string, string > *stringTokens) {
    //first check globals
    int result = 0;
    if (globals->GetInt(token, result))
        return result;
    //then check custom tokens
    if (globals->GetCustomInt(token, result))
        return result;
    //then check tokens
    if (intTokens) {
        map < string, int >::iterator hit = intTokens->find(token);
        if (hit != intTokens->end()) {
            return hit->second;
        }
    }
    if (stringTokens) {
        map < string, string >::iterator hit = stringTokens->find(token);
        if (hit != stringTokens->end()) {
            string value = hit->second;
            return atoi(value.c_str());
        }
    }
    return 0;
}

void cConditionalParameter::TokenizeValue(string sep) {
    string buffer = value;
    bool sepFound = true;
    while (sepFound) {
        size_t posSep = buffer.find(sep);
        if (posSep == string::npos) {
            InsertCondition(buffer);
            sepFound = false;
        }
        string token = buffer.substr(0, posSep);
        buffer = buffer.replace(0, posSep + sep.size(), "");
        InsertCondition(token);
    }
}

void cConditionalParameter::InsertCondition(string cond) {
    cond.erase( std::remove_if( cond.begin(), cond.end(), ::isspace ), cond.end() );

    if (cond.size() < 1)
        return;

    size_t tokenStart = cond.find('{');
    size_t tokenEnd = cond.find('}');

    if (tokenStart == string::npos || tokenEnd == string::npos || tokenStart > tokenEnd)
        return;

    string tokenName = cond.substr(tokenStart + 1, tokenEnd - tokenStart - 1);
    string rest = cond.replace(tokenStart, tokenEnd - tokenStart + 1, "");

    sCondition sCond;
    sCond.tokenName = tokenName;
    sCond.type = ctBool;
    sCond.compareValue = 0;
    sCond.isNegated = false;
    if (!rest.compare("not")) {
        sCond.isNegated = true;
    } else if (!rest.compare("isset")) {
        sCond.type = ctStringSet;
    } else if (!rest.compare("empty")) {
        sCond.type = ctStringEmpty;
    } else if (startswith(rest.c_str(), "gt(")) {
        string compVal = rest.substr(4, rest.size() - 5);
        sCond.compareValue = atoi(compVal.c_str());
        sCond.type = ctGreater;
    }  else if (startswith(rest.c_str(), "lt(")) {
        string compVal = rest.substr(4, rest.size() - 5);
        sCond.compareValue = atoi(compVal.c_str());
        sCond.type = ctLower;
    }  else if (startswith(rest.c_str(), "eq(")) {
        string compVal = rest.substr(4, rest.size() - 5);
        sCond.compareValue = atoi(compVal.c_str());
        sCond.type = ctEquals;
    }

    conditions.push_back(sCond);
}

void cConditionalParameter::Debug(void) {
    dsyslog("skindesigner: Condition %s, Type: %s, cond is %s", value.c_str(), (type == cpAnd)?"and combination":((type == cpOr)?"or combination":"single param") , isTrue?"true":"false");
    for (vector<sCondition>::iterator it = conditions.begin(); it != conditions.end(); it++) {
        dsyslog("skindesigner: cond token %s, type: %d, compareValue %d, negated: %d", it->tokenName.c_str(), it->type, it->compareValue, it->isNegated);
    }
}