Blame libio/wmemstream.c

Packit 6c4009
/* Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include "libioP.h"
Packit 6c4009
#include "strfile.h"
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct _IO_FILE_wmemstream
Packit 6c4009
{
Packit 6c4009
  _IO_strfile _sf;
Packit 6c4009
  wchar_t **bufloc;
Packit 6c4009
  size_t *sizeloc;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int _IO_wmem_sync (FILE* fp) __THROW;
Packit 6c4009
static void _IO_wmem_finish (FILE* fp, int) __THROW;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const struct _IO_jump_t _IO_wmem_jumps libio_vtable =
Packit 6c4009
{
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT (finish, _IO_wmem_finish),
Packit 6c4009
  JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
Packit 6c4009
  JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
Packit 6c4009
  JUMP_INIT (uflow, (_IO_underflow_t) _IO_wdefault_uflow),
Packit 6c4009
  JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
Packit 6c4009
  JUMP_INIT (xsputn, _IO_wdefault_xsputn),
Packit 6c4009
  JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
Packit 6c4009
  JUMP_INIT (seekoff, _IO_wstr_seekoff),
Packit 6c4009
  JUMP_INIT (seekpos, _IO_default_seekpos),
Packit 6c4009
  JUMP_INIT (setbuf, _IO_default_setbuf),
Packit 6c4009
  JUMP_INIT (sync, _IO_wmem_sync),
Packit 6c4009
  JUMP_INIT (doallocate, _IO_wdefault_doallocate),
Packit 6c4009
  JUMP_INIT (read, _IO_default_read),
Packit 6c4009
  JUMP_INIT (write, _IO_default_write),
Packit 6c4009
  JUMP_INIT (seek, _IO_default_seek),
Packit 6c4009
  JUMP_INIT (close, _IO_default_close),
Packit 6c4009
  JUMP_INIT (stat, _IO_default_stat),
Packit 6c4009
  JUMP_INIT (showmanyc, _IO_default_showmanyc),
Packit 6c4009
  JUMP_INIT (imbue, _IO_default_imbue)
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Open a stream that writes into a malloc'd buffer that is expanded as
Packit 6c4009
   necessary.  *BUFLOC and *SIZELOC are updated with the buffer's location
Packit 6c4009
   and the number of characters written on fflush or fclose.  */
Packit 6c4009
FILE *
Packit 6c4009
open_wmemstream (wchar_t **bufloc, size_t *sizeloc)
Packit 6c4009
{
Packit 6c4009
  struct locked_FILE
Packit 6c4009
  {
Packit 6c4009
    struct _IO_FILE_wmemstream fp;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
    _IO_lock_t lock;
Packit 6c4009
#endif
Packit 6c4009
    struct _IO_wide_data wd;
Packit 6c4009
  } *new_f;
Packit 6c4009
  wchar_t *buf;
Packit 6c4009
Packit 6c4009
  new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
Packit 6c4009
  if (new_f == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  new_f->fp._sf._sbf._f._lock = &new_f->lock;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  buf = calloc (1, BUFSIZ);
Packit 6c4009
  if (buf == NULL)
Packit 6c4009
    {
Packit 6c4009
      free (new_f);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
  _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
Packit 6c4009
  _IO_fwide (&new_f->fp._sf._sbf._f, 1);
Packit 6c4009
  _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
Packit 6c4009
			BUFSIZ / sizeof (wchar_t), buf);
Packit 6c4009
  new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF;
Packit 6c4009
  new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
Packit 6c4009
  new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
Packit 6c4009
Packit 6c4009
  new_f->fp.bufloc = bufloc;
Packit 6c4009
  new_f->fp.sizeloc = sizeloc;
Packit 6c4009
Packit 6c4009
  /* Disable single thread optimization.  BZ 21735.  */
Packit 6c4009
  new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
Packit 6c4009
Packit 6c4009
  return (FILE *) &new_f->fp._sf._sbf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
_IO_wmem_sync (FILE *fp)
Packit 6c4009
{
Packit 6c4009
  struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
Packit 6c4009
Packit 6c4009
  if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
Packit 6c4009
    {
Packit 6c4009
      _IO_wstr_overflow (fp, '\0');
Packit 6c4009
      --fp->_wide_data->_IO_write_ptr;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *mp->bufloc = fp->_wide_data->_IO_write_base;
Packit 6c4009
  *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
Packit 6c4009
		  - fp->_wide_data->_IO_write_base);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
_IO_wmem_finish (FILE *fp, int dummy)
Packit 6c4009
{
Packit 6c4009
  struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
Packit 6c4009
Packit 6c4009
  *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base,
Packit 6c4009
				     (fp->_wide_data->_IO_write_ptr
Packit 6c4009
				      - fp->_wide_data->_IO_write_base + 1)
Packit 6c4009
				     * sizeof (wchar_t));
Packit 6c4009
  if (*mp->bufloc != NULL)
Packit 6c4009
    {
Packit 6c4009
      size_t len = (fp->_wide_data->_IO_write_ptr
Packit 6c4009
		    - fp->_wide_data->_IO_write_base);
Packit 6c4009
      (*mp->bufloc)[len] = '\0';
Packit 6c4009
      *mp->sizeloc = len;
Packit 6c4009
Packit 6c4009
      fp->_wide_data->_IO_buf_base = NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _IO_wstr_finish (fp, 0);
Packit 6c4009
}