strTemplateCreate (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

ALL

Firmware version:

2.10.00


strTemplateCreate parses the given string and generates a string template based on it.

String templates can be used to quickly replace specific sub-strings in a large string with another string.

 

It detects names surrounded by specific characters, which can then be replaced when generating new strings.

Using the default sov of "${" and eov of "}", it matches e.g. "${variable}".

 

The string template functions are only available in NX32L compilation mode.

 

String templates use the following steps:

1.Call strTemplateCreate with the source string to generate the template

2.Optional: Use strTemplateVarInfo to examine the found variables.

3.Use strTemplateSetVar to set the value to use for the variables.

4.Call strTemplateGenerateString to generate a string with the new variables.

5.Repeat step 3 and 4 as many times as needed.

6.Call strTemplateFree to release the string template.

 

Note: To avoid having to escape special characters in the source string, the string can be defined as a raw string, see STRING_BEGIN for more details.

 

Input:

str : STRING

The string to use as template.

 

sov : STRING (Default "$${" )

The start-of-variable string.

 

eov : STRING (Default "}" )

The end-of-variable string.

 

Output:

st: SYSHANDLE

A handle to the string template.

 

Returns: INT

1

- Success

0

- Function is not supported.

-1

- Failed to create handle.

-2

- Failed to parse string.

-3

- Not enough resources to parse string.

-4

- sov or eov is invalid.

 

 

 

Declaration:

FUNCTION strTemplateCreate;
VAR_INPUT
  st       : ACCESS SYSHANDLE;
  str     : STRING;
  sov     : STRING := "$${";
  eov     : STRING := "}";
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
 
PROGRAM test;
VAR
  st       : SYSHANDLE;
  rc       : INT;
  str_in   : STRING;
  str_out  : STRING;
END_VAR;
 
  str_in := "Time since reset: $${time}, Temperature: $${temp}, Values: $"$${time}, $${temp}, $${value}$"";
 
  // Parse string and create template.
  rc := strTemplateCreate(st:=st, str:=str_in, sov := "$${", eov := "}");
  DebugFmt(message:="strTemplateCreate: \1", v1:=rc);
 
  // Set the value for the fixed variable
  rc := strTemplateSetVar(st:=st, name:="value", value := "value");
  DebugFmt(message:="strTemplateSetVar: \1", v1:=rc);
 
BEGIN
  // Set the value for the dynamic variables
  rc := strTemplateSetVar(st:=st, name:="time", value:=dintToStr(v:=boardTimeSinceReset()/1000));
  DebugFmt(message:="strTemplateSetVar: \1", v1:=rc);
 
  rc := strTemplateSetVar(st:=st, name:="temp", value:=floatToStr(v:=FLOAT(boardTemperature())/100.0));
  DebugFmt(message:="strTemplateSetVar: \1", v1:=rc);
 
  // Generate string
  rc := strTemplateGenerateString(st:=st, str:=str_out);
  DebugFmt(message:="strTemplateGenerateString: \1", v1:=rc);
 
  // Print string to device output
  DebugMsg(message:=str_out);
 
  Sleep(delay:=10000);
END;
 
END_PROGRAM;