2004-09-06 06:22:09 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "RageFileDriverMemory.h"
|
2005-09-03 21:26:04 +00:00
|
|
|
#include "RageFile.h"
|
2004-09-06 06:22:09 +00:00
|
|
|
#include "RageUtil.h"
|
|
|
|
|
#include "RageUtil_FileDB.h"
|
2004-09-06 06:25:03 +00:00
|
|
|
#include <errno.h>
|
2004-09-06 06:22:09 +00:00
|
|
|
|
|
|
|
|
struct RageFileObjMemFile
|
|
|
|
|
{
|
|
|
|
|
RageFileObjMemFile():
|
|
|
|
|
m_iRefs(0),
|
|
|
|
|
m_Mutex("RageFileObjMemFile") { }
|
2005-12-10 08:15:44 +00:00
|
|
|
RString m_sBuf;
|
2004-09-06 06:22:09 +00:00
|
|
|
int m_iRefs;
|
|
|
|
|
RageMutex m_Mutex;
|
2004-12-10 09:10:14 +00:00
|
|
|
|
|
|
|
|
static void AddReference( RageFileObjMemFile *pFile )
|
|
|
|
|
{
|
|
|
|
|
pFile->m_Mutex.Lock();
|
|
|
|
|
++pFile->m_iRefs;
|
|
|
|
|
pFile->m_Mutex.Unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ReleaseReference( RageFileObjMemFile *pFile )
|
|
|
|
|
{
|
|
|
|
|
pFile->m_Mutex.Lock();
|
|
|
|
|
const int iRefs = --pFile->m_iRefs;
|
|
|
|
|
const bool bShouldDelete = (pFile->m_iRefs == 0);
|
|
|
|
|
pFile->m_Mutex.Unlock();
|
|
|
|
|
ASSERT( iRefs >= 0 );
|
|
|
|
|
|
|
|
|
|
if( bShouldDelete )
|
|
|
|
|
delete pFile;
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
};
|
|
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
RageFileObjMem::RageFileObjMem( RageFileObjMemFile *pFile )
|
|
|
|
|
{
|
2004-12-10 09:10:14 +00:00
|
|
|
if( pFile == NULL )
|
|
|
|
|
pFile = new RageFileObjMemFile;
|
|
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
m_pFile = pFile;
|
|
|
|
|
m_iFilePos = 0;
|
2004-12-10 09:10:14 +00:00
|
|
|
RageFileObjMemFile::AddReference( m_pFile );
|
2004-12-10 08:53:52 +00:00
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
RageFileObjMem::~RageFileObjMem()
|
|
|
|
|
{
|
2004-12-10 09:10:14 +00:00
|
|
|
RageFileObjMemFile::ReleaseReference( m_pFile );
|
2004-12-10 08:53:52 +00:00
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
int RageFileObjMem::ReadInternal( void *buffer, size_t bytes )
|
|
|
|
|
{
|
2008-07-21 10:34:43 +00:00
|
|
|
LockMut(m_pFile->m_Mutex);
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
m_iFilePos = min( m_iFilePos, GetFileSize() );
|
|
|
|
|
bytes = min( bytes, (size_t) GetFileSize() - m_iFilePos );
|
2008-07-21 10:34:43 +00:00
|
|
|
if( bytes == 0 )
|
|
|
|
|
return 0;
|
2004-12-10 08:53:52 +00:00
|
|
|
memcpy( buffer, &m_pFile->m_sBuf[m_iFilePos], bytes );
|
|
|
|
|
m_iFilePos += bytes;
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
return bytes;
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
int RageFileObjMem::WriteInternal( const void *buffer, size_t bytes )
|
|
|
|
|
{
|
|
|
|
|
m_pFile->m_Mutex.Lock();
|
2004-12-11 02:25:38 +00:00
|
|
|
m_pFile->m_sBuf.replace( m_iFilePos, bytes, (const char *) buffer, bytes );
|
2004-12-10 08:53:52 +00:00
|
|
|
m_pFile->m_Mutex.Unlock();
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
m_iFilePos += bytes;
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
int RageFileObjMem::SeekInternal( int offset )
|
|
|
|
|
{
|
|
|
|
|
m_iFilePos = clamp( offset, 0, GetFileSize() );
|
|
|
|
|
return m_iFilePos;
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 08:53:52 +00:00
|
|
|
int RageFileObjMem::GetFileSize() const
|
|
|
|
|
{
|
|
|
|
|
LockMut(m_pFile->m_Mutex);
|
|
|
|
|
return m_pFile->m_sBuf.size();
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2004-12-10 10:04:32 +00:00
|
|
|
RageFileObjMem::RageFileObjMem( const RageFileObjMem &cpy ):
|
|
|
|
|
RageFileObj( cpy )
|
2004-12-10 08:53:52 +00:00
|
|
|
{
|
2004-12-10 09:27:11 +00:00
|
|
|
m_pFile = cpy.m_pFile;
|
|
|
|
|
m_iFilePos = cpy.m_iFilePos;
|
|
|
|
|
RageFileObjMemFile::AddReference( m_pFile );
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2006-10-20 00:01:18 +00:00
|
|
|
RageFileObjMem *RageFileObjMem::Copy() const
|
2004-12-10 09:27:11 +00:00
|
|
|
{
|
|
|
|
|
RageFileObjMem *pRet = new RageFileObjMem( *this );
|
2004-12-10 08:53:52 +00:00
|
|
|
return pRet;
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2005-12-10 08:15:44 +00:00
|
|
|
const RString &RageFileObjMem::GetString() const
|
2004-12-10 09:27:11 +00:00
|
|
|
{
|
|
|
|
|
return m_pFile->m_sBuf;
|
|
|
|
|
}
|
2004-09-06 06:22:09 +00:00
|
|
|
|
2005-12-10 08:15:44 +00:00
|
|
|
void RageFileObjMem::PutString( const RString &sBuf )
|
2005-01-08 21:50:50 +00:00
|
|
|
{
|
|
|
|
|
m_pFile->m_Mutex.Lock();
|
|
|
|
|
m_pFile->m_sBuf = sBuf;
|
|
|
|
|
m_pFile->m_Mutex.Unlock();
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-06 06:22:09 +00:00
|
|
|
RageFileDriverMem::RageFileDriverMem():
|
2004-09-06 06:25:03 +00:00
|
|
|
RageFileDriver( new NullFilenameDB ),
|
|
|
|
|
m_Mutex("RageFileDriverMem")
|
2004-09-06 06:22:09 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageFileDriverMem::~RageFileDriverMem()
|
|
|
|
|
{
|
|
|
|
|
for( unsigned i = 0; i < m_Files.size(); ++i )
|
|
|
|
|
{
|
|
|
|
|
RageFileObjMemFile *pFile = m_Files[i];
|
2004-12-10 09:10:14 +00:00
|
|
|
RageFileObjMemFile::ReleaseReference( pFile );
|
2004-09-06 06:22:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-10 08:15:44 +00:00
|
|
|
RageFileBasic *RageFileDriverMem::Open( const RString &sPath, int mode, int &err )
|
2004-09-06 06:22:09 +00:00
|
|
|
{
|
|
|
|
|
LockMut(m_Mutex);
|
|
|
|
|
|
|
|
|
|
if( mode == RageFile::WRITE )
|
|
|
|
|
{
|
|
|
|
|
/* If the file exists, delete it. */
|
|
|
|
|
Remove( sPath );
|
|
|
|
|
|
|
|
|
|
RageFileObjMemFile *pFile = new RageFileObjMemFile;
|
|
|
|
|
|
2004-12-10 09:10:14 +00:00
|
|
|
/* Add one reference, representing the file in the filesystem. */
|
|
|
|
|
RageFileObjMemFile::AddReference( pFile );
|
|
|
|
|
|
2004-09-06 06:22:09 +00:00
|
|
|
m_Files.push_back( pFile );
|
|
|
|
|
FDB->AddFile( sPath, 0, 0, pFile );
|
|
|
|
|
|
2004-12-09 11:47:20 +00:00
|
|
|
return new RageFileObjMem( pFile );
|
2004-09-06 06:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageFileObjMemFile *pFile = (RageFileObjMemFile *) FDB->GetFilePriv( sPath );
|
|
|
|
|
if( pFile == NULL )
|
|
|
|
|
{
|
|
|
|
|
err = ENOENT;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-09 11:47:20 +00:00
|
|
|
return new RageFileObjMem( pFile );
|
2004-09-06 06:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
2005-12-10 08:15:44 +00:00
|
|
|
bool RageFileDriverMem::Remove( const RString &sPath )
|
2004-09-06 06:22:09 +00:00
|
|
|
{
|
|
|
|
|
LockMut(m_Mutex);
|
|
|
|
|
|
|
|
|
|
RageFileObjMemFile *pFile = (RageFileObjMemFile *) FDB->GetFilePriv( sPath );
|
|
|
|
|
if( pFile == NULL )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/* Unregister the file. */
|
|
|
|
|
FDB->DelFile( sPath );
|
|
|
|
|
vector<RageFileObjMemFile *>::iterator it = find( m_Files.begin(), m_Files.end(), pFile );
|
|
|
|
|
ASSERT( it != m_Files.end() );
|
|
|
|
|
m_Files.erase( it );
|
|
|
|
|
|
2004-12-10 09:10:14 +00:00
|
|
|
RageFileObjMemFile::ReleaseReference( pFile );
|
2004-09-06 06:22:09 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct FileDriverEntry_MEM: public FileDriverEntry
|
|
|
|
|
{
|
|
|
|
|
FileDriverEntry_MEM(): FileDriverEntry( "MEM" ) { }
|
2005-12-28 03:59:52 +00:00
|
|
|
RageFileDriver *Create( const RString &sRoot ) const { return new RageFileDriverMem(); }
|
2004-09-06 06:22:09 +00:00
|
|
|
} const g_RegisterDriver;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (c) 2004 Glenn Maynard
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|