diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-20 17:43:21 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-21 16:14:31 +0200 |
commit | 12727806af641970a651b8f969cba33677ae7395 (patch) | |
tree | f02cb947ac2379cdd9bc15e093ec732dfd5bc45f /include/util | |
parent | ca9b6ba17257f88b8d575f18cab0b1e23660cfa5 (diff) |
Add a helper to libutil for splitting token separated lines
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include/util')
-rw-r--r-- | include/util/parse.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/include/util/parse.h b/include/util/parse.h index bc635ad..8da07b1 100644 --- a/include/util/parse.h +++ b/include/util/parse.h @@ -15,6 +15,18 @@ enum { ISTREAM_LINE_SKIP_EMPTY = 0x04, }; +enum { + SPLIT_LINE_OK = 0, + SPLIT_LINE_ALLOC = -1, + SPLIT_LINE_UNMATCHED_QUOTE = -2, + SPLIT_LINE_ESCAPE = -3, +}; + +typedef struct { + size_t count; + char *args[]; +} split_line_t; + #ifdef __cplusplus extern "C" { #endif @@ -48,6 +60,31 @@ extern "C" { SQFS_INTERNAL int istream_get_line(sqfs_istream_t *strm, char **out, size_t *line_num, int flags); +/** + * @brief Split a line of special character separated tokens + * + * The underlying string is modified, replacing sequences of separator + * characters with a single null byte and compacting the string. Every + * occourance of a termianted string is recorded in the returned structure. + * + * @param line A modifyable buffer holding a line + * @param len The maximum length of the string in the buffer to process + * @param sep A string of valid separator caracaters + * @param out Returns the token list, free this with free() + * + * @return Zero on success, a negative SPLIT_LINE_* error code on failure + */ +SQFS_INTERNAL int split_line(char *line, size_t len, + const char *sep, split_line_t **out); + +/** + * @brief Remove the first N components of a tokenized line + * + * @param sep A successfully split up line + * @param count Number of components to remove from the front + */ +SQFS_INTERNAL void split_line_remove_front(split_line_t *sep, size_t count); + #ifdef __cplusplus } #endif |