|
Packit |
5b56b6 |
/* Class autosprintf - formatted output to an ostream.
|
|
Packit |
5b56b6 |
Copyright (C) 2002, 2012-2015 Free Software Foundation, Inc.
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
5b56b6 |
it under the terms of the GNU Lesser General Public License as published by
|
|
Packit |
5b56b6 |
the Free Software Foundation; either version 2.1 of the License, or
|
|
Packit |
5b56b6 |
(at your option) any later version.
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
5b56b6 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5b56b6 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
5b56b6 |
GNU Lesser General Public License for more details.
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
You should have received a copy of the GNU Lesser General Public License
|
|
Packit |
5b56b6 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
#ifndef _AUTOSPRINTF_H
|
|
Packit |
5b56b6 |
#define _AUTOSPRINTF_H
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
/* This feature is available in gcc versions 2.5 and later. */
|
|
Packit |
5b56b6 |
#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
|
|
Packit |
5b56b6 |
# define _AUTOSPRINTF_ATTRIBUTE_FORMAT() /* empty */
|
|
Packit |
5b56b6 |
#else
|
|
Packit |
5b56b6 |
/* The __-protected variants of 'format' and 'printf' attributes
|
|
Packit |
5b56b6 |
are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
|
|
Packit |
5b56b6 |
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
|
|
Packit |
5b56b6 |
# define _AUTOSPRINTF_ATTRIBUTE_FORMAT() \
|
|
Packit |
5b56b6 |
__attribute__ ((__format__ (__printf__, 2, 3)))
|
|
Packit |
5b56b6 |
# else
|
|
Packit |
5b56b6 |
# define _AUTOSPRINTF_ATTRIBUTE_FORMAT() \
|
|
Packit |
5b56b6 |
__attribute__ ((format (printf, 2, 3)))
|
|
Packit |
5b56b6 |
# endif
|
|
Packit |
5b56b6 |
#endif
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
#include <string>
|
|
Packit |
5b56b6 |
#include <iostream>
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
namespace gnu
|
|
Packit |
5b56b6 |
{
|
|
Packit |
5b56b6 |
/* A temporary object, usually allocated on the stack, representing
|
|
Packit |
5b56b6 |
the result of an asprintf() call. */
|
|
Packit |
5b56b6 |
class autosprintf
|
|
Packit |
5b56b6 |
{
|
|
Packit |
5b56b6 |
public:
|
|
Packit |
5b56b6 |
/* Constructor: takes a format string and the printf arguments. */
|
|
Packit |
5b56b6 |
autosprintf (const char *format, ...)
|
|
Packit |
5b56b6 |
_AUTOSPRINTF_ATTRIBUTE_FORMAT();
|
|
Packit |
5b56b6 |
/* Copy constructor. */
|
|
Packit |
5b56b6 |
autosprintf (const autosprintf& src);
|
|
Packit |
5b56b6 |
autosprintf& operator = (autosprintf copy);
|
|
Packit |
5b56b6 |
/* Destructor: frees the temporarily allocated string. */
|
|
Packit |
5b56b6 |
~autosprintf ();
|
|
Packit |
5b56b6 |
/* Conversion to string. */
|
|
Packit |
5b56b6 |
operator char * () const;
|
|
Packit |
5b56b6 |
operator std::string () const;
|
|
Packit |
5b56b6 |
/* Output to an ostream. */
|
|
Packit |
5b56b6 |
friend inline std::ostream& operator<< (std::ostream& stream, const autosprintf& tmp)
|
|
Packit |
5b56b6 |
{
|
|
Packit |
5b56b6 |
stream << (tmp.str ? tmp.str : "(error in autosprintf)");
|
|
Packit |
5b56b6 |
return stream;
|
|
Packit |
5b56b6 |
}
|
|
Packit |
5b56b6 |
private:
|
|
Packit |
5b56b6 |
char *str;
|
|
Packit |
5b56b6 |
};
|
|
Packit |
5b56b6 |
}
|
|
Packit |
5b56b6 |
|
|
Packit |
5b56b6 |
#endif /* _AUTOSPRINTF_H */
|