Add gettokens() string function
This commit is contained in:
@@ -96,7 +96,8 @@ char* uc_sprintbs(char *result, unsigned short value);
|
|||||||
|
|
||||||
/** Like http://swtch.com/plan9port/man/man3/getfields.html
|
/** Like http://swtch.com/plan9port/man/man3/getfields.html
|
||||||
*/
|
*/
|
||||||
int getfields(char *str, char **args, int max, int mflag, const char *set);
|
int getfields(char *str, char **args, int max, int mflag, const char *sep);
|
||||||
|
int gettokens(char *str, char **args, int max, const char *sep);
|
||||||
|
|
||||||
//Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.)
|
//Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.)
|
||||||
///result should be at least of size 12,
|
///result should be at least of size 12,
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include "ucore/string.h"
|
||||||
|
|
||||||
|
//tokenize string, and exclude the quotes
|
||||||
|
static char *qtoken(char *s, const char *sep)
|
||||||
|
{
|
||||||
|
int inquote = 0;
|
||||||
|
char *t = s;
|
||||||
|
while (*t != 0 && (inquote || strchr(sep, *t) == NULL)) {
|
||||||
|
if (*t != '"') {
|
||||||
|
*s++ = *t++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inquote) {
|
||||||
|
inquote = 1;
|
||||||
|
t++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t[1] != '"') {
|
||||||
|
t++;
|
||||||
|
inquote = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//double quotes, merged to 1 quote
|
||||||
|
t++;
|
||||||
|
*s++ = *t++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*s != 0) {
|
||||||
|
*s = 0;
|
||||||
|
if (t == s) {
|
||||||
|
t++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gettokens(char *str, char **args, int maxargs, const char *sep)
|
||||||
|
{
|
||||||
|
int nargs;
|
||||||
|
|
||||||
|
for (nargs = 0; nargs < maxargs; nargs++) {
|
||||||
|
while (*str != 0 && strchr(sep, *str) != NULL) {
|
||||||
|
*str++ = 0;
|
||||||
|
}
|
||||||
|
if (*str == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
args[nargs] = str;
|
||||||
|
str = qtoken(str, sep);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nargs;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user