From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 122778 invoked by alias); 19 Oct 2016 01:12:25 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 122737 invoked by uid 89); 19 Oct 2016 01:12:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=va_list, *format, Hx-languages-length:2156 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 19 Oct 2016 01:12:22 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7D24861E61 for ; Wed, 19 Oct 2016 01:12:21 +0000 (UTC) Received: from cascais.lan (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u9J1CJjW019701 for ; Tue, 18 Oct 2016 21:12:21 -0400 From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH v2 01/31] Introduce string_printf Date: Wed, 19 Oct 2016 01:12:00 -0000 Message-Id: <1476839539-8374-2-git-send-email-palves@redhat.com> In-Reply-To: <1476839539-8374-1-git-send-email-palves@redhat.com> References: <1476839539-8374-1-git-send-email-palves@redhat.com> X-SW-Source: 2016-10/txt/msg00524.txt.bz2 This introduces the string_printf function. Like asprintf, but returns a std::string. gdb/ChangeLog: yyyy-mm-yy Pedro Alves * common/common-utils.c (string_printf): New function. * common/common-utils.h: Include . (string_printf): Declare. --- gdb/common/common-utils.c | 30 ++++++++++++++++++++++++++++++ gdb/common/common-utils.h | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c index 5a346ec..05ba3aa 100644 --- a/gdb/common/common-utils.c +++ b/gdb/common/common-utils.c @@ -150,6 +150,36 @@ xsnprintf (char *str, size_t size, const char *format, ...) return ret; } +/* See documentation in common-utils.h. */ + +std::string +string_printf (const char* fmt, ...) +{ + std::string str; + va_list vp; + + /* Start by assuming some reasonable size will be sufficient. */ + str.resize (1024); + + while (1) + { + size_t size; + int result; + + va_start (vp, fmt); + size = str.size (); + result = vsnprintf (&str[0], size, fmt, vp); + va_end (vp); + + str.resize (result); + + if (result < size) + break; + } + + return str; +} + char * savestring (const char *ptr, size_t len) { diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 47def11..a9053ff 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -20,6 +20,8 @@ #ifndef COMMON_UTILS_H #define COMMON_UTILS_H +#include + /* If possible, define FUNCTION_NAME, a macro containing the name of the function being defined. Since this macro may not always be defined, all uses must be protected by appropriate macro definition @@ -56,6 +58,10 @@ char *xstrvprintf (const char *format, va_list ap) int xsnprintf (char *str, size_t size, const char *format, ...) ATTRIBUTE_PRINTF (3, 4); +/* Returns a std::string built from a printf-style format string. */ +std::string string_printf (const char* fmt, ...) + ATTRIBUTE_PRINTF (1, 2); + /* Make a copy of the string at PTR with LEN characters (and add a null character at the end in the copy). Uses malloc to get the space. Returns the address of the copy. */ -- 2.5.5