Files
itgmania212121/stepmania/src/RageFile.cpp
T

179 lines
3.3 KiB
C++
Raw Normal View History

2003-07-22 07:47:27 +00:00
#include "global.h"
/*
2003-11-27 07:46:36 +00:00
-----------------------------------------------------------------------------
2003-07-22 07:47:27 +00:00
Class: RageFile
2003-11-27 07:46:36 +00:00
2003-07-22 07:47:27 +00:00
Desc: See header.
2003-11-27 07:46:36 +00:00
2003-07-22 07:47:27 +00:00
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
Chris Danford
2003-11-27 07:46:36 +00:00
Steve Checkoway
-----------------------------------------------------------------------------
*/
2003-07-22 07:47:27 +00:00
2003-11-27 07:46:36 +00:00
#include "global.h"
2003-07-22 07:47:27 +00:00
#include "RageFile.h"
#include "RageUtil.h"
void FixSlashesInPlace( CString &sPath )
{
2003-11-27 07:46:36 +00:00
sPath.Replace( "/", SLASH );
sPath.Replace( "\\", SLASH );
2003-07-22 07:47:27 +00:00
}
CString FixSlashes( CString sPath )
{
2003-11-27 07:46:36 +00:00
sPath.Replace( "/", SLASH );
sPath.Replace( "\\", SLASH );
return sPath;
2003-07-22 07:47:27 +00:00
}
void CollapsePath( CString &sPath )
{
2003-11-27 07:46:36 +00:00
CStringArray as;
split( sPath, SLASH, as );
for( unsigned i=0; i<as.size(); i++ )
{
if( as[i] == ".." )
2003-07-22 07:47:27 +00:00
{
2003-11-27 07:46:36 +00:00
as.erase( as.begin()+i-1 );
as.erase( as.begin()+i-1 );
i -= 2;
2003-07-22 07:47:27 +00:00
}
2003-11-27 07:46:36 +00:00
}
sPath = join( SLASH, as );
}
2003-12-03 23:50:53 +00:00
RageFile::RageFile( const CString& path, RageFile::OpenMode mode )
{
mFP = NULL;
2003-12-03 23:50:53 +00:00
m_BufUsed = 0;
Open(path, mode);
}
2003-12-03 23:50:53 +00:00
bool RageFile::Open( const CString& path, RageFile::OpenMode mode )
2003-11-27 07:46:36 +00:00
{
2003-12-03 23:50:53 +00:00
Close();
mPath = path;
FixSlashesInPlace(mPath);
mFP = fopen( mPath, mode == READ? "r":"w" );
ResetBuf();
2003-11-27 07:46:36 +00:00
return mFP == NULL;
}
void RageFile::Close()
{
2003-12-03 23:50:53 +00:00
if (IsOpen())
fclose(mFP);
2003-11-27 07:46:36 +00:00
2003-12-03 23:50:53 +00:00
mFP = NULL;
2003-07-22 07:47:27 +00:00
}
2003-12-03 23:50:53 +00:00
void RageFile::ResetBuf()
2003-11-27 07:46:36 +00:00
{
2003-12-03 23:50:53 +00:00
m_BufUsed = 0;
m_pBuf = m_Buffer;
}
/* Read up to the next \n, and return it in out. Strip the \n. If the \n is
* preceded by a \r (DOS newline), strip that, too. */
void RageFile::GetLine( CString &out )
{
out = "";
2003-11-27 07:46:36 +00:00
if (!IsOpen())
RageException::Throw("\"%s\" is not open.", mPath.c_str());
2003-12-03 23:50:53 +00:00
while( 1 )
{
bool done = false;
/* Find the end of the block we'll move to out. */
char *p = (char *) memchr( m_pBuf, '\n', m_BufUsed );
if( p == NULL )
p = m_pBuf+m_BufUsed; /* everything */
else
done = true;
if( p >= m_pBuf )
{
char *RealEnd = p;
if( done && p > m_pBuf && p[-1] == '\r' )
--RealEnd; /* not including \r */
out.append( m_pBuf, RealEnd );
if( done )
++p; /* skip \n */
m_BufUsed -= p-m_pBuf;
m_pBuf = p;
}
if( done )
break;
/* We need more data. That implies that we moved everything. */
ASSERT( !m_BufUsed );
m_pBuf = m_Buffer;
size_t size = Read( m_pBuf, sizeof(m_Buffer) );
if( size <= 0 )
break; // EOF or error
m_BufUsed += size;
}
}
2003-12-04 04:26:49 +00:00
#if defined(_WIN32)
#define NEWLINE "\r\n"
#else
#define NEWLINE "\n"
#endif
int RageFile::PutLine( const CString &str )
{
if( Write(str.data(), str.size()) == -1 )
return -1;
return Write( NEWLINE, strlen(NEWLINE) );
}
2003-12-03 23:50:53 +00:00
int RageFile::Read( void *buffer, size_t bytes )
{
if( !IsOpen() )
RageException::Throw("\"%s\" is not open.", mPath.c_str());
int ret = 0;
int FromBuffer = min( (int) bytes, m_BufUsed );
memcpy( buffer, m_pBuf, FromBuffer );
ret += FromBuffer;
buffer = (char *) buffer + FromBuffer;
bytes -= FromBuffer;
2003-11-27 07:46:36 +00:00
2003-12-03 23:50:53 +00:00
if( bytes )
{
int FromFile = fread( buffer, 1L, bytes, mFP );
if( FromFile < 0 )
return -1;
ret += FromFile;
}
return ret;
2003-11-27 07:46:36 +00:00
}
2003-12-03 23:50:53 +00:00
int RageFile::Write(const void *buffer, size_t bytes)
2003-11-27 07:46:36 +00:00
{
2003-12-03 23:50:53 +00:00
if (!IsOpen())
RageException::Throw("\"%s\" is not open.", mPath.c_str());
return fwrite(buffer, 1L, bytes, mFP);
2003-11-27 07:46:36 +00:00
}
2003-12-03 23:50:53 +00:00
CString RageFile::GetLine()
2003-11-27 07:46:36 +00:00
{
2003-12-03 23:50:53 +00:00
CString ret;
GetLine( ret );
return ret;
2003-11-27 07:46:36 +00:00
}
2003-12-03 23:50:53 +00:00