| 122 | |
| 123 | |
| 124 | inline std::string string_vsprintf(const char* format, std::va_list ap) |
| 125 | { |
| 126 | #if defined ENABLE_GLIB && ENABLE_GLIB |
| 127 | // there's also g_vasprintf(), but only since glib 2.4. |
| 128 | gchar* buf = g_strdup_vprintf(format, ap); |
| 129 | std::string ret = (buf ? buf : ""); |
| 130 | if (buf) |
| 131 | g_free(buf); |
| 132 | |
| 133 | #elif defined HAVE_VASPRINTF && HAVE_VASPRINTF |
| 134 | std::string ret; |
| 135 | char* str = 0; |
| 136 | if (vasprintf(&str, format, ap) > 0) // GNU extension |
| 137 | ret = (str ? str : ""); |
| 138 | std::free(str); |
| 139 | |
| 140 | #else |
| 141 | |
| 142 | std::string ret; |
| 143 | int size = string_vsprintf_get_buffer_size(format, ap); |
| 144 | if (size > 0) { |
| 145 | char* buf = new char[size]; |
| 146 | int written = 0; |
| 147 | |
| 148 | written = std::vsnprintf(buf, size, format, ap); // writes size chars, including 0. |
| 149 | |
| 150 | if (written > 0) |
| 151 | ret = buf; |
| 152 | delete[] buf; |
| 153 | } |
| 154 | |
| 155 | #endif |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | |
| 160 |
no test coverage detected