Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/util.cpp
#include "util.h" | #include "util.h" | ||||
#include <cstdio> | #include <cstdio> | ||||
std::string vstrprintf(const std::string &format, va_list ap) { | std::string vstrprintf(const char *format, va_list ap) { | ||||
char buffer[50000]; | char buffer[50000]; | ||||
char *p = buffer; | char *p = buffer; | ||||
int limit = sizeof(buffer); | int limit = sizeof(buffer); | ||||
int ret; | int ret; | ||||
loop { | loop { | ||||
va_list arg_ptr; | va_list arg_ptr; | ||||
va_copy(arg_ptr, ap); | va_copy(arg_ptr, ap); | ||||
ret = vsnprintf(p, limit, format.c_str(), arg_ptr); | ret = vsnprintf(p, limit, format, arg_ptr); | ||||
va_end(arg_ptr); | va_end(arg_ptr); | ||||
if (ret >= 0 && ret < limit) break; | if (ret >= 0 && ret < limit) break; | ||||
if (p != buffer) delete[] p; | if (p != buffer) delete[] p; | ||||
limit *= 2; | limit *= 2; | ||||
p = new char[limit]; | p = new char[limit]; | ||||
if (p == nullptr) throw std::bad_alloc(); | if (p == nullptr) throw std::bad_alloc(); | ||||
} | } | ||||
std::string str(p, p + ret); | std::string str(p, p + ret); | ||||
if (p != buffer) delete[] p; | if (p != buffer) delete[] p; | ||||
return str; | return str; | ||||
} | } |