lib: Add a function to split a string into substrings
Some environment variables provide a space-separated list of strings. It is easier to process these when they are broken out into an array of strings. Add a utility function to handle this. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
@@ -328,6 +328,30 @@ char *strmhz(char *buf, unsigned long hz);
|
||||
*/
|
||||
void str_to_upper(const char *in, char *out, size_t len);
|
||||
|
||||
/**
|
||||
* str_to_list() - Convert a string to a list of string pointers
|
||||
*
|
||||
* Splits a string containing space-delimited substrings into a number of
|
||||
* separate strings, e.g. "this is" becomes {"this", "is", NULL}. If @instr is
|
||||
* empty then this returns just {NULL}. The string should have only a single
|
||||
* space between items, with no leading or trailing spaces.
|
||||
*
|
||||
* @instr: String to process (this is alloced by this function)
|
||||
* Returns: List of string pointers, terminated by NULL. Each entry points to
|
||||
* a string. If @instr is empty, the list consists just of a single NULL entry.
|
||||
* Note that the first entry points to the alloced string.
|
||||
* Returns NULL if out of memory
|
||||
*/
|
||||
const char **str_to_list(const char *instr);
|
||||
|
||||
/**
|
||||
* str_free_list() - Free a string list
|
||||
*
|
||||
* @ptr: String list to free, as created by str_to_list(). This can also be
|
||||
* NULL, in which case the function does nothing
|
||||
*/
|
||||
void str_free_list(const char **ptr);
|
||||
|
||||
/**
|
||||
* vsscanf - Unformat a buffer into a list of arguments
|
||||
* @inp: input buffer
|
||||
|
Reference in New Issue
Block a user