Skip to content

Commit

Permalink
Clean up the physfs_rwops re-write
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidityC committed Nov 5, 2024
1 parent c504b8b commit 11ea58a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 71 deletions.
70 changes: 23 additions & 47 deletions src/physfsrwops.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <[email protected]>
* This file was originally authored by Ryan C. Gordon ([email protected]).
* It's been patched by Linus Probert ([email protected]) to work for
* SDL3 IOStreams which have replaced rwops.
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* The file is still under no warranty or license. Do what you like with it.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* The original file taken from: https://github.com/icculus/physfs
*/

#include <stdio.h>
#include "physfsrwops.h"
#include "util.h"

static const char *getLastPhysfsError(void)
{
Expand All @@ -38,69 +29,55 @@ static Sint64 physfsrwops_seek(void *userdata, Sint64 offset, enum SDL_IOWhence
PHYSFS_File *handle = (PHYSFS_File *) userdata;
PHYSFS_sint64 pos = 0;

if (whence == SDL_IO_SEEK_SET)
if (whence == SDL_IO_SEEK_SET) {
pos = (PHYSFS_sint64) offset;

else if (whence == SDL_IO_SEEK_CUR)
{
} else if (whence == SDL_IO_SEEK_CUR) {
const PHYSFS_sint64 current = PHYSFS_tell(handle);
if (current == -1)
{
if (current == -1) {

SDL_SetError("Can't find position in file: %s",
getLastPhysfsError());
return -1;
} /* if */
}

if (offset == 0) /* this is a "tell" call. We're done. */
{
if (offset == 0) {
return (Sint64) current;
} /* if */
}

pos = current + ((PHYSFS_sint64) offset);
} /* else if */

else if (whence == SDL_IO_SEEK_END)
{
} else if (whence == SDL_IO_SEEK_END) {
const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
if (len == -1)
{
if (len == -1) {
SDL_SetError("Can't find end of file: %s", getLastPhysfsError());
return -1;
} /* if */
}

pos = len + ((PHYSFS_sint64) offset);
} /* else if */

else
{
} else {
SDL_SetError("Invalid 'whence' parameter.");
return -1;
} /* else */
}

if ( pos < 0 )
{
if ( pos < 0 ) {
SDL_SetError("Attempt to seek past start of file.");
return -1;
} /* if */
}

if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
{
if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos)) {
SDL_SetError("PhysicsFS error: %s", getLastPhysfsError());
return -1;
} /* if */
}

return (Sint64) pos;
} /* physfsrwops_seek */
}


static size_t physfsrwops_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
{
PHYSFS_File *handle = (PHYSFS_File *) userdata;
const PHYSFS_uint64 readlen = (PHYSFS_uint64) size;
const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen);
if (rc != ((PHYSFS_sint64) readlen))
{
if (rc != ((PHYSFS_sint64) readlen)) {
if (!PHYSFS_eof(handle)) {
SDL_SetError("PhysicsFS error: %s", getLastPhysfsError());
*status = SDL_IO_STATUS_ERROR;
Expand Down Expand Up @@ -134,8 +111,7 @@ static size_t SDLCALL physfsrwops_write(void *userdata, const void *ptr,
static bool physfsrwops_close(void *userdata)
{
PHYSFS_File *handle = (PHYSFS_File *) userdata;
if (!PHYSFS_close(handle))
{
if (!PHYSFS_close(handle)) {
SDL_SetError("PhysicsFS error: %s", getLastPhysfsError());
return false;
} /* if */
Expand Down
35 changes: 11 additions & 24 deletions src/physfsrwops.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
/*
* This code provides a glue layer between PhysicsFS and Simple Directmedia
* Layer's (SDL) RWops i/o abstraction.
* This file was originally authored by Ryan C. Gordon ([email protected]).
* It's been patched by Linus Probert ([email protected]) to work for
* SDL3 IOStreams which have replaced rwops.
*
* License: this code is public domain. I make no warranty that it is useful,
* correct, harmless, or environmentally safe.
* The file is still under no warranty or license. Do what you like with it.
*
* This particular file may be used however you like, including copying it
* verbatim into a closed-source project, exploiting it commercially, and
* removing any trace of my name from the source (although I hope you won't
* do that). I welcome enhancements and corrections to this file, but I do
* not require you to send me patches if you make changes. This code has
* NO WARRANTY.
*
* Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
* Please see LICENSE.txt in the root of the source tree.
*
* SDL 1.2 falls under the LGPL license. SDL 1.3+ is zlib, like PhysicsFS.
* You can get SDL at https://www.libsdl.org/
*
* This file was written by Ryan C. Gordon. ([email protected]).
* The original file taken from: https://github.com/icculus/physfs
*/

#ifndef _INCLUDE_PHYSFSRWOPS_H_
Expand All @@ -34,7 +21,7 @@ extern "C" {
/**
* Open a platform-independent filename for reading, and make it accessible
* via an SDL_IOStream structure. The file will be closed in PhysicsFS when the
* RWops is closed. PhysicsFS should be configured to your liking before
* IOStream is closed. PhysicsFS should be configured to your liking before
* opening files through this method.
*
* @param filename File to open in platform-independent notation.
Expand All @@ -46,7 +33,7 @@ PHYSFS_DECL SDL_IOStream *PHYSFSIO_openRead(const char *fname);
/**
* Open a platform-independent filename for writing, and make it accessible
* via an SDL_IOStream structure. The file will be closed in PhysicsFS when the
* RWops is closed. PhysicsFS should be configured to your liking before
* IOStream is closed. PhysicsFS should be configured to your liking before
* opening files through this method.
*
* @param filename File to open in platform-independent notation.
Expand All @@ -58,7 +45,7 @@ PHYSFS_DECL SDL_IOStream *PHYSFSIO_openWrite(const char *fname);
/**
* Open a platform-independent filename for appending, and make it accessible
* via an SDL_IOStream structure. The file will be closed in PhysicsFS when the
* RWops is closed. PhysicsFS should be configured to your liking before
* IOStream is closed. PhysicsFS should be configured to your liking before
* opening files through this method.
*
* @param filename File to open in platform-independent notation.
Expand All @@ -70,14 +57,14 @@ PHYSFS_DECL SDL_IOStream *PHYSFSIO_openAppend(const char *fname);
/**
* Make a SDL_IOStream from an existing PhysicsFS file handle. You should
* dispose of any references to the handle after successful creation of
* the RWops. The actual PhysicsFS handle will be destroyed when the
* RWops is closed.
* the IOStream. The actual PhysicsFS handle will be destroyed when the
* IOStream is closed.
*
* @param handle a valid PhysicsFS file handle.
* @return A valid SDL_IOStream structure on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
PHYSFS_DECL SDL_IOStream *PHYSFSIO_makeRWops(PHYSFS_File *handle);
PHYSFS_DECL SDL_IOStream *PHYSFSIO_makeIOStream(PHYSFS_File *handle);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 11ea58a

Please sign in to comment.