9 void CMessageDisplay::FormatMessageWithString(const char *fmt
, const char *str
) {
10 char *buf(new char[strlen(fmt
)+strlen(str
)]);
11 sprintf(buf
, fmt
, str
);
16 void CMessageDisplay::FormatMessageWith2Strings(const char *fmt
, const char *str1
, const char *str2
) {
17 char *buf(new char[strlen(fmt
)+strlen(str1
)+strlen(str2
)]);
18 sprintf(buf
, fmt
, str1
, str2
);
23 //The following implements a varargs version of the above,
24 // dynamically allocating enough storage for the formatted string
25 // using snprintf. However, this doesn't work on Solaris,
26 // hence commenting out.
28 //Note: vector is guaranteed to store elements contiguously.
29 // C++98 did not guarantee this, but this was corrected in a 2003
30 // technical corrigendum. As Bjarne Stroustrup says,
31 // "this was always the intent and all implementations always did it that way"
33 for (int len = strlen(fmt)+1024; ;) {
37 int res = vsnprintf(&buf[0], len, fmt, args);
39 if (res>=0 && res<len) {
40 //ok, buf big enough, now contains string
41 Message(&buf[0], true);
45 //on windows, returns -1 for "buffer not big enough" => double size & retry.
46 // However, on linux, -1 indicates "some other error".
47 // So make sure we don't infinite loop but instead break out somehow...
49 printf("Could not allocate big enough buffer, or other error, when trying to print:\n");
54 return; //exit loop + function, no call to Message()
56 } else len = res+1; //that identifies necessary size of buffer