5 #ifndef HAVE_DECL_VSNPRINTF
6 #include "support/snprintf.h"
11 /* This output manipulator gives the option to use Old style format
12 specifications in ostreams. Note that this is done at the expense
13 of typesafety, so if possible this manipulator should be avoided.
14 When is it allowed to use this manipulator? I wrote it to be used
15 i18n strings and gettext, and it should only(?) be used in that
18 Ad. the implementation. I have only tested this on egcs-2.91.66 with
19 glibc 2.1.2. So further testing is needed. The loop in fmt(...) will
20 usually spin one or two times, but might spin more times with older
21 glibc libraries, since the returned -1 when size is too small. Newer
22 glibc returns the needed size.
23 One problem can be that vsnprintf is not implemented on all archs,
24 but AFAIK it is part of the new ANSI C standard.
29 string fmt(char const * fmtstr ...)
32 char * str = new char[size];
36 int const r = vsnprintf(str, size, fmtstr, ap);
38 if (r == -1) { // size is too small
40 size *= 2; // seems quite safe to double.
42 } else if (r >= size) { // r gives the needed size