tagging vde-2 version 2.3.2
[vde.git] / 2.3.2 / src / common / open_memstream.c
blobaffee33f3b5005e128316be8f09602e17fb0449a
1 /*
2 * Copyright (C) 2005 Richard Kettlewell
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <assert.h>
26 #include <config.h>
27 #include <vde.h>
28 #include <vdecommon.h>
30 /* BSD-compatible implementation of open_memstream */
32 #if HAVE_FUNOPEN
33 struct memstream {
34 char *buffer;
35 size_t size, space;
37 char **ptr;
38 size_t *sizeloc;
41 static int memstream_writefn(void *u, const char *buffer, int bytes) {
42 struct memstream *m = u;
43 size_t needed = m->size + bytes + 1;
44 size_t newspace;
45 char *newbuffer;
47 assert(bytes >= 0);
48 if(needed > m->space) {
49 newspace = m->space ? m->space : 32;
50 while(newspace && newspace < needed)
51 newspace *= 2;
52 if(!newspace) {
53 errno = ENOMEM;
54 return -1;
56 if(!(newbuffer = realloc(m->buffer, newspace)))
57 return -1;
58 m->buffer = newbuffer;
59 m->space = newspace;
61 memcpy(m->buffer + m->size, buffer, bytes);
62 m->size += bytes;
63 m->buffer[m->size] = 0;
65 *m->ptr = m->buffer;
66 *m->sizeloc = m->size;
67 return bytes;
70 FILE *open_memstream(char **ptr, size_t *sizeloc) {
71 struct memstream *m;
73 if(!(m = malloc(sizeof *m))) return 0;
74 m->buffer = 0;
75 m->size = 0;
76 m->space = 0;
77 m->ptr = ptr;
78 m->sizeloc = sizeloc;
79 *ptr = 0;
80 *sizeloc = 0;
81 return funopen(m,
82 0, /* read */
83 memstream_writefn,
84 0, /* seek */
85 0); /* close */
87 #endif