Files
itgmania212121/src/RageFile.cpp
T

430 lines
8.6 KiB
C++
Raw Normal View History

/*
* This provides an interface to open files in RageFileManager's namespace
2004-12-11 02:25:38 +00:00
* This is just a simple RageFileBasic wrapper on top of another RageFileBasic;
* when a file is open, is acts like the underlying RageFileBasic, except that
* a few extra sanity checks are made to check file modes.
*/
2003-11-27 07:46:36 +00:00
#include "global.h"
2004-12-11 02:25:38 +00:00
#include "RageFileBasic.h"
2003-07-22 07:47:27 +00:00
#include "RageFile.h"
#include "RageUtil.h"
2003-12-04 08:25:59 +00:00
#include "RageFileDriver.h"
2003-07-22 07:47:27 +00:00
2003-12-04 08:25:59 +00:00
RageFile::RageFile()
{
2006-02-14 11:16:39 +00:00
m_File = NULL;
2003-12-04 08:25:59 +00:00
}
RageFile::RageFile( const RageFile &cpy ):
2004-12-11 02:25:38 +00:00
RageFileBasic( cpy )
2003-12-10 05:04:37 +00:00
{
2003-12-10 05:48:31 +00:00
/* This will copy the file driver, including its internal file pointer. */
m_File = cpy.m_File->Copy();
2003-12-10 05:04:37 +00:00
m_Path = cpy.m_Path;
m_Mode = cpy.m_Mode;
}
2006-10-20 00:01:18 +00:00
RageFile *RageFile::Copy() const
{
return new RageFile( *this );
}
2005-12-28 19:17:34 +00:00
RString RageFile::GetPath() const
2003-12-04 08:25:59 +00:00
{
2004-12-09 11:31:23 +00:00
if ( !IsOpen() )
2005-12-28 19:17:34 +00:00
return RString();
2003-12-04 08:25:59 +00:00
2005-12-28 19:17:34 +00:00
RString sRet = m_File->GetDisplayPath();
2004-12-09 11:31:23 +00:00
if( sRet != "" )
return sRet;
return GetRealPath();
2003-12-04 08:25:59 +00:00
}
2005-12-28 19:17:34 +00:00
bool RageFile::Open( const RString& path, int mode )
2003-11-27 07:46:36 +00:00
{
2003-12-04 08:25:59 +00:00
ASSERT( FILEMAN );
2003-12-03 23:50:53 +00:00
Close();
2003-12-04 08:25:59 +00:00
m_Path = path;
FixSlashesInPlace(m_Path);
m_Mode = mode;
2003-12-03 23:50:53 +00:00
if( (m_Mode&READ) && (m_Mode&WRITE) )
{
SetError( "Reading and writing are mutually exclusive" );
return false;
}
if( !(m_Mode&READ) && !(m_Mode&WRITE) )
{
SetError( "Neither reading nor writing specified" );
return false;
}
2003-12-04 08:25:59 +00:00
int error;
2004-12-09 11:52:49 +00:00
m_File = FILEMAN->Open( path, mode, error );
2003-12-04 08:25:59 +00:00
if( m_File == NULL )
{
SetError( strerror(error) );
return false;
}
2006-02-14 11:16:39 +00:00
return true;
2003-11-27 07:46:36 +00:00
}
void RageFile::Close()
{
if( m_File == NULL )
return;
delete m_File;
if( m_Mode & WRITE )
FILEMAN->CacheFile( m_File, m_Path );
m_File = NULL;
2003-07-22 07:47:27 +00:00
}
#define ASSERT_OPEN ASSERT_M( IsOpen(), ssprintf("\"%s\" is not open.", m_Path.c_str()) );
#define ASSERT_READ ASSERT_OPEN; ASSERT_M( !!(m_Mode&READ), ssprintf("\"%s\" is not open for reading", m_Path.c_str()) );
#define ASSERT_WRITE ASSERT_OPEN; ASSERT_M( !!(m_Mode&WRITE), ssprintf("\"%s\" is not open for writing", m_Path.c_str()) );
2005-12-28 19:17:34 +00:00
int RageFile::GetLine( RString &out )
2004-06-23 01:53:30 +00:00
{
ASSERT_READ;
return m_File->GetLine( out );
2004-06-23 01:53:30 +00:00
}
2005-12-28 19:17:34 +00:00
int RageFile::PutLine( const RString &str )
2003-11-27 07:46:36 +00:00
{
ASSERT_WRITE;
return m_File->PutLine( str );
2003-12-03 23:50:53 +00:00
}
void RageFile::EnableCRC32( bool on )
{
ASSERT_OPEN;
m_File->EnableCRC32( on );
}
2004-12-13 04:04:11 +00:00
bool RageFile::GetCRC32( uint32_t *iRet )
{
ASSERT_OPEN;
return m_File->GetCRC32( iRet );
}
bool RageFile::AtEOF() const
2003-12-03 23:50:53 +00:00
{
ASSERT_READ;
return m_File->AtEOF();
2003-12-04 08:25:59 +00:00
}
void RageFile::ClearError()
2003-12-04 04:26:49 +00:00
{
if( m_File != NULL )
m_File->ClearError();
m_sError = "";
2003-12-04 04:26:49 +00:00
}
2005-12-28 19:17:34 +00:00
RString RageFile::GetError() const
2003-12-03 23:50:53 +00:00
{
if( m_File != NULL && m_File->GetError() != "" )
return m_File->GetError();
return m_sError;
}
2003-12-03 23:50:53 +00:00
2005-12-28 19:17:34 +00:00
void RageFile::SetError( const RString &err )
{
if( m_File != NULL )
m_File->ClearError();
m_sError = err;
}
2003-12-04 08:25:59 +00:00
int RageFile::Read( void *pBuffer, size_t iBytes )
{
ASSERT_READ;
return m_File->Read( pBuffer, iBytes );
2003-11-27 07:46:36 +00:00
}
2003-12-04 08:25:59 +00:00
int RageFile::Seek( int offset )
2003-11-27 07:46:36 +00:00
{
ASSERT_READ;
return m_File->Seek( offset );
}
2003-12-04 08:25:59 +00:00
int RageFile::Tell() const
{
ASSERT_READ;
return m_File->Tell();
2003-12-04 08:25:59 +00:00
}
2004-02-17 21:42:06 +00:00
int RageFile::GetFileSize() const
2003-12-04 08:25:59 +00:00
{
ASSERT_READ;
return m_File->GetFileSize();
2003-11-27 07:46:36 +00:00
}
2003-12-03 23:50:53 +00:00
2005-12-28 19:17:34 +00:00
int RageFile::Read( RString &buffer, int bytes )
2003-12-10 05:04:37 +00:00
{
ASSERT_READ;
return m_File->Read( buffer, bytes );
2003-12-10 05:04:37 +00:00
}
2003-12-04 08:25:59 +00:00
2003-12-12 07:47:38 +00:00
int RageFile::Write( const void *buffer, size_t bytes )
{
ASSERT_WRITE;
return m_File->Write( buffer, bytes );
2003-12-12 07:47:38 +00:00
}
2003-12-04 21:10:16 +00:00
int RageFile::Write( const void *buffer, size_t bytes, int nmemb )
{
ASSERT_WRITE;
return m_File->Write( buffer, bytes, nmemb );
2003-12-04 21:10:16 +00:00
}
2003-12-12 07:47:38 +00:00
int RageFile::Flush()
{
if( !m_File )
{
SetError( "Not open" );
return -1;
}
return m_File->Flush();
2003-12-12 07:47:38 +00:00
}
2003-12-04 21:10:16 +00:00
int RageFile::Read( void *buffer, size_t bytes, int nmemb )
{
ASSERT_READ;
return m_File->Read( buffer, bytes, nmemb );
2003-12-04 21:10:16 +00:00
}
int RageFile::Seek( int offset, int whence )
{
ASSERT_READ;
return m_File->Seek( offset, whence );
2003-12-04 21:10:16 +00:00
}
2004-05-06 00:42:06 +00:00
2005-12-28 19:17:34 +00:00
void FileReading::ReadBytes( RageFileBasic &f, void *buf, int size, RString &sError )
{
if( sError.size() != 0 )
return;
int ret = f.Read( buf, size );
if( ret == -1 )
sError = f.GetError();
else if( ret < size )
sError = "Unexpected end of file";
}
2005-12-28 19:17:34 +00:00
RString FileReading::ReadString( RageFileBasic &f, int size, RString &sError )
2005-05-18 06:38:25 +00:00
{
if( sError.size() != 0 )
2005-12-28 19:17:34 +00:00
return RString();
2005-05-18 06:38:25 +00:00
2005-12-28 19:17:34 +00:00
RString sBuf;
2005-05-18 06:38:25 +00:00
int ret = f.Read( sBuf, size );
if( ret == -1 )
sError = f.GetError();
else if( ret < size )
sError = "Unexpected end of file";
return sBuf;
}
2005-12-28 19:17:34 +00:00
void FileReading::SkipBytes( RageFileBasic &f, int iBytes, RString &sError )
{
if( sError.size() != 0 )
return;
iBytes += f.Tell();
FileReading::Seek( f, iBytes, sError );
}
2005-12-28 19:17:34 +00:00
void FileReading::Seek( RageFileBasic &f, int iOffset, RString &sError )
{
if( sError.size() != 0 )
return;
int iGot = f.Seek( iOffset );
if( iGot == iOffset )
return;
if( iGot == -1 )
sError = f.GetError();
else if( iGot < iOffset )
sError = "Unexpected end of file";
}
2005-12-28 19:17:34 +00:00
uint8_t FileReading::read_8( RageFileBasic &f, RString &sError )
{
uint8_t val;
ReadBytes( f, &val, sizeof(uint8_t), sError );
if( sError.size() == 0 )
return val;
else
return 0;
}
2005-12-28 19:17:34 +00:00
uint16_t FileReading::read_u16_le( RageFileBasic &f, RString &sError )
{
uint16_t val;
ReadBytes( f, &val, sizeof(uint16_t), sError );
if( sError.size() == 0 )
return Swap16LE( val );
else
return 0;
}
2005-12-28 19:17:34 +00:00
int16_t FileReading::read_16_le( RageFileBasic &f, RString &sError )
{
int16_t val;
ReadBytes( f, &val, sizeof(int16_t), sError );
if( sError.size() == 0 )
return Swap16LE( val );
else
return 0;
}
2005-12-28 19:17:34 +00:00
uint32_t FileReading::read_u32_le( RageFileBasic &f, RString &sError )
{
uint32_t val;
ReadBytes( f, &val, sizeof(uint32_t), sError );
if( sError.size() == 0 )
return Swap32LE( val );
else
return 0;
}
2005-12-28 19:17:34 +00:00
int32_t FileReading::read_32_le( RageFileBasic &f, RString &sError )
{
int32_t val;
ReadBytes( f, &val, sizeof(int32_t), sError );
if( sError.size() == 0 )
return Swap32LE( val );
else
return 0;
}
2007-03-11 21:24:50 +00:00
// lua start
#include "LuaBinding.h"
class LunaRageFile: public Luna<RageFile>
{
public:
static int destroy( T* p, lua_State *L )
{
SAFE_DELETE(p);
return 1;
}
static int Open( T* p, lua_State *L )
{
lua_pushboolean( L, p->Open( SArg(1), IArg(2) ) );
return 1;
}
static int Close( T* p, lua_State *L )
{
p->Close();
return 1;
}
static int Write( T* p, lua_State *L )
{
lua_pushinteger( L, p->Write( SArg(1) ) );
return 1;
}
static int Read( T* p, lua_State *L )
{
RString string;
p->Read(string);
lua_pushstring( L, string );
return 1;
}
static int Seek( T* p, lua_State *L )
{
lua_pushinteger( L, p->Seek( IArg(1) ) );
return 1;
}
2007-03-11 21:24:50 +00:00
static int GetLine( T* p, lua_State *L )
{
RString string;
p->GetLine(string);
lua_pushstring( L, string );
return 1;
}
static int PutLine( T* p, lua_State *L )
{
lua_pushinteger( L, p->PutLine( SArg(1) ) );
return 1;
}
static int GetError( T* p, lua_State *L )
{
lua_pushstring( L, p->GetError() );
return 1;
}
2007-03-11 21:24:50 +00:00
LunaRageFile()
{
ADD_METHOD( Open );
ADD_METHOD( Close );
ADD_METHOD( Write );
ADD_METHOD( Read );
ADD_METHOD( Seek );
2007-03-11 21:24:50 +00:00
ADD_METHOD( GetLine );
ADD_METHOD( PutLine );
ADD_METHOD( GetError );
2007-03-11 21:24:50 +00:00
ADD_METHOD( destroy );
}
};
LUA_REGISTER_CLASS( RageFile )
namespace RageFileUtil
{
int CreateRageFile( lua_State *L )
{
RageFile *pFile = new RageFile;
pFile->PushSelf( L );
return 1;
}
const luaL_Reg RageFileUtilTable[] =
{
LIST_METHOD( CreateRageFile ),
{ NULL, NULL }
};
LUA_REGISTER_NAMESPACE( RageFileUtil );
}
2004-05-06 00:42:06 +00:00
/*
* Copyright (c) 2003-2004 Glenn Maynard, Chris Danford
2004-05-06 00:42:06 +00:00
* 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.
*/