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