llio.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Declarations for low-level IO functions.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_LLIO__
  14. #define __KP_LLIO__ 1
  15. #include "interp.hpp"
  16. #include "initop.hpp"
  17. KP_DECLS_BEGIN
  18. #ifdef KP_PLATFORM_UNIX
  19. typedef int fhandle_t;
  20. inline bool
  21. fhandle_valid_p (fhandle_t fh)
  22. {
  23. return (fh >= 0);
  24. }
  25. inline fhandle_t
  26. llio_stdfh (unsigned int idx)
  27. {
  28. return (idx <= 2 ? (fhandle_t)idx : -1);
  29. }
  30. #elif defined (KP_PLATFORM_WINDOWS)
  31. typedef void* fhandle_t;
  32. KP_EXPORT bool fhandle_valid_p (fhandle_t fh);
  33. #else
  34. # error "unsupported platform"
  35. #endif
  36. /* Open a file handle given a path and mode. Store the matching
  37. * stream flags in *FLAGP if not null. */
  38. KP_EXPORT fhandle_t llio_open (interpreter *interp, const char *path,
  39. const char *mode, int *flagp = nullptr);
  40. // Read at most NBYTES bytes from the file handle FH into DST.
  41. KP_EXPORT result<int64_t> llio_readb (interpreter *interp, fhandle_t fh,
  42. char *dst, uint64_t nbytes);
  43. // Write at most NBYTES bytes from SRC into file handle FH.
  44. KP_EXPORT result<int64_t> llio_writeb (interpreter *interp, fhandle_t fh,
  45. const char *src, uint64_t nbytes);
  46. /* Set the file handle position to OFFSET, according to WHENCE in
  47. * file handle FH. */
  48. KP_EXPORT result<bool> llio_seek (interpreter *interp, fhandle_t fh,
  49. int64_t& offset, int whence);
  50. // Truncate file handle FH to SIZE bytes.
  51. KP_EXPORT bool llio_truncate (fhandle_t fh, uint64_t size);
  52. // Close file handle FH.
  53. KP_EXPORT bool llio_close (fhandle_t fh);
  54. struct fhandle_stat
  55. {
  56. int64_t atime;
  57. int64_t mtime;
  58. int64_t ctime;
  59. uint64_t size;
  60. uint32_t nlink;
  61. #ifdef KP_PLATFORM_UNIX
  62. uint64_t dev;
  63. uint64_t ino;
  64. uint32_t mode;
  65. int uid;
  66. int gid;
  67. uint64_t rdev;
  68. uint64_t blksize;
  69. uint64_t blocks;
  70. #elif defined (KP_PLATFORM_WINDOWS)
  71. uint32_t attr;
  72. uint32_t serialno;
  73. uint64_t index;
  74. #else
  75. # error "unsupported platform"
  76. #endif
  77. };
  78. // Return information for file handle FH in OUT.
  79. KP_EXPORT result<bool> llio_fhstat (interpreter *interp,
  80. fhandle_t fh, fhandle_stat& out);
  81. // Return a string that represents the realpath for PATH.
  82. KP_EXPORT result<object> llio_fhpath (interpreter *interp, const char *path);
  83. KP_EXPORT result<int64_t> llio_readt (interpreter *interp, fhandle_t fh,
  84. char *dst, uint64_t nbytes);
  85. KP_EXPORT result<int64_t> llio_writet (interpreter *interp, fhandle_t fh,
  86. const char *src, uint64_t nbytes);
  87. #ifdef KP_PLATFORM_WINDOWS
  88. // Return the standard file handle for index IDX.
  89. KP_EXPORT fhandle_t llio_stdfh (unsigned int idx);
  90. #endif
  91. // Init OP for low-level IO.
  92. KP_EXPORT init_op init_llio;
  93. KP_DECLS_END
  94. #endif