wmemstream.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 1995-2026 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. <https://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. /* Open a stream that writes into a malloc'd buffer that is expanded as
  26. necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
  27. and the number of characters written on fflush or fclose. */
  28. FILE *
  29. open_wmemstream (wchar_t **bufloc, size_t *sizeloc)
  30. {
  31. struct locked_FILE
  32. {
  33. struct _IO_FILE_wmemstream fp;
  34. #ifdef _IO_MTSAFE_IO
  35. _IO_lock_t lock;
  36. #endif
  37. struct _IO_wide_data wd;
  38. } *new_f;
  39. wchar_t *buf;
  40. new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  41. if (new_f == NULL)
  42. return NULL;
  43. #ifdef _IO_MTSAFE_IO
  44. new_f->fp._sf._sbf._f._lock = &new_f->lock;
  45. #endif
  46. buf = calloc (1, BUFSIZ);
  47. if (buf == NULL)
  48. {
  49. free (new_f);
  50. return NULL;
  51. }
  52. _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
  53. _IO_fwide (&new_f->fp._sf._sbf._f, 1);
  54. _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
  55. BUFSIZ / sizeof (wchar_t), buf);
  56. new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF;
  57. new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
  58. new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
  59. new_f->fp.bufloc = bufloc;
  60. new_f->fp.sizeloc = sizeloc;
  61. /* Disable single thread optimization. BZ 21735. */
  62. new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
  63. return (FILE *) &new_f->fp._sf._sbf;
  64. }
  65. int
  66. _IO_wmem_sync (FILE *fp)
  67. {
  68. struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
  69. if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
  70. {
  71. _IO_wstr_overflow (fp, '\0');
  72. --fp->_wide_data->_IO_write_ptr;
  73. }
  74. *mp->bufloc = fp->_wide_data->_IO_write_base;
  75. *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
  76. - fp->_wide_data->_IO_write_base);
  77. return 0;
  78. }
  79. void
  80. _IO_wmem_finish (FILE *fp, int dummy)
  81. {
  82. struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
  83. *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base,
  84. (fp->_wide_data->_IO_write_ptr
  85. - fp->_wide_data->_IO_write_base + 1)
  86. * sizeof (wchar_t));
  87. if (*mp->bufloc != NULL)
  88. {
  89. size_t len = (fp->_wide_data->_IO_write_ptr
  90. - fp->_wide_data->_IO_write_base);
  91. (*mp->bufloc)[len] = '\0';
  92. *mp->sizeloc = len;
  93. fp->_wide_data->_IO_buf_base = NULL;
  94. }
  95. _IO_wstr_finish (fp, 0);
  96. }