vppinfra: add unformat_{single,double}_quoted_string function 67/39867/2
authorDamjan Marion <damarion@cisco.com>
Mon, 13 Nov 2023 21:43:17 +0000 (21:43 +0000)
committerDave Wallace <dwallacelf@gmail.com>
Tue, 14 Nov 2023 21:32:22 +0000 (21:32 +0000)
Change-Id: I8ee90be1b772074c1130b98c71b3be48c973b2e2
Type: improvement
Signed-off-by: Damjan Marion <damarion@cisco.com>
src/vppinfra/format.h
src/vppinfra/unformat.c

index cf5e1f6..2451173 100644 (file)
@@ -312,6 +312,10 @@ unformat_function_t unformat_memory_size;
 /* Unformat C string array, takes array length as 2nd argument */
 unformat_function_t unformat_c_string_array;
 
+/* Unformat sigle and double quoted string */
+unformat_function_t unformat_single_quoted_string;
+unformat_function_t unformat_double_quoted_string;
+
 /* Format base 10 e.g. 100, 100K, 100M, 100G */
 u8 *format_base10 (u8 *s, va_list *va);
 
index cea5b67..fe1a46e 100644 (file)
@@ -1143,6 +1143,46 @@ unformat_c_string_array (unformat_input_t *input, va_list *va)
   return rv;
 }
 
+static uword
+__unformat_quoted_string (unformat_input_t *input, u8 **sp, char quote)
+{
+  u8 *s = 0;
+  uword c, p = 0;
+
+  while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
+    if (!is_white_space (c))
+      break;
+
+  if (c != quote)
+    return 0;
+
+  while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
+    {
+      if (c == quote && p != '\\')
+       {
+         *sp = s;
+         return 1;
+       }
+      vec_add1 (s, c);
+      p = c;
+    }
+  vec_free (s);
+
+  return 0;
+}
+
+__clib_export uword
+unformat_single_quoted_string (unformat_input_t *input, va_list *va)
+{
+  return __unformat_quoted_string (input, va_arg (*va, u8 **), '\'');
+}
+
+__clib_export uword
+unformat_double_quoted_string (unformat_input_t *input, va_list *va)
+{
+  return __unformat_quoted_string (input, va_arg (*va, u8 **), '"');
+}
+
 #endif /* CLIB_UNIX */