|
@@ -575,97 +575,194 @@ char *faux_str_chars(const char *str, const char *chars_to_search)
|
|
|
return faux_str_charsn(str, chars_to_search, strlen(str));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-const char *faux_str_nextword(const char *string,
|
|
|
- size_t *len, size_t *offset, size_t *quoted)
|
|
|
+
|
|
|
+static char *faux_str_deesc(const char *string, size_t len)
|
|
|
{
|
|
|
- const char *word;
|
|
|
+ const char *s = string;
|
|
|
+ char *res = NULL;
|
|
|
+ char *p = NULL;
|
|
|
+ bool_t escaped = BOOL_FALSE;
|
|
|
|
|
|
- *quoted = 0;
|
|
|
+ assert(string);
|
|
|
+ if (!string)
|
|
|
+ return NULL;
|
|
|
+ if (0 == len)
|
|
|
+ return NULL;
|
|
|
|
|
|
-
|
|
|
- while (*string && isspace(*string)) {
|
|
|
- string++;
|
|
|
- (*offset)++;
|
|
|
- }
|
|
|
-
|
|
|
- if (*string == '"') {
|
|
|
- *quoted = 1;
|
|
|
- string++;
|
|
|
- }
|
|
|
- word = string;
|
|
|
- *len = 0;
|
|
|
-
|
|
|
-
|
|
|
- while (*string) {
|
|
|
- if (*string == '\\') {
|
|
|
- string++;
|
|
|
- (*len)++;
|
|
|
- if (*string) {
|
|
|
- (*len)++;
|
|
|
- string++;
|
|
|
- }
|
|
|
+ res = faux_zmalloc(len + 1);
|
|
|
+ assert(res);
|
|
|
+ if (!res)
|
|
|
+ return NULL;
|
|
|
+ p = res;
|
|
|
+
|
|
|
+ while ((*s != '\0') && (s < (string +len))) {
|
|
|
+ if (('\\' == *s) && !escaped) {
|
|
|
+ escaped = BOOL_TRUE;
|
|
|
+ s++;
|
|
|
continue;
|
|
|
}
|
|
|
-
|
|
|
- if (!*quoted && isspace(*string))
|
|
|
- break;
|
|
|
- if (*string == '"') {
|
|
|
-
|
|
|
- *quoted = 2;
|
|
|
- break;
|
|
|
- }
|
|
|
- (*len)++;
|
|
|
- string++;
|
|
|
+ escaped = BOOL_FALSE;
|
|
|
+ *p = *s;
|
|
|
+ s++;
|
|
|
+ p++;
|
|
|
}
|
|
|
+ *p = '\0';
|
|
|
|
|
|
- return word;
|
|
|
+ return res;
|
|
|
}
|
|
|
-*/
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-char *lub_string_ndecode(const char *string, unsigned int len)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @param [in] str String to parse.
|
|
|
+ * @param [out] offset Pointer to first symbol after found substring.
|
|
|
+ * @return Allocated buffer with found substring (without quotes).
|
|
|
+ * @warning Returned alocated buffer must be freed later by faux_str_free()
|
|
|
+ */
|
|
|
+char *faux_str_nextword(const char *str, const char **saveptr,
|
|
|
+ const char *alt_quotes)
|
|
|
{
|
|
|
- const char *s = string;
|
|
|
- char *res, *p;
|
|
|
- int esc = 0;
|
|
|
+ const char *string = str;
|
|
|
+ const char *word = NULL;
|
|
|
+ size_t len = 0;
|
|
|
+ const char dbl_quote = '"';
|
|
|
+ bool_t dbl_quoted = BOOL_FALSE;
|
|
|
+ char alt_quote = '\0';
|
|
|
+ unsigned int alt_quote_num = 0;
|
|
|
+ bool_t alt_quoted = BOOL_FALSE;
|
|
|
+ char *result = NULL;
|
|
|
|
|
|
- if (!string)
|
|
|
- return NULL;
|
|
|
+
|
|
|
+ while (*string && isspace(*string))
|
|
|
+ string++;
|
|
|
+
|
|
|
+ word = string;
|
|
|
+
|
|
|
+ while (*string != '\0') {
|
|
|
+
|
|
|
+
|
|
|
+ if (dbl_quoted) {
|
|
|
+
|
|
|
+ if (*string == dbl_quote) {
|
|
|
+ if (len > 0) {
|
|
|
+ char *s = faux_str_deesc(word, len);
|
|
|
+ faux_str_cat(&result, s);
|
|
|
+ faux_str_free(s);
|
|
|
+ }
|
|
|
+ dbl_quoted = BOOL_FALSE;
|
|
|
+ string++;
|
|
|
+ word = string;
|
|
|
+ len = 0;
|
|
|
+
|
|
|
+ } else if (*string == '\\') {
|
|
|
+
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+
|
|
|
+ if (*string) {
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+ }
|
|
|
|
|
|
- p = res = faux_zmalloc(len + 1);
|
|
|
+
|
|
|
+ } else if (alt_quoted) {
|
|
|
+ unsigned int qnum = alt_quote_num;
|
|
|
+ while (string && (*string == alt_quote) && qnum) {
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+ qnum--;
|
|
|
+ }
|
|
|
+ if (0 == qnum) {
|
|
|
+
|
|
|
+ len -= alt_quote_num;
|
|
|
+ if (len > 0)
|
|
|
+ faux_str_catn(&result, word, len);
|
|
|
+ alt_quoted = BOOL_FALSE;
|
|
|
+ word = string;
|
|
|
+ len = 0;
|
|
|
+ } else if (qnum == alt_quote_num) {
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+ }
|
|
|
|
|
|
- while (*s && (s < (string +len))) {
|
|
|
- if (!esc) {
|
|
|
- if ('\\' == *s)
|
|
|
- esc = 1;
|
|
|
- else
|
|
|
- *p = *s;
|
|
|
+
|
|
|
} else {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- esc = 0;
|
|
|
+ char *p = NULL;
|
|
|
+
|
|
|
+ if (*string == dbl_quote) {
|
|
|
+ if (len > 0) {
|
|
|
+ char *s = faux_str_deesc(word, len);
|
|
|
+ faux_str_cat(&result, s);
|
|
|
+ faux_str_free(s);
|
|
|
+ }
|
|
|
+ dbl_quoted = BOOL_TRUE;
|
|
|
+ string++;
|
|
|
+ word = string;
|
|
|
+ len = 0;
|
|
|
+
|
|
|
+ } else if ((p = strchr(alt_quotes, *string))) {
|
|
|
+ if (len > 0) {
|
|
|
+ char *s = faux_str_deesc(word, len);
|
|
|
+ faux_str_cat(&result, s);
|
|
|
+ faux_str_free(s);
|
|
|
+ }
|
|
|
+ alt_quoted = BOOL_TRUE;
|
|
|
+ alt_quote = *string;
|
|
|
+ alt_quote_num = 0;
|
|
|
+ while (string && (*string == alt_quote)) {
|
|
|
+ string++;
|
|
|
+ alt_quote_num++;
|
|
|
+ }
|
|
|
+ word = string;
|
|
|
+ len = 0;
|
|
|
+
|
|
|
+ } else if (isspace(*string)) {
|
|
|
+ if (len > 0) {
|
|
|
+ char *s = faux_str_deesc(word, len);
|
|
|
+ faux_str_cat(&result, s);
|
|
|
+ faux_str_free(s);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ } else if (*string == '\\') {
|
|
|
+
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+
|
|
|
+ if (*string) {
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ string++;
|
|
|
+ len++;
|
|
|
+ }
|
|
|
}
|
|
|
- if (!esc)
|
|
|
- p++;
|
|
|
- s++;
|
|
|
}
|
|
|
- *p = '\0';
|
|
|
|
|
|
- return res;
|
|
|
+ if (len > 0) {
|
|
|
+ if (alt_quoted) {
|
|
|
+ faux_str_catn(&result, word, len);
|
|
|
+ } else {
|
|
|
+ char *s = faux_str_deesc(word, len);
|
|
|
+ faux_str_cat(&result, s);
|
|
|
+ faux_str_free(s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (saveptr)
|
|
|
+ *saveptr = string;
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
*/
|
|
|
|
|
|
|
|
@@ -756,26 +853,3 @@ const char *lub_string_suffix(const char *string)
|
|
|
return p2;
|
|
|
}
|
|
|
*/
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-unsigned int lub_string_wordcount(const char *line)
|
|
|
-{
|
|
|
- const char *word;
|
|
|
- unsigned int result = 0;
|
|
|
- size_t len = 0, offset = 0;
|
|
|
- size_t quoted;
|
|
|
-
|
|
|
- for (word = lub_string_nextword(line, &len, &offset, "ed);
|
|
|
- *word || quoted;
|
|
|
- word = lub_string_nextword(word + len, &len, &offset, "ed)) {
|
|
|
-
|
|
|
- len += quoted ? quoted - 1 : 0;
|
|
|
- result++;
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
-}
|
|
|
-*/
|