RetroArch/runahead/mem_util.c

51 lines
967 B
C
Raw Normal View History

2018-03-29 13:49:39 +00:00
#include <stdlib.h>
2018-03-28 19:22:07 +00:00
#include "mem_util.h"
char *strcpy_alloc(const char *sourceStr)
{
size_t len = 0;
char *result = NULL;
if (sourceStr)
2018-03-28 19:22:07 +00:00
len = strlen(sourceStr);
2018-03-28 19:22:07 +00:00
if (len == 0)
return NULL;
2018-03-28 19:22:07 +00:00
result = (char*)malloc(len + 1);
strcpy(result, sourceStr);
return result;
}
char *strcpy_alloc_force(const char *sourceStr)
{
char *result = strcpy_alloc(sourceStr);
if (!result)
result = (char*)calloc(1, 1);
2018-03-28 19:22:07 +00:00
return result;
}
void strcat_alloc(char ** destStr_p, const char *appendStr)
{
size_t len1, len2, newLen;
char *destStr = *destStr_p;
2018-03-28 19:22:07 +00:00
if (!destStr)
2018-03-28 19:22:07 +00:00
{
destStr = strcpy_alloc_force(appendStr);
2018-03-28 19:22:07 +00:00
*destStr_p = destStr;
return;
}
if (!appendStr)
2018-03-28 19:22:07 +00:00
return;
len1 = strlen(destStr);
len2 = strlen(appendStr);
newLen = len1 + len2 + 1;
destStr = (char*)realloc(destStr, newLen);
2018-03-28 19:22:07 +00:00
*destStr_p = destStr;
strcpy(destStr + len1, appendStr);
}