2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Default file operations structures for liblbx.
4 * Copyright (C) 2010 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/>.
24 /* Default I/O operations for ordinary files. */
25 static size_t file_read(void *buf, size_t size, void *handle)
27 return fread(buf, 1, size, (FILE *)handle);
30 static int file_seek(void *handle, long offset, int whence)
32 return fseek((FILE *)handle, offset, whence);
35 static long file_tell(void *handle)
37 return ftell((FILE *)handle);
40 static int file_eof(void *handle)
42 return feof((FILE *)handle);
45 const struct lbx_file_ops lbx_default_fops = {
52 /* Read function for pipes that tracks the number of bytes read. */
53 static size_t pipe_read(void *buf, size_t size, void *handle)
55 struct lbx_pipe_state *state = handle;
58 rc = fread(buf, 1, size, state->f);
63 /* Seek function for pipes that reads data into the void. */
64 static int pipe_seek(void *handle, long offset, int whence)
66 struct lbx_pipe_state *state = handle;
71 distance = offset - state->offset;
84 while (distance > 0) {
85 static unsigned char oblivion[1024];
86 size_t rc, amount = MIN(sizeof oblivion, distance);
88 rc = pipe_read(oblivion, amount, handle);
98 static long pipe_tell(void *handle)
100 struct lbx_pipe_state *state = handle;
102 return state->offset;
105 static int pipe_eof(void *handle)
107 struct lbx_pipe_state *state = handle;
109 return feof(state->f);
112 const struct lbx_file_ops lbx_pipe_fops = {
119 static size_t lbx_read(void *buf, size_t size, void *handle)
121 return lbx_file_read(handle, buf, size);
124 static int lbx_seek(void *handle, long offset, int whence)
126 return lbx_file_seek(handle, offset, whence);
129 static long lbx_tell(void *handle)
131 return lbx_file_tell(handle);
134 static int lbx_eof(void *handle)
136 return lbx_file_eof(handle);
139 const struct lbx_file_ops lbx_arch_fops = {