Accessing IDL_STRING Values

It is important to realize that the s field of an IDL_STRING struct does not contain a valid string pointer in the case of a null string (i.e., when slen is zero). A common error that can cause IDL to crash is illustrated by the following code fragment:

void print_str(IDL_STRING *s) 
{ 
  printf("%s", s->s); 
} 

The problem with this code is that it fails to consider the case where the argument s describes a null string. The proper way to write this code is as follows:

void print str(IDL_STRING *s)  
{ 
  printf("%s", IDL_STRING_STR(s)); 
} 

The macro IDL_STRING_STR takes as its argument a pointer to an IDL_STRING struct. If the string is null, it returns a pointer to a zero length null-terminated string, otherwise it returns the string pointer from the struct. Consistent use of this macro will avoid the most common sort of error involving strings.

It is common for IDL system routines to accept arguments that provide names. Such arguments must be scalar strings, or string arrays that contain a single element. To properly process such an argument, it is necessary to screen out non-string types or multi-element arrays, locate the string descriptor, and use the IDL_STRING_STR() macro to extract a usable NULL terminated C string from it. The IDL_VarGetString() is used for this purpose. It encapsulates all of the error checking, and always returns a pointer to a NULL terminated C string, throwing the proper IDL_MSG_LONGJMP error via the IDL_Message() function when this is not possible:

char *IDL_VarGetString(IDL_VPTR v) 

where

v

Variable from which string value is desired.