summaryrefslogtreecommitdiff
path: root/c_src/pstring.h
blob: 3a8b602ff84a3c70564bec278f58a957f61b1b94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (c) 2015, NORDUnet A/S.
 * See LICENSE for licensing information.
 */

#ifndef PSTRING_H
#define PSTRING_H

typedef struct ps_string {
        unsigned char length;
        char value[255];
} ps_string;

#define PS_STRING(s) (&(ps_string){strlen(s), s})
#define PS_PRINTF(s) s->length, s->value

static inline ps_string *
ps_strdup(const ps_string *s)
{
        size_t size = s->length + 1;
        ps_string *copy = malloc(size);
        memcpy(copy, s, size);
        return copy;
}

static inline ps_string *
ps_resize(const ps_string *s, size_t length)
{
        assert(length <= s->length);
        size_t newsize = length + 1;
        ps_string *copy = malloc(newsize);
        memcpy(copy->value, s->value, length);
        copy->length = length;
        return copy;
}

#endif