cStuff/string.c

105 lines
1.6 KiB
C

/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file string.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff string module implementation
*
* @see https://lowenware.com/
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "string.h"
int
cstuff_strcpy(char **out, const char *in)
{
return cstuff_strncpy(out, in, strlen(in));
}
int
cstuff_strncpy(char **out, const char *in, int len)
{
char *s;
if (!(s = malloc( len + 1 )))
return -1;
strncpy(s, in, len);
s[len]=0;
*out = s;
return len;
}
int
cstuff_strset(char **out, const char *in)
{
return cstuff_strnset(out, in, strlen(in));
}
int
cstuff_strnset(char **out, const char *in, int len)
{
char *s;
if (!(s = realloc(*out, len+1)))
return -1;
strncpy(s, in, len);
s[len] = 0;
*out = s;
return len;
}
int
cstuff_sprintf(char **out, const char *format, ...)
{
int result;
va_list vl;
va_start(vl, format);
result = cstuff_vsprintf(out, format, vl);
va_end(vl);
return result;
}
int
cstuff_vsprintf(char **out, const char *format, va_list args)
{
int result;
va_list vc;
char tmp, *s;
va_copy(vc, args);
result = vsnprintf(&tmp, 1, format, vc);
va_end(vc);
if (!(s = malloc(result + 1)))
return -1;
*out = s;
result = vsnprintf(s, result+1, format, args);
return result;
}