[physfs] file class for PhysicsFS in C++

Amir Ramezani amir.ramezani1370 at gmail.com
Sat Mar 19 09:42:22 EDT 2016


hello,
i'm currently working on a file class for SoLoud
<http://soloud-audio.com> (which is based on physics fs), in order to
be able to load my sound-data from archives
but, when i'm using it, no crash occurs, and i don't here any sound
and i want to fix it
this is the code:
soloud_physfs.h
/*
SoLoud audio engine
Copyright (c) 2013-2015 Jari Komppa

file class for PhysicsFS filesystem
Copyright (se) 2016 amir ramezani

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
   distribution.
*/

 #ifndef SOLOUD_PHYSFS_H
#define SOLOUD_PHYSFS_H

#include <physfs.h>
#include "soloud_file.h"

namespace SoLoud
{

//this class is used for reading from and writing into physics FS files
class PhysfsFile: public File
{
PHYSFS_File *mFileHandle;
public:

virtual int eof();
virtual unsigned int read(unsigned char *aDst, unsigned int aBytes);
virtual unsigned int length();
virtual void seek(int aOffset);
virtual unsigned int pos();
virtual ~PhysfsFile();
PhysfsFile();
PhysfsFile(const char* aFilename);
PhysfsFile(PHYSFS_File *fp);
result open(const char *aFilename);
virtual PHYSFS_File *getPhysfsFilePtr();
};
};

#endif


soloud_physfs.cpp:
/*
SoLoud audio engine
Copyright (c) 2013-2015 Jari Komppa

file class for physicsFS filesystem
Copyright (se) 2016 amir ramezani

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
   distribution.
*/

#include "soloud_error.h"
#include "soloud_physfs.h"

namespace SoLoud
{

PhysfsFile::PhysfsFile()
{
mFileHandle=0;
}

PhysfsFile::PhysfsFile(const char* aFilename)
{
open(aFilename);
}

PhysfsFile::PhysfsFile(PHYSFS_File *fp):
mFileHandle(fp)
{

}

int PhysfsFile::eof()
{
return PHYSFS_eof(mFileHandle); //weather we received at the end of the file
}

unsigned int PhysfsFile::read(unsigned char *aDst, unsigned int aBytes)
{
return (unsigned int) PHYSFS_read(mFileHandle, aDst, aBytes, 1);
}

unsigned int PhysfsFile::length()
{
int pos=PHYSFS_tell(mFileHandle); //determine our position in the file
PHYSFS_seek(mFileHandle, PHYSFS_fileLength(mFileHandle)+pos); //go to
the end of file
int len=PHYSFS_tell(mFileHandle); //get the length of file
PHYSFS_seek(mFileHandle, len-pos); //go back to our previous position
in the file
return len;
}

void PhysfsFile::seek(int aOffset)
{
PHYSFS_seek(mFileHandle, aOffset); //move to aOffset
}

unsigned int PhysfsFile::pos()
{
return PHYSFS_tell(mFileHandle); //get the position of the file
}

PhysfsFile::~PhysfsFile()
{
if(mFileHandle)
{
PHYSFS_close(mFileHandle); //close it
}
}

result PhysfsFile::open(const char *aFilename)
{
if(PHYSFS_exists(aFilename)==0)
{
return FILE_NOT_FOUND;
}
mFileHandle=PHYSFS_openRead(aFilename);
if(!mFileHandle)
{
return FILE_LOAD_FAILED;
}
return SO_NO_ERROR;
}

PHYSFS_File *PhysfsFile::getPhysfsFilePtr()
{
return mFileHandle;
}

}

i don't know where the problem is, and i've read and checked my code
thanks


More information about the physfs mailing list