hfile_internal.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* hfile_internal.h -- internal parts of low-level input/output streams.
  2. Copyright (C) 2013-2014 Genome Research Ltd.
  3. Author: John Marshall <jm18@sanger.ac.uk>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notices and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. DEALINGS IN THE SOFTWARE. */
  19. #ifndef HFILE_INTERNAL_H
  20. #define HFILE_INTERNAL_H
  21. #include "htslib/hfile.h"
  22. struct hFILE_backend {
  23. /* As per read(2), returning the number of bytes read (possibly 0) or
  24. negative (and setting errno) on errors. Front-end code will call this
  25. repeatedly if necessary to attempt to get the desired byte count. */
  26. ssize_t (*read)(hFILE *fp, void *buffer, size_t nbytes) HTS_RESULT_USED;
  27. /* As per write(2), returning the number of bytes written or negative (and
  28. setting errno) on errors. Front-end code will call this repeatedly if
  29. necessary until the desired block is written or an error occurs. */
  30. ssize_t (*write)(hFILE *fp, const void *buffer, size_t nbytes)
  31. HTS_RESULT_USED;
  32. /* As per lseek(2), returning the resulting offset within the stream or
  33. negative (and setting errno) on errors. */
  34. off_t (*seek)(hFILE *fp, off_t offset, int whence) HTS_RESULT_USED;
  35. /* Performs low-level flushing, if any, e.g., fsync(2); for writing streams
  36. only. Returns 0 for success or negative (and sets errno) on errors. */
  37. int (*flush)(hFILE *fp) HTS_RESULT_USED;
  38. /* Closes the underlying stream (for output streams, the buffer will
  39. already have been flushed), returning 0 for success or negative (and
  40. setting errno) on errors, as per close(2). */
  41. int (*close)(hFILE *fp) HTS_RESULT_USED;
  42. };
  43. /* These are called from the hopen() dispatcher, and should call hfile_init()
  44. to malloc a struct "derived" from hFILE and initialise it appropriately,
  45. including setting base.backend to their own backend vector. */
  46. hFILE *hopen_net(const char *filename, const char *mode);
  47. /* May be called by hopen_*() functions to decode a fopen()-style mode into
  48. open(2)-style flags. */
  49. int hfile_oflags(const char *mode);
  50. /* Must be called by hopen_*() functions to allocate the hFILE struct and set
  51. up its base. Capacity is a suggested buffer size (e.g., via fstat(2))
  52. or 0 for a default-sized buffer. */
  53. hFILE *hfile_init(size_t struct_size, const char *mode, size_t capacity);
  54. /* May be called by hopen_*() functions to undo the effects of hfile_init()
  55. in the event opening the stream subsequently fails. (This is safe to use
  56. even if fp is NULL. This takes care to preserve errno.) */
  57. void hfile_destroy(hFILE *fp);
  58. #endif