2 changed files with 165 additions and 0 deletions
@ -0,0 +1,104 @@ |
|||
/******************************************************************************
|
|||
* |
|||
* 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; |
|||
} |
|||
|
@ -0,0 +1,61 @@ |
|||
/******************************************************************************
|
|||
* |
|||
* Copyright (c) 2017-2019 by Löwenware Ltd |
|||
* Please, refer LICENSE file for legal information |
|||
* |
|||
******************************************************************************/ |
|||
|
|||
/**
|
|||
* @file string.h |
|||
* @author Ilja Kartašov <ik@lowenware.com> |
|||
* @brief String module declarations |
|||
* |
|||
* @see https://lowenware.com/
|
|||
*/ |
|||
|
|||
#ifndef CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87 |
|||
#define CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87 |
|||
|
|||
#include <stdarg.h> |
|||
#include "retcode.h" |
|||
|
|||
|
|||
/** @brief Copies in string to newly allocated out buffer
|
|||
* @param out a pointer to an address where new pointer must be stored |
|||
* @param in an input string |
|||
* @return length of string or -1 if out of memory |
|||
*/ |
|||
int |
|||
cstuff_strcpy(char **out, const char *in); |
|||
|
|||
|
|||
int |
|||
cstuff_strncpy(char **out, const char *in, int len); |
|||
|
|||
|
|||
/** @brief Overwrites content of out buffer with in string using realloc
|
|||
* @param out a pointer to an address where new string must be stored |
|||
* @param in an input string |
|||
* @return length of string or -1 if out of memory |
|||
*/ |
|||
int |
|||
cstuff_strset(char **out, const char *in); |
|||
|
|||
|
|||
int |
|||
cstuff_strnset(char **out, const char *in, int len); |
|||
|
|||
|
|||
/** @brief Allocates memory and prints a formatted string into it
|
|||
* @param out a pointer to an address where new string must be stored |
|||
* @param format a format for string (see manual for stdlib sprintf) |
|||
* @return length of string or -1 if out of memory |
|||
*/ |
|||
int |
|||
cstuff_sprintf(char **out, const char *format, ...); |
|||
|
|||
|
|||
int |
|||
cstuff_vsprintf(char **out, const char *format, va_list args); |
|||
|
|||
#endif /* !CSTUFF_STRING_H */ |
Loading…
Reference in new issue