src/pEp_string.c
changeset 2376 997e80887f22
child 2437 97d602cc813b
child 2461 85c8e5aad3bc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/pEp_string.c	Mon Jan 15 11:09:36 2018 +0100
     1.3 @@ -0,0 +1,52 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#include "pEp_string.h"
     1.8 +
     1.9 +#include <stdlib.h>
    1.10 +#include <assert.h>
    1.11 +
    1.12 +DYNAMIC_API char * new_string(const char *src, size_t len)
    1.13 +{
    1.14 +    assert(src || len);
    1.15 +    if (!(src || len))
    1.16 +        return NULL;
    1.17 +
    1.18 +    char *s = NULL;
    1.19 +    if (src) {
    1.20 +        if (len)
    1.21 +            s = strndup(src, len);
    1.22 +        else
    1.23 +            s = strdup(src);
    1.24 +        assert(s);
    1.25 +    }
    1.26 +    else {
    1.27 +        s = calloc(1, len);
    1.28 +        assert(s);
    1.29 +    }
    1.30 +
    1.31 +    return s;
    1.32 +}
    1.33 +
    1.34 +
    1.35 +DYNAMIC_API void free_string(char *s)
    1.36 +{
    1.37 +    free(s);
    1.38 +}
    1.39 +
    1.40 +DYNAMIC_API char * string_dup(const char *src, size_t len)
    1.41 +{
    1.42 +    assert(src);
    1.43 +    if (!src)
    1.44 +        return NULL;
    1.45 +
    1.46 +    char *s = NULL;
    1.47 +    if (len)
    1.48 +        s = strndup(src, len);
    1.49 +    else
    1.50 +        s = strdup(src);
    1.51 +    assert(s);
    1.52 +
    1.53 +    return s;
    1.54 +}
    1.55 +