From 35c8b969679577b3de1d5b020365f1325515142a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilja=20Karta=C5=A1ov?= Date: Wed, 19 Jun 2019 21:04:31 +0200 Subject: [PATCH] Add string module --- string.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ string.h | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 string.c create mode 100644 string.h diff --git a/string.c b/string.c new file mode 100644 index 0000000..67c956d --- /dev/null +++ b/string.c @@ -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 + * @brief cStuff string module implementation + * + * @see https://lowenware.com/ + */ + +#include +#include +#include +#include + +#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; +} + diff --git a/string.h b/string.h new file mode 100644 index 0000000..a5cd9c7 --- /dev/null +++ b/string.h @@ -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 + * @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 +#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 */