| 0 | 1 /*  hfile_net.c -- network backend for low-level input/output streams. | 
|  | 2 | 
|  | 3     Copyright (C) 2013-2014 Genome Research Ltd. | 
|  | 4 | 
|  | 5     Author: John Marshall <jm18@sanger.ac.uk> | 
|  | 6 | 
|  | 7 Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 8 of this software and associated documentation files (the "Software"), to deal | 
|  | 9 in the Software without restriction, including without limitation the rights | 
|  | 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|  | 11 copies of the Software, and to permit persons to whom the Software is | 
|  | 12 furnished to do so, subject to the following conditions: | 
|  | 13 | 
|  | 14 The above copyright notice and this permission notice shall be included in | 
|  | 15 all copies or substantial portions of the Software. | 
|  | 16 | 
|  | 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|  | 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 
|  | 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|  | 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
|  | 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 
|  | 23 DEALINGS IN THE SOFTWARE.  */ | 
|  | 24 | 
|  | 25 #include <stdlib.h> | 
|  | 26 #include <errno.h> | 
|  | 27 | 
|  | 28 #include "hfile_internal.h" | 
|  | 29 | 
|  | 30 #include "htslib/knetfile.h" | 
|  | 31 | 
|  | 32 typedef struct { | 
|  | 33     hFILE base; | 
|  | 34     knetFile *netfp; | 
|  | 35 } hFILE_net; | 
|  | 36 | 
|  | 37 static int net_inited = 0; | 
|  | 38 | 
|  | 39 #ifdef _WIN32 | 
|  | 40 static void net_exit(void) | 
|  | 41 { | 
|  | 42     knet_win32_destroy(); | 
|  | 43 } | 
|  | 44 #endif | 
|  | 45 | 
|  | 46 static int net_init(void) | 
|  | 47 { | 
|  | 48 #ifdef _WIN32 | 
|  | 49     if (knet_win32_init() != 0) return -1; | 
|  | 50 | 
|  | 51     // In the unlikely event atexit() fails, it's better to succeed here and | 
|  | 52     // carry on and do the I/O; then eventually when the program exits, we'll | 
|  | 53     // merely have failed to clean up properly, as if we had aborted. | 
|  | 54     (void) atexit(net_exit); | 
|  | 55 #endif | 
|  | 56 | 
|  | 57     net_inited = 1; | 
|  | 58     return 0; | 
|  | 59 } | 
|  | 60 | 
|  | 61 static ssize_t net_read(hFILE *fpv, void *buffer, size_t nbytes) | 
|  | 62 { | 
|  | 63     hFILE_net *fp = (hFILE_net *) fpv; | 
|  | 64     return knet_read(fp->netfp, buffer, nbytes); | 
|  | 65 } | 
|  | 66 | 
|  | 67 static off_t net_seek(hFILE *fpv, off_t offset, int whence) | 
|  | 68 { | 
|  | 69     hFILE_net *fp = (hFILE_net *) fpv; | 
|  | 70     return knet_seek(fp->netfp, offset, whence); | 
|  | 71 } | 
|  | 72 | 
|  | 73 static int net_close(hFILE *fpv) | 
|  | 74 { | 
|  | 75     hFILE_net *fp = (hFILE_net *) fpv; | 
|  | 76     return knet_close(fp->netfp); | 
|  | 77 } | 
|  | 78 | 
|  | 79 static const struct hFILE_backend net_backend = | 
|  | 80 { | 
|  | 81     net_read, NULL, net_seek, NULL, net_close | 
|  | 82 }; | 
|  | 83 | 
|  | 84 hFILE *hopen_net(const char *filename, const char *mode) | 
|  | 85 { | 
|  | 86     hFILE_net *fp; | 
|  | 87 | 
|  | 88     // Do any networking initialisation if this is the first use. | 
|  | 89     if (! net_inited) { if (net_init() < 0) return NULL; } | 
|  | 90 | 
|  | 91     fp = (hFILE_net *) hfile_init(sizeof (hFILE_net), mode, 0); | 
|  | 92     if (fp == NULL) return NULL; | 
|  | 93 | 
|  | 94     fp->netfp = knet_open(filename, mode); | 
|  | 95     if (fp->netfp == NULL) { hfile_destroy((hFILE *) fp); return NULL; } | 
|  | 96 | 
|  | 97     fp->base.backend = &net_backend; | 
|  | 98     return &fp->base; | 
|  | 99 } |