1*946379e7Schristos/* Class autosprintf - formatted output to an ostream. 2*946379e7Schristos Copyright (C) 2002 Free Software Foundation, Inc. 3*946379e7Schristos 4*946379e7Schristos This program is free software; you can redistribute it and/or modify it 5*946379e7Schristos under the terms of the GNU Library General Public License as published 6*946379e7Schristos by the Free Software Foundation; either version 2, or (at your option) 7*946379e7Schristos any later version. 8*946379e7Schristos 9*946379e7Schristos This program is distributed in the hope that it will be useful, 10*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 11*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12*946379e7Schristos Library General Public License for more details. 13*946379e7Schristos 14*946379e7Schristos You should have received a copy of the GNU Library General Public 15*946379e7Schristos License along with this program; if not, write to the Free Software 16*946379e7Schristos Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 17*946379e7Schristos USA. */ 18*946379e7Schristos 19*946379e7Schristos#ifndef _AUTOSPRINTF_H 20*946379e7Schristos#define _AUTOSPRINTF_H 21*946379e7Schristos 22*946379e7Schristos#ifndef __attribute__ 23*946379e7Schristos/* This feature is available in gcc versions 2.5 and later. */ 24*946379e7Schristos# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ 25*946379e7Schristos# define __attribute__(Spec) /* empty */ 26*946379e7Schristos# endif 27*946379e7Schristos/* The __-protected variants of `format' and `printf' attributes 28*946379e7Schristos are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */ 29*946379e7Schristos# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) 30*946379e7Schristos# define __format__ format 31*946379e7Schristos# define __printf__ printf 32*946379e7Schristos# endif 33*946379e7Schristos#endif 34*946379e7Schristos 35*946379e7Schristos#include <string> 36*946379e7Schristos#include <iostream> 37*946379e7Schristos 38*946379e7Schristosnamespace gnu 39*946379e7Schristos{ 40*946379e7Schristos /* A temporary object, usually allocated on the stack, representing 41*946379e7Schristos the result of an asprintf() call. */ 42*946379e7Schristos class autosprintf 43*946379e7Schristos { 44*946379e7Schristos public: 45*946379e7Schristos /* Constructor: takes a format string and the printf arguments. */ 46*946379e7Schristos autosprintf (const char *format, ...) 47*946379e7Schristos __attribute__ ((__format__ (__printf__, 2, 3))); 48*946379e7Schristos /* Copy constructor. */ 49*946379e7Schristos autosprintf (const autosprintf& src); 50*946379e7Schristos /* Destructor: frees the temporarily allocated string. */ 51*946379e7Schristos ~autosprintf (); 52*946379e7Schristos /* Conversion to string. */ 53*946379e7Schristos operator char * () const; 54*946379e7Schristos operator std::string () const; 55*946379e7Schristos /* Output to an ostream. */ 56*946379e7Schristos friend inline std::ostream& operator<< (std::ostream& stream, const autosprintf& tmp) 57*946379e7Schristos { 58*946379e7Schristos stream << (tmp.str ? tmp.str : "(error in autosprintf)"); 59*946379e7Schristos return stream; 60*946379e7Schristos } 61*946379e7Schristos private: 62*946379e7Schristos char *str; 63*946379e7Schristos }; 64*946379e7Schristos} 65*946379e7Schristos 66*946379e7Schristos#endif /* _AUTOSPRINTF_H */ 67