wmemstream.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright (C) 1995-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include "libioP.h"
  15. #include "strfile.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <wchar.h>
  19. struct _IO_FILE_wmemstream
  20. {
  21. _IO_strfile _sf;
  22. wchar_t **bufloc;
  23. size_t *sizeloc;
  24. };
  25. static int _IO_wmem_sync (FILE* fp) __THROW;
  26. static void _IO_wmem_finish (FILE* fp, int) __THROW;
  27. static const struct _IO_jump_t _IO_wmem_jumps libio_vtable =
  28. {
  29. JUMP_INIT_DUMMY,
  30. JUMP_INIT (finish, _IO_wmem_finish),
  31. JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
  32. JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
  33. JUMP_INIT (uflow, (_IO_underflow_t) _IO_wdefault_uflow),
  34. JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
  35. JUMP_INIT (xsputn, _IO_wdefault_xsputn),
  36. JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
  37. JUMP_INIT (seekoff, _IO_wstr_seekoff),
  38. JUMP_INIT (seekpos, _IO_default_seekpos),
  39. JUMP_INIT (setbuf, _IO_default_setbuf),
  40. JUMP_INIT (sync, _IO_wmem_sync),
  41. JUMP_INIT (doallocate, _IO_wdefault_doallocate),
  42. JUMP_INIT (read, _IO_default_read),
  43. JUMP_INIT (write, _IO_default_write),
  44. JUMP_INIT (seek, _IO_default_seek),
  45. JUMP_INIT (close, _IO_default_close),
  46. JUMP_INIT (stat, _IO_default_stat),
  47. JUMP_INIT (showmanyc, _IO_default_showmanyc),
  48. JUMP_INIT (imbue, _IO_default_imbue)
  49. };
  50. /* Open a stream that writes into a malloc'd buffer that is expanded as
  51. necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
  52. and the number of characters written on fflush or fclose. */
  53. FILE *
  54. open_wmemstream (wchar_t **bufloc, size_t *sizeloc)
  55. {
  56. struct locked_FILE
  57. {
  58. struct _IO_FILE_wmemstream fp;
  59. #ifdef _IO_MTSAFE_IO
  60. _IO_lock_t lock;
  61. #endif
  62. struct _IO_wide_data wd;
  63. } *new_f;
  64. wchar_t *buf;
  65. new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  66. if (new_f == NULL)
  67. return NULL;
  68. #ifdef _IO_MTSAFE_IO
  69. new_f->fp._sf._sbf._f._lock = &new_f->lock;
  70. #endif
  71. buf = calloc (1, BUFSIZ);
  72. if (buf == NULL)
  73. {
  74. free (new_f);
  75. return NULL;
  76. }
  77. _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
  78. _IO_fwide (&new_f->fp._sf._sbf._f, 1);
  79. _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
  80. BUFSIZ / sizeof (wchar_t), buf);
  81. new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF;
  82. new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
  83. new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
  84. new_f->fp.bufloc = bufloc;
  85. new_f->fp.sizeloc = sizeloc;
  86. /* Disable single thread optimization. BZ 21735. */
  87. new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
  88. return (FILE *) &new_f->fp._sf._sbf;
  89. }
  90. static int
  91. _IO_wmem_sync (FILE *fp)
  92. {
  93. struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
  94. if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
  95. {
  96. _IO_wstr_overflow (fp, '\0');
  97. --fp->_wide_data->_IO_write_ptr;
  98. }
  99. *mp->bufloc = fp->_wide_data->_IO_write_base;
  100. *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
  101. - fp->_wide_data->_IO_write_base);
  102. return 0;
  103. }
  104. static void
  105. _IO_wmem_finish (FILE *fp, int dummy)
  106. {
  107. struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
  108. *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base,
  109. (fp->_wide_data->_IO_write_ptr
  110. - fp->_wide_data->_IO_write_base + 1)
  111. * sizeof (wchar_t));
  112. if (*mp->bufloc != NULL)
  113. {
  114. size_t len = (fp->_wide_data->_IO_write_ptr
  115. - fp->_wide_data->_IO_write_base);
  116. (*mp->bufloc)[len] = '\0';
  117. *mp->sizeloc = len;
  118. fp->_wide_data->_IO_buf_base = NULL;
  119. }
  120. _IO_wstr_finish (fp, 0);
  121. }