Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-14 09:28:04

0001 /* Class autosprintf - formatted output to an ostream.
0002    Copyright (C) 2002, 2012-2026 Free Software Foundation, Inc.
0003 
0004    This program is free software: you can redistribute it and/or modify
0005    it under the terms of the GNU Lesser General Public License as published by
0006    the Free Software Foundation; either version 2.1 of the License, or
0007    (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012    GNU Lesser General Public License for more details.
0013 
0014    You should have received a copy of the GNU Lesser General Public License
0015    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
0016 
0017 /* Written by Bruno Haible.  */
0018 
0019 #ifndef _AUTOSPRINTF_H
0020 #define _AUTOSPRINTF_H
0021 
0022 /* This feature is available in gcc versions 2.5 and later and in clang.  */
0023 #if !((__GNUC__ >= 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || defined __clang__) && !__STRICT_ANSI__)
0024 # define _AUTOSPRINTF_ATTRIBUTE_FORMAT() /* empty */
0025 #else
0026 /* The __-protected variants of 'format' and 'printf' attributes are
0027    accepted by gcc versions 2.6.4 (effectively 2.7) and later and in clang.  */
0028 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) || defined __clang__
0029 #  define _AUTOSPRINTF_ATTRIBUTE_FORMAT() \
0030   __attribute__ ((__format__ (__printf__, 2, 3)))
0031 # else
0032 #  define _AUTOSPRINTF_ATTRIBUTE_FORMAT() \
0033   __attribute__ ((format (printf, 2, 3)))
0034 # endif
0035 #endif
0036 
0037 #include <string>
0038 #include <iostream>
0039 
0040 namespace gnu
0041 {
0042   /* A temporary object, usually allocated on the stack, representing
0043      the result of an asprintf() call.  */
0044   class autosprintf
0045   {
0046   public:
0047     /* Constructor: takes a format string and the printf arguments.  */
0048     autosprintf (const char *format, ...)
0049                 _AUTOSPRINTF_ATTRIBUTE_FORMAT();
0050     /* Copy constructor.  */
0051     autosprintf (const autosprintf& src);
0052     /* Assignment operator.  */
0053     autosprintf& operator = (autosprintf temporary);
0054     /* Destructor: frees the temporarily allocated string.  */
0055     ~autosprintf ();
0056     /* Conversion to string.  */
0057     operator char * () const;
0058     operator std::string () const;
0059     /* Output to an ostream.  */
0060     friend inline std::ostream& operator<< (std::ostream& stream, const autosprintf& tmp)
0061     {
0062       stream << (tmp.str ? tmp.str : "(error in autosprintf)");
0063       return stream;
0064     }
0065   private:
0066     char *str;
0067   };
0068 }
0069 
0070 #endif /* _AUTOSPRINTF_H */