diff options
Diffstat (limited to 'libtemplate/templatefunction.c')
| -rw-r--r-- | libtemplate/templatefunction.c | 141 |
1 files changed, 139 insertions, 2 deletions
diff --git a/libtemplate/templatefunction.c b/libtemplate/templatefunction.c index 4b0c66f..2a6e5a3 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<eParamType, int>(type, val));
} else {
+ numericDynamicParameters.erase(type);
numericDynamicParameters.insert(pair<eParamType, string>(type, parsedValue));
}
return param.Valid();
@@ -844,7 +877,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 +890,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 +979,34 @@ void cTemplateFunction::ParseConditionalTextToken(string &value, size_t start, s }
+void cTemplateFunction::ParsePrintfTextToken(string &value, size_t start, size_t end) {
+ cTextToken token;
+ 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<string> 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 +1139,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 +1354,21 @@ int cTemplateFunction::CalculateTextBoxHeight(void) { return ((textLinesTall+textLinesFull) * fontHeight);
}
+int cTemplateFunction::ReplaceIntToken(string intTok) {
+ if (intTokens) {
+ map<string,int>::iterator hit = intTokens->find(intTok);
+ if (hit != intTokens->end())
+ return hit->second;
+ }
+ if (stringTokens) {
+ map<string,string>::iterator hit = stringTokens->find(intTok);
+ if (hit != stringTokens->end()) {
+ return atoi(hit->second.c_str());
+ }
+ }
+ return 0;
+}
+
/*******************************************************************
* Helper Functions
*******************************************************************/
@@ -1466,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<cTextToken>::iterator it2 = (*it).subTokens.begin(); it2 != (*it).subTokens.end(); it2++) {
@@ -1478,6 +1610,11 @@ void cTemplateFunction::Debug(void) { esyslog("skindesigner: %s \"%s\"", tokTypeCond.c_str(), (*it2).value.c_str());
}
}
+ if (tokenType == ttPrintfToken) {
+ for (vector<string>::iterator it2 = (*it).parameters.begin(); it2 != (*it).parameters.end(); it2++) {
+ esyslog("skindesigner: Printf parameter: %s", (*it2).c_str());
+ }
+ }
}
}
if (fontName.size() > 0) {
|
