annotate ezBAMQC/src/htslib/hfile_net.c @ 20:9de3bbec2479 draft default tip

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