vppinfra: create unformat function for data size parsing 23/22523/2
authorMathiasRaoul <mathias.raoul@gmail.com>
Fri, 4 Oct 2019 09:53:45 +0000 (09:53 +0000)
committerDave Wallace <dwallacelf@gmail.com>
Fri, 4 Oct 2019 14:02:04 +0000 (14:02 +0000)
Type: feature

Signed-off-by: MathiasRaoul <mathias.raoul@gmail.com>
Change-Id: I8d71078a9ed42326e19453ea10008c6bb6992c52

src/vppinfra/format.h
src/vppinfra/unformat.c

index c4becff..678029e 100644 (file)
@@ -317,6 +317,8 @@ uword unformat_init_unix_env (unformat_input_t * input, char *var);
 unformat_function_t unformat_unix_gid;
 #endif /* CLIB_UNIX */
 
+uword unformat_data_size (unformat_input_t * input, va_list * args);
+
 /* Test code. */
 int test_format_main (unformat_input_t * input);
 int test_unformat_main (unformat_input_t * input);
index 52b8bb7..3c67113 100644 (file)
@@ -1077,6 +1077,30 @@ unformat_init_unix_env (unformat_input_t * input, char *var)
   return val != 0;
 }
 
+uword
+unformat_data_size (unformat_input_t * input, va_list * args)
+{
+  u64 _a;
+  u64 *a = va_arg (*args, u64 *);
+  if (unformat (input, "%lluGb", &_a))
+    *a = _a << 30;
+  else if (unformat (input, "%lluG", &_a))
+    *a = _a << 30;
+  else if (unformat (input, "%lluMb", &_a))
+    *a = _a << 20;
+  else if (unformat (input, "%lluM", &_a))
+    *a = _a << 20;
+  else if (unformat (input, "%lluKb", &_a))
+    *a = _a << 10;
+  else if (unformat (input, "%lluK", &_a))
+    *a = _a << 10;
+  else if (unformat (input, "%llu", a))
+    ;
+  else
+    return 0;
+  return 1;
+}
+
 #endif /* CLIB_UNIX */