libsigrok  unreleased development snapshot
sigrok hardware access and backend library
Functions
String utilities

Helper functions for handling or converting libsigrok-related strings. More...

Functions

SR_PRIV int sr_atod_ascii_digits (const char *str, double *ret, int *digits)
 Convert text to a floating point value, and get its precision. More...
 
int sr_sprintf_ascii (char *buf, const char *format,...)
 Compose a string with a format string in the buffer pointed to by buf. More...
 
int sr_vsprintf_ascii (char *buf, const char *format, va_list args)
 Compose a string with a format string in the buffer pointed to by buf. More...
 
int sr_snprintf_ascii (char *buf, size_t buf_size, const char *format,...)
 Composes a string with a format string (like printf) in the buffer pointed by buf (taking buf_size as the maximum buffer capacity to fill). More...
 
int sr_vsnprintf_ascii (char *buf, size_t buf_size, const char *format, va_list args)
 Composes a string with a format string (like printf) in the buffer pointed by buf (taking buf_size as the maximum buffer capacity to fill). More...
 
int sr_parse_rational (const char *str, struct sr_rational *ret)
 Convert a string representation of a numeric value to a sr_rational. More...
 
char * sr_si_string_u64 (uint64_t x, const char *unit)
 Convert a numeric value value to its "natural" string representation in SI units. More...
 
char * sr_samplerate_string (uint64_t samplerate)
 Convert a numeric samplerate value to its "natural" string representation. More...
 
char * sr_period_string (uint64_t v_p, uint64_t v_q)
 Convert a numeric period value to the "natural" string representation of its period value. More...
 
char * sr_voltage_string (uint64_t v_p, uint64_t v_q)
 Convert a numeric voltage value to the "natural" string representation of its voltage value. More...
 
int sr_parse_sizestring (const char *sizestring, uint64_t *size)
 Convert a "natural" string representation of a size value to uint64_t. More...
 
uint64_t sr_parse_timestring (const char *timestring)
 Convert a "natural" string representation of a time value to an uint64_t value in milliseconds. More...
 
gboolean sr_parse_boolstring (const char *boolstr)
 
int sr_parse_period (const char *periodstr, uint64_t *p, uint64_t *q)
 
int sr_parse_voltage (const char *voltstr, uint64_t *p, uint64_t *q)
 
char ** sr_parse_probe_names (const char *spec, const char **dflt_names, size_t dflt_count, size_t max_count, size_t *ret_count)
 Parse a probe names specification, allocate a string vector. More...
 
void sr_free_probe_names (char **names)
 Release previously allocated probe names (string vector). More...
 
char * sr_text_trim_spaces (char *s)
 Trim leading and trailing whitespace off text. More...
 
char * sr_text_next_line (char *s, size_t l, char **next, size_t *taken)
 Check for another complete text line, trim, return consumed char count. More...
 
char * sr_text_next_word (char *s, char **next)
 Isolates another space separated word in a text line. More...
 
int sr_next_power_of_two (size_t value, size_t *bits, size_t *power)
 Get the number of necessary bits to hold a given value. More...
 

Detailed Description

Helper functions for handling or converting libsigrok-related strings.

Function Documentation

SR_PRIV int sr_atod_ascii_digits ( const char *  str,
double *  ret,
int digits 
)

Convert text to a floating point value, and get its precision.

Parameters
[in]strThe input text to convert.
[out]retThe conversion result, a double precision float number.
[out]digitsThe number of significant decimals.
Returns
SR_OK in case of successful text to number conversion.
SR_ERR when conversion fails.
Since
0.6.0

Definition at line 341 of file strutil.c.

References SR_ERR, SR_OK, and SR_PRIV.

void sr_free_probe_names ( char **  names)

Release previously allocated probe names (string vector).

Parameters
[in]namesThe previously allocated string vector.
Since
0.6.0

Definition at line 1665 of file strutil.c.

int sr_next_power_of_two ( size_t  value,
size_t *  bits,
size_t *  power 
)

Get the number of necessary bits to hold a given value.

Also gets the next power-of-two value at or above the caller provided value.

Parameters
[in]valueThe value that must get stored.
[out]bitsThe required number of bits.
[out]powerThe corresponding power-of-two.
Returns
SR_OK upon success, SR_ERR* otherwise.

TODO Move this routine to a more appropriate location, it is not strictly string related.

Since
0.6.0

