src/pEp_string.c
author Hernâni Marques <hernani@pep.foundation>
Wed, 27 Jun 2018 00:52:09 +0200
changeset 2771 b0c0d39f643d
parent 2462 48b526a0daac
child 4792 7056435ab9e7
permissions -rw-r--r--
typos (by reading for IETF)
vb@2376
     1
// This file is under GNU General Public License 3.0
vb@2376
     2
// see LICENSE.txt
vb@2376
     3
krista@2461
     4
#include "platform.h"
vb@2376
     5
#include "pEp_string.h"
vb@2376
     6
vb@2376
     7
#include <stdlib.h>
vb@2376
     8
#include <assert.h>
vb@2376
     9
vb@2376
    10
DYNAMIC_API char * new_string(const char *src, size_t len)
vb@2376
    11
{
vb@2376
    12
    char *s = NULL;
vb@2376
    13
    if (src) {
vb@2376
    14
        if (len)
vb@2376
    15
            s = strndup(src, len);
vb@2376
    16
        else
vb@2376
    17
            s = strdup(src);
vb@2376
    18
        assert(s);
vb@2376
    19
    }
vb@2376
    20
    else {
krista@2461
    21
        s = calloc(1, len + 1);
vb@2376
    22
        assert(s);
vb@2376
    23
    }
vb@2376
    24
vb@2376
    25
    return s;
vb@2376
    26
}
vb@2376
    27
vb@2376
    28
vb@2376
    29
DYNAMIC_API void free_string(char *s)
vb@2376
    30
{
vb@2376
    31
    free(s);
vb@2376
    32
}
vb@2376
    33
vb@2376
    34
DYNAMIC_API char * string_dup(const char *src, size_t len)
vb@2376
    35
{
vb@2376
    36
    assert(src);
vb@2376
    37
    if (!src)
vb@2376
    38
        return NULL;
vb@2376
    39
vb@2376
    40
    char *s = NULL;
vb@2376
    41
    if (len)
vb@2376
    42
        s = strndup(src, len);
vb@2376
    43
    else
vb@2376
    44
        s = strdup(src);
vb@2376
    45
    assert(s);
vb@2376
    46
vb@2376
    47
    return s;
vb@2376
    48
}
vb@2376
    49