X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fstlplus%2Fportability%2Fdprintf.cpp;h=d9a9e4f0a23cfb0b14a98d49ee2733d5e9e72620;hp=b0e05c6b7097bd785c3a55c996bb0d383fba3dc0;hb=4f6e4488a55f7e3ba3f7485d78177f793c0eab9a;hpb=574af38ed616d1adfa5e6ce35f67cda1f707f89d diff --git a/src/stlplus/portability/dprintf.cpp b/src/stlplus/portability/dprintf.cpp index b0e05c6..d9a9e4f 100644 --- a/src/stlplus/portability/dprintf.cpp +++ b/src/stlplus/portability/dprintf.cpp @@ -1,92 +1,92 @@ -//////////////////////////////////////////////////////////////////////////////// - -// Author: Andy Rushton -// Copyright: (c) Southampton University 1999-2004 -// (c) Andy Rushton 2004-2009 -// License: BSD License, see ../docs/license.html - -// Notes: - -// Feb 2007: Rewritten in terms of platform-specific fixes to the -// buffer-overflow problem. Using native functions for this has the added -// benefit of giving access to other features of the C-runtime such as Unicode -// support. - -//////////////////////////////////////////////////////////////////////////////// - -#include "dprintf.hpp" -#include -#include -#include -#include -#include - -//////////////////////////////////////////////////////////////////////////////// - -namespace stlplus -{ - -//////////////////////////////////////////////////////////////////////////////// - - int vdprintf(std::string& formatted, const char* format, va_list args) - { -#ifdef MSWINDOWS - int length = 0; - char* buffer = 0; - for(int buffer_length = 256; ; buffer_length*=2) - { - buffer = (char*)malloc(buffer_length); - if (!buffer) return -1; - length = _vsnprintf(buffer, buffer_length-1, format, args); - if (length >= 0) - { - buffer[length] = 0; - formatted += std::string(buffer); - free(buffer); - break; - } - free(buffer); - } - return length; -#else - char* buffer = 0; - int length = vasprintf(&buffer, format, args); - if (!buffer) return -1; - if (length >= 0) - formatted += std::string(buffer); - free(buffer); - return length; -#endif - } - - int dprintf(std::string& formatted, const char* format, ...) - { - va_list args; - va_start(args, format); - int result = vdprintf(formatted, format, args); - va_end(args); - return result; - } - - std::string vdformat(const char* format, va_list args) throw(std::invalid_argument) - { - std::string formatted; - int length = vdprintf(formatted, format, args); - if (length < 0) throw std::invalid_argument("dprintf"); - return formatted; - } - - std::string dformat(const char* format, ...) throw(std::invalid_argument) - { - std::string formatted; - va_list args; - va_start(args, format); - int length = vdprintf(formatted, format, args); - va_end(args); - if (length < 0) throw std::invalid_argument("dprintf"); - return formatted; - } - -//////////////////////////////////////////////////////////////////////////////// - -} // end namespace stlplus +//////////////////////////////////////////////////////////////////////////////// + +// Author: Andy Rushton +// Copyright: (c) Southampton University 1999-2004 +// (c) Andy Rushton 2004 onwards +// License: BSD License, see ../docs/license.html + +// Notes: + +// Feb 2007: Rewritten in terms of platform-specific fixes to the +// buffer-overflow problem. Using native functions for this has the added +// benefit of giving access to other features of the C-runtime such as Unicode +// support. + +//////////////////////////////////////////////////////////////////////////////// + +#include "dprintf.hpp" +#include +#include +#include +#include +#include + +//////////////////////////////////////////////////////////////////////////////// + +namespace stlplus +{ + +//////////////////////////////////////////////////////////////////////////////// + + int vdprintf(std::string& formatted, const char* format, va_list args) + { +#ifdef MSWINDOWS + int length = 0; + char* buffer = 0; + for(int buffer_length = 256; ; buffer_length*=2) + { + buffer = (char*)malloc(buffer_length); + if (!buffer) return -1; + length = _vsnprintf(buffer, buffer_length-1, format, args); + if (length >= 0) + { + buffer[length] = 0; + formatted += std::string(buffer); + free(buffer); + break; + } + free(buffer); + } + return length; +#else + char* buffer = 0; + int length = vasprintf(&buffer, format, args); + if (!buffer) return -1; + if (length >= 0) + formatted += std::string(buffer); + free(buffer); + return length; +#endif + } + + int dprintf(std::string& formatted, const char* format, ...) + { + va_list args; + va_start(args, format); + int result = vdprintf(formatted, format, args); + va_end(args); + return result; + } + + std::string vdformat(const char* format, va_list args) throw(std::invalid_argument) + { + std::string formatted; + int length = vdprintf(formatted, format, args); + if (length < 0) throw std::invalid_argument("dprintf"); + return formatted; + } + + std::string dformat(const char* format, ...) throw(std::invalid_argument) + { + std::string formatted; + va_list args; + va_start(args, format); + int length = vdprintf(formatted, format, args); + va_end(args); + if (length < 0) throw std::invalid_argument("dprintf"); + return formatted; + } + +//////////////////////////////////////////////////////////////////////////////// + +} // end namespace stlplus