C++ 安全字符串拼接

 #include <stdio.h>
#include <stdint.h>
#include <stdarg.h> #if defined(__GNUC__)
#endif #if defined(_MSC_VER)
#define snprintf(buf,size,fmt,...) sprintf_s(buf,size,fmt,__VA_ARGS__)
#define vsnprintf(buf,size,fmt,...) vsprintf_s(buf,size,fmt,__VA_ARGS__)
#endif #define STRAPPEND(buf, size, offset, fmt, ...) do { \
if (offset < size) \
{ \
offset += snprintf(buf + offset, size - offset, fmt, __VA_ARGS__); \
}\
}while () inline void StrAppend(char buf[], const uint32_t bufLen, uint32_t &offset, const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
if (offset < bufLen)
{
offset += vsnprintf(buf + offset, bufLen - offset, fmt, argptr);
}
va_end(argptr);
} int32_t main(int32_t argc, char *argv[])
{
char buf[] = { };
uint32_t offset = ;
StrAppend(buf, , offset, "%d ", );
StrAppend(buf, , offset, ",%s--", "qwe");
StrAppend(buf, , offset, ",%0.2f!!!", 1.0);
printf("%s\n", buf);
offset = ;
STRAPPEND(buf, , offset, "%d ", );
STRAPPEND(buf, , offset, ",%s--", "qwe1");
STRAPPEND(buf, , offset, ",%0.3f!!!", 11.0);
printf("%s\n", buf);
return ;
}
上一篇:easyui combotree下拉框多选赋值


下一篇:magento产品成功添加到购物车后跳转到不同页面 添加 add to cart 按钮