The purpose of this function to provide a convenient way of generating formatted STL strings inline. This is especially useful when throwing exceptions that take a std::string for a message. The length of the formatted output string is limited only by memory. Memory temporarily allocated for the output string is disposed of before returning. For Win32, the maximum output size is 2048 characters.
| 37 | //! \param fmt Format string using printf-style format markers. |
| 38 | //! \return An STL string object of the formatted output. |
| 39 | std::string format_string(const char *fmt, ...) |
| 40 | { |
| 41 | char *buf = 0; |
| 42 | va_list vargs; |
| 43 | va_start(vargs, fmt); |
| 44 | int result; |
| 45 | #if WIN32 || __CYGWIN__ |
| 46 | buf = (char *)malloc(WIN32_FMT_BUF_LEN); |
| 47 | if (buf) |
| 48 | { |
| 49 | #if WIN32 |
| 50 | result = _vsnprintf(buf, WIN32_FMT_BUF_LEN, fmt, vargs); |
| 51 | #else //__CYGWIN__ |
| 52 | result = vsnprintf(buf, WIN32_FMT_BUF_LEN, fmt, vargs); |
| 53 | #endif // WIN32 |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | result = -1; |
| 58 | } |
| 59 | #else |
| 60 | result = vasprintf(&buf, fmt, vargs); |
| 61 | #endif // WIN32 __CYGWIN__ |
| 62 | va_end(vargs); |
| 63 | if (result != -1 && buf) |
| 64 | { |
| 65 | smart_free_ptr<char> freebuf(buf); |
| 66 | return std::string(buf); |
| 67 | } |
| 68 | return ""; |
| 69 | } |
no outgoing calls
no test coverage detected