mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-28 02:20:04 +01:00
Add g_variant_{new,get}_byte_array()
Now that strings are strictly utf8 this provides a convenient API for storing non-utf8 string data.
This commit is contained in:
parent
4c189ba6b0
commit
a52b6f7df2
@ -1161,6 +1161,85 @@ g_variant_dup_string (GVariant *value,
|
||||
return g_strdup (g_variant_get_string (value, length));
|
||||
}
|
||||
|
||||
/**
|
||||
* g_variant_new_byte_array:
|
||||
* @array: a pointer to an array of bytes
|
||||
* @length: the length of @array, or -1
|
||||
* @returns: a new floating #GVariant instance
|
||||
*
|
||||
* Constructs an array of bytes #GVariant from the given array of bytes.
|
||||
*
|
||||
* If @length is -1 then @array is taken to be a normal C string (in the
|
||||
* sense that it is terminated by a nul character). The nul character
|
||||
* is included in the array. If length is not -1 then it gives the
|
||||
* length of @array which may then contain nul chracters with no special
|
||||
* meaning.
|
||||
*
|
||||
* Since: 2.26
|
||||
**/
|
||||
GVariant *
|
||||
g_variant_new_byte_array (gconstpointer array,
|
||||
gssize length)
|
||||
{
|
||||
if (length == -1)
|
||||
{
|
||||
const gchar *bytes = array;
|
||||
|
||||
length = 0;
|
||||
while (bytes[length++]);
|
||||
}
|
||||
|
||||
return g_variant_new_from_trusted (G_VARIANT_TYPE ("ay"),
|
||||
array, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_variant_get_byte_array:
|
||||
* @value: an array of bytes #GVariant
|
||||
* @length: the length of the result, or %NULL
|
||||
* @returns: a pointer to the byte data, or %NULL
|
||||
*
|
||||
* Gets the contents of an array of bytes #GVariant.
|
||||
*
|
||||
* If @length is non-%NULL then it points to a location at which to
|
||||
* store the length of the array and nul bytes contained within the
|
||||
* array have no special meaning.
|
||||
*
|
||||
* If @length is %NULL then the caller has no way to determine what the
|
||||
* length of the returned data might be. In this case, the function
|
||||
* ensures that the last byte of the array is a nul byte and, if it is
|
||||
* not, returns %NULL instead. In this way, the caller is assured that
|
||||
* any non-%NULL pointer that is returned will be nul-terminated.
|
||||
*
|
||||
* The return value remains valid as long as @value exists.
|
||||
*
|
||||
* Since: 2.26
|
||||
**/
|
||||
gconstpointer
|
||||
g_variant_get_byte_array (GVariant *value,
|
||||
gsize *length)
|
||||
{
|
||||
gconstpointer data;
|
||||
gsize size;
|
||||
|
||||
TYPE_CHECK (value, G_VARIANT_TYPE ("ay"), NULL);
|
||||
|
||||
data = g_variant_get_data (value);
|
||||
size = g_variant_get_size (value);
|
||||
|
||||
if (length == NULL)
|
||||
{
|
||||
const gchar *bytes = data;
|
||||
|
||||
if (bytes[size - 1] != '\0')
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
*length = size;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_variant_new_strv:
|
||||
* @strv: an array of strings
|
||||
|
@ -78,6 +78,8 @@ gboolean g_variant_is_object_path (const g
|
||||
GVariant * g_variant_new_signature (const gchar *signature);
|
||||
gboolean g_variant_is_signature (const gchar *string);
|
||||
GVariant * g_variant_new_variant (GVariant *value);
|
||||
GVariant * g_variant_new_byte_array (gconstpointer array,
|
||||
gssize length);
|
||||
GVariant * g_variant_new_strv (const gchar * const *strv,
|
||||
gssize length);
|
||||
|
||||
@ -96,6 +98,8 @@ const gchar * g_variant_get_string (GVarian
|
||||
gsize *length);
|
||||
gchar * g_variant_dup_string (GVariant *value,
|
||||
gsize *length);
|
||||
gconstpointer g_variant_get_byte_array (GVariant *value,
|
||||
gsize *length);
|
||||
const gchar ** g_variant_get_strv (GVariant *value,
|
||||
gsize *length);
|
||||
gchar ** g_variant_dup_strv (GVariant *value,
|
||||
|
Loading…
x
Reference in New Issue
Block a user