需求描述
这两天的C语言开发工作中用到了多次字符串格式化拼接,分享一下自己写的一个接口,动态分配内存,减少因缓冲区大小不够导致字符串被截断的烦恼
代码实现
核心代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int str_append_to_string(char **str, const char *fmt, ...)
{
int ret = 0;
char *ptr = NULL;
va_list args;
int limit_size = 0;
static int init_size = 256;
static int use_size = 0;
int size = 0;
int append_ok = 0;
if (fmt == NULL || strlen(fmt) <= 0)
{
printf("[%s:%d] param error\n", __FUNCTION__, __LINE__);
return -1;
}
ptr = *str;
if (ptr == NULL)
{
ptr = (char *)malloc(init_size * sizeof(char));
if (ptr == NULL)
{
printf("[%s:%d] malloc failed\n", __FUNCTION__, __LINE__);
return -1;
}
init_size = 256;
use_size = 0;
memset(ptr, 0, (init_size * sizeof(char)));
}
do
{
limit_size = init_size - use_size - 1;
va_start(args, fmt);
size = vsnprintf((ptr + use_size), limit_size, fmt, args);
va_end(args);
if (size > 0 && size <= limit_size)
{
use_size += size;
append_ok = 1;
continue;
}
// need more memory
init_size += init_size >> 2;
char *ptr_backup = ptr; // 备份ptr指针
ptr = (char *)realloc(ptr, init_size * sizeof(char)); // realloc失败的话,ptr之前malloc的内存是不会被释放的
if (ptr == NULL)
{
printf("[%s:%d] realloc failed\n", __FUNCTION__, __LINE__);
ptr = ptr_backup; // 还原ptr指针
ret = -1;
break;
}
} while(!append_ok);
*str = ptr;
return ret;
}
调用代码
int main(int argc, char*argv[])
{
int ret = -1;
char *string = NULL;
ret = str_append_to_string(&string, "letters/%s/id_card/%d/name/%s", "ABC", 123, "Bob");
if (ret == 0)
{
printf("string:%s\n", string);
}
if (string != NULL)
{
free(string);
string = NULL;
}
return 0;
}