1 /* Class autosprintf - formatted output to an ostream.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 /* Tell glibc's <stdio.h> to provide a prototype for vasprintf().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
24 # define _GNU_SOURCE 1
28 #include "autosprintf.h"
33 #include "lib-asprintf.h"
38 /* Constructor: takes a format string and the printf arguments. */
39 autosprintf::autosprintf (const char *format
, ...)
42 va_start (args
, format
);
43 if (vasprintf (&str
, format
, args
) < 0)
48 /* Copy constructor. Necessary because the destructor is nontrivial. */
49 autosprintf::autosprintf (const autosprintf
& src
)
51 str
= (src
.str
!= NULL
? strdup (src
.str
) : NULL
);
54 /* Destructor: frees the temporarily allocated string. */
55 autosprintf::~autosprintf ()
60 /* Conversion to string. */
61 autosprintf::operator char * () const
65 size_t length
= strlen (str
) + 1;
66 char *copy
= new char[length
];
67 memcpy (copy
, str
, length
);
73 autosprintf::operator std::string () const
75 return std::string (str
? str
: "(error in autosprintf)");