rudolfovich

va_list to std::string

Aug 17th, 2014
942
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <cstdarg>
  3. #include <cstring>
  4. #include <string>
  5.  
  6. using ::std::string;
  7.  
  8. #pragma warning(disable : 4996)
  9.  
  10. #ifndef va_copy
  11. #ifdef _MSC_VER
  12. #define va_copy(dst, src) dst=src
  13. #elif !(__cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__))
  14. #define va_copy(dst, src) memcpy((void*)dst, (void*)src, sizeof(*src))
  15. #endif
  16. #endif
  17.  
  18. ///
  19. /// \breif Format message
  20. /// \param dst String to store formatted message
  21. /// \param format Format of message
  22. /// \param ap Variable argument list
  23. ///
  24. void toString(string &dst, const char *format, va_list ap) throw() {
  25.   int length;
  26.   va_list apStrLen;
  27.   va_copy(apStrLen, ap);
  28.   length = vsnprintf(NULL, 0, format, apStrLen);
  29.   va_end(apStrLen);
  30.   if (length > 0) {
  31.     dst.resize(length);
  32.     vsnprintf((char *)dst.data(), dst.size() + 1, format, ap);
  33.   } else {
  34.     dst = "Format error! format: ";
  35.     dst.append(format);
  36.   }
  37. }
  38.  
  39. ///
  40. /// \breif Format message
  41. /// \param dst String to store formatted message
  42. /// \param format Format of message
  43. /// \param ... Variable argument list
  44. ///
  45. void toString(string &dst, const char *format, ...) throw() {
  46.   va_list ap;
  47.   va_start(ap, format);
  48.   toString(dst, format, ap);
  49.   va_end(ap);
  50. }
  51.  
  52. ///
  53. /// \breif Format message
  54. /// \param format Format of message
  55. /// \param ... Variable argument list
  56. ///
  57. string toString(const char *format, ...) throw() {
  58.   string dst;
  59.   va_list ap;
  60.   va_start(ap, format);
  61.   toString(dst, format, ap);
  62.   va_end(ap);
  63.   return dst;
  64. }
  65.  
  66. ///
  67. /// \breif Format message
  68. /// \param format Format of message
  69. /// \param ap Variable argument list
  70. ///
  71. string toString(const char *format, va_list ap) throw() {
  72.   string dst;
  73.   toString(dst, format, ap);
  74.   return dst;
  75. }
  76.  
  77. int main() {
  78.   int a = 32;
  79.   const char * str = "This works!";
  80.  
  81.   string test(toString("\nSome testing: a = %d, %s\n", a, str));
  82.   printf(test.c_str());
  83.  
  84.   a = 0x7fffffff;
  85.   test = toString("\nMore testing: a = %d, %s\n", a, "This works too..");
  86.   printf(test.c_str());
  87.  
  88.   a = 0x80000000;
  89.   toString(test, "\nMore testing: a = %d, %s\n", a, "This way is cheaper");
  90.   printf(test.c_str());
  91.  
  92.   return 0;
  93. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×