use RageFile

This commit is contained in:
Glenn Maynard
2003-12-04 19:13:29 +00:00
parent 4717e7619e
commit 20c3074e5b
2 changed files with 33 additions and 41 deletions
+27 -26
View File
@@ -308,20 +308,20 @@ void Course::Save()
{ {
ASSERT( !m_bIsAutogen ); ASSERT( !m_bIsAutogen );
FILE* fp = fopen( m_sPath, "w" ); RageFile f;
if( fp == NULL ) if( !f.Open( m_sPath, RageFile::WRITE ) )
{ {
LOG->Warn( "Could not write course file '%s'.", m_sPath.c_str() ); LOG->Warn( "Could not write course file '%s': %s", m_sPath.c_str(), f.GetError().c_str() );
return; return;
} }
fprintf( fp, "#COURSE:%s;\n", m_sName.c_str() ); f.PutLine( ssprintf("#COURSE:%s;", m_sName.c_str()) );
if( m_bRepeat ) if( m_bRepeat )
fprintf( fp, "#REPEAT:YES;\n" ); f.PutLine( "#REPEAT:YES;" );
if( m_iLives != -1 ) if( m_iLives != -1 )
fprintf( fp, "#LIVES:%i;\n", m_iLives ); f.PutLine( ssprintf("#LIVES:%i;", m_iLives) );
if( m_iMeter != -1 ) if( m_iMeter != -1 )
fprintf( fp, "#METER:%i;\n", m_iMeter ); f.PutLine( ssprintf("#METER:%i;", m_iMeter) );
for( unsigned i=0; i<m_entries.size(); i++ ) for( unsigned i=0; i<m_entries.size(); i++ )
{ {
@@ -330,19 +330,21 @@ void Course::Save()
for( unsigned j = 0; j < entry.attacks.size(); ++j ) for( unsigned j = 0; j < entry.attacks.size(); ++j )
{ {
if( j == 0 ) if( j == 0 )
fprintf( fp, "#MODS:\n" ); f.PutLine( "#MODS:" );
CString line;
const Attack &a = entry.attacks[j]; const Attack &a = entry.attacks[j];
fprintf( fp, " TIME=%.2f:LEN=%.2f:MODS=%s", line += ssprintf( " TIME=%.2f:LEN=%.2f:MODS=%s",
a.fStartSecond, a.fSecsRemaining, a.sModifier.c_str() ); a.fStartSecond, a.fSecsRemaining, a.sModifier.c_str() );
if( j+1 < entry.attacks.size() ) if( j+1 < entry.attacks.size() )
fprintf( fp, ":" ); line += ":";
else else
fprintf( fp, ";" ); line += ";";
fprintf( fp, "\n" ); f.PutLine( line );
} }
CString line;
switch( entry.type ) switch( entry.type )
{ {
case COURSE_ENTRY_FIXED: case COURSE_ENTRY_FIXED:
@@ -353,44 +355,43 @@ void Course::Save()
ASSERT( !as.empty() ); ASSERT( !as.empty() );
CString sGroup = as[ as.size()-2 ]; CString sGroup = as[ as.size()-2 ];
CString sSong = as[ as.size()-1 ]; CString sSong = as[ as.size()-1 ];
fprintf( fp, "#SONG:" + sGroup + SLASH + sSong ); line += "#SONG:" + sGroup + '/' + sSong;
} }
break; break;
case COURSE_ENTRY_RANDOM: case COURSE_ENTRY_RANDOM:
fprintf( fp, "#SONG:*" ); line += "#SONG:*";
break; break;
case COURSE_ENTRY_RANDOM_WITHIN_GROUP: case COURSE_ENTRY_RANDOM_WITHIN_GROUP:
fprintf( fp, "#SONG:%s/*", entry.group_name.c_str() ); line += ssprintf( "#SONG:%s/*", entry.group_name.c_str() );
break; break;
case COURSE_ENTRY_BEST: case COURSE_ENTRY_BEST:
fprintf( fp, "#SONG:BEST%d", entry.players_index+1 ); line += ssprintf( "#SONG:BEST%d", entry.players_index+1 );
break; break;
case COURSE_ENTRY_WORST: case COURSE_ENTRY_WORST:
fprintf( fp, "#SONG:WORST%d", entry.players_index+1 ); line += ssprintf( "#SONG:WORST%d", entry.players_index+1 );
break; break;
default: default:
ASSERT(0); ASSERT(0);
} }
fprintf( fp, ":" ); line += ":";
if( entry.difficulty != DIFFICULTY_INVALID ) if( entry.difficulty != DIFFICULTY_INVALID )
fprintf( fp, "%s", DifficultyToString(entry.difficulty).c_str() ); line += DifficultyToString(entry.difficulty);
else if( entry.low_meter != -1 && entry.high_meter != -1 ) else if( entry.low_meter != -1 && entry.high_meter != -1 )
fprintf( fp, "%d..%d", entry.low_meter, entry.high_meter ); line += ssprintf( "%d..%d", entry.low_meter, entry.high_meter );
fprintf( fp, ":%s", entry.modifiers.c_str() ); line += ssprintf( ":%s", entry.modifiers.c_str() );
bool default_mystery = (entry.type == COURSE_ENTRY_RANDOM || entry.type == COURSE_ENTRY_RANDOM_WITHIN_GROUP); bool default_mystery = (entry.type == COURSE_ENTRY_RANDOM || entry.type == COURSE_ENTRY_RANDOM_WITHIN_GROUP);
if( default_mystery != entry.mystery ) if( default_mystery != entry.mystery )
{ {
if( entry.modifiers != "" ) if( entry.modifiers != "" )
fprintf( fp, "," ); line += ",";
fprintf( fp, entry.mystery? "noshowcourse":"showcourse" ); line += entry.mystery? "noshowcourse":"showcourse";
} }
fprintf( fp, ";\n" ); line += ";";
f.PutLine( line );
} }
fclose( fp );
} }
+6 -15
View File
@@ -30,16 +30,7 @@
#include "MsdFile.h" #include "MsdFile.h"
#include "RageLog.h" #include "RageLog.h"
#include "RageUtil.h" #include "RageUtil.h"
#include "RageFile.h"
#include <fcntl.h>
#if defined(WIN32)
#include <io.h>
#endif
#if !defined(O_BINARY)
#define O_BINARY 0
#endif
#include <string.h>
#include <errno.h>
void MsdFile::AddParam( char *buf, int len ) void MsdFile::AddParam( char *buf, int len )
{ {
@@ -143,22 +134,22 @@ void MsdFile::ReadBuf( char *buf, int len )
bool MsdFile::ReadFile( CString sNewPath ) bool MsdFile::ReadFile( CString sNewPath )
{ {
error = ""; error = "";
int fd;
RageFile f;
/* Open a file. */ /* Open a file. */
if( (fd = open(sNewPath, O_RDONLY|O_BINARY, 0)) == -1 ) if( !f.Open( sNewPath ) )
{ {
error = strerror(errno); error = f.GetError();
return false; return false;
} }
/* XXX: f.GetFileSizeInBytes() */
int iBufferSize = GetFileSizeInBytes(sNewPath); int iBufferSize = GetFileSizeInBytes(sNewPath);
// allocate a string to hold the file // allocate a string to hold the file
char* szFileString = new char[iBufferSize]; char* szFileString = new char[iBufferSize];
int iBytesRead = read( fd, szFileString, iBufferSize ); int iBytesRead = f.Read( szFileString, iBufferSize );
close( fd );
ASSERT( iBufferSize >= iBytesRead ); ASSERT( iBufferSize >= iBytesRead );