libgslutil  1.2.3
Functions
String to data type conversion routines

Functions

int safe_strtobool (const char *data, int *out))
 Convert string to boolean. More...
 
int safe_strtoint (const char *data, int *out))
 Convert string to integer. More...
 
int safe_strtod (const char *data, double *out))
 Convert string to double-precision floating point. More...
 
int safe_strtoenum (const char *data, int *out, const char *const *values))
 Convert string to enumeration value. More...
 
int safe_strtooctalmode (const char *data, int *out, int accept_mask))
 Convert string to octal mode, suitable for filesystem. More...
 

Detailed Description

This set of routines provides conversion between strings and data types such as integers, floating point numbers, and so on.

Function Documentation

◆ safe_strtobool()

int safe_strtobool ( const char *  data,
int *  out 
)

Convert string to boolean.

Parameters
dataScalar (string) value.
[out]outIf the conversion is successful, the result is written into this value.
Return values
0on success.
non-0on error.

Attempts to convert a scalar (string) value into a boolean. If the string value represents an integer, then 0 is false and non-0 is true. Also recognised are the literals "false" and "true". Trailing and leading whitespace are skipped. On success, the result is written into out and 0 is returned.

◆ safe_strtoint()

int safe_strtoint ( const char *  data,
int *  out 
)

Convert string to integer.

Parameters
dataScalar (string) value.
[out]outIf the conversion is successful, the result is written into this value.
Return values
0on success.
non-0on error.

Attempts to convert a scalar (string) value into an integer. Trailing and leading whitespace are skipped. On success, the result is written into out and 0 is returned.

◆ safe_strtod()

int safe_strtod ( const char *  data,
double *  out 
)

Convert string to double-precision floating point.

Parameters
dataScalar (string) value.
[out]outIf the conversion is successful, the result is written into this value.
Return values
0on success.
non-0on error.

Attempts to convert a scalar (string) value into a double-precision floating point number. Trailing and leading whitespace are skipped. On success, the result is written into out and 0 is returned.

◆ safe_strtoenum()

int safe_strtoenum ( const char *  data,
int *  out,
const char *const *  values 
)

Convert string to enumeration value.

Parameters
dataScalar (string) value.
[out]outIf the conversion is successful, the result is written into this value.
valuesA null-terminated array of choices; out is an index into this array.
Return values
0on success.
non-0on error.

Attempts to convert a scalar (string) value into an index into an array of choices (values). Trailing and leading whitespace are skipped. The conversion is case sensitive. The array values must be null terminated. On success, the result is written into out and 0 is returned.

◆ safe_strtooctalmode()

int safe_strtooctalmode ( const char *  data,
int *  out,
int  accept_mask 
)

Convert string to octal mode, suitable for filesystem.

Parameters
dataScalar (string) value.
[out]outIf the conversion is successful, the result is written into this value.
accept_maskA bitmask specifying the values that will be accepted.
Return values
0on success.
non-0on error (and see errno).

Attempts to convert a string (scalar) value into an octal mode suitable for filesystem use. The string itself does not have to be prefixed with "0" since we force octal conversion. If the conversion is successful, and the result does not contain any bits not set in accept_mask, the result is written into out and 0 is returned.