Cli args: read_quoted_string

This commit is contained in:
DrZlo13 2021-12-07 01:37:31 +10:00
parent 1677d0a3b1
commit 752500a202
2 changed files with 27 additions and 15 deletions

View File

@ -44,18 +44,26 @@ bool cli_args_read_string_and_trim(mstring_t* args, mstring_t* word) {
return true;
}
bool cli_args_read_quoted_string_and_trim(mstring_t* args, mstring_t* word) {
if(mstring_size(args) < 2 || mstring_get_char(args, 0) != '\"') {
return false;
}
size_t second_quote_pos = mstring_search_char(args, '\"', 1);
if(second_quote_pos == 0) {
return false;
}
mstring_set_n(word, args, 1, second_quote_pos - 1);
mstring_right(args, second_quote_pos + 1);
mstring_strim(args, " \r\n\t");
return true;
}
bool cli_args_read_probably_quoted_string_and_trim(mstring_t* args, mstring_t* word) {
if(mstring_size(args) > 1 && mstring_get_char(args, 0) == '\"') {
size_t second_quote_pos = mstring_search_char(args, '\"', 1);
if(second_quote_pos == 0) {
return false;
}
mstring_set_n(word, args, 1, second_quote_pos - 1);
mstring_right(args, second_quote_pos + 1);
mstring_strim(args, " \r\n\t");
return true;
return cli_args_read_quoted_string_and_trim(args, word);
} else {
return cli_args_read_string_and_trim(args, word);
}

View File

@ -13,7 +13,6 @@
#include <m-string.h>
/** Extract int value and trim arguments string
*
* @param args - arguments string
* @param word first argument, output
* @return true - success
@ -23,7 +22,6 @@ bool cli_args_read_int_and_trim(mstring_t* args, int* value);
/**
* @brief Extract first argument from arguments string and trim arguments string
*
* @param args arguments string
* @param word first argument, output
* @return true - success
@ -31,9 +29,17 @@ bool cli_args_read_int_and_trim(mstring_t* args, int* value);
*/
bool cli_args_read_string_and_trim(mstring_t* args, mstring_t* word);
/**
* @brief Extract the first quoted argument from the argument string and trim the argument string. If the argument is not quoted, returns false.
* @param args arguments string
* @param word first argument, output, without quotes
* @return true - success
* @return false - arguments string does not contain anything or not quoted
*/
bool cli_args_read_quoted_string_and_trim(mstring_t* args, mstring_t* word);
/**
* @brief Extract the first quoted argument from the argument string and trim the argument string. If the argument is not quoted, calls args_read_string_and_trim.
*
* @param args arguments string
* @param word first argument, output, without quotes
* @return true - success
@ -45,7 +51,6 @@ bool cli_args_read_probably_quoted_string_and_trim(mstring_t* args, mstring_t* w
/**
* @brief Get length of first word from arguments string
*
* @param args arguments string
* @return size_t length of first word
*/
@ -53,7 +58,6 @@ size_t cli_args_get_first_word_length(mstring_t* args);
/**
* @brief Get length of arguments string
*
* @param args arguments string
* @return size_t length of arguments string
*/