Wrapped fopen/close/read/write functions inside a simple "IOFile" class. Reading, writing, and error checking became simpler in most cases. It should be near impossible to forget to close a file now that the destructor takes care of it. (I hope this fixes Issue 3635) I have tested the functionality of most things, but it is possible I broke something. :p

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7328 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak
2011-03-11 10:21:46 +00:00
parent 4f69672b2b
commit 59fd1008ca
68 changed files with 1112 additions and 1154 deletions
+10 -15
View File
@@ -23,7 +23,6 @@ enum {BUF_SIZE = 32*1024};
WaveFileWriter::WaveFileWriter()
{
file = NULL;
conv_buffer = 0;
skip_silence = false;
}
@@ -46,7 +45,7 @@ bool WaveFileWriter::Start(const char *filename, unsigned int HLESampleRate)
return false;
}
file = fopen(filename, "wb");
file.Open(filename, "wb");
if (!file)
{
PanicAlertT("The file %s could not be opened for writing. Please check if it's already opened by another program.", filename);
@@ -73,36 +72,32 @@ bool WaveFileWriter::Start(const char *filename, unsigned int HLESampleRate)
Write(100 * 1000 * 1000 - 32);
// We are now at offset 44
if (ftello(file) != 44)
PanicAlert("wrong offset: %lld", (long long)ftello(file));
if (file.Tell() != 44)
PanicAlert("wrong offset: %lld", (long long)file.Tell());
return true;
}
void WaveFileWriter::Stop()
{
if (!file)
return;
// u32 file_size = (u32)ftello(file);
fseeko(file, 4, SEEK_SET);
file.Seek(4, SEEK_SET);
Write(audio_size + 36);
fseeko(file, 40, SEEK_SET);
file.Seek(40, SEEK_SET);
Write(audio_size);
fclose(file);
file = 0;
file.Close();
}
void WaveFileWriter::Write(u32 value)
{
fwrite(&value, 4, 1, file);
file.WriteArray(&value, 1);
}
void WaveFileWriter::Write4(const char *ptr)
{
fwrite(ptr, 4, 1, file);
file.WriteBytes(ptr, 4);
}
void WaveFileWriter::AddStereoSamples(const short *sample_data, int count)
@@ -115,7 +110,7 @@ void WaveFileWriter::AddStereoSamples(const short *sample_data, int count)
if (sample_data[i]) all_zero = false;
if (all_zero) return;
}
fwrite(sample_data, count * 4, 1, file);
file.WriteBytes(sample_data, count * 4);
audio_size += count * 4;
}
@@ -140,6 +135,6 @@ void WaveFileWriter::AddStereoSamplesBE(const short *sample_data, int count)
for (int i = 0; i < count * 2; i++)
conv_buffer[i] = Common::swap16((u16)sample_data[i]);
fwrite(conv_buffer, count * 4, 1, file);
file.WriteBytes(conv_buffer, count * 4);
audio_size += count * 4;
}
+4 -2
View File
@@ -28,17 +28,19 @@
#ifndef _WAVEFILE_H_
#define _WAVEFILE_H_
#include <stdio.h>
#include "FileUtil.h"
class WaveFileWriter
{
FILE *file;
File::IOFile file;
bool skip_silence;
u32 audio_size;
short *conv_buffer;
void Write(u32 value);
void Write4(const char *ptr);
WaveFileWriter& operator=(const WaveFileWriter&)/* = delete*/;
public:
WaveFileWriter();
~WaveFileWriter();
+26 -37
View File
@@ -27,8 +27,6 @@
// - Zero backwards/forwards compatibility
// - Serialization code for anything complex has to be manually written.
#include <stdio.h>
#include <map>
#include <vector>
#include <string>
@@ -189,63 +187,58 @@ public:
return false;
// Check file size
u64 fileSize = File::GetSize(_rFilename);
u64 headerSize = sizeof(SChunkHeader);
if (fileSize < headerSize) {
const u64 fileSize = File::GetSize(_rFilename);
static const u64 headerSize = sizeof(SChunkHeader);
if (fileSize < headerSize)
{
ERROR_LOG(COMMON,"ChunkReader: File too small");
return false;
}
FILE* pFile = fopen(_rFilename.c_str(), "rb");
if (!pFile) {
File::IOFile pFile(_rFilename, "rb");
if (!pFile)
{
ERROR_LOG(COMMON,"ChunkReader: Can't open file for reading");
return false;
}
// read the header
SChunkHeader header;
if (fread(&header, 1, headerSize, pFile) != headerSize) {
fclose(pFile);
if (!pFile.ReadArray(&header, 1))
{
ERROR_LOG(COMMON,"ChunkReader: Bad header size");
return false;
}
// Check revision
if (header.Revision != _Revision) {
fclose(pFile);
if (header.Revision != _Revision)
{
ERROR_LOG(COMMON,"ChunkReader: Wrong file revision, got %d expected %d",
header.Revision, _Revision);
header.Revision, _Revision);
return false;
}
// get size
int sz = (int)(fileSize - headerSize);
if (header.ExpectedSize != sz) {
fclose(pFile);
const int sz = (int)(fileSize - headerSize);
if (header.ExpectedSize != sz)
{
ERROR_LOG(COMMON,"ChunkReader: Bad file size, got %d expected %d",
sz, header.ExpectedSize);
sz, header.ExpectedSize);
return false;
}
// read the state
u8* buffer = new u8[sz];
if ((int)fread(buffer, 1, sz, pFile) != sz) {
fclose(pFile);
if (!pFile.ReadBytes(buffer, sz))
{
ERROR_LOG(COMMON,"ChunkReader: Error reading file");
return false;
}
// done reading
if (fclose(pFile)) {
ERROR_LOG(COMMON,"ChunkReader: Error closing file! might be corrupted!");
// should never happen!
return false;
}
u8 *ptr = buffer;
PointerWrap p(&ptr, PointerWrap::MODE_READ);
_class.DoState(p);
delete [] buffer;
delete[] buffer;
INFO_LOG(COMMON, "ChunkReader: Done loading %s" , _rFilename.c_str());
return true;
@@ -255,11 +248,10 @@ public:
template<class T>
static bool Save(const std::string& _rFilename, int _Revision, T& _class)
{
FILE* pFile;
INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str());
pFile = fopen(_rFilename.c_str(), "wb");
if (!pFile) {
File::IOFile pFile(_rFilename, "wb");
if (!pFile)
{
ERROR_LOG(COMMON,"ChunkReader: Error opening file for write");
return false;
}
@@ -281,20 +273,17 @@ public:
header.ExpectedSize = (int)sz;
// Write to file
if (fwrite(&header, sizeof(SChunkHeader), 1, pFile) != 1) {
if (!pFile.WriteArray(&header, 1))
{
ERROR_LOG(COMMON,"ChunkReader: Failed writing header");
return false;
}
if (fwrite(buffer, sz, 1, pFile) != 1) {
if (!pFile.WriteBytes(buffer, sz))
{
ERROR_LOG(COMMON,"ChunkReader: Failed writing data");
return false;
}
if (fclose(pFile)) {
ERROR_LOG(COMMON,"ChunkReader: Error closing file! might be corrupted!");
return false;
}
INFO_LOG(COMMON,"ChunkReader: Done writing %s",
_rFilename.c_str());
+104
View File
@@ -700,4 +700,108 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str)
return true;
}
IOFile::IOFile()
: m_file(NULL), m_good(true)
{}
IOFile::IOFile(std::FILE* file)
: m_file(file), m_good(true)
{}
IOFile::IOFile(const std::string& filename, const char openmode[])
: m_file(NULL), m_good(true)
{
Open(filename, openmode);
}
IOFile::~IOFile()
{
Close();
}
bool IOFile::Open(const std::string& filename, const char openmode[])
{
Close();
#ifdef _WIN32
fopen_s(&m_file, filename.c_str(), openmode);
#else
m_file = fopen(filename.c_str(), openmode);
#endif
m_good = IsOpen();
return m_good;
}
bool IOFile::Close()
{
if (!IsOpen() || 0 != std::fclose(m_file))
m_good = false;
m_file = NULL;
return m_good;
}
std::FILE* IOFile::ReleaseHandle()
{
std::FILE* const ret = m_file;
m_file = NULL;
return ret;
}
void IOFile::SetHandle(std::FILE* file)
{
Close();
Clear();
m_file = file;
}
u64 IOFile::GetSize()
{
if (IsOpen())
return File::GetSize(m_file);
else
return 0;
}
bool IOFile::Seek(s64 off, int origin)
{
if (!IsOpen() || 0 != fseeko(m_file, off, origin))
m_good = false;
return m_good;
}
u64 IOFile::Tell()
{
if (IsOpen())
return ftello(m_file);
else
return -1;
}
bool IOFile::Flush()
{
if (!IsOpen() || 0 != std::fflush(m_file))
m_good = false;
return m_good;
}
bool IOFile::Resize(u64 size)
{
if (!IsOpen() || 0 !=
#ifdef _WIN32
// ector: _chsize sucks, not 64-bit safe
// F|RES: changed to _chsize_s. i think it is 64-bit safe
_chsize_s(_fileno(m_file), size)
#else
// TODO: handle 64bit and growing
ftruncate(fileno(m_file), size)
#endif
)
m_good = false;
return m_good;
}
} // namespace
+73
View File
@@ -18,6 +18,8 @@
#ifndef _FILEUTIL_H_
#define _FILEUTIL_H_
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <string.h>
@@ -140,6 +142,77 @@ std::string GetBundleDirectory();
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
bool ReadFileToString(bool text_file, const char *filename, std::string &str);
// simple wrapper for cstdlib file functions to
// hopefully will make error checking easier
// and make forgetting an fclose() harder
class IOFile : NonCopyable
{
public:
IOFile();
IOFile(std::FILE* file);
IOFile(const std::string& filename, const char openmode[]);
~IOFile();
bool Open(const std::string& filename, const char openmode[]);
bool Close();
template <typename T>
bool ReadArray(T* data, size_t length)
{
if (!IsOpen() || length != std::fread(data, sizeof(T), length, m_file))
m_good = false;
return m_good;
}
template <typename T>
bool WriteArray(const T* data, size_t length)
{
if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file))
m_good = false;
return m_good;
}
bool ReadBytes(void* data, size_t length)
{
return ReadArray(reinterpret_cast<char*>(data), length);
}
bool WriteBytes(const void* data, size_t length)
{
return WriteArray(reinterpret_cast<const char*>(data), length);
}
bool IsOpen() { return NULL != m_file; }
// m_good is set to false when a read, write or other function fails
bool IsGood() { return m_good; }
operator void*() { return m_good ? m_file : NULL; }
std::FILE* ReleaseHandle();
std::FILE* GetHandle() { return m_file; }
void SetHandle(std::FILE* file);
bool Seek(s64 off, int origin);
u64 Tell();
u64 GetSize();
bool Resize(u64 size);
bool Flush();
// clear error state
void Clear() { m_good = true; std::clearerr(m_file); }
private:
IOFile& operator=(const IOFile&) /*= delete*/;
std::FILE* m_file;
bool m_good;
};
} // namespace
#endif
+7 -12
View File
@@ -83,7 +83,7 @@ LogManager::LogManager()
m_Log[LogTypes::MEMCARD_MANAGER] = new LogContainer("MemCard Manager", "MemCard Manager");
m_Log[LogTypes::NETPLAY] = new LogContainer("NETPLAY", "Netplay");
m_fileLog = new FileLogListener(File::GetUserPath(F_MAINLOG_IDX));
m_fileLog = new FileLogListener(File::GetUserPath(F_MAINLOG_IDX).c_str());
m_consoleLog = new ConsoleListener();
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) {
@@ -176,21 +176,16 @@ void LogContainer::trigger(LogTypes::LOG_LEVELS level, const char *msg) {
}
}
FileLogListener::FileLogListener(std::string filename) {
m_filename = filename;
m_logfile = fopen(filename.c_str(), "a+");
FileLogListener::FileLogListener(const char *filename)
{
m_logfile.open(filename, std::ios::app);
setEnable(true);
}
FileLogListener::~FileLogListener() {
if (m_logfile)
fclose(m_logfile);
}
void FileLogListener::Log(LogTypes::LOG_LEVELS, const char *msg) {
void FileLogListener::Log(LogTypes::LOG_LEVELS, const char *msg)
{
if (!m_enable || !isValid())
return;
fwrite(msg, strlen(msg) * sizeof(char), 1, m_logfile);
fflush(m_logfile);
m_logfile << msg << std::flush;
}
+3 -5
View File
@@ -21,10 +21,10 @@
#include "Log.h"
#include "StringUtil.h"
#include "Thread.h"
#include "FileUtil.h"
#include <vector>
#include <string.h>
#include <stdio.h>
#define MAX_MESSAGES 8000
#define MAX_MSGLEN 1024
@@ -40,8 +40,7 @@ public:
class FileLogListener : public LogListener {
public:
FileLogListener(std::string filename);
~FileLogListener();
FileLogListener(const char *filename);
void Log(LogTypes::LOG_LEVELS, const char *msg);
@@ -60,8 +59,7 @@ public:
const char *getName() const { return "file"; }
private:
std::string m_filename;
FILE *m_logfile;
std::ofstream m_logfile;
bool m_enable;
};
+12 -23
View File
@@ -52,20 +52,14 @@ std::string CreateTitleContentPath(u64 _titleID)
bool CheckTitleTMD(u64 _titleID)
{
std::string TitlePath;
TitlePath = CreateTitleContentPath(_titleID) + "/title.tmd";
const std::string TitlePath = CreateTitleContentPath(_titleID) + "/title.tmd";
if (File::Exists(TitlePath))
{
FILE* pTMDFile = fopen(TitlePath.c_str(), "rb");
if(pTMDFile)
{
u64 TitleID = 0xDEADBEEFDEADBEEFULL;
fseeko(pTMDFile, 0x18C, SEEK_SET);
fread(&TitleID, 8, 1, pTMDFile);
fclose(pTMDFile);
if (_titleID == Common::swap64(TitleID))
return true;
}
File::IOFile pTMDFile(TitlePath, "rb");
u64 TitleID = 0;
pTMDFile.Seek(0x18C, SEEK_SET);
if (pTMDFile.ReadArray(&TitleID, 1) && _titleID == Common::swap64(TitleID))
return true;
}
INFO_LOG(DISCIO, "Invalid or no tmd for title %08x %08x", (u32)(_titleID >> 32), (u32)(_titleID & 0xFFFFFFFF));
return false;
@@ -73,19 +67,14 @@ bool CheckTitleTMD(u64 _titleID)
bool CheckTitleTIK(u64 _titleID)
{
std::string TikPath = Common::CreateTicketFileName(_titleID);
const std::string TikPath = Common::CreateTicketFileName(_titleID);
if (File::Exists(TikPath))
{
FILE* pTIKFile = fopen(TikPath.c_str(), "rb");
if(pTIKFile)
{
u64 TitleID = 0xDEADBEEFDEADBEEFULL;
fseeko(pTIKFile, 0x1dC, SEEK_SET);
fread(&TitleID, 8, 1, pTIKFile);
fclose(pTIKFile);
if (_titleID == Common::swap64(TitleID))
return true;
}
File::IOFile pTIKFile(TikPath, "rb");
u64 TitleID = 0;
pTIKFile.Seek(0x1dC, SEEK_SET);
if (pTIKFile.ReadArray(&TitleID, 1) && _titleID == Common::swap64(TitleID))
return true;
}
INFO_LOG(DISCIO, "Invalid or no tik for title %08x %08x", (u32)(_titleID >> 32), (u32)(_titleID & 0xFFFFFFFF));
return false;
+66 -54
View File
@@ -57,29 +57,32 @@ bool SysConf::LoadFromFile(const char *filename)
else
return false;
}
FILE* f = fopen(filename, "rb");
if (f == NULL)
return false;
bool result = LoadFromFileInternal(f);
if (result)
m_Filename = filename;
fclose(f);
return result;
File::IOFile f(filename, "rb");
if (f.IsOpen())
{
if (LoadFromFileInternal(f.ReleaseHandle()))
{
m_Filename = filename;
return true;
}
}
return false;
}
bool SysConf::LoadFromFileInternal(FILE *f)
bool SysConf::LoadFromFileInternal(File::IOFile f)
{
// Fill in infos
SSysConfHeader s_Header;
if (fread(&s_Header.version, sizeof(s_Header.version), 1, f) != 1) return false;
if (fread(&s_Header.numEntries, sizeof(s_Header.numEntries), 1, f) != 1) return false;
f.ReadArray(s_Header.version, 4);
f.ReadArray(&s_Header.numEntries, 1);
s_Header.numEntries = Common::swap16(s_Header.numEntries) + 1;
for (u16 index = 0; index < s_Header.numEntries; index++)
{
SSysConfEntry tmpEntry;
if (fread(&tmpEntry.offset, sizeof(tmpEntry.offset), 1, f) != 1) return false;
f.ReadArray(&tmpEntry.offset, 1);
tmpEntry.offset = Common::swap16(tmpEntry.offset);
m_Entries.push_back(tmpEntry);
}
@@ -89,52 +92,62 @@ bool SysConf::LoadFromFileInternal(FILE *f)
i < m_Entries.end() - 1; i++)
{
SSysConfEntry& curEntry = *i;
if (fseeko(f, curEntry.offset, SEEK_SET) != 0) return false;
f.Seek(curEntry.offset, SEEK_SET);
u8 description = 0;
if (fread(&description, sizeof(description), 1, f) != 1) return false;
f.ReadArray(&description, 1);
// Data type
curEntry.type = (SysconfType)((description & 0xe0) >> 5);
// Length of name in bytes - 1
curEntry.nameLength = (description & 0x1f) + 1;
// Name
if (fread(&curEntry.name, curEntry.nameLength, 1, f) != 1) return false;
f.ReadArray(curEntry.name, curEntry.nameLength);
curEntry.name[curEntry.nameLength] = '\0';
// Get length of data
curEntry.dataLength = 0;
switch (curEntry.type)
{
case Type_BigArray:
if (fread(&curEntry.dataLength, 2, 1, f) != 1) return false;
f.ReadArray(&curEntry.dataLength, 1);
curEntry.dataLength = Common::swap16(curEntry.dataLength);
break;
case Type_SmallArray:
if (fread(&curEntry.dataLength, 1, 1, f) != 1) return false;
{
u8 dlength = 0;
f.ReadBytes(&dlength, 1);
curEntry.dataLength = dlength;
break;
}
case Type_Byte:
case Type_Bool:
curEntry.dataLength = 1;
break;
case Type_Short:
curEntry.dataLength = 2;
break;
case Type_Long:
curEntry.dataLength = 4;
break;
default:
PanicAlertT("Unknown entry type %i in SYSCONF (%s@%x)!",
curEntry.type, curEntry.name, curEntry.offset);
return false;
break;
}
// Fill in the actual data
if (curEntry.dataLength)
{
curEntry.data = new u8[curEntry.dataLength];
if (fread(curEntry.data, curEntry.dataLength, 1, f) != 1) return false;
f.ReadArray(curEntry.data, curEntry.dataLength);
}
}
return true;
return f.IsGood();
}
// Returns the size of the item in file
@@ -294,88 +307,87 @@ void SysConf::GenerateSysConf()
m_Entries.push_back(items[i]);
File::CreateFullPath(m_FilenameDefault);
FILE *g = fopen(m_FilenameDefault.c_str(), "wb");
File::IOFile g(m_FilenameDefault, "wb");
// Write the header and item offsets
fwrite(&s_Header.version, sizeof(s_Header.version), 1, g);
fwrite(&s_Header.numEntries, sizeof(u16), 1, g);
for (int i = 0; i < 27; ++i)
g.WriteArray(&s_Header.version, 1);
g.WriteArray(&s_Header.numEntries, 1);
for (int i = 0; i != 27; ++i)
{
u16 tmp_offset = Common::swap16(items[i].offset);
fwrite(&tmp_offset, 2, 1, g);
const u16 tmp_offset = Common::swap16(items[i].offset);
g.WriteArray(&tmp_offset, 1);
}
const u16 end_data_offset = Common::swap16(current_offset);
fwrite(&end_data_offset, 2, 1, g);
g.WriteArray(&end_data_offset, 1);
// Write the items
const u8 null_byte = 0;
for (int i = 0; i < 27; ++i)
for (int i = 0; i != 27; ++i)
{
u8 description = (items[i].type << 5) | (items[i].nameLength - 1);
fwrite(&description, sizeof(description), 1, g);
fwrite(&items[i].name, items[i].nameLength, 1, g);
g.WriteArray(&description, 1);
g.WriteArray(&items[i].name, items[i].nameLength);
switch (items[i].type)
{
case Type_BigArray:
{
u16 tmpDataLength = Common::swap16(items[i].dataLength);
fwrite(&tmpDataLength, 2, 1, g);
fwrite(items[i].data, items[i].dataLength, 1, g);
fwrite(&null_byte, 1, 1, g);
const u16 tmpDataLength = Common::swap16(items[i].dataLength);
g.WriteArray(&tmpDataLength, 1);
g.WriteBytes(items[i].data, items[i].dataLength);
g.WriteArray(&null_byte, 1);
}
break;
case Type_SmallArray:
fwrite(&items[i].dataLength, 1, 1, g);
fwrite(items[i].data, items[i].dataLength, 1, g);
fwrite(&null_byte, 1, 1, g);
g.WriteArray(&items[i].dataLength, 1);
g.WriteBytes(items[i].data, items[i].dataLength);
g.WriteBytes(&null_byte, 1);
break;
default:
fwrite(items[i].data, items[i].dataLength, 1, g);
g.WriteBytes(items[i].data, items[i].dataLength);
break;
}
}
// Pad file to the correct size
const u64 cur_size = File::GetSize(g);
for (unsigned int i = 0; i < 16380 - cur_size; ++i)
fwrite(&null_byte, 1, 1, g);
const u64 cur_size = g.GetSize();
for (unsigned int i = 0; i != 16380 - cur_size; ++i)
g.WriteBytes(&null_byte, 1);
// Write the footer
const char footer[5] = "SCed";
fwrite(&footer, 4, 1, g);
g.WriteBytes("SCed", 4);
fclose(g);
m_Filename = m_FilenameDefault;
}
bool SysConf::SaveToFile(const char *filename)
{
FILE *f = fopen(filename, "r+b");
if (f == NULL)
return false;
File::IOFile f(filename, "r+b");
for (std::vector<SSysConfEntry>::iterator i = m_Entries.begin();
i < m_Entries.end() - 1; i++)
{
// Seek to after the name of this entry
if (fseeko(f, i->offset + i->nameLength + 1, SEEK_SET) != 0) return false;
f.Seek(i->offset + i->nameLength + 1, SEEK_SET);
// We may have to write array length value...
if (i->type == Type_BigArray)
{
u16 tmpDataLength = Common::swap16(i->dataLength);
if (fwrite(&tmpDataLength, 2, 1, f) != 1) return false;
const u16 tmpDataLength = Common::swap16(i->dataLength);
f.WriteArray(&tmpDataLength, 1);
}
else if (i->type == Type_SmallArray)
{
if (fwrite(&i->dataLength, 1, 1, f) != 1) return false;
const u8 len = i->dataLength;
f.WriteArray(&len, 1);
}
// Now write the actual data
if (fwrite(i->data, i->dataLength, 1, f) != 1) return false;
f.WriteBytes(i->data, i->dataLength);
}
fclose(f);
return true;
return f.IsGood();
}
bool SysConf::Save()
+1 -1
View File
@@ -179,7 +179,7 @@ public:
bool Reload();
private:
bool LoadFromFileInternal(FILE *f);
bool LoadFromFileInternal(File::IOFile f);
void GenerateSysConf();
void Clear();
+4 -4
View File
@@ -205,16 +205,16 @@ bool CBoot::SetupWiiMemory(unsigned int _CountryCode)
break;
}
FILE* pTmp = fopen(filename.c_str(), "rb");
{
File::IOFile pTmp(filename, "rb");
if (!pTmp)
{
PanicAlertT("SetupWiiMem: Cant find setting file");
return false;
}
fread(Memory::GetPointer(0x3800), 256, 1, pTmp);
fclose(pTmp);
pTmp.ReadBytes(Memory::GetPointer(0x3800), 256);
}
/*
Set hardcoded global variables to Wii memory. These are partly collected from
+7 -6
View File
@@ -29,15 +29,16 @@ CDolLoader::CDolLoader(u8* _pBuffer, u32 _Size)
CDolLoader::CDolLoader(const char* _szFilename)
: m_isWii(false)
{
u64 size = File::GetSize(_szFilename);
u8* tmpBuffer = new u8[(size_t)size];
const u64 size = File::GetSize(_szFilename);
u8* const tmpBuffer = new u8[(size_t)size];
FILE* pStream = fopen(_szFilename, "rb");
fread(tmpBuffer, (size_t)size, 1, pStream);
fclose(pStream);
{
File::IOFile pStream(_szFilename, "rb");
pStream.ReadBytes(tmpBuffer, (size_t)size);
}
Initialize(tmpBuffer, (u32)size);
delete [] tmpBuffer;
delete[] tmpBuffer;
}
CDolLoader::~CDolLoader()
+14 -9
View File
@@ -27,11 +27,14 @@ bool CBoot::IsElfWii(const char *filename)
{
/* We already check if filename existed before we called this function, so
there is no need for another check, just read the file right away */
FILE *f = fopen(filename, "rb");
u64 filesize = File::GetSize(f);
u8 *mem = new u8[(size_t)filesize];
fread(mem, 1, (size_t)filesize, f);
fclose(f);
const u64 filesize = File::GetSize(filename);
u8 *const mem = new u8[(size_t)filesize];
{
File::IOFile f(filename, "rb");
f.ReadBytes(mem, (size_t)filesize);
}
ElfReader reader(mem);
// TODO: Find a more reliable way to distinguish.
@@ -44,11 +47,13 @@ bool CBoot::IsElfWii(const char *filename)
bool CBoot::Boot_ELF(const char *filename)
{
FILE *f = fopen(filename, "rb");
u64 filesize = File::GetSize(f);
const u64 filesize = File::GetSize(filename);
u8 *mem = new u8[(size_t)filesize];
fread(mem, 1, (size_t)filesize, f);
fclose(f);
{
File::IOFile f(filename, "rb");
f.ReadBytes(mem, (size_t)filesize);
}
ElfReader reader(mem);
reader.LoadInto(0x80000000);
+13 -15
View File
@@ -108,19 +108,20 @@ u64 CBoot::Install_WiiWAD(const char* _pFilename)
std::string TMDFileName(ContentPath);
TMDFileName += "title.tmd";
FILE* pTMDFile = fopen(TMDFileName.c_str(), "wb");
if (pTMDFile == NULL) {
File::IOFile pTMDFile(TMDFileName, "wb");
if (!pTMDFile)
{
PanicAlertT("WAD installation failed: error creating %s", TMDFileName.c_str());
return 0;
}
fwrite(ContentLoader.GetTmdHeader(), DiscIO::INANDContentLoader::TMD_HEADER_SIZE, 1, pTMDFile);
pTMDFile.WriteBytes(ContentLoader.GetTmdHeader(), DiscIO::INANDContentLoader::TMD_HEADER_SIZE);
for (u32 i = 0; i < ContentLoader.GetContentSize(); i++)
{
DiscIO::SNANDContent Content = ContentLoader.GetContent()[i];
fwrite(Content.m_Header, DiscIO::INANDContentLoader::CONTENT_HEADER_SIZE, 1, pTMDFile);
pTMDFile.WriteBytes(Content.m_Header, DiscIO::INANDContentLoader::CONTENT_HEADER_SIZE);
char APPFileName[1024];
if (Content.m_Type & 0x8000) //shared
@@ -136,15 +137,14 @@ u64 CBoot::Install_WiiWAD(const char* _pFilename)
if (!File::Exists(APPFileName))
{
File::CreateFullPath(APPFileName);
FILE* pAPPFile = fopen(APPFileName, "wb");
if (pAPPFile == NULL)
File::IOFile pAPPFile(APPFileName, "wb");
if (!pAPPFile)
{
PanicAlertT("WAD installation failed: error creating %s", APPFileName);
return 0;
}
fwrite(Content.m_pData, Content.m_Size, 1, pAPPFile);
fclose(pAPPFile);
pAPPFile.WriteBytes(Content.m_pData, Content.m_Size);
}
else
{
@@ -152,7 +152,7 @@ u64 CBoot::Install_WiiWAD(const char* _pFilename)
}
}
fclose(pTMDFile);
pTMDFile.Close();
//Extract and copy WAD's ticket to ticket directory
@@ -164,8 +164,9 @@ u64 CBoot::Install_WiiWAD(const char* _pFilename)
sprintf(TicketFileName, "%sticket/%08x/%08x.tik",
File::GetUserPath(D_WIIUSER_IDX).c_str(), TitleID_HI, TitleID_LO);
FILE* pTicketFile = fopen(TicketFileName, "wb");
if (pTicketFile == NULL) {
File::IOFile pTicketFile(TicketFileName, "wb");
if (!pTicketFile)
{
PanicAlertT("WAD installation failed: error creating %s", TicketFileName);
return 0;
}
@@ -173,13 +174,10 @@ u64 CBoot::Install_WiiWAD(const char* _pFilename)
DiscIO::WiiWAD Wad(_pFilename);
if (!Wad.IsValid())
{
fclose(pTicketFile);
return 0;
}
fwrite(Wad.GetTicket(), Wad.GetTicketSize(), 1, pTicketFile);
fclose(pTicketFile);
pTicketFile.WriteBytes(Wad.GetTicket(), Wad.GetTicketSize());
if (!DiscIO::cUIDsys::AccessInstance().AddTitle(TitleID))
{
+2 -3
View File
@@ -104,13 +104,12 @@ void Console_Submit(const char *cmd)
u32 end;
sscanf(cmd, "%s %08x %08x %s", temp, &start, &end, filename);
FILE *f = fopen(filename, "wb");
File::IOFile f(filename, "wb");
for (u32 i = start; i < end; i++)
{
u8 b = Memory::ReadUnchecked_U8(i);
fputc(b,f);
fputc(b, f.GetHandle());
}
fclose(f);
INFO_LOG(CONSOLE, "Dumped from %08x to %08x to %s",start,end,filename);
}
CASE("disa")
+4 -3
View File
@@ -31,6 +31,7 @@
#include "DSPHost.h"
#include "DSPAnalyzer.h"
#include "MemoryUtil.h"
#include "FileUtil.h"
#include "DSPHWInterface.h"
#include "DSPIntUtil.h"
@@ -45,12 +46,12 @@ static std::mutex ExtIntCriticalSection;
static bool LoadRom(const char *fname, int size_in_words, u16 *rom)
{
FILE *pFile = fopen(fname, "rb");
File::IOFile pFile(fname, "rb");
const size_t size_in_bytes = size_in_words * sizeof(u16);
if (pFile)
{
fread(rom, 1, size_in_bytes, pFile);
fclose(pFile);
pFile.ReadArray(rom, size_in_words);
pFile.Close();
// Byteswap the rom.
for (int i = 0; i < size_in_words; i++)
+18 -18
View File
@@ -50,7 +50,7 @@ DSPDisassembler::~DSPDisassembler()
{
// Some old code for logging unknown ops.
std::string filename = File::GetUserPath(D_DUMPDSP_IDX) + "UnkOps.txt";
FILE *uo = fopen(filename.c_str(), "w");
File::IOFile uo(filename, "w");
if (!uo)
return;
@@ -61,18 +61,17 @@ DSPDisassembler::~DSPDisassembler()
if (iter->second > 0)
{
count++;
fprintf(uo, "OP%04x\t%d", iter->first, iter->second);
fprintf(uo.GetHandle(), "OP%04x\t%d", iter->first, iter->second);
for (int j = 15; j >= 0; j--) // print op bits
{
if ((j & 0x3) == 3)
fprintf(uo, "\tb");
fprintf(uo, "%d", (iter->first >> j) & 0x1);
fprintf(uo.GetHandle(), "\tb");
fprintf(uo.GetHandle(), "%d", (iter->first >> j) & 0x1);
}
fprintf(uo, "\n");
fprintf(uo.GetHandle(), "\n");
}
}
fprintf(uo, "Unknown opcodes count: %d\n", count);
fclose(uo);
fprintf(uo.GetHandle(), "Unknown opcodes count: %d\n", count);
}
bool DSPDisassembler::Disassemble(int start_pc, const std::vector<u16> &code, int base_addr, std::string &text)
@@ -80,9 +79,10 @@ bool DSPDisassembler::Disassemble(int start_pc, const std::vector<u16> &code, in
const char *tmp1 = "tmp1.bin";
// First we have to dump the code to a bin file.
FILE *f = fopen(tmp1, "wb");
fwrite(&code[0], 1, code.size() * 2, f);
fclose(f);
{
File::IOFile f(tmp1, "wb");
f.WriteArray(&code[0], code.size());
}
// Run the two passes.
return DisFile(tmp1, base_addr, 1, text) && DisFile(tmp1, base_addr, 2, text);
@@ -335,25 +335,25 @@ bool DSPDisassembler::DisOpcode(const u16 *binbuf, int base_addr, int pass, u16
bool DSPDisassembler::DisFile(const char* name, int base_addr, int pass, std::string &output)
{
FILE* in = fopen(name, "rb");
if (in == NULL)
File::IOFile in(name, "rb");
if (!in)
{
printf("gd_dis_file: No input\n");
return false;
}
int size = (int)File::GetSize(in) & ~1;
u16 *binbuf = new u16[size / 2];
fread(binbuf, 1, size, in);
fclose(in);
const int size = ((int)in.GetSize() & ~1) / 2;
u16 *const binbuf = new u16[size];
in.ReadArray(binbuf, size);
in.Close();
// Actually do the disassembly.
for (u16 pc = 0; pc < (size / 2);)
for (u16 pc = 0; pc < size;)
{
DisOpcode(binbuf, base_addr, pass, &pc, output);
if (pass == 2)
output.append("\n");
}
delete [] binbuf;
delete[] binbuf;
return true;
}
+4 -6
View File
@@ -24,16 +24,14 @@ CDump::CDump(const char* _szFilename) :
m_pData(NULL),
m_bInit(false)
{
FILE* pStream = fopen(_szFilename, "rb");
if (pStream != NULL)
File::IOFile pStream(_szFilename, "rb");
if (pStream)
{
m_size = (size_t)File::GetSize(pStream);
m_size = (size_t)pStream.GetSize();
m_pData = new u8[m_size];
fread(m_pData, m_size, 1, pStream);
fclose(pStream);
pStream.ReadArray(m_pData, m_size);
}
}
+7 -9
View File
@@ -21,7 +21,7 @@
#pragma once
#include "Common.h"
#include "FileUtil.h"
// C L A S S
@@ -34,11 +34,10 @@ public:
m_bInit(false)
{
// try to open file
FILE* pStream = NULL;
fopen_s(&pStream, _szFilename, "rb");
File::IOFile pStream(_szFilename, "rb");
if (pStream)
{
fread(&m_dolheader, 1, sizeof(SDolHeader), pStream);
pStream.ReadArray(&m_dolheader, 1);
// swap memory
u32* p = (u32*)&m_dolheader;
@@ -52,8 +51,8 @@ public:
{
u8* pTemp = new u8[m_dolheader.textSize[i]];
fseek(pStream, m_dolheader.textOffset[i], SEEK_SET);
fread(pTemp, 1, m_dolheader.textSize[i], pStream);
pStream.Seek(m_dolheader.textOffset[i], SEEK_SET);
pStream.ReadArray(pTemp, m_dolheader.textSize[i]);
for (size_t num=0; num<m_dolheader.textSize[i]; num++)
CMemory::Write_U8(pTemp[num], m_dolheader.textAddress[i] + num);
@@ -69,8 +68,8 @@ public:
{
u8* pTemp = new u8[m_dolheader.dataSize[i]];
fseek(pStream, m_dolheader.dataOffset[i], SEEK_SET);
fread(pTemp, 1, m_dolheader.dataSize[i], pStream);
pStream.Seek(m_dolheader.dataOffset[i], SEEK_SET);
pStream.ReadArray(pTemp, m_dolheader.dataSize[i]);
for (size_t num=0; num<m_dolheader.dataSize[i]; num++)
CMemory::Write_U8(pTemp[num], m_dolheader.dataAddress[i] + num);
@@ -79,7 +78,6 @@ public:
}
}
fclose(pStream);
m_bInit = true;
}
}
@@ -102,12 +102,8 @@ void CUCode_Rom::BootUCode()
char binFile[MAX_PATH];
sprintf(binFile, "%sDSP_UC_%08X.bin", File::GetUserPath(D_DUMPDSP_IDX).c_str(), ector_crc);
FILE* pFile = fopen(binFile, "wb");
if (pFile)
{
fwrite((u8*)Memory::GetPointer(m_CurrentUCode.m_RAMAddress), m_CurrentUCode.m_Length, 1, pFile);
fclose(pFile);
}
File::IOFile pFile(binFile, "wb");
pFile.WriteArray((u8*)Memory::GetPointer(m_CurrentUCode.m_RAMAddress), m_CurrentUCode.m_Length);
#endif
DEBUG_LOG(DSPHLE, "CurrentUCode SOURCE Addr: 0x%08x", m_CurrentUCode.m_RAMAddress);

Some files were not shown because too many files have changed in this diff Show More