ocat: splitter: properly this time

This commit is contained in:
Jan-Piet Mens
2015-08-26 19:38:20 +02:00
parent 3f194a35ef
commit 567bb68a29
3 changed files with 23 additions and 6 deletions
+1 -6
View File
@@ -166,12 +166,7 @@ int main(int argc, char **argv)
switch (c) {
case 1: /* No short option */
fields = json_mkarray();
char *f[60], **ff;
splitter(optarg, ",", f);
for (ff = f; ff && *ff; ff++) {
json_append_element(fields, json_mkstring(*ff));
}
fields = json_splitter(optarg, ",");
break;
case 'l':
list = 1;
+21
View File
@@ -140,3 +140,24 @@ int splitter(char *s, char *sep, char **parts)
free(ds);
return (nt);
}
/*
* Split a string separated by characters in `sep' into a JSON
* array and return that.
*/
JsonNode *json_splitter(char *s, char *sep)
{
char *token, *ds = strdup(s);
JsonNode *array = json_mkarray();
if (!ds || !array)
return (NULL);
for (token = strtok(ds, sep); token && *token; token = strtok(NULL, sep)) {
json_append_element(array, json_mkstring(token));
}
free(ds);
return (array);
}
+1
View File
@@ -14,5 +14,6 @@ char *slurp_file(char *filename, int fold_newlines);
int json_copy_to_object(JsonNode * obj, JsonNode * object_or_array, int clobber);
int json_copy_from_file(JsonNode * obj, char *filename);
int splitter(char *s, char *sep, char **parts);
JsonNode *json_splitter(char *s, char *sep);
#endif