chiark / gitweb /
Hands-off reader type
[disorder] / lib / hreader.h
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2010 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file lib/hreader.h
19  * @brief Hands-off reader - read files without keeping them open
20  */
21 #ifndef HREADER_H
22 #define HREADER_H
23
24 #include <unistd.h>
25
26 /** @brief A hands-off reader
27  *
28  * Allows files to be read without holding them open.
29  */
30 struct hreader {
31   const char *path;             /* file to read */
32   off_t offset;                 /* how far we've read so far */
33   char *buffer;                 /* input buffer */
34   size_t bufsize;               /* buffer size */
35   size_t bytes;                 /* size of last read */
36   size_t consumed;              /* bytes consumed by caller from last read */
37 };
38
39 /** @brief Initialize a hands-off reader
40  * @param path File to read
41  * @param h Reader to initialize
42  */
43 void hreader_init(const char *path, struct hreader *h);
44
45 /** @brief Read some bytes
46  * @param h Reader to read from
47  * @param buffer Where to store bytes
48  * @param n Maximum bytes to read
49  * @return Bytes read, or 0 at EOF, or -1 on error
50  */
51 int hreader_read(struct hreader *h, void *buffer, size_t n);
52
53 /** @brief Read more bytes
54  * @param h Reader to update
55  * @return Bytes available to read
56  *
57  * If not all bytes were consumed so far then just returns the number
58  * of bytes left to consume.
59  */
60 int hreader_fill(struct hreader *h);
61
62 /** @brief Consume some bytes
63  * @param h Reader to update
64  * @param n Bytes to consume
65  */
66 void hreader_consume(struct hreader *h, size_t n);
67
68 #endif /* HREADER_H */
69
70
71 /*
72 Local Variables:
73 c-basic-offset:2
74 comment-column:40
75 fill-column:79
76 indent-tabs-mode:nil
77 End:
78 */