Back to index

glibc  2.9
memstream.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995-97,99,2000,2002-2004,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include "libioP.h"
00020 #include "strfile.h"
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 
00024 
00025 struct _IO_FILE_memstream
00026 {
00027   _IO_strfile _sf;
00028   char **bufloc;
00029   _IO_size_t *sizeloc;
00030 };
00031 
00032 
00033 static int _IO_mem_sync (_IO_FILE* fp) __THROW;
00034 static void _IO_mem_finish (_IO_FILE* fp, int) __THROW;
00035 
00036 
00037 static const struct _IO_jump_t _IO_mem_jumps =
00038 {
00039   JUMP_INIT_DUMMY,
00040   JUMP_INIT (finish, _IO_mem_finish),
00041   JUMP_INIT (overflow, INTUSE(_IO_str_overflow)),
00042   JUMP_INIT (underflow, INTUSE(_IO_str_underflow)),
00043   JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
00044   JUMP_INIT (pbackfail, INTUSE(_IO_str_pbackfail)),
00045   JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
00046   JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
00047   JUMP_INIT (seekoff, INTUSE(_IO_str_seekoff)),
00048   JUMP_INIT (seekpos, _IO_default_seekpos),
00049   JUMP_INIT (setbuf, _IO_default_setbuf),
00050   JUMP_INIT (sync, _IO_mem_sync),
00051   JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
00052   JUMP_INIT (read, _IO_default_read),
00053   JUMP_INIT (write, _IO_default_write),
00054   JUMP_INIT (seek, _IO_default_seek),
00055   JUMP_INIT (close, _IO_default_close),
00056   JUMP_INIT (stat, _IO_default_stat),
00057   JUMP_INIT(showmanyc, _IO_default_showmanyc),
00058   JUMP_INIT(imbue, _IO_default_imbue)
00059 };
00060 
00061 /* Open a stream that writes into a malloc'd buffer that is expanded as
00062    necessary.  *BUFLOC and *SIZELOC are updated with the buffer's location
00063    and the number of characters written on fflush or fclose.  */
00064 _IO_FILE *
00065 open_memstream (bufloc, sizeloc)
00066      char **bufloc;
00067      _IO_size_t *sizeloc;
00068 {
00069   struct locked_FILE
00070   {
00071     struct _IO_FILE_memstream fp;
00072 #ifdef _IO_MTSAFE_IO
00073     _IO_lock_t lock;
00074 #endif
00075     struct _IO_wide_data wd;
00076   } *new_f;
00077   char *buf;
00078 
00079   new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
00080   if (new_f == NULL)
00081     return NULL;
00082 #ifdef _IO_MTSAFE_IO
00083   new_f->fp._sf._sbf._f._lock = &new_f->lock;
00084 #endif
00085 
00086   buf = calloc (1, _IO_BUFSIZ);
00087   if (buf == NULL)
00088     return NULL;
00089   INTUSE(_IO_init) (&new_f->fp._sf._sbf._f, 0);
00090   _IO_JUMPS ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf) = &_IO_mem_jumps;
00091   _IO_str_init_static_internal (&new_f->fp._sf, buf, _IO_BUFSIZ, buf);
00092   new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
00093   new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc;
00094   new_f->fp._sf._s._free_buffer = (_IO_free_type) free;
00095 
00096   new_f->fp.bufloc = bufloc;
00097   new_f->fp.sizeloc = sizeloc;
00098 
00099   return (_IO_FILE *) &new_f->fp._sf._sbf;
00100 }
00101 libc_hidden_def (open_memstream)
00102 
00103 
00104 static int
00105 _IO_mem_sync (fp)
00106      _IO_FILE* fp;
00107 {
00108   struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
00109 
00110   if (fp->_IO_write_ptr == fp->_IO_write_end)
00111     {
00112       INTUSE(_IO_str_overflow) (fp, '\0');
00113       --fp->_IO_write_ptr;
00114     }
00115   else
00116     *fp->_IO_write_ptr = '\0';
00117 
00118   *mp->bufloc = fp->_IO_write_base;
00119   *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
00120 
00121   return 0;
00122 }
00123 
00124 
00125 static void
00126 _IO_mem_finish (fp, dummy)
00127      _IO_FILE* fp;
00128      int dummy;
00129 {
00130   struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
00131 
00132   *mp->bufloc = (char *) realloc (fp->_IO_write_base,
00133                               fp->_IO_write_ptr - fp->_IO_write_base + 1);
00134   if (*mp->bufloc != NULL)
00135     {
00136       (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
00137       *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
00138 
00139       fp->_IO_buf_base = NULL;
00140     }
00141 
00142   _IO_str_finish (fp, 0);
00143 }