复杂格式的gvariant解析
Posted
技术标签:
【中文标题】复杂格式的gvariant解析【英文标题】:gvariant parsing for a complex format 【发布时间】:2015-06-07 15:22:47 【问题描述】:在我的应用程序中,我将通过 dbus 收到一条消息,该消息必须使用 gvariant 函数进行解析。
格式字符串为G_VARIANT_TYPE("a(ysb((sss)(sss)))"
构造gVariant的示例代码如下
int ParseVariant (GVariant *value);
int main()
GVariantBuilder *builder;
GVariant *value;
builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ysb((sss)(sss)))"));
g_variant_builder_add (builder, "(ysb((sss)(sss)))", 'J', "Test1", TRUE, "Hn", "di", "hedfs", "dd", "dr", "hdf");
g_variant_builder_add (builder, "(ysb((sss)(sss)))", 'J', "Test2", TRUE, "Hn", "di", "hedfs", "dd", "dr", "hdf");
//g_variant_builder_add (builder, "(ysb((sss)(sss)))", 'J', "Test3", TRUE, "Hn", "di", "hedfs", "dd", "dr", "hdf");
GVariant *result = g_variant_new ("((yu)ya(ysb((sss)(sss))))", 'R', 23, 'E', builder);
g_variant_builder_unref (builder);
ParseVariant (result);
我正在寻找一个解析器函数 (ParseVariant (result)),我可以使用它来提取变体结果的每个元素。
对此的任何帮助都非常感谢。
提前致谢。
【问题讨论】:
您尝试了哪些方法,遇到了哪些具体问题?总之 g_variant_get 是你需要使用的。 developer.gnome.org/glib/stable/…。如果您在尝试后仍有问题,请发布您的具体问题。 【参考方案1】:这是我在 Gtk 邮件列表中针对您的问题提供的答案,您还提供了 ParseVariant()
:
#include <glib.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
int ParseVariant (GVariant *value)
GVariant * list;
gchar ch1,ch2,ch3;
gchar *str1,*str2,*str3,*str4,*str5,*str6,*str7;
gboolean bool1;
gint x;
GVariantIter iter;
g_print ("Inside Parse\n");
g_variant_get (value, "((yu)y@a(ysb((sss)(sss))))", &ch1, &x, &ch2, &list);
g_variant_iter_init (&iter, list);
while (g_variant_iter_loop (&iter, "(ysb((sss)(sss)))", &ch3, &str1, &bool1, &str2, &str3, &str4, &str5, &str6, &str7))
g_print ("%s\n", str1);
g_variant_unref(list);
int main()
GVariantBuilder * builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ysb((sss)(sss)))"));
g_variant_builder_add (builder, "(ysb((sss)(sss)))", 'J', "Test1", TRUE, "Hn", "di", "hedfs", "dd", "dr", "hdf");
g_variant_builder_add (builder, "(ysb((sss)(sss)))", 'J', "Test2", TRUE, "Hn", "di", "hedfs", "dd", "dr", "hdf");
GVariant * result = g_variant_new ("((yu)ya(ysb((sss)(sss))))", 'R', 23, 'E', builder);
g_variant_builder_unref (builder);
ParseVariant (result);
【讨论】:
以上是关于复杂格式的gvariant解析的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 QDBus 解析 String, Dict of String, Variant 的字典?