chiark / gitweb /
edb1d839817fa8e31299ba1d1f956bb70c5c6fbc
[disorder] / lib / hreader.c
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.c
19  * @brief Hands-off reader - read files without keeping them open
20  */
21 #include <config.h>
22 #include "hreader.h"
23 #include "mem.h"
24 #include <string.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 static int hreader_fill(struct hreader *h, off_t offset);
31
32 int hreader_init(const char *path, struct hreader *h) {
33   struct stat sb;
34   if(stat(path, &sb) < 0)
35     return -1;
36   memset(h, 0, sizeof *h);
37   h->path = xstrdup(path);
38   h->size = sb.st_size;
39   h->bufsize = 65536;
40   h->buffer = xmalloc_noptr(h->bufsize);
41   return 0;
42 }
43
44 int hreader_read(struct hreader *h, void *buffer, size_t n) {
45   int r = hreader_pread(h, buffer, n, h->read_offset);
46   if(r > 0)
47     h->read_offset += r;
48   return r;
49 }
50
51 int hreader_pread(struct hreader *h, void *buffer, size_t n, off_t offset) {
52   size_t bytes_read = 0;
53
54   while(bytes_read < n) {
55     // If the desired byte range is outside the file, fetch new contents
56     if(offset < h->buf_offset || offset >= h->buf_offset + (off_t)h->bytes) {
57       int r = hreader_fill(h, offset);
58       if(r < 0)
59         return -1;                      /* disaster! */
60       else if(r == 0)
61         break;                          /* end of file */
62     }
63     // Figure out how much we can read this time round
64     size_t left = h->bytes - (offset - h->buf_offset);
65     // Truncate the read if we don't want that much
66     if(left > (n - bytes_read))
67       left = n - bytes_read;
68     memcpy((char *)buffer + bytes_read,
69            h->buffer + (offset - h->buf_offset),
70            left);
71     offset += left;
72     bytes_read += left;
73   }
74   return bytes_read;
75 }
76
77 static int hreader_fill(struct hreader *h, off_t offset) {
78   int fd = open(h->path, O_RDONLY);
79   if(fd < 0)
80     return -1;
81   int n = pread(fd, h->buffer, h->bufsize, offset);
82   close(fd);
83   if(n < 0)
84     return -1;
85   h->buf_offset = offset;
86   h->bytes = n;
87   return n;
88 }
89
90 off_t hreader_seek(struct hreader *h, off_t offset, int whence) {
91   switch(whence) {
92   case SEEK_SET: break;
93   case SEEK_CUR: offset += h->read_offset; break;
94   case SEEK_END: offset += h->size; break;
95   default: einval: errno = EINVAL; return -1;
96   }
97   if(offset < 0) goto einval;
98   h->read_offset = offset;
99   return offset;
100 }
101
102 /*
103 Local Variables:
104 c-basic-offset:2
105 comment-column:40
106 fill-column:79
107 indent-tabs-mode:nil
108 End:
109 */