Do not use a virtual varargs function

This stuff breaks in some compiler (configurations)
pull/55/head
Nils Maier 2013-03-01 16:05:39 +01:00
parent 7f6a446750
commit b0556a7519
6 changed files with 24 additions and 22 deletions

View File

@ -156,13 +156,9 @@ size_t BufferedFile::transfer(std::ostream& out)
return count; return count;
} }
int BufferedFile::printf(const char* format, ...) int BufferedFile::vprintf(const char* format, va_list va)
{ {
va_list ap; return vfprintf(fp_, format, va);
va_start(ap, format);
int r = vfprintf(fp_, format, ap);
va_end(ap);
return r;
} }
int BufferedFile::flush() int BufferedFile::flush()

View File

@ -73,8 +73,7 @@ public:
// Convenient method. Read data to end of file and write them into // Convenient method. Read data to end of file and write them into
// given stream. Returns written size. // given stream. Returns written size.
size_t transfer(std::ostream& out); size_t transfer(std::ostream& out);
// wrapper for fprintf virtual int vprintf(const char* format, va_list va);
virtual int printf(const char* format, ...);
// wrapper for fflush // wrapper for fflush
virtual int flush(); virtual int flush();
virtual bool supportsColor(); virtual bool supportsColor();

View File

@ -43,7 +43,7 @@ class NullOutputFile:public OutputFile {
public: public:
virtual ~NullOutputFile() {} virtual ~NullOutputFile() {}
virtual size_t write(const char* str) { return 0; } virtual size_t write(const char* str) { return 0; }
virtual int printf(const char* format, ...) { return 0; } virtual int vprintf(const char* format, va_list va) { return 0; }
virtual int flush() { return 0; } virtual int flush() { return 0; }
virtual bool supportsColor() { return false; } virtual bool supportsColor() { return false; }
}; };

View File

@ -38,6 +38,7 @@
#include "common.h" #include "common.h"
#include <cstdlib> #include <cstdlib>
#include <cstdarg>
namespace aria2 { namespace aria2 {
@ -45,8 +46,15 @@ class OutputFile {
public: public:
virtual ~OutputFile() {} virtual ~OutputFile() {}
virtual size_t write(const char* str) = 0; virtual size_t write(const char* str) = 0;
virtual int printf(const char* format, ...) = 0;
virtual int flush() = 0; virtual int flush() = 0;
virtual int vprintf(const char* format, va_list va) = 0;
inline int printf(const char *format, ...) {
va_list va;
va_start(va, format);
int rv = vprintf(format, va);
va_end(va);
return rv;
}
// Returns true if the output medium supports ANSI color codes. // Returns true if the output medium supports ANSI color codes.
virtual bool supportsColor() = 0; virtual bool supportsColor() = 0;
}; };

View File

@ -72,18 +72,16 @@ size_t WinConsoleFile::write(const char* str)
return written; return written;
} }
int WinConsoleFile::printf(const char* format, ...) int WinConsoleFile::vprintf(const char* format, va_list va)
{ {
char buf[2048]; ssize_t r = _vscprintf(format, va);
va_list ap; if (r <= 0) {
va_start(ap, format); return 0;
int r = vsnprintf(buf, sizeof(buf), format, ap); }
va_end(ap); char *buf = new char[++r];
if(r == -1) { r = vsnprintf(buf, r, format, va);
// MINGW32 vsnprintf returns -1 if output is truncated. if (r < 0) {
r = strlen(buf); delete [] buf;
} else if(r < 0) {
// Reachable?
return 0; return 0;
} }
DWORD written; DWORD written;
@ -95,6 +93,7 @@ int WinConsoleFile::printf(const char* format, ...)
WriteFile(GetStdHandle(stdHandle_), WriteFile(GetStdHandle(stdHandle_),
buf, r, &written, 0); buf, r, &written, 0);
} }
delete [] buf;
return written; return written;
} }

View File

@ -45,7 +45,7 @@ public:
WinConsoleFile(DWORD stdHandle); WinConsoleFile(DWORD stdHandle);
virtual ~WinConsoleFile(); virtual ~WinConsoleFile();
virtual size_t write(const char* str); virtual size_t write(const char* str);
virtual int printf(const char* format, ...); virtual int vprintf(const char* format, va_list va);
virtual int flush(); virtual int flush();
virtual bool supportsColor(); virtual bool supportsColor();
private: private: