adding string handling
authorVolker Birk <vb@pep.foundation>
Mon, 15 Jan 2018 11:09:36 +0100
changeset 2376997e80887f22
parent 2375 195b929a52b6
child 2377 008d02a90566
adding string handling
src/pEp_string.c
src/pEp_string.h
     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 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/pEp_string.h	Mon Jan 15 11:09:36 2018 +0100
     2.3 @@ -0,0 +1,52 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#pragma once
     2.8 +
     2.9 +#include <string.h>
    2.10 +#include "dynamic_api.h"
    2.11 +
    2.12 +#ifdef __cplusplus
    2.13 +extern "C" {
    2.14 +#endif
    2.15 +
    2.16 +
    2.17 +// new_string() - allocate a new string
    2.18 +//
    2.19 +//  parameters:
    2.20 +//      src (in)        string to copy or NULL
    2.21 +//      len (in)        length of newly created string or 0 for default
    2.22 +//
    2.23 +//  return value:
    2.24 +//      pointer to string object or NULL if out of memory
    2.25 +//
    2.26 +//  caveat:
    2.27 +//      one of the two parameters has to be set at least
    2.28 +
    2.29 +DYNAMIC_API char * new_string(const char *src, size_t len);
    2.30 +
    2.31 +
    2.32 +// free_string() - free memory occupied by string
    2.33 +//
    2.34 +//  parameters:
    2.35 +//      ts (in)         pointer to string to free
    2.36 +
    2.37 +DYNAMIC_API void free_string(char *s);
    2.38 +
    2.39 +
    2.40 +// string_dup() - duplicate a string
    2.41 +//
    2.42 +//  parameters:
    2.43 +//      src (in)        string to duplicate
    2.44 +//      len (in)        length of newly created string or 0 for default
    2.45 +//
    2.46 +//  return value:
    2.47 +//      pointer to copy or NULL if out of memory
    2.48 +
    2.49 +DYNAMIC_API char * string_dup(const char *src, size_t len);
    2.50 +
    2.51 +
    2.52 +#ifdef __cplusplus
    2.53 +}
    2.54 +#endif
    2.55 +