]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/portability/dprintf.cpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / portability / dprintf.cpp
diff --git a/src/stlplus/portability/dprintf.cpp b/src/stlplus/portability/dprintf.cpp
new file mode 100644 (file)
index 0000000..f03217e
--- /dev/null
@@ -0,0 +1,92 @@
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+//   Author:    Andy Rushton\r
+//   Copyright: (c) Southampton University 1999-2004\r
+//              (c) Andy Rushton           2004-2009\r
+//   License:   BSD License, see ../docs/license.html\r
+\r
+//   Notes:\r
+\r
+//   Feb 2007: Rewritten in terms of platform-specific fixes to the\r
+//   buffer-overflow problem. Using native functions for this has the added\r
+//   benefit of giving access to other features of the C-runtime such as Unicode\r
+//   support.\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+#include "dprintf.hpp"\r
+#include <stdio.h>\r
+#include <limits.h>\r
+#include <float.h>\r
+#include <ctype.h>\r
+#include <stdlib.h>\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+namespace stlplus\r
+{\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+  int vdprintf(std::string& formatted, const char* format, va_list args)\r
+  {\r
+#ifdef MSWINDOWS\r
+    int length = 0;\r
+    char* buffer = 0;\r
+    for(int buffer_length = 256; ; buffer_length*=2)\r
+    {\r
+      buffer = (char*)malloc(buffer_length);\r
+      if (!buffer) return -1;\r
+      length = _vsnprintf(buffer, buffer_length-1, format, args);\r
+      if (length >= 0)\r
+      {\r
+        buffer[length] = 0;\r
+        formatted += std::string(buffer);\r
+        free(buffer);\r
+        break;\r
+      }\r
+      free(buffer);\r
+    }\r
+    return length;\r
+#else\r
+    char* buffer = 0;\r
+    int length = vasprintf(&buffer, format, args);\r
+    if (!buffer) return -1;\r
+    if (length >= 0)\r
+      formatted += std::string(buffer);\r
+    free(buffer);\r
+    return length;\r
+#endif\r
+  }\r
+\r
+  int dprintf(std::string& formatted, const char* format, ...)\r
+  {\r
+    va_list args;\r
+    va_start(args, format);\r
+    int result = vdprintf(formatted, format, args);\r
+    va_end(args);\r
+    return result;\r
+  }\r
+\r
+  std::string vdformat(const char* format, va_list args) throw(std::invalid_argument)\r
+  {\r
+    std::string formatted;\r
+    int length = vdprintf(formatted, format, args);\r
+    if (length < 0) throw std::invalid_argument("dprintf");\r
+    return formatted;\r
+  }\r
+\r
+  std::string dformat(const char* format, ...) throw(std::invalid_argument)\r
+  {\r
+    std::string formatted;\r
+    va_list args;\r
+    va_start(args, format);\r
+    int length = vdprintf(formatted, format, args);\r
+    va_end(args);\r
+    if (length < 0) throw std::invalid_argument("dprintf");\r
+    return formatted;\r
+  }\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
This page took 0.024521 seconds and 4 git commands to generate.