From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40173) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKlGa-0006W6-VG for qemu-devel@nongnu.org; Tue, 04 Mar 2014 04:04:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WKlGV-0007mQ-SD for qemu-devel@nongnu.org; Tue, 04 Mar 2014 04:04:00 -0500 Received: from mail-qc0-x22d.google.com ([2607:f8b0:400d:c01::22d]:50691) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKlGV-0007kk-Ks for qemu-devel@nongnu.org; Tue, 04 Mar 2014 04:03:55 -0500 Received: by mail-qc0-f173.google.com with SMTP id r5so3766969qcx.4 for ; Tue, 04 Mar 2014 01:03:54 -0800 (PST) Sender: Paolo Bonzini Message-ID: <531596F5.3070001@redhat.com> Date: Tue, 04 Mar 2014 10:03:49 +0100 From: Paolo Bonzini MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v19 07/11] qapi: make string output visitor parse int list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Hu Tao , qemu-devel@nongnu.org Cc: imammedo@redhat.com, Hu Tao , gaowanlong@cn.fujitsu.com Il 04/03/2014 08:28, Hu Tao ha scritto: > From: Hu Tao > > Signed-off-by: Hu Tao > --- > qapi/string-output-visitor.c | 156 +++++++++++++++++++++++++++++++++++-- > tests/test-string-output-visitor.c | 26 +++++++ > 2 files changed, 177 insertions(+), 5 deletions(-) > > diff --git a/qapi/string-output-visitor.c b/qapi/string-output-visitor.c > index fb1d2e8..bc9bb36 100644 > --- a/qapi/string-output-visitor.c > +++ b/qapi/string-output-visitor.c > @@ -17,11 +17,57 @@ > #include "qemu/host-utils.h" > #include > > +enum ListMode { > + LM_NONE, /* not traversing a list of repeated options */ > + LM_STARTED, /* start_list() succeeded */ > + > + LM_IN_PROGRESS, /* next_list() has been called. > + * > + * Generating the next list link will consume the most > + * recently parsed QemuOpt instance of the repeated > + * option. > + * > + * Parsing a value into the list link will examine the > + * next QemuOpt instance of the repeated option, and > + * possibly enter LM_SIGNED_INTERVAL or > + * LM_UNSIGNED_INTERVAL. > + */ > + > + LM_SIGNED_INTERVAL, /* next_list() has been called. > + * > + * Generating the next list link will consume the most > + * recently stored element from the signed interval, > + * parsed from the most recent QemuOpt instance of the > + * repeated option. This may consume QemuOpt itself > + * and return to LM_IN_PROGRESS. > + * > + * Parsing a value into the list link will store the > + * next element of the signed interval. > + */ > + > + LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */ > + > + LM_END > +}; > + > +typedef enum ListMode ListMode; > + > struct StringOutputVisitor > { > Visitor visitor; > bool human; > + bool head; > char *string; > + ListMode list_mode; > + /* When parsing a list of repeating options as integers, values of the form > + * "a-b", representing a closed interval, are allowed. Elements in the > + * range are generated individually. > + */ > + union { > + int64_t s; > + uint64_t u; > + } range_start, range_end; > + > }; > > static void string_output_set(StringOutputVisitor *sov, char *string) > @@ -34,13 +80,60 @@ static void print_type_int(Visitor *v, int64_t *obj, const char *name, > Error **errp) > { > StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); > - char *out; > + char *out = NULL; > > - if (sov->human) { > - out = g_strdup_printf("%lld (%#llx)", (long long) *obj, (long long) *obj); > - } else { > - out = g_strdup_printf("%lld", (long long) *obj); > + switch (sov->list_mode) { > + case LM_NONE: > + if (sov->human) { > + out = g_strdup_printf("%lld (%#llx)", (long long) *obj, > + (long long) *obj); > + } else { > + out = g_strdup_printf("%lld", (long long) *obj); > + } > + sov->list_mode = LM_END; > + break; > + > + case LM_STARTED: > + sov->range_start.s = *obj; > + sov->range_end.s = *obj; > + sov->list_mode = LM_IN_PROGRESS; > + break; > + > + case LM_IN_PROGRESS: > + assert(sov->range_end.s + 1 == *obj); > + sov->range_end.s++; > + break; > + > + case LM_END: > + assert(sov->range_end.s + 1 == *obj); > + sov->range_end.s++; > + if (sov->range_end.s == sov->range_start.s) { > + if (sov->human) { > + out = g_strdup_printf("%lld (%#llx)", > + (long long)sov->range_start.s, > + (long long)sov->range_start.s); > + } else { > + out = g_strdup_printf("%lld", (long long)sov->range_start.s); > + } > + } else { > + if (sov->human) { > + out = g_strdup_printf("%lld(%#llx)-%lld(%#llx)", > + (long long) sov->range_start.s, > + (long long) sov->range_start.s, > + (long long) sov->range_end.s, > + (long long) sov->range_end.s); Perhaps "10-15 (0xa-0xf)"? > + } else { > + out = g_strdup_printf("%lld-%lld", > + (long long) sov->range_start.s, > + (long long) sov->range_end.s); > + } > + } This looks wrong. You do not insert any separator, and you do not handle things like "0-3,8-11". You probably should use a GString instead of string_output_set. Paolo > + break; > + > + default: > + abort(); > } > + > string_output_set(sov, out); > } > > @@ -103,6 +196,56 @@ static void print_type_number(Visitor *v, double *obj, const char *name, > string_output_set(sov, g_strdup_printf("%f", *obj)); > } > > +static void > +start_list(Visitor *v, const char *name, Error **errp) > +{ > + StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); > + > + /* we can't traverse a list in a list */ > + assert(sov->list_mode == LM_NONE); > + sov->list_mode = LM_STARTED; > + sov->head = true; > +} > + > +static GenericList * > +next_list(Visitor *v, GenericList **list, Error **errp) > +{ > + StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); > + GenericList *ret = NULL; > + if (*list) { > + if (sov->head) { > + ret = *list; > + } else { > + ret = (*list)->next; > + } > + > + if (sov->head) { > + if (ret && ret->next == NULL) { > + sov->list_mode = LM_NONE; > + } > + sov->head = false; > + } else { > + if (ret && ret->next == NULL) { > + sov->list_mode = LM_END; > + } > + } > + } > + > + return ret; > +} > + > +static void > +end_list(Visitor *v, Error **errp) > +{ > + StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); > + > + assert(sov->list_mode == LM_STARTED || > + sov->list_mode == LM_END || > + sov->list_mode == LM_IN_PROGRESS); > + sov->list_mode = LM_NONE; > + sov->head = true; > +} > + > char *string_output_get_string(StringOutputVisitor *sov) > { > char *string = sov->string; > @@ -134,6 +277,9 @@ StringOutputVisitor *string_output_visitor_new(bool human) > v->visitor.type_bool = print_type_bool; > v->visitor.type_str = print_type_str; > v->visitor.type_number = print_type_number; > + v->visitor.start_list = start_list; > + v->visitor.next_list = next_list; > + v->visitor.end_list = end_list; > > return v; > } > diff --git a/tests/test-string-output-visitor.c b/tests/test-string-output-visitor.c > index 22363d1..511ef14 100644 > --- a/tests/test-string-output-visitor.c > +++ b/tests/test-string-output-visitor.c > @@ -57,6 +57,30 @@ static void test_visitor_out_int(TestOutputVisitorData *data, > g_free(str); > } > > +static void test_visitor_out_intList(TestOutputVisitorData *data, > + const void *unused) > +{ > + int64_t value[] = {-2, -1, 0, 1, 2, 3, 4}; > + intList *list = NULL, **tmp = &list; > + int i; > + Error *errp = NULL; > + char *str; > + > + for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) { > + *tmp = g_malloc0(sizeof(**tmp)); > + (*tmp)->value = value[i]; > + tmp = &(*tmp)->next; > + } > + > + visit_type_intList(data->ov, &list, NULL, &errp); > + g_assert(error_is_set(&errp) == 0); > + > + str = string_output_get_string(data->sov); > + g_assert(str != NULL); > + g_assert_cmpstr(str, ==, "-2-4"); > + g_free(str); > +} > + > static void test_visitor_out_bool(TestOutputVisitorData *data, > const void *unused) > { > @@ -182,6 +206,8 @@ int main(int argc, char **argv) > &out_visitor_data, test_visitor_out_enum); > output_visitor_test_add("/string-visitor/output/enum-errors", > &out_visitor_data, test_visitor_out_enum_errors); > + output_visitor_test_add("/string-visitor/output/intList", > + &out_visitor_data, test_visitor_out_intList); > > g_test_run(); > >