1<HTML> 2<HEAD> 3<!-- This HTML file has been created by texi2html 1.52b 4 from autosprintf.texi on 1 September 2007 --> 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<P> 24Copyright (C) 2002-2003, 2006-2007 Free Software Foundation, Inc. 25 26</P> 27<P> 28This manual is free documentation. It is dually licensed under the 29GNU FDL and the GNU GPL. This means that you can redistribute this 30manual under either of these two licenses, at your choice. 31 32</P> 33<P> 34This manual is covered by the GNU FDL. Permission is granted to copy, 35distribute and/or modify this document under the terms of the 36GNU Free Documentation License (FDL), either version 1.2 of the 37License, or (at your option) any later version published by the 38Free Software Foundation (FSF); with no Invariant Sections, with no 39Front-Cover Text, and with no Back-Cover Texts. 40A copy of the license is at <A HREF="http://www.gnu.org/licenses/fdl.html">http://www.gnu.org/licenses/fdl.html</A>. 41 42</P> 43<P> 44This manual is covered by the GNU GPL. You can redistribute it and/or 45modify it under the terms of the GNU General Public License (GPL), either 46version 2 of the License, or (at your option) any later version published 47by the Free Software Foundation (FSF). 48A copy of the license is at <A HREF="http://www.gnu.org/licenses/gpl.html">http://www.gnu.org/licenses/gpl.html</A>. 49 50</P> 51 52 53 54<H1><A NAME="SEC1" HREF="autosprintf.html#TOC1">1 Introduction</A></H1> 55 56<P> 57This package makes the C formatted output routines (<CODE>fprintf</CODE> et al.) 58usable in C++ programs, for use with the <CODE><string></CODE> strings and the 59<CODE><iostream></CODE> streams. 60 61</P> 62<P> 63It allows to write code like 64 65</P> 66 67<PRE> 68cerr << autosprintf ("syntax error in %s:%d: %s", filename, line, errstring); 69</PRE> 70 71<P> 72instead of 73 74</P> 75 76<PRE> 77cerr << "syntax error in " << filename << ":" << line << ": " << errstring; 78</PRE> 79 80<P> 81The benefits of the autosprintf syntax are: 82 83</P> 84 85<UL> 86<LI> 87 88It reuses the standard POSIX printf facility. Easy migration from C to C++. 89 90<LI> 91 92English sentences are kept together. 93 94<LI> 95 96It makes internationalization possible. Internationalization requires format 97strings, because in some cases the translator needs to change the order of a 98sentence, and more generally it is easier for the translator to work with a 99single string for a sentence than with multiple string pieces. 100 101<LI> 102 103It reduces the risk of programming errors due to forgotten state in the 104output stream (e.g. <CODE>cout << hex;</CODE> not followed by <CODE>cout << dec;</CODE>). 105</UL> 106 107 108 109<H1><A NAME="SEC2" HREF="autosprintf.html#TOC2">2 The <CODE>autosprintf</CODE> class</A></H1> 110 111<P> 112An instance of class <CODE>autosprintf</CODE> just contains a string with the 113formatted output result. Such an instance is usually allocated as an 114automatic storage variable, i.e. on the stack, not with <CODE>new</CODE> on the 115heap. 116 117</P> 118<P> 119The constructor <CODE>autosprintf (const char *format, ...)</CODE> takes a format 120string and additional arguments, like the C function <CODE>printf</CODE>. 121 122</P> 123<P> 124Conversions to <CODE>char *</CODE> and <CODE>std::string</CODE> are defined that return 125the encapsulated string. The conversion to <CODE>char *</CODE> returns a freshly 126allocated copy of the encapsulated string; it needs to be freed using 127<CODE>delete[]</CODE>. The conversion to <CODE>std::string</CODE> returns a copy of 128the encapsulated string, with automatic memory management. 129 130</P> 131<P> 132The destructor <CODE>~autosprintf ()</CODE> destroys the encapsulated string. 133 134</P> 135<P> 136An <CODE>operator <<</CODE> is provided that outputs the encapsulated string to the 137given <CODE>ostream</CODE>. 138 139</P> 140 141 142<H1><A NAME="SEC3" HREF="autosprintf.html#TOC3">3 Using <CODE>autosprintf</CODE> in own programs</A></H1> 143 144<P> 145To use the <CODE>autosprintf</CODE> class in your programs, you need to add 146 147</P> 148 149<PRE> 150#include "autosprintf.h" 151using gnu::autosprintf; 152</PRE> 153 154<P> 155to your source code. 156The include file defines the class <CODE>autosprintf</CODE>, in a namespace called 157<CODE>gnu</CODE>. The <SAMP>‘using’</SAMP> statement makes it possible to use the class 158without the (otherwise natural) <CODE>gnu::</CODE> prefix. 159 160</P> 161<P> 162When linking your program, you need to link with <CODE>libasprintf</CODE>, because 163that's where the class is defined. In projects using GNU <CODE>autoconf</CODE>, 164this means adding <SAMP>‘AC_LIB_LINKFLAGS([asprintf])’</SAMP> to <CODE>configure.in</CODE> 165or <CODE>configure.ac</CODE>, and using the @LIBASPRINTF@ Makefile variable that 166it provides. 167 168</P> 169<P><HR><P> 170This document was generated on 1 September 2007 using the 171<A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A> 172translator version 1.52b.</P> 173</BODY> 174</HTML> 175