]> git.draconx.ca Git - liblbx.git/blobdiff - src/fops.c
Trivial manual fixes.
[liblbx.git] / src / fops.c
index 2a3db82854e56449ba63d100106ac442b2d0d01c..521b50bd0a35cdaca3e7e2398a16ed0dfe1fc1fa 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
  *  Default file operations structures for liblbx.
- *  Copyright (C) 2010 Nick Bowler
+ *  Copyright © 2010, 2014 Nick Bowler
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+#include <config.h>
 #include <stdio.h>
+#include <errno.h>
+#include <assert.h>
 
 #include "misc.h"
+#include "error.h"
 #include "lbx.h"
 
 /* Default I/O operations for ordinary files. */
 static size_t file_read(void *buf, size_t size, void *handle)
 {
-       return fread(buf, 1, size, (FILE *)handle);
+       size_t rc = fread(buf, 1, size, (FILE *)handle);
+
+       if (rc < size && ferror((FILE *)handle))
+               lbx_error_raise(-errno);
+       return rc;
 }
 
 static int file_seek(void *handle, long offset, int whence)
 {
-       return fseek((FILE *)handle, offset, whence);
-}
+       if (fseek((FILE *)handle, offset, whence) == -1) {
+               lbx_error_raise(-errno);
+               return -1;
+       }
 
-static long file_tell(void *handle)
-{
-       return ftell((FILE *)handle);
+       return 0;
 }
 
 static int file_eof(void *handle)
@@ -45,7 +53,6 @@ static int file_eof(void *handle)
 const struct lbx_file_ops lbx_default_fops = {
        .read = file_read,
        .seek = file_seek,
-       .tell = file_tell,
        .eof  = file_eof,
 };
 
@@ -55,7 +62,7 @@ static size_t pipe_read(void *buf, size_t size, void *handle)
        struct lbx_pipe_state *state = handle;
        size_t rc;
 
-       rc = fread(buf, 1, size, state->f);
+       rc = file_read(buf, size, state->f);
        state->offset += rc;
        return rc;
 }
@@ -76,6 +83,8 @@ static int pipe_seek(void *handle, long offset, int whence)
        case SEEK_END:
                distance = -1;
                break;
+       default:
+               assert(0);
        }
 
        if (distance < 0)
@@ -92,16 +101,10 @@ static int pipe_seek(void *handle, long offset, int whence)
                        return -1;
        }
 
+       clearerr(state->f);
        return 0;
 }
 
-static long pipe_tell(void *handle)
-{
-       struct lbx_pipe_state *state = handle;
-
-       return state->offset;
-}
-
 static int pipe_eof(void *handle)
 {
        struct lbx_pipe_state *state = handle;
@@ -112,7 +115,6 @@ static int pipe_eof(void *handle)
 const struct lbx_file_ops lbx_pipe_fops = {
        .read = pipe_read,
        .seek = pipe_seek,
-       .tell = pipe_tell,
        .eof  = pipe_eof,
 };
 
@@ -126,11 +128,6 @@ static int lbx_seek(void *handle, long offset, int whence)
        return lbx_file_seek(handle, offset, whence);
 }
 
-static long lbx_tell(void *handle)
-{
-       return lbx_file_tell(handle);
-}
-
 static int lbx_eof(void *handle)
 {
        return lbx_file_eof(handle);
@@ -139,6 +136,5 @@ static int lbx_eof(void *handle)
 const struct lbx_file_ops lbx_arch_fops = {
        .read = lbx_read,
        .seek = lbx_seek,
-       .tell = lbx_tell,
        .eof  = lbx_eof,
 };