hfile_net.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* hfile_net.c -- network backend for 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. #include <stdlib.h>
  20. #include <errno.h>
  21. #include "hfile_internal.h"
  22. #include "htslib/knetfile.h"
  23. typedef struct {
  24. hFILE base;
  25. knetFile *netfp;
  26. } hFILE_net;
  27. static int net_inited = 0;
  28. #ifdef _WIN32
  29. static void net_exit(void)
  30. {
  31. knet_win32_destroy();
  32. }
  33. #endif
  34. static int net_init(void)
  35. {
  36. #ifdef _WIN32
  37. if (knet_win32_init() != 0) return -1;
  38. // In the unlikely event atexit() fails, it's better to succeed here and
  39. // carry on and do the I/O; then eventually when the program exits, we'll
  40. // merely have failed to clean up properly, as if we had aborted.
  41. (void) atexit(net_exit);
  42. #endif
  43. net_inited = 1;
  44. return 0;
  45. }
  46. static ssize_t net_read(hFILE *fpv, void *buffer, size_t nbytes)
  47. {
  48. hFILE_net *fp = (hFILE_net *) fpv;
  49. return knet_read(fp->netfp, buffer, nbytes);
  50. }
  51. static off_t net_seek(hFILE *fpv, off_t offset, int whence)
  52. {
  53. hFILE_net *fp = (hFILE_net *) fpv;
  54. return knet_seek(fp->netfp, offset, whence);
  55. }
  56. static int net_close(hFILE *fpv)
  57. {
  58. hFILE_net *fp = (hFILE_net *) fpv;
  59. return knet_close(fp->netfp);
  60. }
  61. static const struct hFILE_backend net_backend =
  62. {
  63. net_read, NULL, net_seek, NULL, net_close
  64. };
  65. hFILE *hopen_net(const char *filename, const char *mode)
  66. {
  67. hFILE_net *fp;
  68. // Do any networking initialisation if this is the first use.
  69. if (! net_inited) { if (net_init() < 0) return NULL; }
  70. fp = (hFILE_net *) hfile_init(sizeof (hFILE_net), mode, 0);
  71. if (fp == NULL) return NULL;
  72. fp->netfp = knet_open(filename, mode);
  73. if (fp->netfp == NULL) { hfile_destroy((hFILE *) fp); return NULL; }
  74. fp->base.backend = &net_backend;
  75. return &fp->base;
  76. }