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-11-30 06:20:25 +00:00
|
|
|
RageFile::RageFile(const CString& path, const char *mode)
|
|
|
|
|
{
|
|
|
|
|
mFP = NULL;
|
|
|
|
|
Open(path, mode);
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-27 07:46:36 +00:00
|
|
|
bool RageFile::Open(const CString& path, const char *mode)
|
|
|
|
|
{
|
2003-11-28 04:57:54 +00:00
|
|
|
Close();
|
2003-11-27 07:46:36 +00:00
|
|
|
mPath = path;
|
|
|
|
|
FixSlashesInPlace(mPath);
|
|
|
|
|
mFP = fopen(mPath, mode);
|
|
|
|
|
|
|
|
|
|
return mFP == NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageFile::Close()
|
|
|
|
|
{
|
|
|
|
|
if (IsOpen())
|
|
|
|
|
fclose(mFP);
|
|
|
|
|
|
|
|
|
|
mFP = NULL;
|
2003-07-22 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
2003-11-27 07:46:36 +00:00
|
|
|
CString RageFile::GetLine()
|
|
|
|
|
{
|
|
|
|
|
if (!IsOpen())
|
|
|
|
|
RageException::Throw("\"%s\" is not open.", mPath.c_str());
|
|
|
|
|
|
|
|
|
|
CString buf("");
|
|
|
|
|
char buffer[256]; // long line!
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
char *ret = fgets(buffer, 256, mFP);
|
|
|
|
|
|
|
|
|
|
buf += buffer;
|
|
|
|
|
|
|
|
|
|
if (ret == NULL)
|
|
|
|
|
break; // EOF or error
|
|
|
|
|
} while (strlen(buffer) == 255);
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t RageFile::Read(void *buffer, size_t bytes)
|
|
|
|
|
{
|
|
|
|
|
if (!IsOpen())
|
|
|
|
|
RageException::Throw("\"%s\" is not open.", mPath.c_str());
|
|
|
|
|
|
|
|
|
|
return fread(buffer, 1L, bytes, mFP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t RageFile::Write(const void *buffer, size_t bytes)
|
|
|
|
|
{
|
|
|
|
|
if (!IsOpen())
|
|
|
|
|
RageException::Throw("\"%s\" is not open.", mPath.c_str());
|
|
|
|
|
|
|
|
|
|
return fwrite(buffer, 1L, bytes, mFP);
|
|
|
|
|
}
|