No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-runtime / libasprintf / autosprintf.cc
blobc145d5f333d7aa4a42935eb4a9f4564cf71788a7
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)
8 any later version.
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,
18 USA. */
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. */
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE 1
25 #endif
27 /* Specification. */
28 #include "autosprintf.h"
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "lib-asprintf.h"
35 namespace gnu
38 /* Constructor: takes a format string and the printf arguments. */
39 autosprintf::autosprintf (const char *format, ...)
41 va_list args;
42 va_start (args, format);
43 if (vasprintf (&str, format, args) < 0)
44 str = NULL;
45 va_end (args);
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 ()
57 free (str);
60 /* Conversion to string. */
61 autosprintf::operator char * () const
63 if (str != NULL)
65 size_t length = strlen (str) + 1;
66 char *copy = new char[length];
67 memcpy (copy, str, length);
68 return copy;
70 else
71 return NULL;
73 autosprintf::operator std::string () const
75 return std::string (str ? str : "(error in autosprintf)");