diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-28 23:46:38 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-28 23:46:38 -0800 |
| commit | 8caf368ce25e9aa20e17428bd52fc94e02b33c29 (patch) | |
| tree | 90145be1087c175dcb78e25427069bc15f33edf6 | |
| parent | 4bae67fe2ddff0f7d281b42f6f920dde3bc23598 (diff) | |
| download | tinyrad-8caf368ce25e9aa20e17428bd52fc94e02b33c29.tar.gz tinyrad-8caf368ce25e9aa20e17428bd52fc94e02b33c29.tar.bz2 tinyrad-8caf368ce25e9aa20e17428bd52fc94e02b33c29.tar.xz tinyrad-8caf368ce25e9aa20e17428bd52fc94e02b33c29.zip | |
libtinyrad: adding support for quoted strings tinyrad_strsplit()
| -rw-r--r-- | lib/libtinyrad/lstrings.c | 41 | ||||
| -rw-r--r-- | 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( | |||
| 511 | char ** argv; | 511 | char ** argv; |
| 512 | char * line; | 512 | char * line; |
| 513 | char * bol; | 513 | char * bol; |
| 514 | char quote; | ||
| 514 | 515 | ||
| 515 | TinyRadDebugTrace(); | 516 | TinyRadDebugTrace(); |
| 516 | 517 | ||
| @@ -534,23 +535,41 @@ tinyrad_strsplit( | |||
| 534 | 535 | ||
| 535 | for(pos = 0; ((line[pos])); pos++) | 536 | for(pos = 0; ((line[pos])); pos++) |
| 536 | { | 537 | { |
| 537 | if (line[pos] == delim) | 538 | switch(line[pos]) |
| 538 | { | 539 | { |
| 539 | argc++; | 540 | case '"': |
| 540 | line[pos] = '\0'; | 541 | case '\'': |
| 541 | if ((rc = tinyrad_strsadd(&argv, bol)) != TRAD_SUCCESS) | 542 | if (line[pos] != delim) |
| 542 | { | 543 | { |
| 543 | free(line); | 544 | quote = str[pos]; |
| 544 | tinyrad_strsfree(argv); | 545 | for(pos += 1; ((line[pos] != '\0') && (line[pos] != quote)); pos++) |
| 545 | return(rc); | 546 | if ( (quote == '"') && (str[pos] == '\\') && (str[pos+1] == '"') ) |
| 547 | pos++; | ||
| 548 | if (str[pos] != quote) | ||
| 549 | { | ||
| 550 | tinyrad_strsfree(argv); | ||
| 551 | return(TRAD_ESYNTAX); | ||
| 552 | }; | ||
| 553 | break; | ||
| 546 | }; | 554 | }; |
| 547 | bol = &line[pos+1]; | 555 | |
| 556 | default: | ||
| 557 | if (line[pos] == delim) | ||
| 558 | { | ||
| 559 | argc++; | ||
| 560 | line[pos] = '\0'; | ||
| 561 | if ((rc = tinyrad_strsadd(&argv, bol)) != TRAD_SUCCESS) | ||
| 562 | { | ||
| 563 | free(line); | ||
| 564 | tinyrad_strsfree(argv); | ||
| 565 | return(rc); | ||
| 566 | }; | ||
| 567 | bol = &line[pos+1]; | ||
| 568 | }; | ||
| 569 | break; | ||
| 548 | }; | 570 | }; |
| 549 | }; | 571 | }; |
| 550 | 572 | ||
| 551 | // if ( (bol == line) && (!(bol[0])) ) | ||
| 552 | // return(TRAD_SUCCESS); | ||
| 553 | |||
| 554 | if ((rc = tinyrad_strsadd(&argv, bol)) != TRAD_SUCCESS) | 573 | if ((rc = tinyrad_strsadd(&argv, bol)) != TRAD_SUCCESS) |
| 555 | { | 574 | { |
| 556 | free(line); | 575 | 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 @@ | |||
| 70 | ///////////////// | 70 | ///////////////// |
| 71 | #pragma mark - Variables | 71 | #pragma mark - Variables |
| 72 | 72 | ||
| 73 | static const char * test_delim[] = | ||
| 74 | { | ||
| 75 | ":", | ||
| 76 | "-", | ||
| 77 | ",", | ||
| 78 | "_", | ||
| 79 | " ", | ||
| 80 | NULL | ||
| 81 | }; | ||
| 82 | |||
| 83 | static const char * const * test_strings[] = | 73 | static const char * const * test_strings[] = |
| 84 | { | 74 | { |
| 85 | (const char * const[]) { "test", "string", "1", NULL }, | 75 | (const char * const[]) { ":", "test", "string", "1", NULL }, |
| 86 | (const char * const[]) { "test", "empty", "", "string", NULL }, | 76 | (const char * const[]) { "-", "test", "string", "1", NULL }, |
| 87 | (const char * const[]) { "test", NULL }, | 77 | (const char * const[]) { ",", "test", "string", "1", NULL }, |
| 88 | (const char * const[]) { "", "test", NULL }, | 78 | (const char * const[]) { "_", "test", "string", "1", NULL }, |
| 89 | (const char * const[]) { "test", "", NULL }, | 79 | (const char * const[]) { " ", "test", "string", "1", NULL }, |
| 90 | (const char * const[]) { "", NULL }, | 80 | (const char * const[]) { ":", "test", "empty", "", "string", NULL }, |
| 91 | (const char * const[]) { "", "", NULL }, | 81 | (const char * const[]) { ":", "test", NULL }, |
| 92 | (const char * const[]) { "", "", "", NULL }, | 82 | (const char * const[]) { ":", "", "test", NULL }, |
| 83 | (const char * const[]) { ":", "test", "", NULL }, | ||
| 84 | (const char * const[]) { ":", "", NULL }, | ||
| 85 | (const char * const[]) { ":", "", "", NULL }, | ||
| 86 | (const char * const[]) { ":", "", "", "", NULL }, | ||
| 87 | (const char * const[]) { ":", "'This-is-a'", "'test:of'", "\"quoted,strings\"to\"split.\"", NULL }, | ||
| 88 | (const char * const[]) { "\"", "'This-is-a'", "'test:of'", "quoted,strings to split.", NULL }, | ||
| 89 | (const char * const[]) { "'", "\"This'is'a\"", "\"test:of\"", "\"quoted\\\"strings\\\"to\\\"split.\"", NULL }, | ||
| 93 | NULL | 90 | NULL |
| 94 | }; | 91 | }; |
| 95 | 92 | ||
| @@ -126,15 +123,13 @@ main( | |||
| 126 | int argc, | 123 | int argc, |
| 127 | char * argv[] ) | 124 | char * argv[] ) |
| 128 | { | 125 | { |
| 129 | int opt; | 126 | int opt; |
| 130 | int c; | 127 | int c; |
| 131 | int opt_index; | 128 | int opt_index; |
| 132 | int x; | 129 | int x; |
| 133 | int y; | 130 | int rc; |
| 134 | int rc; | 131 | unsigned opts; |
| 135 | unsigned opts; | 132 | const char * delim; |
| 136 | static const char * const * delims; | ||
| 137 | const char * user_delim[2]; | ||
| 138 | 133 | ||
| 139 | // getopt options | 134 | // getopt options |
| 140 | static char short_opt[] = "dD:hVvq"; | 135 | static char short_opt[] = "dD:hVvq"; |
| @@ -151,7 +146,6 @@ main( | |||
| 151 | 146 | ||
| 152 | trutils_initialize(PROGRAM_NAME); | 147 | trutils_initialize(PROGRAM_NAME); |
| 153 | 148 | ||
| 154 | delims = test_delim; | ||
| 155 | opts = 0; | 149 | opts = 0; |
| 156 | 150 | ||
| 157 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | 151 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) |
| @@ -163,9 +157,7 @@ main( | |||
| 163 | break; | 157 | break; |
| 164 | 158 | ||
| 165 | case 'D': | 159 | case 'D': |
| 166 | user_delim[0] = optarg; | 160 | delim = optarg; |
| 167 | user_delim[1] = NULL; | ||
| 168 | delims = user_delim; | ||
| 169 | break; | 161 | break; |
| 170 | 162 | ||
| 171 | case 'd': | 163 | case 'd': |
| @@ -213,16 +205,15 @@ main( | |||
| 213 | 205 | ||
| 214 | if (optind < argc) | 206 | if (optind < argc) |
| 215 | { | 207 | { |
| 216 | for(y = 0; ((delims[y])); y++) | 208 | delim = ((delim)) ? delim : ":"; |
| 217 | if ((rc = my_test_array(opts, (const char *const *)&argv[optind], delims[y])) != 0) | 209 | if ((rc = my_test_array(opts, (const char *const *)&argv[optind], delim)) != 0) |
| 218 | return(1); | 210 | return(1); |
| 219 | return(0); | 211 | return(0); |
| 220 | }; | 212 | }; |
| 221 | 213 | ||
| 222 | for(x = 0; ((test_strings[x])); x++) | 214 | for(x = 0; ((test_strings[x])); x++) |
| 223 | for(y = 0; ((delims[y])); y++) | 215 | if ((rc = my_test_array(opts, &test_strings[x][1], test_strings[x][0])) != 0) |
| 224 | if ((rc = my_test_array(opts, test_strings[x], delims[y])) != 0) | 216 | return(1); |
| 225 | return(1); | ||
| 226 | 217 | ||
| 227 | return(0); | 218 | return(0); |
| 228 | } | 219 | } |
