From f9f2c47cb258874dcdd75bd2154a60c65f068657 Mon Sep 17 00:00:00 2001 From: louis Date: Sat, 1 Nov 2014 13:03:52 +0100 Subject: added printf function for --- libtemplate/templatefunction.c | 93 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) (limited to 'libtemplate/templatefunction.c') diff --git a/libtemplate/templatefunction.c b/libtemplate/templatefunction.c index 4b0c66f..df3e653 100644 --- a/libtemplate/templatefunction.c +++ b/libtemplate/templatefunction.c @@ -844,7 +844,9 @@ bool cTemplateFunction::SetTextTokens(string value) { //search for conditional token or normal token size_t tokenStart = value.find_first_of('{'); size_t conditionStart = value.find_first_of('|'); + size_t printfStart = value.find("{printf("); if (tokenStart == string::npos && conditionStart == string::npos) { + //find constant strings if (value.size() > 0) { cTextToken token; token.type = ttConstString; @@ -855,12 +857,25 @@ bool cTemplateFunction::SetTextTokens(string value) { continue; } else if (tokenStart != string::npos && conditionStart == string::npos) { size_t tokenEnd = value.find_first_of('}'); - ParseTextToken(value, tokenStart, tokenEnd); + if (printfStart != string::npos && printfStart <= tokenStart) { + //replace printf text token + ParsePrintfTextToken(value, printfStart, tokenEnd); + } else { + //replace normal text token + ParseTextToken(value, tokenStart, tokenEnd); + } } else if (tokenStart != string::npos && conditionStart != string::npos) { if (tokenStart < conditionStart) { size_t tokenEnd = value.find_first_of('}'); - ParseTextToken(value, tokenStart, tokenEnd); + if (printfStart != string::npos && printfStart <= tokenStart) { + //replace printf text token + ParsePrintfTextToken(value, printfStart, tokenEnd); + } else { + //replace normal text token + ParseTextToken(value, tokenStart, tokenEnd); + } } else { + //replace conditional text token size_t conditionEnd = value.find_first_of('|', conditionStart+1); ParseConditionalTextToken(value, conditionStart, conditionEnd); } @@ -931,6 +946,26 @@ void cTemplateFunction::ParseConditionalTextToken(string &value, size_t start, s } +void cTemplateFunction::ParsePrintfTextToken(string &value, size_t start, size_t end) { + cTextToken token; + token.type = ttPrintfToken; + //fetch parameter list from printf + string printfParams = value.substr(start + 8, end - start - 9); + value = value.replace(0, end - start + 1, ""); + splitstring s(printfParams.c_str()); + vector flds = s.split(',', 1); + + int numParams = flds.size(); + if (numParams < 1) + return; + string formatString = trim(flds[0]); + token.value = formatString.substr(1, formatString.size() - 2); + for (int i=1; i < numParams; i++) { + token.parameters.push_back(trim(flds[i])); + } + textTokens.push_back(token); +} + bool cTemplateFunction::SetScrollMode(string value) { eScrollMode mode = smNone; bool ok = true; @@ -1063,6 +1098,45 @@ void cTemplateFunction::ParseStringParameters(void) { found = true; } } + } else if ((*it).type == ttPrintfToken) { + cTextToken token = *it; + int paramCount = token.parameters.size(); + string printfResult = ""; + switch (paramCount) { + case 1: { + int param1 = ReplaceIntToken(token.parameters[0]); + printfResult = *cString::sprintf(token.value.c_str(), param1); + break; } + case 2: { + int param1 = ReplaceIntToken(token.parameters[0]); + int param2 = ReplaceIntToken(token.parameters[1]); + printfResult = *cString::sprintf(token.value.c_str(), param1, param2); + break; } + case 3: { + int param1 = ReplaceIntToken(token.parameters[0]); + int param2 = ReplaceIntToken(token.parameters[1]); + int param3 = ReplaceIntToken(token.parameters[2]); + printfResult = *cString::sprintf(token.value.c_str(), param1, param2, param3); + break; } + case 4: { + int param1 = ReplaceIntToken(token.parameters[0]); + int param2 = ReplaceIntToken(token.parameters[1]); + int param3 = ReplaceIntToken(token.parameters[2]); + int param4 = ReplaceIntToken(token.parameters[3]); + printfResult = *cString::sprintf(token.value.c_str(), param1, param2, param3, param4); + break; } + case 5: { + int param1 = ReplaceIntToken(token.parameters[0]); + int param2 = ReplaceIntToken(token.parameters[1]); + int param3 = ReplaceIntToken(token.parameters[2]); + int param4 = ReplaceIntToken(token.parameters[3]); + int param5 = ReplaceIntToken(token.parameters[4]); + printfResult = *cString::sprintf(token.value.c_str(), param1, param2, param3, param4, param5); + break; } + default: + break; + } + text << printfResult; } } parsedText = text.str(); @@ -1239,6 +1313,21 @@ int cTemplateFunction::CalculateTextBoxHeight(void) { return ((textLinesTall+textLinesFull) * fontHeight); } +int cTemplateFunction::ReplaceIntToken(string intTok) { + if (intTokens) { + map::iterator hit = intTokens->find(intTok); + if (hit != intTokens->end()) + return hit->second; + } + if (stringTokens) { + map::iterator hit = stringTokens->find(intTok); + if (hit != stringTokens->end()) { + return atoi(hit->second.c_str()); + } + } + return 0; +} + /******************************************************************* * Helper Functions *******************************************************************/ -- cgit v1.2.3 From f478ad10bbac192819c98a2edab464fb6347d5e9 Mon Sep 17 00:00:00 2001 From: louis Date: Sat, 1 Nov 2014 16:02:56 +0100 Subject: optimized performance when creating a menu list --- libtemplate/templatefunction.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'libtemplate/templatefunction.c') diff --git a/libtemplate/templatefunction.c b/libtemplate/templatefunction.c index df3e653..699e872 100644 --- a/libtemplate/templatefunction.c +++ b/libtemplate/templatefunction.c @@ -272,6 +272,36 @@ bool cTemplateFunction::CalculateParameters(void) { return paramsValid; } +bool cTemplateFunction::ReCalculateParameters(void) { + bool paramValid = true; + bool paramsValid = true; + for (map< eParamType, string >::iterator param = nativeParameters.begin(); param != nativeParameters.end(); param++) { + paramValid = true; + eParamType type = param->first; + string value = param->second; + switch (type) { + case ptX: + case ptY: + case ptWidth: + case ptHeight: + case ptMenuItemWidth: + case ptFontSize: + case ptFloatWidth: + case ptFloatHeight: + case ptMaxLines: + case ptColumnWidth: + case ptRowHeight: + SetNumericParameter(type, value); + break; + } + if (!paramValid) { + paramsValid = false; + esyslog("skindesigner: %s: invalid parameter %d, value %s", GetFuncName().c_str(), type, value.c_str()); + } + } + return paramsValid; +} + void cTemplateFunction::CompleteParameters(void) { switch (type) { case ftDrawImage: { @@ -733,8 +763,11 @@ bool cTemplateFunction::SetNumericParameter(eParamType type, string value) { if (this->type < ftLoop && type == ptY) { val += containerY; } + numericParameters.erase(type); + numericDynamicParameters.erase(type); numericParameters.insert(pair(type, val)); } else { + numericDynamicParameters.erase(type); numericDynamicParameters.insert(pair(type, parsedValue)); } return param.Valid(); -- cgit v1.2.3 From 80ac2607514422cfd77efb3429e0f70fc8713c39 Mon Sep 17 00:00:00 2001 From: louis Date: Sun, 2 Nov 2014 11:19:05 +0100 Subject: fixed bug in printf function --- libtemplate/templatefunction.c | 43 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'libtemplate/templatefunction.c') diff --git a/libtemplate/templatefunction.c b/libtemplate/templatefunction.c index 699e872..2a6e5a3 100644 --- a/libtemplate/templatefunction.c +++ b/libtemplate/templatefunction.c @@ -981,20 +981,28 @@ void cTemplateFunction::ParseConditionalTextToken(string &value, size_t start, s void cTemplateFunction::ParsePrintfTextToken(string &value, size_t start, size_t end) { cTextToken token; - token.type = ttPrintfToken; - //fetch parameter list from printf - string printfParams = value.substr(start + 8, end - start - 9); - value = value.replace(0, end - start + 1, ""); - splitstring s(printfParams.c_str()); - vector flds = s.split(',', 1); - - int numParams = flds.size(); - if (numParams < 1) - return; - string formatString = trim(flds[0]); - token.value = formatString.substr(1, formatString.size() - 2); - for (int i=1; i < numParams; i++) { - token.parameters.push_back(trim(flds[i])); + if (start > 0) { + string constString = value.substr(0, start); + value = value.replace(0, start, ""); + token.type = ttConstString; + token.value = constString; + textTokens.push_back(token); + } else { + token.type = ttPrintfToken; + //fetch parameter list from printf + string printfParams = value.substr(start + 8, end - start - 9); + value = value.replace(0, end - start + 1, ""); + splitstring s(printfParams.c_str()); + vector flds = s.split(',', 1); + + int numParams = flds.size(); + if (numParams < 1) + return; + string formatString = trim(flds[0]); + token.value = formatString.substr(1, formatString.size() - 2); + for (int i=1; i < numParams; i++) { + token.parameters.push_back(trim(flds[i])); + } } textTokens.push_back(token); } @@ -1588,6 +1596,8 @@ void cTemplateFunction::Debug(void) { tokType = "Token: "; else if (tokenType == ttConditionalToken) tokType = "Conditional Token: "; + else if (tokenType == ttPrintfToken) + tokType = "PrintF Token: "; esyslog("skindesigner: %s %d = \"%s\"", tokType.c_str(), i++, (*it).value.c_str()); if (tokenType == ttConditionalToken) { for (vector::iterator it2 = (*it).subTokens.begin(); it2 != (*it).subTokens.end(); it2++) { @@ -1600,6 +1610,11 @@ void cTemplateFunction::Debug(void) { esyslog("skindesigner: %s \"%s\"", tokTypeCond.c_str(), (*it2).value.c_str()); } } + if (tokenType == ttPrintfToken) { + for (vector::iterator it2 = (*it).parameters.begin(); it2 != (*it).parameters.end(); it2++) { + esyslog("skindesigner: Printf parameter: %s", (*it2).c_str()); + } + } } } if (fontName.size() > 0) { -- cgit v1.2.3