strfile.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 1993-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. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #ifndef STRFILE_H_
  23. #define STRFILE_H_
  24. #include "libioP.h"
  25. typedef void *(*_IO_alloc_type) (size_t);
  26. typedef void (*_IO_free_type) (void*);
  27. struct _IO_str_fields
  28. {
  29. /* These members are preserved for ABI compatibility. The glibc
  30. implementation always calls malloc/free for user buffers if
  31. _IO_USER_BUF or _IO_FLAGS2_USER_WBUF are not set. */
  32. _IO_alloc_type _allocate_buffer_unused;
  33. _IO_free_type _free_buffer_unused;
  34. };
  35. /* This is needed for the Irix6 N32 ABI, which has a 64 bit off_t type,
  36. but a 32 bit pointer type. In this case, we get 4 bytes of padding
  37. after the vtable pointer. Putting them in a structure together solves
  38. this problem. */
  39. struct _IO_streambuf
  40. {
  41. FILE _f;
  42. const struct _IO_jump_t *vtable;
  43. };
  44. typedef struct _IO_strfile_
  45. {
  46. struct _IO_streambuf _sbf;
  47. struct _IO_str_fields _s;
  48. } _IO_strfile;
  49. /* frozen: set when the program has requested that the array object not
  50. be altered, reallocated, or freed. */
  51. #define _IO_STR_FROZEN(FP) ((FP)->_f._flags & _IO_USER_BUF)
  52. typedef struct
  53. {
  54. _IO_strfile f;
  55. /* This is used for the characters which do not fit in the buffer
  56. provided by the user. */
  57. char overflow_buf[64];
  58. } _IO_strnfile;
  59. extern const struct _IO_jump_t _IO_strn_jumps attribute_hidden;
  60. typedef struct
  61. {
  62. _IO_strfile f;
  63. /* This is used for the characters which do not fit in the buffer
  64. provided by the user. */
  65. wchar_t overflow_buf[64];
  66. } _IO_wstrnfile;
  67. extern const struct _IO_jump_t _IO_wstrn_jumps attribute_hidden;
  68. /* Initialize an _IO_strfile SF to read from narrow string STRING, and
  69. return the corresponding FILE object. It is not necessary to fclose
  70. the FILE when it is no longer needed. */
  71. static inline FILE *
  72. _IO_strfile_read (_IO_strfile *sf, const char *string)
  73. {
  74. sf->_sbf._f._lock = NULL;
  75. _IO_no_init (&sf->_sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
  76. _IO_JUMPS (&sf->_sbf) = &_IO_str_jumps;
  77. _IO_str_init_static_internal (sf, (char*)string, 0, NULL);
  78. return &sf->_sbf._f;
  79. }
  80. /* Initialize an _IO_strfile SF and _IO_wide_data WD to read from wide
  81. string STRING, and return the corresponding FILE object. It is not
  82. necessary to fclose the FILE when it is no longer needed. */
  83. static inline FILE *
  84. _IO_strfile_readw (_IO_strfile *sf, struct _IO_wide_data *wd,
  85. const wchar_t *string)
  86. {
  87. sf->_sbf._f._lock = NULL;
  88. _IO_no_init (&sf->_sbf._f, _IO_USER_LOCK, 0, wd, &_IO_wstr_jumps);
  89. _IO_fwide (&sf->_sbf._f, 1);
  90. _IO_wstr_init_static (&sf->_sbf._f, (wchar_t *)string, 0, NULL);
  91. return &sf->_sbf._f;
  92. }
  93. #endif /* strfile.h. */