From patchwork Thu Apr 15 14:10:54 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 50260 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 90B6EB7D00 for ; Fri, 16 Apr 2010 00:57:32 +1000 (EST) Received: from localhost ([127.0.0.1]:53181 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2QQl-0007k2-BT for incoming@patchwork.ozlabs.org; Thu, 15 Apr 2010 10:52:35 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O2Ptb-0003ZJ-0c for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:19 -0400 Received: from [140.186.70.92] (port=50355 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2PtQ-0003Fy-4y for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O2Pmv-0000Gk-5F for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:42 -0400 Received: from e28smtp08.in.ibm.com ([122.248.162.8]:39518) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O2Pmu-0000FK-1K for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:24 -0400 Received: from d28relay05.in.ibm.com (d28relay05.in.ibm.com [9.184.220.62]) by e28smtp08.in.ibm.com (8.14.3/8.13.1) with ESMTP id o3FDHMCo011482 for ; Thu, 15 Apr 2010 18:47:22 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay05.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3FEBJp03277044 for ; Thu, 15 Apr 2010 19:41:19 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o3FEBJos030457 for ; Thu, 15 Apr 2010 19:41:19 +0530 Received: from skywalker.in.ibm.com ([9.77.204.187]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o3FEBDa8030329; Thu, 15 Apr 2010 19:41:18 +0530 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Thu, 15 Apr 2010 19:40:54 +0530 Message-Id: <1271340671-19558-5-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4.360.g11766c In-Reply-To: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: ericvh@gmail.com, aliguori@us.ibm.com, aneesh.kumar@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH -V5 04/21] virtio-9p: Add string manipulation support. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Anthony Liguori Add helpers to do string manipulation. Signed-off-by: Anthony Liguori Signed-off-by: Aneesh Kumar K.V --- hw/virtio-9p.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 127 insertions(+), 0 deletions(-) diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 0b95eeb..08df084 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -21,6 +21,126 @@ int dotu = 1; int debug_9p_pdu; +static void v9fs_string_init(V9fsString *str) +{ + str->data = NULL; + str->size = 0; +} + +static void v9fs_string_free(V9fsString *str) +{ + qemu_free(str->data); + str->data = NULL; + str->size = 0; +} + +static void v9fs_string_null(V9fsString *str) +{ + v9fs_string_free(str); +} + +static int number_to_string(void *arg, char type) +{ + unsigned int ret = 0; + + switch (type) { + case 'u': { + unsigned int num = *(unsigned int *)arg; + + do { + ret++; + num = num/10; + } while (num); + break; + } + default: + printf("Number_to_string: Unknown number format\n"); + return -1; + } + + return ret; +} + +static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap) +{ + va_list ap2; + char *iter = (char *)fmt; + int len = 0; + int nr_args = 0; + char *arg_char_ptr; + unsigned int arg_uint; + + /* Find the number of %'s that denotes an argument */ + for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { + nr_args++; + iter++; + } + + len = strlen(fmt) - 2*nr_args; + + if (!nr_args) { + goto alloc_print; + } + + va_copy(ap2, ap); + + iter = (char *)fmt; + + /* Now parse the format string */ + for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { + iter++; + switch (*iter) { + case 'u': + arg_uint = va_arg(ap2, unsigned int); + len += number_to_string((void *)&arg_uint, 'u'); + break; + case 's': + arg_char_ptr = va_arg(ap2, char *); + len += strlen(arg_char_ptr); + break; + case 'c': + len += 1; + break; + default: + fprintf(stderr, + "v9fs_string_alloc_printf:Incorrect format %c", *iter); + return -1; + } + iter++; + } + +alloc_print: + *strp = qemu_malloc((len + 1) * sizeof(**strp)); + + return vsprintf(*strp, fmt, ap); +} + +static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...) +{ + va_list ap; + int err; + + v9fs_string_free(str); + + va_start(ap, fmt); + err = v9fs_string_alloc_printf(&str->data, fmt, ap); + BUG_ON(err == -1); + va_end(ap); + + str->size = err; +} + +static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs) +{ + v9fs_string_free(lhs); + v9fs_string_sprintf(lhs, "%s", rhs->data); +} + +static size_t v9fs_string_size(V9fsString *str) +{ + return str->size; +} + static V9fsPDU *alloc_pdu(V9fsState *s) { V9fsPDU *pdu = NULL; @@ -313,6 +433,13 @@ static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu) * They will be removed in the subsequent patches */ (void)pdu_unmarshal; (void) complete_pdu; + (void) v9fs_string_init; + (void) v9fs_string_free; + (void) v9fs_string_null; + (void) v9fs_string_sprintf; + (void) v9fs_string_copy; + (void) v9fs_string_size; + } static void v9fs_version(V9fsState *s, V9fsPDU *pdu)