Refactored BlobReaders: instead of constructor + IsValid() method, a factory method is used which returns NULL if no valid BlobReader could be created. The new code does more sanity checks and leaks less objects.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@323 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
parent
4c54c0cad8
commit
ef6fb30b3c
|
@ -38,29 +38,34 @@ class PlainFileReader
|
|||
{
|
||||
HANDLE hFile;
|
||||
s64 size;
|
||||
public:
|
||||
|
||||
PlainFileReader(const char* filename)
|
||||
private:
|
||||
PlainFileReader(HANDLE hFile_)
|
||||
{
|
||||
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
|
||||
hFile = hFile_;
|
||||
DWORD size_low, size_high;
|
||||
size_low = GetFileSize(hFile, &size_high);
|
||||
size = ((u64)size_low) | ((u64)size_high << 32);
|
||||
}
|
||||
|
||||
public:
|
||||
static PlainFileReader* Create(const char* filename)
|
||||
{
|
||||
HANDLE hFile = CreateFile(
|
||||
filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL
|
||||
);
|
||||
if (hFile != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
return new PlainFileReader(nFile);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
~PlainFileReader()
|
||||
{
|
||||
if (hFile)
|
||||
{
|
||||
CloseHandle(hFile);
|
||||
hFile = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool IsValid() const {return(hFile != INVALID_HANDLE_VALUE);}
|
||||
|
||||
|
||||
u64 GetDataSize() const {return(size);}
|
||||
|
@ -99,11 +104,10 @@ class PlainFileReader
|
|||
{
|
||||
FILE* file_;
|
||||
s64 size;
|
||||
public:
|
||||
|
||||
PlainFileReader(const char* filename)
|
||||
private:
|
||||
PlainFileReader(FILE* file__)
|
||||
{
|
||||
file_ = fopen(filename, "rb");
|
||||
file_ = file__;
|
||||
#if 0
|
||||
fseek64(file_, 0, SEEK_END);
|
||||
#else
|
||||
|
@ -114,18 +118,21 @@ class PlainFileReader
|
|||
}
|
||||
|
||||
|
||||
~PlainFileReader()
|
||||
public:
|
||||
static PlainFileReader* Create(const char* filename)
|
||||
{
|
||||
FILE* file_ = fopen(filename, "rb");
|
||||
if (file_)
|
||||
{
|
||||
fclose(file_);
|
||||
return new PlainFileReader(file_);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool IsValid() const
|
||||
~PlainFileReader()
|
||||
{
|
||||
return(file_ != 0);
|
||||
fclose(file_);
|
||||
}
|
||||
|
||||
|
||||
|
@ -166,16 +173,12 @@ class CompressedBlobReader
|
|||
u64 counter;
|
||||
Common::IMappedFile* mapped_file;
|
||||
|
||||
public:
|
||||
|
||||
CompressedBlobReader(const char* filename)
|
||||
private:
|
||||
CompressedBlobReader(Common::IMappedFile* mapped_file_)
|
||||
{
|
||||
mapped_file = mapped_file_;
|
||||
counter = 0;
|
||||
mapped_file = Common::IMappedFile::CreateMappedFile();
|
||||
|
||||
if (mapped_file)
|
||||
{
|
||||
mapped_file->Open(filename);
|
||||
u8* start = mapped_file->Lock(0, sizeof(BlobHeader));
|
||||
memcpy(&header, start, sizeof(BlobHeader));
|
||||
mapped_file->Unlock(start);
|
||||
|
@ -189,18 +192,26 @@ class CompressedBlobReader
|
|||
cache_tags[i] = (u64)(s64) - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const BlobHeader& GetHeader() const
|
||||
public:
|
||||
static CompressedBlobReader* Create(const char* filename)
|
||||
{
|
||||
return(header);
|
||||
}
|
||||
|
||||
|
||||
bool IsValid() const
|
||||
Common::IMappedFile* mapped_file =
|
||||
Common::IMappedFile::CreateMappedFile();
|
||||
if (mapped_file)
|
||||
{
|
||||
return(mapped_file != 0);
|
||||
bool ok = mapped_file->Open(filename);
|
||||
if (ok)
|
||||
{
|
||||
return new CompressedBlobReader(mapped_file);
|
||||
}
|
||||
else
|
||||
{
|
||||
delete mapped_file;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -212,13 +223,14 @@ class CompressedBlobReader
|
|||
}
|
||||
|
||||
mapped_file->Unlock((u8*)block_pointers);
|
||||
|
||||
if (mapped_file)
|
||||
{
|
||||
mapped_file->Close();
|
||||
delete mapped_file;
|
||||
}
|
||||
|
||||
mapped_file = 0;
|
||||
|
||||
const BlobHeader& GetHeader() const
|
||||
{
|
||||
return(header);
|
||||
}
|
||||
|
||||
|
||||
|
@ -500,7 +512,8 @@ bool DecompressBlobToFile(const char* infile, const char* outfile,
|
|||
return(false);
|
||||
}
|
||||
|
||||
CompressedBlobReader* reader = new CompressedBlobReader(infile);
|
||||
CompressedBlobReader* reader = CompressedBlobReader::Create(infile);
|
||||
if (!reader) return false;
|
||||
|
||||
FILE* f = fopen(outfile, "wb");
|
||||
const BlobHeader& header = reader->GetHeader();
|
||||
|
@ -517,6 +530,7 @@ bool DecompressBlobToFile(const char* infile, const char* outfile,
|
|||
fwrite(buffer, header.block_size, 1, f);
|
||||
}
|
||||
|
||||
delete reader;
|
||||
delete[] buffer;
|
||||
#ifdef _WIN32
|
||||
// TODO(ector): _chsize sucks, not 64-bit safe
|
||||
|
@ -548,25 +562,9 @@ bool IsCompressedBlob(const char* filename)
|
|||
|
||||
IBlobReader* CreateBlobReader(const char* filename)
|
||||
{
|
||||
IBlobReader* reader;
|
||||
|
||||
if (IsCompressedBlob(filename))
|
||||
{
|
||||
reader = new CompressedBlobReader(filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
reader = new PlainFileReader(filename);
|
||||
}
|
||||
|
||||
if (reader->IsValid())
|
||||
{
|
||||
return(reader);
|
||||
}
|
||||
else
|
||||
{
|
||||
delete reader;
|
||||
return(0);
|
||||
}
|
||||
return IsCompressedBlob(filename)
|
||||
? static_cast<IBlobReader*>(CompressedBlobReader::Create(filename))
|
||||
: static_cast<IBlobReader*>(PlainFileReader::Create(filename));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -33,7 +33,6 @@ class IBlobReader
|
|||
virtual u64 GetRawSize() const = 0;
|
||||
virtual u64 GetDataSize() const = 0;
|
||||
virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
|
||||
virtual bool IsValid() const = 0;
|
||||
|
||||
|
||||
protected:
|
||||
|
|
Loading…
Reference in New Issue