2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Default file operations structures for liblbx.
4 * Copyright © 2010, 2014 Nick Bowler
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.
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.
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/>.
28 /* Default I/O operations for ordinary files. */
29 static size_t file_read(void *buf, size_t size, void *handle)
31 size_t rc = fread(buf, 1, size, (FILE *)handle);
33 if (rc < size && ferror((FILE *)handle))
34 lbx_error_raise(-errno);
38 static int file_seek(void *handle, long offset, int whence)
40 if (fseek((FILE *)handle, offset, whence) == -1) {
41 lbx_error_raise(-errno);
48 static int file_eof(void *handle)
50 return feof((FILE *)handle);
53 const struct lbx_file_ops lbx_default_fops = {
59 /* Read function for pipes that tracks the number of bytes read. */
60 static size_t pipe_read(void *buf, size_t size, void *handle)
62 struct lbx_pipe_state *state = handle;
65 rc = file_read(buf, size, state->f);
70 /* Seek function for pipes that reads data into the void. */
71 static int pipe_seek(void *handle, long offset, int whence)
73 struct lbx_pipe_state *state = handle;
78 distance = offset - state->offset;
93 while (distance > 0) {
94 static unsigned char oblivion[1024];
95 size_t rc, amount = MIN(sizeof oblivion, distance);
97 rc = pipe_read(oblivion, amount, handle);
108 static int pipe_eof(void *handle)
110 struct lbx_pipe_state *state = handle;
112 return feof(state->f);
115 const struct lbx_file_ops lbx_pipe_fops = {
121 static size_t lbx_read(void *buf, size_t size, void *handle)
123 return lbx_file_read(handle, buf, size);
126 static int lbx_seek(void *handle, long offset, int whence)
128 return lbx_file_seek(handle, offset, whence);
131 static int lbx_eof(void *handle)
133 return lbx_file_eof(handle);
136 const struct lbx_file_ops lbx_arch_fops = {