• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/gettext-0.17/gettext-runtime/libasprintf/
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.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18   USA.  */
19
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
26
27/* Specification.  */
28#include "autosprintf.h"
29
30#include <stdarg.h>
31#include <stdlib.h>
32#include <string.h>
33#include "lib-asprintf.h"
34
35namespace gnu
36{
37
38  /* Constructor: takes a format string and the printf arguments.  */
39  autosprintf::autosprintf (const char *format, ...)
40  {
41    va_list args;
42    va_start (args, format);
43    if (vasprintf (&str, format, args) < 0)
44      str = NULL;
45    va_end (args);
46  }
47
48  /* Copy constructor.  Necessary because the destructor is nontrivial.  */
49  autosprintf::autosprintf (const autosprintf& src)
50  {
51    str = (src.str != NULL ? strdup (src.str) : NULL);
52  }
53
54  /* Destructor: frees the temporarily allocated string.  */
55  autosprintf::~autosprintf ()
56  {
57    free (str);
58  }
59
60  /* Conversion to string.  */
61  autosprintf::operator char * () const
62  {
63    if (str != NULL)
64      {
65        size_t length = strlen (str) + 1;
66        char *copy = new char[length];
67        memcpy (copy, str, length);
68        return copy;
69      }
70    else
71      return NULL;
72  }
73  autosprintf::operator std::string () const
74  {
75    return std::string (str ? str : "(error in autosprintf)");
76  }
77}
78