eina: add an ability to quickly convert from an Eina_Value to an Eina_Binbuf.
authorCedric Bail <cedric@osg.samsung.com>
Thu, 14 Sep 2017 00:26:04 +0000 (17:26 -0700)
committerCedric Bail <cedric@osg.samsung.com>
Thu, 14 Sep 2017 00:26:04 +0000 (17:26 -0700)
src/lib/eina/eina_binbuf.h
src/lib/eina/eina_value.c
src/lib/eina/eina_value.h

index 0aa0e1a11751e72920d75fb52666c7ea73a1330f..e84453731ec20dfbb953222051ac4c881bd0f847 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdarg.h>
 
 #include "eina_types.h"
+#include "eina_slice.h"
 
 /**
  * @addtogroup Eina_Binary_Buffer_Group Binary Buffer
index d3a9d6dec68814e09e974a5d41b730f4df146b39..f22718b1473c2215bda5a3d745a9d9029f867046 100644 (file)
@@ -37,6 +37,7 @@
 #include "eina_lock.h"
 #include "eina_file.h"
 #include "eina_rectangle.h"
+#include "eina_binbuf.h"
 
 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
 #include "eina_safety_checks.h"
@@ -5572,6 +5573,34 @@ eina_value_to_string(const Eina_Value *value)
    return tmp.value.ptr; /* steal value */
 }
 
+EAPI Eina_Binbuf *
+eina_value_to_binbuf(Eina_Value *value)
+{
+   Eina_Value tmp = EINA_VALUE_EMPTY;
+   Eina_Value_Blob out;
+   Eina_Binbuf *buf;
+
+   if (value->type != EINA_VALUE_TYPE_BLOB)
+     {
+        eina_value_setup(&tmp, EINA_VALUE_TYPE_BLOB);
+
+        if (!eina_value_convert(value, &tmp))
+          return NULL;
+
+        value = &tmp;
+     }
+
+   eina_value_get(value, &out);
+   if (!out.memory) return NULL;
+
+   buf = eina_binbuf_new();
+   eina_binbuf_append_length(buf, out.memory, out.size);
+
+   eina_value_flush(&tmp);
+
+   return buf;
+}
+
 EAPI Eina_Value *
 eina_value_array_new(const Eina_Value_Type *subtype, unsigned int step)
 {
index 305d3785481bef4537f3df8e766b79af2d0f435f..773de021198c448030856dc9ef3b01d681d1b35e 100644 (file)
@@ -27,6 +27,7 @@
 #include "eina_list.h"
 #include "eina_hash.h"
 #include "eina_rectangle.h"
+#include "eina_binbuf.h"
 
 /**
  * @page eina_value_example_01_page Eina_Value usage
@@ -1070,6 +1071,22 @@ static inline Eina_Bool eina_value_pget(const Eina_Value *value,
 EAPI Eina_Bool eina_value_convert(const Eina_Value *value,
                                   Eina_Value *convert) EINA_ARG_NONNULL(1, 2);
 
+/**
+ * @brief Converts one value to Eina_Binbuf.
+ * @param value Source value object.
+ * @return @c NULL if it failed to get a memory content, a valid Eina_Binbuf otherwise.
+ *
+ * Converts one value to EINA_TYPE_VALUE_BLOB if necessary by calling
+ * @c eina_value_convert() function.
+ *
+ * @note You are responsible for destroying the Eina_Binbuf returned.
+ *
+ * @see eina_value_to_string()
+ * @see eina_value_convert()
+ *
+ * @since 1.20
+ */
+EAPI Eina_Binbuf *eina_value_to_binbuf(Eina_Value *value);
 
 /**
  * @brief Converts value to string.
@@ -1077,6 +1094,7 @@ EAPI Eina_Bool eina_value_convert(const Eina_Value *value,
  * @return newly allocated memory or @c NULL on failure.
  *
  * @see eina_value_convert()
+ * @see eina_value_to_binbuf()
  * @since 1.2
  */
 EAPI char *eina_value_to_string(const Eina_Value *value) EINA_ARG_NONNULL(1);