Is it possible to define a CAPL function that returns a text string?

I am developing CAPL scripts in Vector CANoe and I need to define several functions that return text strings. In C, I would write something like this:

char * ErrorCodeToMsg(int code)

      

or

char [] ErrorCodeToMsg(int code)

      

In CAPL, both definitions fail with parse error

. The only working solution I have come up with so far is:

variables {
  char retval[256];
}

void ErrorCodeToMsg(int code) {
  char [] msg = "Hello word";
  strncpy(retval, msg, 256);
}

      

Of course, this is very ugly, because each call ErrorCodeToMsg

requires two operators instead of one. Is there a better way?

+3


source to share


2 answers


You should do it in the same way as with string functions:

 void ErrorCodeToMsg(char buffer[], int code){
 buffer = myListOfCodes[code];
 }

      



the value will be stored in the buffer using its reference value. Unable to return string to Capl. This is why you cannot access String System variables using @

Selector.

+2


source


I have implemented a workaround for functions that return string constants. It consists of defining an array of possible return values char errorMsg[][]

and defining a function int ErrorCodeToMsg(errno)

that is a return and an index in this array, so it is called like this:

write("Error: %s", errorMsg[ErrorCodeToMsg(errno)]);

      



Note that this method is prone to errors when coding by hand, as it is easy to get the function and array due to synchronization after modification. In my case, the error codes are defined in the specification (XML file), so an array of error messages and a function are automatically generated ErrorCodeToMsg

.

0


source







All Articles