1 #include <stdlib.h> 2 3 #include "memory-stream.h" 4 5 bool memory_stream_open(struct memory_stream * m)6memory_stream_open(struct memory_stream *m) 7 { 8 *m = (struct memory_stream){ 0 }; 9 m->fp = open_memstream(&m->str, &m->str_len); 10 11 return m->fp != NULL; 12 } 13 14 char * memory_stream_close(struct memory_stream * m)15memory_stream_close(struct memory_stream *m) 16 { 17 char *str; 18 int ret; 19 20 ret = fclose(m->fp); 21 str = m->str; 22 *m = (struct memory_stream){ 0 }; 23 24 if (ret != 0) { 25 free(str); 26 str = NULL; 27 } 28 29 return str; 30 } 31 32 void memory_stream_cleanup(struct memory_stream * m)33memory_stream_cleanup(struct memory_stream *m) 34 { 35 free(memory_stream_close(m)); 36 } 37