1<HTML>
2<HEAD>
3<!-- This HTML file has been created by texi2html 1.52b
4     from autosprintf.texi on 22 August 2006 -->
5
6<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
7<TITLE>GNU autosprintf</TITLE>
8</HEAD>
9<BODY>
10<H1>GNU autosprintf, version 1.0</H1>
11<H2>Formatted Output to Strings in C++</H2>
12<ADDRESS>Bruno Haible</ADDRESS>
13<P>
14<P><HR><P>
15<H1>Table of Contents</H1>
16<UL>
17<LI><A NAME="TOC1" HREF="autosprintf.html#SEC1">1  Introduction</A>
18<LI><A NAME="TOC2" HREF="autosprintf.html#SEC2">2  The <CODE>autosprintf</CODE> class</A>
19<LI><A NAME="TOC3" HREF="autosprintf.html#SEC3">3  Using <CODE>autosprintf</CODE> in own programs</A>
20</UL>
21<P><HR><P>
22
23
24<H1><A NAME="SEC1" HREF="autosprintf.html#TOC1">1  Introduction</A></H1>
25
26<P>
27This package makes the C formatted output routines (<CODE>fprintf</CODE> et al.)
28usable in C++ programs, for use with the <CODE>&#60;string&#62;</CODE> strings and the
29<CODE>&#60;iostream&#62;</CODE> streams.
30
31</P>
32<P>
33It allows to write code like
34
35</P>
36
37<PRE>
38cerr &#60;&#60; autosprintf ("syntax error in %s:%d: %s", filename, line, errstring);
39</PRE>
40
41<P>
42instead of
43
44</P>
45
46<PRE>
47cerr &#60;&#60; "syntax error in " &#60;&#60; filename &#60;&#60; ":" &#60;&#60; line &#60;&#60; ": " &#60;&#60; errstring;
48</PRE>
49
50<P>
51The benefits of the autosprintf syntax are:
52
53</P>
54
55<UL>
56<LI>
57
58It reuses the standard POSIX printf facility. Easy migration from C to C++.
59
60<LI>
61
62English sentences are kept together.
63
64<LI>
65
66It makes internationalization possible. Internationalization requires format
67strings, because in some cases the translator needs to change the order of a
68sentence, and more generally it is easier for the translator to work with a
69single string for a sentence than with multiple string pieces.
70
71<LI>
72
73It reduces the risk of programming errors due to forgotten state in the
74output stream (e.g. <CODE>cout &#60;&#60; hex;</CODE> not followed by <CODE>cout &#60;&#60; dec;</CODE>).
75</UL>
76
77
78
79<H1><A NAME="SEC2" HREF="autosprintf.html#TOC2">2  The <CODE>autosprintf</CODE> class</A></H1>
80
81<P>
82An instance of class <CODE>autosprintf</CODE> just contains a string with the
83formatted output result. Such an instance is usually allocated as an
84automatic storage variable, i.e. on the stack, not with <CODE>new</CODE> on the
85heap.
86
87</P>
88<P>
89The constructor <CODE>autosprintf (const char *format, ...)</CODE> takes a format
90string and additional arguments, like the C function <CODE>printf</CODE>.
91
92</P>
93<P>
94Conversions to <CODE>char *</CODE> and <CODE>std::string</CODE> are defined that return
95the encapsulated string.
96
97</P>
98<P>
99The destructor <CODE>~autosprintf ()</CODE> destroys the encapsulated string.
100
101</P>
102<P>
103An <CODE>operator &#60;&#60;</CODE> is provided that outputs the encapsulated string to the
104given <CODE>ostream</CODE>.
105
106</P>
107
108
109<H1><A NAME="SEC3" HREF="autosprintf.html#TOC3">3  Using <CODE>autosprintf</CODE> in own programs</A></H1>
110
111<P>
112To use the <CODE>autosprintf</CODE> class in your programs, you need to add
113
114</P>
115
116<PRE>
117#include "autosprintf.h"
118using gnu::autosprintf;
119</PRE>
120
121<P>
122to your source code.
123The include file defines the class <CODE>autosprintf</CODE>, in a namespace called
124<CODE>gnu</CODE>. The <SAMP>&lsquo;using&rsquo;</SAMP> statement makes it possible to use the class
125without the (otherwise natural) <CODE>gnu::</CODE> prefix.
126
127</P>
128<P>
129When linking your program, you need to link with <CODE>libasprintf</CODE>, because
130that's where the class is defined. In projects using GNU <CODE>autoconf</CODE>,
131this means adding <SAMP>&lsquo;AC_LIB_LINKFLAGS([asprintf])&rsquo;</SAMP> to <CODE>configure.in</CODE>
132or <CODE>configure.ac</CODE>, and using the @LIBASPRINTF@ Makefile variable that
133it provides.
134
135</P>
136<P><HR><P>
137This document was generated on 22 August 2006 using the
138<A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A>
139translator version 1.52b.</P>
140</BODY>
141</HTML>
142