mirror of https://github.com/OpenTTD/OpenTTD
Codechange: replace FileCloser with AutoCloseFile, i.e. RAII manage the FILE* instead of creating a second object to manage the life time
parent
eb2dd8ebd7
commit
dba954e022
|
@ -1038,20 +1038,18 @@ void SanitizeFilename(std::string &filename)
|
|||
*/
|
||||
std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
|
||||
{
|
||||
FILE *in = fopen(filename.c_str(), "rb");
|
||||
AutoCloseFile in(fopen(filename.c_str(), "rb"));
|
||||
if (in == nullptr) return nullptr;
|
||||
|
||||
FileCloser fc(in);
|
||||
|
||||
fseek(in, 0, SEEK_END);
|
||||
size_t len = ftell(in);
|
||||
fseek(in, 0, SEEK_SET);
|
||||
fseek(in.get(), 0, SEEK_END);
|
||||
size_t len = ftell(in.get());
|
||||
fseek(in.get(), 0, SEEK_SET);
|
||||
if (len > maxsize) return nullptr;
|
||||
|
||||
std::unique_ptr<char[]> mem = std::make_unique<char[]>(len + 1);
|
||||
|
||||
mem.get()[len] = 0;
|
||||
if (fread(mem.get(), len, 1, in) != 1) return nullptr;
|
||||
if (fread(mem.get(), len, 1, in.get()) != 1) return nullptr;
|
||||
|
||||
lenp = len;
|
||||
return mem;
|
||||
|
|
|
@ -81,18 +81,6 @@ public:
|
|||
|
||||
DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
|
||||
|
||||
/** Auto-close a file upon scope exit. */
|
||||
class FileCloser {
|
||||
FILE *f;
|
||||
|
||||
public:
|
||||
FileCloser(FILE *_f) : f(_f) {}
|
||||
~FileCloser()
|
||||
{
|
||||
fclose(f);
|
||||
}
|
||||
};
|
||||
|
||||
/** Helper to manage a FILE with a \c std::unique_ptr. */
|
||||
struct FileDeleter {
|
||||
void operator()(FILE *f)
|
||||
|
|
|
@ -49,11 +49,9 @@ void CDECL StrgenFatalI(const std::string &msg)
|
|||
LanguageStrings ReadRawLanguageStrings(const std::string &file)
|
||||
{
|
||||
size_t to_read;
|
||||
FILE *fh = FioFOpenFile(file, "rb", GAME_DIR, &to_read);
|
||||
AutoCloseFile fh(FioFOpenFile(file, "rb", GAME_DIR, &to_read));
|
||||
if (fh == nullptr) return LanguageStrings();
|
||||
|
||||
FileCloser fhClose(fh);
|
||||
|
||||
auto pos = file.rfind(PATHSEPCHAR);
|
||||
if (pos == std::string::npos) return LanguageStrings();
|
||||
std::string langname = file.substr(pos + 1);
|
||||
|
@ -64,7 +62,7 @@ LanguageStrings ReadRawLanguageStrings(const std::string &file)
|
|||
LanguageStrings ret(langname.substr(0, langname.find('.')));
|
||||
|
||||
char buffer[2048];
|
||||
while (to_read != 0 && fgets(buffer, sizeof(buffer), fh) != nullptr) {
|
||||
while (to_read != 0 && fgets(buffer, sizeof(buffer), fh.get()) != nullptr) {
|
||||
size_t len = strlen(buffer);
|
||||
|
||||
/* Remove trailing spaces/newlines from the string. */
|
||||
|
|
|
@ -433,16 +433,14 @@ bool DLSFile::LoadFile(const wchar_t *file)
|
|||
{
|
||||
Debug(driver, 2, "DMusic: Try to load DLS file {}", FS2OTTD(file));
|
||||
|
||||
FILE *f = _wfopen(file, L"rb");
|
||||
AutoCloseFile f(_wfopen(file, L"rb"));
|
||||
if (f == nullptr) return false;
|
||||
|
||||
FileCloser f_scope(f);
|
||||
|
||||
/* Check DLS file header. */
|
||||
ChunkHeader hdr;
|
||||
FOURCC dls_type;
|
||||
if (fread(&hdr, sizeof(hdr), 1, f) != 1) return false;
|
||||
if (fread(&dls_type, sizeof(dls_type), 1, f) != 1) return false;
|
||||
if (fread(&hdr, sizeof(hdr), 1, f.get()) != 1) return false;
|
||||
if (fread(&dls_type, sizeof(dls_type), 1, f.get()) != 1) return false;
|
||||
if (hdr.type != FOURCC_RIFF || dls_type != FOURCC_DLS) return false;
|
||||
|
||||
hdr.length -= sizeof(FOURCC);
|
||||
|
@ -455,49 +453,49 @@ bool DLSFile::LoadFile(const wchar_t *file)
|
|||
/* Iterate over all chunks in the file. */
|
||||
while (hdr.length > 0) {
|
||||
ChunkHeader chunk;
|
||||
if (fread(&chunk, sizeof(chunk), 1, f) != 1) return false;
|
||||
if (fread(&chunk, sizeof(chunk), 1, f.get()) != 1) return false;
|
||||
hdr.length -= chunk.length + sizeof(chunk);
|
||||
|
||||
if (chunk.type == FOURCC_LIST) {
|
||||
/* Unwrap list header. */
|
||||
if (fread(&chunk.type, sizeof(chunk.type), 1, f) != 1) return false;
|
||||
if (fread(&chunk.type, sizeof(chunk.type), 1, f.get()) != 1) return false;
|
||||
chunk.length -= sizeof(chunk.type);
|
||||
}
|
||||
|
||||
switch (chunk.type) {
|
||||
case FOURCC_COLH:
|
||||
if (fread(&header, sizeof(header), 1, f) != 1) return false;
|
||||
if (fread(&header, sizeof(header), 1, f.get()) != 1) return false;
|
||||
break;
|
||||
|
||||
case FOURCC_LINS: // List chunk
|
||||
if (!this->ReadDLSInstrumentList(f, chunk.length)) return false;
|
||||
if (!this->ReadDLSInstrumentList(f.get(), chunk.length)) return false;
|
||||
break;
|
||||
|
||||
case FOURCC_WVPL: // List chunk
|
||||
if (!this->ReadDLSWaveList(f, chunk.length)) return false;
|
||||
if (!this->ReadDLSWaveList(f.get(), chunk.length)) return false;
|
||||
break;
|
||||
|
||||
case FOURCC_PTBL:
|
||||
POOLTABLE ptbl;
|
||||
if (fread(&ptbl, sizeof(ptbl), 1, f) != 1) return false;
|
||||
fseek(f, ptbl.cbSize - sizeof(ptbl), SEEK_CUR);
|
||||
if (fread(&ptbl, sizeof(ptbl), 1, f.get()) != 1) return false;
|
||||
fseek(f.get(), ptbl.cbSize - sizeof(ptbl), SEEK_CUR);
|
||||
|
||||
/* Read all defined cues. */
|
||||
for (ULONG i = 0; i < ptbl.cCues; i++) {
|
||||
POOLCUE cue;
|
||||
if (fread(&cue, sizeof(cue), 1, f) != 1) return false;
|
||||
if (fread(&cue, sizeof(cue), 1, f.get()) != 1) return false;
|
||||
this->pool_cues.push_back(cue);
|
||||
}
|
||||
break;
|
||||
|
||||
case FOURCC_INFO:
|
||||
/* We don't care about info stuff. */
|
||||
fseek(f, chunk.length, SEEK_CUR);
|
||||
fseek(f.get(), chunk.length, SEEK_CUR);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug(driver, 7, "DLS: Ignoring unknown chunk {}{}{}{}", (char)(chunk.type & 0xFF), (char)((chunk.type >> 8) & 0xFF), (char)((chunk.type >> 16) & 0xFF), (char)((chunk.type >> 24) & 0xFF));
|
||||
fseek(f, chunk.length, SEEK_CUR);
|
||||
fseek(f.get(), chunk.length, SEEK_CUR);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,14 +104,13 @@ uint32_t NewGRFProfiler::Finish()
|
|||
std::string filename = this->GetOutputFilename();
|
||||
IConsolePrint(CC_DEBUG, "Finished profile of NewGRF [{:08X}], writing {} events to '{}'.", BSWAP32(this->grffile->grfid), this->calls.size(), filename);
|
||||
|
||||
FILE *f = FioFOpenFile(filename, "wt", Subdirectory::NO_DIRECTORY);
|
||||
FileCloser fcloser(f);
|
||||
AutoCloseFile f(FioFOpenFile(filename, "wt", Subdirectory::NO_DIRECTORY));
|
||||
|
||||
uint32_t total_microseconds = 0;
|
||||
|
||||
fmt::print(f, "Tick,Sprite,Feature,Item,CallbackID,Microseconds,Depth,Result\n");
|
||||
fmt::print(f.get(), "Tick,Sprite,Feature,Item,CallbackID,Microseconds,Depth,Result\n");
|
||||
for (const Call &c : this->calls) {
|
||||
fmt::print(f, "{},{},{:#X},{},{:#X},{},{},{}\n", c.tick, c.root_sprite, c.feat, c.item, (uint)c.cb, c.time, c.subs, c.result);
|
||||
fmt::print(f.get(), "{},{},{:#X},{},{:#X},{},{},{}\n", c.tick, c.root_sprite, c.feat, c.item, (uint)c.cb, c.time, c.subs, c.result);
|
||||
total_microseconds += c.time;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue