libcmo21/Unvirt/StringHelper.cpp
yyc12345 5a69ce338e finish linux related code writting. remove all offensive content.
- REMOVE all offensive content in README and etc. This project respect to every contributors.
- However, for commercial reason, we limit the contribution from Dassault.
- Change license.
- Finish linux platform code.
2023-03-26 20:32:13 +08:00

31 lines
836 B
C++

#include "StringHelper.hpp"
#include <stdexcept>
namespace Unvirt::StringHelper {
void StdstringPrintf(std::string& strl, const char* format, ...) {
va_list argptr;
va_start(argptr, format);
StdstringVPrintf(strl, format, argptr);
va_end(argptr);
}
void StdstringVPrintf(std::string& strl, const char* format, va_list argptr) {
va_list args1;
va_copy(args1, argptr);
va_list args2;
va_copy(args2, argptr);
int count = std::vsnprintf(nullptr, 0, format, args1);
if (count < 0) throw new std::length_error("Invalid length returned by vsnprintf.");
va_end(args1);
strl.resize(count + 1);
int write_result = std::vsnprintf(strl.data(), strl.size(), format, args2);
va_end(args2);
if (write_result < 0 || write_result > count) throw new std::length_error("Invalid write result in vsnprintf.");
}
}