Updated from /src/gettext-0.10.4/intl/finddomain.c
[glibc/history.git] / libio / memstream.c
blob71519a51906ac210cd86f199d19ee1f576e06f25
1 /* Copyright (C) 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include "strfile.h"
20 #include "libioP.h"
21 #include <stdlib.h>
24 struct _IO_FILE_memstream
26 _IO_strfile _sf;
27 char **bufloc;
28 _IO_size_t *sizeloc;
32 static int _IO_mem_sync __P ((_IO_FILE* fp));
33 static void _IO_mem_finish __P ((_IO_FILE* fp));
36 static const struct _IO_jump_t _IO_mem_jumps =
38 JUMP_INIT_DUMMY,
39 JUMP_INIT (finish, _IO_mem_finish),
40 JUMP_INIT (overflow, _IO_str_overflow),
41 JUMP_INIT (underflow, _IO_str_underflow),
42 JUMP_INIT (uflow, _IO_default_uflow),
43 JUMP_INIT (pbackfail, _IO_str_pbackfail),
44 JUMP_INIT (xsputn, _IO_default_xsputn),
45 JUMP_INIT (xsgetn, _IO_default_xsgetn),
46 JUMP_INIT (seekoff, _IO_str_seekoff),
47 JUMP_INIT (seekpos, _IO_default_seekpos),
48 JUMP_INIT (setbuf, _IO_default_setbuf),
49 JUMP_INIT (sync, _IO_mem_sync),
50 JUMP_INIT (doallocate, _IO_default_doallocate),
51 JUMP_INIT (read, _IO_default_read),
52 JUMP_INIT (write, _IO_default_write),
53 JUMP_INIT (seek, _IO_default_seek),
54 JUMP_INIT (close, _IO_default_close),
55 JUMP_INIT (stat, _IO_default_stat)
58 /* Open a stream that writes into a malloc'd buffer that is expanded as
59 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
60 and the number of characters written on fflush or fclose. */
61 _IO_FILE *
62 open_memstream (bufloc, sizeloc)
63 char **bufloc;
64 _IO_size_t *sizeloc;
66 struct _IO_FILE_memstream *fp;
67 char *buf;
69 fp = (struct _IO_FILE_memstream *)
70 malloc (sizeof (struct _IO_FILE_memstream));
71 if (fp == NULL)
72 return NULL;
74 buf = ALLOC_BUF (_IO_BUFSIZ);
75 _IO_init (&fp->_sf._f, 0);
76 _IO_JUMPS (&fp->_sf._f) = &_IO_mem_jumps;
77 _IO_str_init_static (&fp->_sf._f, buf, _IO_BUFSIZ, buf);
78 fp->_sf._f._flags &= ~_IO_USER_BUF;
79 fp->_sf._s._allocate_buffer = (_IO_alloc_type) malloc;
80 fp->_sf._s._free_buffer = (_IO_free_type) free;
82 fp->bufloc = bufloc;
83 fp->sizeloc = sizeloc;
85 return &fp->_sf._f;
89 static int
90 _IO_mem_sync (fp)
91 _IO_FILE* fp;
93 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
94 int res;
96 res = _IO_default_sync (fp);
97 if (res < 0)
98 return res;
100 if (fp->_IO_write_ptr == fp->_IO_write_end)
102 _IO_str_overflow (fp, '\0');
103 --fp->_IO_write_ptr;
105 else
106 *fp->_IO_write_ptr = '\0';
108 *mp->bufloc = fp->_IO_write_base;
109 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
111 return 0;
115 static void
116 _IO_mem_finish (fp)
117 _IO_FILE* fp;
119 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
121 *mp->bufloc = (char *) realloc (fp->_IO_write_base,
122 fp->_IO_write_ptr - fp->_IO_write_base + 1);
123 if (*mp->bufloc != NULL)
125 (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
126 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
129 fp->_IO_buf_base = NULL;
131 _IO_default_finish (fp);