Definition at line 1837 of file strutil.c.

References SR_OK.

gboolean sr_parse_boolstring ( const char *  boolstr)
Since
0.1.0

Definition at line 1253 of file strutil.c.

int sr_parse_period ( const char *  periodstr,
uint64_t *  p,
uint64_t *  q 
)
Since
0.2.0

Definition at line 1273 of file strutil.c.

References SR_ERR_ARG, and SR_OK.

char** sr_parse_probe_names ( const char *  spec,
const char **  dflt_names,
size_t  dflt_count,
size_t  max_count,
size_t *  ret_count 
)

Parse a probe names specification, allocate a string vector.

Parameters
[in]specThe input spec, list of probes or aliases.
[in]dflt_namesThe default probe names, a string array.
[in]dflt_countThe default probe names count. Either must match the unterminated array size, or can be 0 when the default names are NULL terminated.
[in]max_countOptional resulting vector size limit.
[out]ret_countOptional result vector size (return value).
Returns
A string vector with resulting probe names. Or #NULL in case of failure.

The input spec is a comma separated list of probe names. Items can be aliases which expand to a corresponding set of signal names. The resulting names list optionally gets padded from the caller's builtin probe names, an empty input spec yields the original names as provided by the caller. Padding is omitted when the spec starts with '-', which may result in a device with fewer channels being created, enough to cover the user's spec, but none extra to maybe enable and use later on. An optional maximum length spec will trim the result set to that size. The resulting vector length optionally is returned to the caller, so that it need not re-get the length.

Calling applications must release the allocated vector by means of sr_free_probe_names().

Since
0.6.0

Definition at line 1563 of file strutil.c.

int sr_parse_rational ( const char *  str,
struct sr_rational ret 
)

Convert a string representation of a numeric value to a sr_rational.

The conversion is strict and will fail if the complete string does not represent a valid number. The function sets errno according to the details of the failure. This version ignores the locale.

Parameters
strThe string representation to convert.
retPointer to sr_rational where the result of the conversion will be stored.
Return values
SR_OKConversion successful.
SR_ERRFailure.
Since
0.5.0

Definition at line 836 of file strutil.c.

References sr_rational::p, sr_rational::q, SR_ERR, and SR_OK.

int sr_parse_sizestring ( const char *  sizestring,
uint64_t *  size 
)

Convert a "natural" string representation of a size value to uint64_t.

E.g. a value of "3k" or "3 K" would be converted to 3000, a value of "15M" would be converted to 15000000.

Value representations other than decimal (such as hex or octal) are not supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported. Spaces (but not other whitespace) between value and suffix are allowed.

Parameters
sizestringA string containing a (decimal) size value.
sizePointer to uint64_t which will contain the string's size value.
Returns
SR_OK upon success, SR_ERR upon errors.
Since
0.1.0

Definition at line 1146 of file strutil.c.

References SR_ERR, SR_GHZ, SR_KHZ, SR_MHZ, and SR_OK.

Referenced by sr_session_load().

+ Here is the caller graph for this function:

uint64_t sr_parse_timestring ( const char *  timestring)

Convert a "natural" string representation of a time value to an uint64_t value in milliseconds.

E.g. a value of "3s" or "3 s" would be converted to 3000, a value of "15ms" would be converted to 15.

Value representations other than decimal (such as hex or octal) are not supported. Only lower-case "s" and "ms" time suffixes are supported. Spaces (but not other whitespace) between value and suffix are allowed.

Parameters
timestringA string containing a (decimal) time value.
Returns
The string's time value as uint64_t, in milliseconds.
Since
0.1.0

Definition at line 1227 of file strutil.c.

int sr_parse_voltage ( const char *  voltstr,
uint64_t *  p,
uint64_t *  q 
)
Since
0.2.0

Definition at line 1306 of file strutil.c.

References SR_ERR_ARG, and SR_OK.

char* sr_period_string ( uint64_t  v_p,
uint64_t  v_q 
)

Convert a numeric period value to the "natural" string representation of its period value.

The period is specified as a rational number's numerator and denominator.

E.g. a pair of (1, 5) would be converted to "200 ms", (10, 100) to "100 ms".

Parameters
v_pThe period numerator.
v_qThe period denominator.
Returns
A newly allocated string representation of the period value, or NULL upon errors. The caller is responsible to g_free() the memory.
Since
0.5.0

