Use a Defer struct to close files and restore lexer state with RAII (#1379)

This commit is contained in:
Sylvie
2024-03-27 10:42:53 -04:00
committed by GitHub
parent 32db0a0f18
commit a68bebf4a2
13 changed files with 131 additions and 172 deletions

View File

@@ -199,6 +199,10 @@ static void writeROM() {
if (!outputFile)
err("Failed to open output file \"%s\"", outputFileName);
}
Defer closeOutputFile{[&] {
if (outputFile)
fclose(outputFile);
}};
if (overlayFileName) {
if (strcmp(overlayFileName, "-")) {
@@ -210,6 +214,10 @@ static void writeROM() {
if (!overlayFile)
err("Failed to open overlay file \"%s\"", overlayFileName);
}
Defer closeOverlayFile{[&] {
if (overlayFile)
fclose(overlayFile);
}};
uint32_t nbOverlayBanks = checkOverlaySize();
@@ -230,11 +238,6 @@ static void writeROM() {
sectionTypeInfo[SECTTYPE_ROMX].size
);
}
if (outputFile)
fclose(outputFile);
if (overlayFile)
fclose(overlayFile);
}
// Checks whether this character is legal as the first character of a symbol's name in a sym file
@@ -533,6 +536,7 @@ static void writeSym() {
}
if (!symFile)
err("Failed to open sym file \"%s\"", symFileName);
Defer closeSymFile{[&] { fclose(symFile); }};
fputs("; File generated by rgblink\n", symFile);
@@ -542,8 +546,6 @@ static void writeSym() {
for (uint32_t bank = 0; bank < sections[type].size(); bank++)
writeSymBank(sections[type][bank], type, bank);
}
fclose(symFile);
}
// Writes the map file, if applicable.
@@ -559,6 +561,7 @@ static void writeMap() {
}
if (!mapFile)
err("Failed to open map file \"%s\"", mapFileName);
Defer closeMapFile{[&] { fclose(mapFile); }};
writeMapSummary();
@@ -568,8 +571,6 @@ static void writeMap() {
for (uint32_t bank = 0; bank < sections[type].size(); bank++)
writeMapBank(sections[type][bank], type, bank);
}
fclose(mapFile);
}
void out_WriteFiles() {