Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hfile.h
Go to the documentation of this file.
1 /* hfile.h -- buffered 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 #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 
54 hFILE *hopen(const char *filename, const char *mode) HTS_RESULT_USED;
55 
61 hFILE *hdopen(int fd, const char *mode) HTS_RESULT_USED;
62 
68 
73 void hclose_abruptly(hFILE *fp);
74 
80 static inline int herrno(hFILE *fp)
81 {
82  return fp->has_errno;
83 }
84 
88 static inline void hclearerr(hFILE *fp)
89 {
90  fp->has_errno = 0;
91 }
92 
98 off_t hseek(hFILE *fp, off_t offset, int whence) HTS_RESULT_USED;
99 
104 static inline off_t htell(hFILE *fp)
105 {
106  return fp->offset + (fp->begin - fp->buffer);
107 }
108 
113 static inline int hgetc(hFILE *fp)
114 {
115  extern int hgetc2(hFILE *);
116  return (fp->end > fp->begin)? (unsigned char) *(fp->begin++) : hgetc2(fp);
117 }
118 
130 ssize_t hpeek(hFILE *fp, void *buffer, size_t nbytes) HTS_RESULT_USED;
131 
138 static inline ssize_t HTS_RESULT_USED
139 hread(hFILE *fp, void *buffer, size_t nbytes)
140 {
141  extern ssize_t hread2(hFILE *, void *, size_t, size_t);
142 
143  size_t n = fp->end - fp->begin;
144  if (n > nbytes) n = nbytes;
145  memcpy(buffer, fp->begin, n);
146  fp->begin += n;
147  return (n == nbytes)? (ssize_t) n : hread2(fp, buffer, nbytes, n);
148 }
149 
154 static inline int hputc(int c, hFILE *fp)
155 {
156  extern int hputc2(int, hFILE *);
157  if (fp->begin < fp->limit) *(fp->begin++) = c;
158  else c = hputc2(c, fp);
159  return c;
160 }
161 
166 static inline int hputs(const char *text, hFILE *fp)
167 {
168  extern int hputs2(const char *, size_t, size_t, hFILE *);
169 
170  size_t nbytes = strlen(text), n = fp->limit - fp->begin;
171  if (n > nbytes) n = nbytes;
172  memcpy(fp->begin, text, n);
173  fp->begin += n;
174  return (n == nbytes)? 0 : hputs2(text, nbytes, n, fp);
175 }
176 
182 static inline ssize_t HTS_RESULT_USED
183 hwrite(hFILE *fp, const void *buffer, size_t nbytes)
184 {
185  extern ssize_t hwrite2(hFILE *, const void *, size_t, size_t);
186 
187  size_t n = fp->limit - fp->begin;
188  if (n > nbytes) n = nbytes;
189  memcpy(fp->begin, buffer, n);
190  fp->begin += n;
191  return (n==nbytes)? (ssize_t) n : hwrite2(fp, buffer, nbytes, n);
192 }
193 
198 int hflush(hFILE *fp) HTS_RESULT_USED;
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 #endif
ssize_t hpeek(hFILE *fp, void *buffer, size_t nbytes) HTS_RESULT_USED
Definition: hfile.c:135
int has_errno
Definition: hfile.h:47
Definition: hfile.h:42
int hflush(hFILE *fp) HTS_RESULT_USED
Definition: hfile.c:200
off_t hseek(hFILE *fp, off_t offset, int whence) HTS_RESULT_USED
Definition: hfile.c:249
hFILE * hdopen(int fd, const char *mode) HTS_RESULT_USED
Definition: hfile.c:407
char * limit
Definition: hfile.h:43
enum @17 mode
void hclose_abruptly(hFILE *fp)
Definition: hfile.c:284
int at_eof
Definition: hfile.h:46
char * buffer
Definition: hfile.h:43
hFILE * hopen(const char *filename, const char *mode) HTS_RESULT_USED
Definition: hfile.c:519
int hputs2(const char *text, size_t totalbytes, size_t ncopied, hFILE *fp)
Definition: hfile.c:244
#define HTS_RESULT_USED
Definition: hts_defs.h:38
int hclose(hFILE *fp) HTS_RESULT_USED
Definition: hfile.c:269
struct hFILE hFILE
Definition: hfile_internal.h:30
const struct hFILE_backend * backend
Definition: hfile.h:44
ssize_t hread2(hFILE *fp, void *destv, size_t nbytes, size_t nread)
Definition: hfile.c:152
off_t offset
Definition: hfile.h:45
int hputc2(int c, hFILE *fp)
Definition: hfile.c:208
char * begin
Definition: hfile.h:43
ssize_t hwrite2(hFILE *fp, const void *srcv, size_t totalbytes, size_t ncopied)
Definition: hfile.c:217
char * end
Definition: hfile.h:43
int hgetc2(hFILE *fp)
Definition: hfile.c:130