Root/b2/vstring.c

1/*
2 * vstring.c - Variable-length strings
3 *
4 * Copyright 2012 by Werner Almesberger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12
13#include <stdlib.h>
14#include <string.h>
15
16#include "vstring.h"
17
18
19void append_n(char **res, int *res_len, const char *s, int len)
20{
21    if (!len)
22        return;
23    if (!*res)
24        *res_len = 0;
25    *res = realloc(*res, *res_len+len+1);
26    if (!*res)
27        abort();
28    memcpy(*res+*res_len, s, len);
29    *res_len += len;
30    (*res)[*res_len] = 0;
31}
32
33
34void append(char **res, int *res_len, const char *s)
35{
36    append_n(res, res_len, s, strlen(s));
37}
38

Archive Download this file

Branches:
master



interactive