simplify and clarify error handling

This commit is contained in:
Glenn Maynard
2004-04-28 01:23:56 +00:00
parent e8c72b91b5
commit ba00dbe76b
2 changed files with 45 additions and 13 deletions
+36 -11
View File
@@ -16,7 +16,7 @@
#include "RageSoundReader_Vorbisfile.h"
#endif
SoundReader_FileReader *SoundReader_FileReader::TryOpenFile( CString filename, CString &error, CString format )
SoundReader_FileReader *SoundReader_FileReader::TryOpenFile( CString filename, CString &error, CString format, bool &bKeepTrying )
{
SoundReader_FileReader *Sample = NULL;
@@ -46,13 +46,37 @@ SoundReader_FileReader *SoundReader_FileReader::TryOpenFile( CString filename, C
LOG->Trace( "Format %s failed: %s", format.c_str(), err.c_str() );
/* If OPEN_MATCH_BUT_FAIL, the error is important; otherwise it's probably
* just a "unknown file"-ish error. */
if( ret == OPEN_MATCH_BUT_FAIL )
/*
* The file failed to open, or failed to read. This indicates a problem that will
* affect all readers, so don't waste time trying more readers. (OPEN_IO_ERROR)
*
* Errors fall in two categories:
* OPEN_UNKNOWN_FILE_FORMAT: Data was successfully read from the file, but it's the
* wrong file format. The error message always looks like "unknown file format" or
* "Not Vorbis data"; ignore it so we always give a consistent error message, and
* continue trying other file formats.
*
* OPEN_FATAL_ERROR: Either the file was opened successfully and appears to be the
* correct format, but a fatal format-specific error was encountered that will probably
* not be fixed by using a different reader (for example, an Ogg file that doesn't
* actually contain any audio streams); or the file failed to open or read ("I/O
* error", "permission denied"), in which case all other readers will probably fail,
* too. The returned error is used, and no other formats will be tried.
*/
bKeepTrying = (ret == OPEN_MATCH_BUT_FAIL);
switch( ret )
{
if( error != "" )
error += "; ";
error += ssprintf("%s: %s", format.c_str(), err.c_str() );
case OPEN_UNKNOWN_FILE_FORMAT:
bKeepTrying = true;
error = "Unknown file format";
break;
case OPEN_FATAL_ERROR:
/* The file matched, but failed to load. We know it's this type of data;
* don't bother trying the other file types. */
bKeepTrying = false;
error = err;
break;
}
return NULL;
@@ -79,18 +103,20 @@ SoundReader *SoundReader_FileReader::OpenFile( CString filename, CString &error
error = "";
bool bKeepTrying = true;
/* If the extension matches a format, try that first. */
if( FileTypes.find(format) != FileTypes.end() )
{
SoundReader_FileReader *NewSample = TryOpenFile( filename, error, format );
SoundReader_FileReader *NewSample = TryOpenFile( filename, error, format, bKeepTrying );
if( NewSample )
return NewSample;
FileTypes.erase( format );
}
for( set<CString>::iterator it = FileTypes.begin(); it != FileTypes.end(); ++it )
for( set<CString>::iterator it = FileTypes.begin(); bKeepTrying && it != FileTypes.end(); ++it )
{
SoundReader_FileReader *NewSample = TryOpenFile( filename, error, *it );
SoundReader_FileReader *NewSample = TryOpenFile( filename, error, *it, bKeepTrying );
if( NewSample )
{
LOG->Warn("File \"%s\" is really %s", filename.c_str(), it->c_str());
@@ -98,6 +124,5 @@ SoundReader *SoundReader_FileReader::OpenFile( CString filename, CString &error
}
}
error = "Unknown file format";
return NULL;
}