Revision 01e7f188 qemu-option.c

b/qemu-option.c
28 28

  
29 29
#include "qemu-common.h"
30 30
#include "qemu-error.h"
31
#include "qemu-objects.h"
31 32
#include "qemu-option.h"
32 33

  
33 34
/*
......
777 778
    return opts;
778 779
}
779 780

  
781
static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
782
{
783
    char buf[32];
784
    const char *value;
785
    int n;
786

  
787
    if (!strcmp(key, "id")) {
788
        return;
789
    }
790

  
791
    switch (qobject_type(obj)) {
792
    case QTYPE_QSTRING:
793
        value = qstring_get_str(qobject_to_qstring(obj));
794
        break;
795
    case QTYPE_QINT:
796
        n = snprintf(buf, sizeof(buf), "%" PRId64,
797
                     qint_get_int(qobject_to_qint(obj)));
798
        assert(n < sizeof(buf));
799
        value = buf;
800
        break;
801
    case QTYPE_QFLOAT:
802
        n = snprintf(buf, sizeof(buf), "%.17g",
803
                     qfloat_get_double(qobject_to_qfloat(obj)));
804
        assert(n < sizeof(buf));
805
        value = buf;
806
        break;
807
    case QTYPE_QBOOL:
808
        strcpy(buf, qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
809
        value = buf;
810
        break;
811
    default:
812
        return;
813
    }
814
    qemu_opt_set(opaque, key, value);
815
}
816

  
817
/*
818
 * Create QemuOpts from a QDict.
819
 * Use value of key "id" as ID if it exists and is a QString.
820
 * Only QStrings, QInts, QFloats and QBools are copied.  Entries with
821
 * other types are silently ignored.
822
 */
823
QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
824
{
825
    QemuOpts *opts;
826

  
827
    opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
828
    if (opts == NULL)
829
        return NULL;
830

  
831
    qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
832
    return opts;
833
}
834

  
835
/*
836
 * Convert from QemuOpts to QDict.
837
 * The QDict values are of type QString.
838
 * TODO We'll want to use types appropriate for opt->desc->type, but
839
 * this is enough for now.
840
 */
841
QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
842
{
843
    QemuOpt *opt;
844
    QObject *val;
845

  
846
    if (!qdict) {
847
        qdict = qdict_new();
848
    }
849
    if (opts->id) {
850
        qdict_put(qdict, "id", qstring_from_str(opts->id));
851
    }
852
    QTAILQ_FOREACH(opt, &opts->head, next) {
853
        val = QOBJECT(qstring_from_str(opt->str));
854
        qdict_put_obj(qdict, opt->name, val);
855
    }
856
    return qdict;
857
}
858

  
780 859
/* Validate parsed opts against descriptions where no
781 860
 * descriptions were provided in the QemuOptsList.
782 861
 */

Also available in: Unified diff