Updated German translation
[dasher.git] / Src / DasherCore / Messages.cpp
blobf7d33193f425751d653f969d92dbb3c39fb78090
1 #include "Messages.h"
2 #include <string.h>
3 #include <vector>
4 #include <stdarg.h>
5 #include <stdio.h>
7 using std::vector;
9 void CMessageDisplay::FormatMessageWithString(const char *fmt, const char *str) {
10 char *buf(new char[strlen(fmt)+strlen(str)]);
11 sprintf(buf, fmt, str);
12 Message(buf, true);
13 delete[] buf;
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);
19 Message(buf,true);
20 delete[] buf;
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"
32 /*vector<char> buf;
33 for (int len = strlen(fmt)+1024; ;) {
34 buf.resize(len);
35 va_list args;
36 va_start(args,fmt);
37 int res = vsnprintf(&buf[0], len, fmt, args);
38 va_end(args);
39 if (res>=0 && res<len) {
40 //ok, buf big enough, now contains string
41 Message(&buf[0], true);
42 return;
44 if (res<0) {
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...
48 if (len*=2 > 1<<16) {
49 printf("Could not allocate big enough buffer, or other error, when trying to print:\n");
50 va_list args2;
51 va_start(args2,fmt);
52 vprintf(fmt,args2);
53 va_end(args2);
54 return; //exit loop + function, no call to Message()
56 } else len = res+1; //that identifies necessary size of buffer
57 }*/