Definition at line 1073 of file strutil.c.

References SR_GHZ, SR_KHZ, and SR_MHZ.

char* sr_samplerate_string ( uint64_t  samplerate)

Convert a numeric samplerate value to its "natural" string representation.

E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", 31500 would become "31.5 kHz".

Parameters
samplerateThe samplerate in Hz.
Returns
A newly allocated string representation of the samplerate value, or NULL upon errors. The caller is responsible to g_free() the memory.
Since
0.1.0

Definition at line 1051 of file strutil.c.

References sr_si_string_u64().

+ Here is the call graph for this function:

char* sr_si_string_u64 ( uint64_t  x,
const char *  unit 
)

Convert a numeric value value to its "natural" string representation in SI units.

E.g. a value of 3000000, with units set to "W", would be converted to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW".

Parameters
xThe value to convert.
unitThe unit to append to the string, or NULL if the string has no units.
Returns
A newly allocated string representation of the samplerate value, or NULL upon errors. The caller is responsible to g_free() the memory.
Since
0.2.0

Definition at line 1009 of file strutil.c.

References SR_GHZ, SR_HZ, SR_KHZ, and SR_MHZ.

Referenced by sr_samplerate_string().

+ Here is the caller graph for this function:

int sr_snprintf_ascii ( char *  buf,
size_t  buf_size,
const char *  format,
  ... 
)

Composes a string with a format string (like printf) in the buffer pointed by buf (taking buf_size as the maximum buffer capacity to fill).

If the resulting string would be longer than n - 1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function. A terminating NUL character is automatically appended after the content written. After the format parameter, the function expects at least as many additional arguments as needed for format.

This version ignores the current locale and uses the locale "C" for Linux, FreeBSD, OSX and Android.

Parameters
bufPointer to a buffer where the resulting C string is stored.
buf_sizeMaximum number of bytes to be used in the buffer. The generated string has a length of at most buf_size - 1, leaving space for the additional terminating NUL character.
formatC string that contains a format string (see printf).
...A sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string.
Returns
On success, the number of characters that would have been written if buf_size had been sufficiently large, not counting the terminating NUL character. On failure, a negative number is returned. Notice that only when this returned value is non-negative and less than buf_size, the string has been completely written.
Since
0.6.0

Definition at line 631 of file strutil.c.

References sr_vsnprintf_ascii().

+ Here is the call graph for this function:

int sr_sprintf_ascii ( char *  buf,
const char *  format,
  ... 
)

Compose a string with a format string in the buffer pointed to by buf.

It is up to the caller to ensure that the allocated buffer is large enough to hold the formatted result.

A terminating NUL character is automatically appended after the content written.

After the format parameter, the function expects at least as many additional arguments as needed for format.

This version ignores the current locale and uses the locale "C" for Linux, FreeBSD, OSX and Android.

Parameters
bufPointer to a buffer where the resulting C string is stored.
formatC string that contains a format string (see printf).
...A sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string.
Returns
On success, the number of characters that would have been written, not counting the terminating NUL character.
Since
0.6.0

Definition at line 467 of file strutil.c.

References sr_vsprintf_ascii().

+ Here is the call graph for this function:

char* sr_text_next_line ( char *  s,
size_t  l,
char **  next,
size_t *  taken 
)

Check for another complete text line, trim, return consumed char count.

Parameters
[in]sThe input text, current read position.
[in]lThe input text, remaining available characters.
[out]nextPosition after the current text line.
[out]takenCount of consumed chars in current text line.
Returns
Start of trimmed and NUL terminated text line. Or #NULL when no text line was found.

Checks for the availability of another text line of input data. Manipulates the caller's input text in place.

