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