Extend the split function to support multiple delimiters

This commit is contained in:
Andrea Mazzoleni 2016-04-26 21:26:15 +02:00
parent 4cc1109444
commit 6053096862
3 changed files with 11 additions and 14 deletions

View file

@ -586,30 +586,27 @@ char* polish(char* s)
return s;
}
unsigned split(char** split_map, unsigned split_max, char* str)
unsigned split(char** split_map, unsigned split_max, char* str, const char* delimiters)
{
unsigned mac = 0;
/* skip initial spaces */
while (isspace(*str))
++str;
/* skip initial delimiers */
str += strspn(str, delimiters);
while (*str != 0 || mac == split_max) {
/* start of the token */
split_map[mac] = str;
++mac;
/* find the first separator or the end of the string */
while (*str != 0 && !isspace(*str))
++str;
/* find the first delimiter or the end of the string */
str += strcspn(str, delimiters);
/* put the final terminator if missing */
if (*str != 0)
*str++ = 0;
/* skip trailing spaces */
while (isspace(*str))
++str;
/* skip trailing delimiters */
str += strspn(str, delimiters);
}
return mac;

View file

@ -244,11 +244,11 @@ const char* quote(char prefix, const char* str, char* buffer);
char* polish(char* s);
/**
* Split a string in multiple tokens separated by spaces.
* Split a string in multiple tokens separated by delimiters.
*
* Multiple spaces are grouped together.
* Multiple delimiters are grouped together.
*/
unsigned split(char** split_map, unsigned split_max, char* line);
unsigned split(char** split_map, unsigned split_max, char* line, const char* delimiters);
/****************************************************************************/
/* path */

View file

@ -101,7 +101,7 @@ static int devresolve_proc(uint64_t device, char* path, size_t path_size)
break;
/* split the line */
split_mac = split(split_map, 9, s);
split_mac = split(split_map, 9, s, " \t\r\n");
/* if too short, it's the wrong line */
if (split_mac < 9)