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