1 /*  hfile.h -- buffered low-level input/output streams.
2 
3     Copyright (C) 2013-2015 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 #ifndef HTSLIB_HFILE_H
26 #define HTSLIB_HFILE_H
27 
28 #include <string.h>
29 
30 #include <sys/types.h>
31 
32 #include "hts_defs.h"
33 
34 // #ifdef __cplusplus
35 // extern "C" {
36 // #endif
37 
38 /* These fields are declared here solely for the benefit of the inline functions
39    below.  They may change in future releases.  User code should not use them
40    directly; you should imagine that hFILE is an opaque incomplete type.  */
41 struct hFILE_backend;
42 typedef struct hFILE {
43     char *buffer, *begin, *end, *limit;
44     const struct hFILE_backend *backend;
45     off_t offset;
46     int at_eof:1;
47     int has_errno;
48 } hFILE;
49 
50 /*!
51   @abstract  Open the named file or URL as a stream
52   @return    An hFILE pointer, or NULL (with errno set) if an error occurred.
53 */
54 hFILE *hopen(const char *filename, const char *mode) HTS_RESULT_USED;
55 
56 /*!
57   @abstract  Associate a stream with an existing open file descriptor
58   @return    An hFILE pointer, or NULL (with errno set) if an error occurred.
59   @notes     For socket descriptors (on Windows), mode should contain 's'.
60 */
61 hFILE *hdopen(int fd, const char *mode) HTS_RESULT_USED;
62 
63 /*!
64   @abstract  Report whether the file name or URL denotes remote storage
65   @return    0 if local, 1 if remote.
66   @notes     "Remote" means involving e.g. explicit network access, with the
67     implication that callers may wish to cache such files' contents locally.
68 */
69 // int hisremote(const char *filename) HTS_RESULT_USED;
70 
71 /*!
72   @abstract  Flush (for output streams) and close the stream
73   @return    0 if successful, or EOF (with errno set) if an error occurred.
74 */
75 int hclose(hFILE *fp) HTS_RESULT_USED;
76 
77 /*!
78   @abstract  Close the stream, without flushing or propagating errors
79   @notes     For use while cleaning up after an error only.  Preserves errno.
80 */
81 void hclose_abruptly(hFILE *fp);
82 
83 /*!
84   @abstract  Return the stream's error indicator
85   @return    Non-zero (in fact, an errno value) if an error has occurred.
86   @notes     This would be called herror() and return true/false to parallel
87     ferror(3), but a networking-related herror(3) function already exists.  */
herrno(hFILE * fp)88 static inline int herrno(hFILE *fp)
89 {
90     return fp->has_errno;
91 }
92 
93 /*!
94   @abstract  Clear the stream's error indicator
95 */
hclearerr(hFILE * fp)96 static inline void hclearerr(hFILE *fp)
97 {
98     fp->has_errno = 0;
99 }
100 
101 /*!
102   @abstract  Reposition the read/write stream offset
103   @return    The resulting offset within the stream (as per lseek(2)),
104     or negative if an error occurred.
105 */
106 off_t hseek(hFILE *fp, off_t offset, int whence) HTS_RESULT_USED;
107 
108 /*!
109   @abstract  Report the current stream offset
110   @return    The offset within the stream, starting from zero.
111 */
htell(hFILE * fp)112 static inline off_t htell(hFILE *fp)
113 {
114     return fp->offset + (fp->begin - fp->buffer);
115 }
116 
117 /*!
118   @abstract  Read one character from the stream
119   @return    The character read, or EOF on end-of-file or error
120 */
hgetc(hFILE * fp)121 static inline int hgetc(hFILE *fp)
122 {
123     extern int hgetc2(hFILE *);
124     return (fp->end > fp->begin)? (unsigned char) *(fp->begin++) : hgetc2(fp);
125 }
126 
127 /*!
128   @abstract  Peek at characters to be read without removing them from buffers
129   @param fp      The file stream
130   @param buffer  The buffer to which the peeked bytes will be written
131   @param nbytes  The number of bytes to peek at; limited by the size of the
132     internal buffer, which could be as small as 4K.
133   @return    The number of bytes peeked, which may be less than nbytes if EOF
134     is encountered; or negative, if there was an I/O error.
135   @notes  The characters peeked at remain in the stream's internal buffer,
136     and will be returned by later hread() etc calls.
137 */
138 ssize_t hpeek(hFILE *fp, void *buffer, size_t nbytes) HTS_RESULT_USED;
139 
140 /*!
141   @abstract  Read a block of characters from the file
142   @return    The number of bytes read, or negative if an error occurred.
143   @notes     The full nbytes requested will be returned, except as limited
144     by EOF or I/O errors.
145 */
146 static inline ssize_t HTS_RESULT_USED
hread(hFILE * fp,void * buffer,size_t nbytes)147 hread(hFILE *fp, void *buffer, size_t nbytes)
148 {
149     extern ssize_t hread2(hFILE *, void *, size_t, size_t);
150 
151     size_t n = fp->end - fp->begin;
152     if (n > nbytes) n = nbytes;
153     memcpy(buffer, fp->begin, n);
154     fp->begin += n;
155     return (n == nbytes)? (ssize_t) n : hread2(fp, buffer, nbytes, n);
156 }
157 
158 /*!
159   @abstract  Write a character to the stream
160   @return    The character written, or EOF if an error occurred.
161 */
hputc(int c,hFILE * fp)162 static inline int hputc(int c, hFILE *fp)
163 {
164     extern int hputc2(int, hFILE *);
165     if (fp->begin < fp->limit) *(fp->begin++) = c;
166     else c = hputc2(c, fp);
167     return c;
168 }
169 
170 /*!
171   @abstract  Write a string to the stream
172   @return    0 if successful, or EOF if an error occurred.
173 */
hputs(const char * text,hFILE * fp)174 static inline int hputs(const char *text, hFILE *fp)
175 {
176     extern int hputs2(const char *, size_t, size_t, hFILE *);
177 
178     size_t nbytes = strlen(text), n = fp->limit - fp->begin;
179     if (n > nbytes) n = nbytes;
180     memcpy(fp->begin, text, n);
181     fp->begin += n;
182     return (n == nbytes)? 0 : hputs2(text, nbytes, n, fp);
183 }
184 
185 /*!
186   @abstract  Write a block of characters to the file
187   @return    Either nbytes, or negative if an error occurred.
188   @notes     In the absence of I/O errors, the full nbytes will be written.
189 */
190 static inline ssize_t HTS_RESULT_USED
hwrite(hFILE * fp,const void * buffer,size_t nbytes)191 hwrite(hFILE *fp, const void *buffer, size_t nbytes)
192 {
193     extern ssize_t hwrite2(hFILE *, const void *, size_t, size_t);
194 
195     size_t n = fp->limit - fp->begin;
196     if (n > nbytes) n = nbytes;
197     memcpy(fp->begin, buffer, n);
198     fp->begin += n;
199     return (n==nbytes)? (ssize_t) n : hwrite2(fp, buffer, nbytes, n);
200 }
201 
202 /*!
203   @abstract  For writing streams, flush buffered output to the underlying stream
204   @return    0 if successful, or EOF if an error occurred.
205 */
206 int hflush(hFILE *fp) HTS_RESULT_USED;
207 
208 // #ifdef __cplusplus
209 // }
210 // #endif
211 
212 #endif
213