fports.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef SCM_FPORTS_H
  2. #define SCM_FPORTS_H
  3. /* Copyright 1995-2001,2006,2008-2009,2011-2012,2017-2019
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/gc.h"
  18. #include "libguile/ports.h"
  19. /* struct allocated for each buffered FPORT. */
  20. typedef struct scm_t_fport {
  21. /* The file descriptor. */
  22. int fdes;
  23. /* Revealed count; 0 indicates not revealed, > 1 revealed. */
  24. unsigned int revealed;
  25. /* Set of scm_fport_option flags. */
  26. unsigned options;
  27. } scm_t_fport;
  28. SCM_API scm_t_port_type *scm_file_port_type;
  29. #define SCM_FSTREAM(x) ((scm_t_fport *) SCM_STREAM (x))
  30. #define SCM_FPORT_FDES(x) (SCM_FSTREAM (x)->fdes)
  31. #define SCM_FPORTP(x) \
  32. (SCM_PORTP (x) && SCM_PORT_TYPE (x) == scm_file_port_type)
  33. #define SCM_OPFPORTP(x) (SCM_FPORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_OPN))
  34. #define SCM_OPINFPORTP(x) (SCM_OPFPORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_RDNG))
  35. #define SCM_OPOUTFPORTP(x) (SCM_OPFPORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_WRTNG))
  36. #define SCM_VALIDATE_FPORT(pos, port) \
  37. SCM_MAKE_VALIDATE_MSG (pos, port, FPORTP, "file port")
  38. #define SCM_VALIDATE_OPFPORT(pos, port) \
  39. SCM_MAKE_VALIDATE_MSG (pos, port, OPFPORTP, "open file port")
  40. SCM_API void scm_evict_ports (int fd);
  41. SCM_INTERNAL int scm_i_mode_to_open_flags (SCM mode, int *is_binary,
  42. const char *FUNC_NAME);
  43. SCM_API SCM scm_open_file_with_encoding (SCM filename, SCM modes,
  44. SCM guess_encoding, SCM encoding);
  45. SCM_API SCM scm_open_file (SCM filename, SCM modes);
  46. SCM_API SCM scm_fdes_to_port (int fdes, char *mode, SCM name);
  47. SCM_API SCM scm_file_port_p (SCM obj);
  48. /* Revealed counts. */
  49. SCM_API int scm_revealed_count (SCM port);
  50. SCM_API SCM scm_port_revealed (SCM port);
  51. SCM_API SCM scm_set_port_revealed_x (SCM port, SCM rcount);
  52. SCM_API SCM scm_adjust_port_revealed_x (SCM port, SCM addend);
  53. SCM_INTERNAL void scm_init_fports_keywords (void);
  54. SCM_INTERNAL void scm_init_fports (void);
  55. /* internal functions */
  56. #ifdef BUILDING_LIBGUILE
  57. enum scm_fport_option
  58. {
  59. /* FD's that aren't created by Guile probably need to be checked for
  60. validity. We also check that the open mode is valid. */
  61. SCM_FPORT_OPTION_VERIFY = 1U<<0,
  62. /* We know some ports aren't seekable and can elide a syscall in
  63. that case. */
  64. SCM_FPORT_OPTION_NOT_SEEKABLE = 1U<<1
  65. };
  66. SCM_INTERNAL int scm_i_fdes_is_valid (int fdes, long mode_bits);
  67. SCM_INTERNAL SCM scm_i_fdes_to_port (int fdes, long mode_bits, SCM name,
  68. unsigned options);
  69. #endif /* BUILDING_LIBGUILE */
  70. #endif /* SCM_FPORTS_H */