From 8caf368ce25e9aa20e17428bd52fc94e02b33c29 Mon Sep 17 00:00:00 2001 From: "David M. Syzdek" Date: Mon, 28 Mar 2022 23:46:38 -0800 Subject: libtinyrad: adding support for quoted strings tinyrad_strsplit() --- lib/libtinyrad/lstrings.c | 41 ++++++++++++++++++++++-------- tests/test-str-split.c | 65 ++++++++++++++++++++--------------------------- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/lib/libtinyrad/lstrings.c b/lib/libtinyrad/lstrings.c index e2b6bdf..2c52d71 100644 --- a/lib/libtinyrad/lstrings.c +++ b/lib/libtinyrad/lstrings.c @@ -511,6 +511,7 @@ tinyrad_strsplit( char ** argv; char * line; char * bol; + char quote; TinyRadDebugTrace(); @@ -534,23 +535,41 @@ tinyrad_strsplit( for(pos = 0; ((line[pos])); pos++) { - if (line[pos] == delim) + switch(line[pos]) { - argc++; - line[pos] = '\0'; - if ((rc = tinyrad_strsadd(&argv, bol)) != TRAD_SUCCESS) + case '"': + case '\'': + if (line[pos] != delim) { - free(line); - tinyrad_strsfree(argv); - return(rc); + quote = str[pos]; + for(pos += 1; ((line[pos] != '\0') && (line[pos] != quote)); pos++) + if ( (quote == '"') && (str[pos] == '\\') && (str[pos+1] == '"') ) + pos++; + if (str[pos] != quote) + { + tinyrad_strsfree(argv); + return(TRAD_ESYNTAX); + }; + break; }; - bol = &line[pos+1]; + + default: + if (line[pos] == delim) + { + argc++; + line[pos] = '\0'; + if ((rc = tinyrad_strsadd(&argv, bol)) != TRAD_SUCCESS) + { + free(line); + tinyrad_strsfree(argv); + return(rc); + }; + bol = &line[pos+1]; + }; + break; }; }; -// if ( (bol == line) && (!(bol[0])) ) -// return(TRAD_SUCCESS); - if ((rc = tinyrad_strsadd(&argv, bol)) != TRAD_SUCCESS) { free(line); diff --git a/tests/test-str-split.c b/tests/test-str-split.c index 9823cd4..f20f7d7 100644 --- a/tests/test-str-split.c +++ b/tests/test-str-split.c @@ -70,26 +70,23 @@ ///////////////// #pragma mark - Variables -static const char * test_delim[] = -{ - ":", - "-", - ",", - "_", - " ", - NULL -}; - static const char * const * test_strings[] = { - (const char * const[]) { "test", "string", "1", NULL }, - (const char * const[]) { "test", "empty", "", "string", NULL }, - (const char * const[]) { "test", NULL }, - (const char * const[]) { "", "test", NULL }, - (const char * const[]) { "test", "", NULL }, - (const char * const[]) { "", NULL }, - (const char * const[]) { "", "", NULL }, - (const char * const[]) { "", "", "", NULL }, + (const char * const[]) { ":", "test", "string", "1", NULL }, + (const char * const[]) { "-", "test", "string", "1", NULL }, + (const char * const[]) { ",", "test", "string", "1", NULL }, + (const char * const[]) { "_", "test", "string", "1", NULL }, + (const char * const[]) { " ", "test", "string", "1", NULL }, + (const char * const[]) { ":", "test", "empty", "", "string", NULL }, + (const char * const[]) { ":", "test", NULL }, + (const char * const[]) { ":", "", "test", NULL }, + (const char * const[]) { ":", "test", "", NULL }, + (const char * const[]) { ":", "", NULL }, + (const char * const[]) { ":", "", "", NULL }, + (const char * const[]) { ":", "", "", "", NULL }, + (const char * const[]) { ":", "'This-is-a'", "'test:of'", "\"quoted,strings\"to\"split.\"", NULL }, + (const char * const[]) { "\"", "'This-is-a'", "'test:of'", "quoted,strings to split.", NULL }, + (const char * const[]) { "'", "\"This'is'a\"", "\"test:of\"", "\"quoted\\\"strings\\\"to\\\"split.\"", NULL }, NULL }; @@ -126,15 +123,13 @@ main( int argc, char * argv[] ) { - int opt; - int c; - int opt_index; - int x; - int y; - int rc; - unsigned opts; - static const char * const * delims; - const char * user_delim[2]; + int opt; + int c; + int opt_index; + int x; + int rc; + unsigned opts; + const char * delim; // getopt options static char short_opt[] = "dD:hVvq"; @@ -151,7 +146,6 @@ main( trutils_initialize(PROGRAM_NAME); - delims = test_delim; opts = 0; while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) @@ -163,9 +157,7 @@ main( break; case 'D': - user_delim[0] = optarg; - user_delim[1] = NULL; - delims = user_delim; + delim = optarg; break; case 'd': @@ -213,16 +205,15 @@ main( if (optind < argc) { - for(y = 0; ((delims[y])); y++) - if ((rc = my_test_array(opts, (const char *const *)&argv[optind], delims[y])) != 0) - return(1); + delim = ((delim)) ? delim : ":"; + if ((rc = my_test_array(opts, (const char *const *)&argv[optind], delim)) != 0) + return(1); return(0); }; for(x = 0; ((test_strings[x])); x++) - for(y = 0; ((delims[y])); y++) - if ((rc = my_test_array(opts, test_strings[x], delims[y])) != 0) - return(1); + if ((rc = my_test_array(opts, &test_strings[x][1], test_strings[x][0])) != 0) + return(1); return(0); } -- cgit v1.2.3