The end-of-line condition is the LF character ('
'). Which covers LF-only as well as CR/LF input data. CR-only and LF/CR are considered unpopular and are not supported. LF/CR may appear to work at the caller's when leading whitespace gets trimmed (line boundaries will be incorrect, but content may get processed as expected). Support for all of the above combinations breaks the detection of empty lines (or becomes unmaintainably complex).

The input buffer must be end-of-line terminated, lack of EOL results in failure to detect the text line. This is motivated by accumulating input in chunks, and the desire to not process incomplete lines before their reception has completed. Callers should enforce EOL if their source of input provides an EOF condition and is unreliable in terms of text line termination.

When another text line is available, it gets NUL terminated and space gets trimmed of both ends. The start position of the trimmed text line is returned. Optionally the number of consumed characters is returned to the caller. Optionally 'next' points to after the returned text line, or #NULL when no other text is available in the input buffer.

The 'taken' value is not preset by this routine, only gets updated. This is convenient for callers which expect to find multiple text lines in a received chunk, before finally discarding processed data from the input buffer (which can involve expensive memory move operations, and may be desirable to defer as much as possible).

Since
0.6.0

Definition at line 1741 of file strutil.c.

References sr_text_trim_spaces().

+ Here is the call graph for this function:

char* sr_text_next_word ( char *  s,
char **  next 
)

Isolates another space separated word in a text line.

Parameters
[in]sThe input text, current read position.
[out]nextThe position after the current word.
Returns
The start of the current word. Or #NULL if there is none.

Advances over leading whitespace. Isolates (NUL terminates) the next whitespace separated word. Optionally returns the position after the current word. Manipulates the caller's input text in place.

Since
0.6.0

Definition at line 1784 of file strutil.c.

char* sr_text_trim_spaces ( char *  s)

Trim leading and trailing whitespace off text.

Parameters
[in]sThe input text.
Returns
Start of trimmed input text.

Manipulates the caller's input text in place.

Since
0.6.0

Definition at line 1681 of file strutil.c.

Referenced by sr_text_next_line().

+ Here is the caller graph for this function:

char* sr_voltage_string ( uint64_t  v_p,
uint64_t  v_q 
)

Convert a numeric voltage value to the "natural" string representation of its voltage value.

The voltage is specified as a rational number's numerator and denominator.

E.g. a value of 300000 would be converted to "300mV", 2 to "2V".

Parameters
v_pThe voltage numerator.
v_qThe voltage denominator.
Returns
A newly allocated string representation of the voltage value, or NULL upon errors. The caller is responsible to g_free() the memory.
Since
0.2.0

Definition at line 1119 of file strutil.c.

int sr_vsnprintf_ascii ( char *  buf,
size_t  buf_size,
const char *  format,
va_list  args 
)

Composes a string with a format string (like printf) in the buffer pointed by buf (taking buf_size as the maximum buffer capacity to fill).

If the resulting string would be longer than n - 1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function. A terminating NUL character is automatically appended after the content written. Internally, the function retrieves arguments from the list identified by args as if va_arg was used on it, and thus the state of args is likely to be altered by the call. In any case, arg should have been initialized by va_start at some point before the call, and it is expected to be released by va_end at some point after the call.

This version ignores the current locale and uses the locale "C" for Linux, FreeBSD, OSX and Android.

Parameters
bufPointer to a buffer where the resulting C string is stored.
buf_sizeMaximum number of bytes to be used in the buffer. The generated string has a length of at most buf_size - 1, leaving space for the additional terminating NUL character.
formatC string that contains a format string (see printf).
argsA value identifying a variable arguments list initialized with va_start.
Returns
On success, the number of characters that would have been written if buf_size had been sufficiently large, not counting the terminating NUL character. On failure, a negative number is returned. Notice that only when this returned value is non-negative and less than buf_size, the string has been completely written.
Since
0.6.0

Definition at line 678 of file strutil.c.

References SR_PRIV.

Referenced by sr_snprintf_ascii().

+ Here is the caller graph for this function:

int sr_vsprintf_ascii ( char *  buf,
const char *  format,
va_list  args 
)

Compose a string with a format string in the buffer pointed to by buf.

It is up to the caller to ensure that the allocated buffer is large enough to hold the formatted result.

Internally, the function retrieves arguments from the list identified by args as if va_arg was used on it, and thus the state of args is likely to be altered by the call.

In any case, args should have been initialized by va_start at some point before the call, and it is expected to be released by va_end at some point after the call.

This version ignores the current locale and uses the locale "C" for Linux, FreeBSD, OSX and Android.

Parameters
bufPointer to a buffer where the resulting C string is stored.
formatC string that contains a format string (see printf).
argsA value identifying a variable arguments list initialized with va_start.
Returns
On success, the number of characters that would have been written, not counting the terminating NUL character.
Since
0.6.0

Definition at line 506 of file strutil.c.

Referenced by sr_sprintf_ascii().

+ Here is the caller graph for this function:

Variable Documentation

const { ... } expands

Definition at line 1408 of file strutil.c.

const { ... } name

Definition at line 1407 of file strutil.c.