Is it possible to define a CAPL function returning a text string?
Asked Answered
F

2

6

I develop CAPL scripts in Vector CANoe, and I need to define several functions returning 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 a parse error. The only working solution I came 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 to ErrorCodeToMsg requires two statements instead of one. Is there a better way?

Fettling answered 19/6, 2015 at 7:50 Comment(0)
N
5

You have to do it as you would do with string-based functions :

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

The value will be stored in the buffer using its reference value. It is not possible to return string in Capl. This is why you can't access String System variables using the @ Selector.

Ninefold answered 19/6, 2015 at 19:59 Comment(1)
+1 for avoiding a global variable. I would still prefer a real char * return value to be able to write("Error: %s", ErrorCodeToMsg(errno)), thanks for confirming it's not going to happen.Fettling
F
0

I have implemented a workaround for functions which return string constants. It consists in defining an array of possible return values char errorMsg[][] and defining a function int ErrorCodeToMsg(errno) which is returns and index in that array, so it is called like this:

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

Note that this method is error-prone when coded manually, because it's easy to get the function and the array out of sync after a modification. In my case, error codes are defined in a specification (XML file), so that array of error messages and the ErrorCodeToMsg function are automatically generated.

Fettling answered 22/6, 2015 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.