]> git.draconx.ca Git - liblbx.git/blob - src/fops.c
e546b1097692f3e0f5b07c9403828772de62f53b
[liblbx.git] / src / fops.c
1 /*
2  *  2ooM: The Master of Orion II Reverse Engineering Project
3  *  Default file operations structures for liblbx.
4  *  Copyright (C) 2010 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <stdio.h>
20 #include <errno.h>
21
22 #include "misc.h"
23 #include "error.h"
24 #include "lbx.h"
25
26 /* Default I/O operations for ordinary files. */
27 static size_t file_read(void *buf, size_t size, void *handle)
28 {
29         size_t rc = fread(buf, 1, size, (FILE *)handle);
30
31         if (rc < size && ferror((FILE *)handle))
32                 lbx_error_raise(-errno);
33         return rc;
34 }
35
36 static int file_seek(void *handle, long offset, int whence)
37 {
38         if (fseek((FILE *)handle, offset, whence) == -1) {
39                 lbx_error_raise(-errno);
40                 return -1;
41         }
42 }
43
44 static long file_tell(void *handle)
45 {
46         return ftell((FILE *)handle);
47 }
48
49 static int file_eof(void *handle)
50 {
51         return feof((FILE *)handle);
52 }
53
54 const struct lbx_file_ops lbx_default_fops = {
55         .read = file_read,
56         .seek = file_seek,
57         .tell = file_tell,
58         .eof  = file_eof,
59 };
60
61 /* Read function for pipes that tracks the number of bytes read. */
62 static size_t pipe_read(void *buf, size_t size, void *handle)
63 {
64         struct lbx_pipe_state *state = handle;
65         size_t rc;
66
67         rc = file_read(buf, size, state->f);
68         state->offset += rc;
69         return rc;
70 }
71
72 /* Seek function for pipes that reads data into the void. */
73 static int pipe_seek(void *handle, long offset, int whence)
74 {
75         struct lbx_pipe_state *state = handle;
76         long distance;
77
78         switch (whence) {
79         case SEEK_SET:
80                 distance = offset - state->offset;
81                 break;
82         case SEEK_CUR:
83                 distance = offset;
84                 break;
85         case SEEK_END:
86                 distance = -1;
87                 break;
88         }
89
90         if (distance < 0)
91                 return -1;
92
93         while (distance > 0) {
94                 static unsigned char oblivion[1024];
95                 size_t rc, amount = MIN(sizeof oblivion, distance);
96
97                 rc = pipe_read(oblivion, amount, handle);
98                 distance -= rc;
99
100                 if (rc < amount)
101                         return -1;
102         }
103
104         clearerr(state->f);
105         return 0;
106 }
107
108 static long pipe_tell(void *handle)
109 {
110         struct lbx_pipe_state *state = handle;
111
112         return state->offset;
113 }
114
115 static int pipe_eof(void *handle)
116 {
117         struct lbx_pipe_state *state = handle;
118
119         return feof(state->f);
120 }
121
122 const struct lbx_file_ops lbx_pipe_fops = {
123         .read = pipe_read,
124         .seek = pipe_seek,
125         .tell = pipe_tell,
126         .eof  = pipe_eof,
127 };
128
129 static size_t lbx_read(void *buf, size_t size, void *handle)
130 {
131         return lbx_file_read(handle, buf, size);
132 }
133
134 static int lbx_seek(void *handle, long offset, int whence)
135 {
136         return lbx_file_seek(handle, offset, whence);
137 }
138
139 static long lbx_tell(void *handle)
140 {
141         return lbx_file_tell(handle);
142 }
143
144 static int lbx_eof(void *handle)
145 {
146         return lbx_file_eof(handle);
147 }
148
149 const struct lbx_file_ops lbx_arch_fops = {
150         .read = lbx_read,
151         .seek = lbx_seek,
152         .tell = lbx_tell,
153         .eof  = lbx_eof,
154 };