|
|
1.1 ! root 1: /* ! 2: * String printing Visitor ! 3: * ! 4: * Copyright Red Hat, Inc. 2012 ! 5: * ! 6: * Author: Paolo Bonzini <[email protected]> ! 7: * ! 8: * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. ! 9: * See the COPYING.LIB file in the top-level directory. ! 10: * ! 11: */ ! 12: ! 13: #include "qemu-common.h" ! 14: #include "string-output-visitor.h" ! 15: #include "qapi/qapi-visit-impl.h" ! 16: #include "qerror.h" ! 17: ! 18: struct StringOutputVisitor ! 19: { ! 20: Visitor visitor; ! 21: char *string; ! 22: }; ! 23: ! 24: static void string_output_set(StringOutputVisitor *sov, char *string) ! 25: { ! 26: g_free(sov->string); ! 27: sov->string = string; ! 28: } ! 29: ! 30: static void print_type_int(Visitor *v, int64_t *obj, const char *name, ! 31: Error **errp) ! 32: { ! 33: StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); ! 34: string_output_set(sov, g_strdup_printf("%lld", (long long) *obj)); ! 35: } ! 36: ! 37: static void print_type_bool(Visitor *v, bool *obj, const char *name, ! 38: Error **errp) ! 39: { ! 40: StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); ! 41: string_output_set(sov, g_strdup(*obj ? "true" : "false")); ! 42: } ! 43: ! 44: static void print_type_str(Visitor *v, char **obj, const char *name, ! 45: Error **errp) ! 46: { ! 47: StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); ! 48: string_output_set(sov, g_strdup(*obj ? *obj : "")); ! 49: } ! 50: ! 51: static void print_type_number(Visitor *v, double *obj, const char *name, ! 52: Error **errp) ! 53: { ! 54: StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); ! 55: string_output_set(sov, g_strdup_printf("%g", *obj)); ! 56: } ! 57: ! 58: char *string_output_get_string(StringOutputVisitor *sov) ! 59: { ! 60: char *string = sov->string; ! 61: sov->string = NULL; ! 62: return string; ! 63: } ! 64: ! 65: Visitor *string_output_get_visitor(StringOutputVisitor *sov) ! 66: { ! 67: return &sov->visitor; ! 68: } ! 69: ! 70: void string_output_visitor_cleanup(StringOutputVisitor *sov) ! 71: { ! 72: g_free(sov->string); ! 73: g_free(sov); ! 74: } ! 75: ! 76: StringOutputVisitor *string_output_visitor_new(void) ! 77: { ! 78: StringOutputVisitor *v; ! 79: ! 80: v = g_malloc0(sizeof(*v)); ! 81: ! 82: v->visitor.type_enum = output_type_enum; ! 83: v->visitor.type_int = print_type_int; ! 84: v->visitor.type_bool = print_type_bool; ! 85: v->visitor.type_str = print_type_str; ! 86: v->visitor.type_number = print_type_number; ! 87: ! 88: return v; ! 89: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.