2019-04-16 14:26:37 +02:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*/
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-04-16 14:26:37 +02:00
|
|
|
|
|
|
|
#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);
|
2019-04-17 10:17:27 +02:00
|
|
|
result = vsnprintf(&tmp, 1, format, vc);
|
2019-04-16 14:26:37 +02:00
|
|
|
va_end(vc);
|
|
|
|
|
|
|
|
if (!(s = malloc(result + 1)))
|
|
|
|
return -1;
|
|
|
|
|
2019-04-25 13:53:32 +02:00
|
|
|
*out = s;
|
2019-04-17 10:17:27 +02:00
|
|
|
result = vsnprintf(s, result+1, format, args);
|
2019-04-16 14:26:37 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|