Skip to content

Commit

Permalink
Use goto to close files in read files
Browse files Browse the repository at this point in the history
- Replaced instances of `return` with `goto` and created a target of said goto at the end of the functions
- Removed check for existing file in `read_swc_hist()` since `OpenFile()` already reports if it does not exist
- Replaced call to `fclose()` in `read_weather_hist()` with `CloseFile()` which already checks if closing the file results in EOF
  • Loading branch information
N1ckP3rsl3y committed Jul 26, 2024
1 parent 2fa7c5a commit 6964cfb
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 222 deletions.
31 changes: 14 additions & 17 deletions src/SW_Carbon.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ void SW_CBN_read(
"Not enough values when reading in the year from %s.",
MyFileName
);
return; /* Exit prematurely due to error */
goto closeFile;

Check warning on line 133 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L133

Added line #L133 was not covered by tests
}

year = sw_strtoi(yearStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 138 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L138

Added line #L138 was not covered by tests
}

// Find scenario
Expand All @@ -150,12 +150,12 @@ void SW_CBN_read(
"scenario from %s.",
MyFileName
);
return; /* Exit prematurely due to error */
goto closeFile;

Check warning on line 153 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L153

Added line #L153 was not covered by tests
}

year = sw_strtoi(yearStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 158 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L158

Added line #L158 was not covered by tests
}

/* Silence clang-tidy clang-analyzer-deadcode.DeadStores */
Expand All @@ -180,21 +180,20 @@ void SW_CBN_read(
"ppm from %s.",
MyFileName
);
return; /* Exit prematurely due to error */
goto closeFile;

Check warning on line 183 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L183

Added line #L183 was not covered by tests
}

year = sw_strtoi(yearStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 188 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L188

Added line #L188 was not covered by tests
}

ppm = sw_strtod(ppmStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 193 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L193

Added line #L193 was not covered by tests
}

if (year < 0) {
CloseFile(&f, LogInfo);
LogError(
LogInfo,
LOGERROR,
Expand All @@ -204,7 +203,7 @@ void SW_CBN_read(
year,
SW_Carbon->scenario
);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 206 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L206

Added line #L206 was not covered by tests
}

SW_Carbon->ppm[year] = ppm;
Expand All @@ -224,7 +223,6 @@ void SW_CBN_read(
1][year].grass != 1.0, due to floating point precision and the chance
that a multiplier of 1.0 was actually calculated */
if (existing_years[year] != 0) {
CloseFile(&f, LogInfo);
LogError(
LogInfo,
LOGERROR,
Expand All @@ -234,14 +232,11 @@ void SW_CBN_read(
year,
SW_Carbon->scenario
);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 235 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L235

Added line #L235 was not covered by tests
}
existing_years[year] = 1;
}

CloseFile(&f, LogInfo);


/* Error checking */

// Must check if the file was empty before checking if the scneario was
Expand All @@ -255,7 +250,7 @@ void SW_CBN_read(
" debugging purposes, SOILWAT2 read in file '%s'\n",
MyFileName
);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 253 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L253

Added line #L253 was not covered by tests
}

// A scenario must be found in order for ppm to have a positive value
Expand All @@ -267,7 +262,7 @@ void SW_CBN_read(
" was not found in carbon.in\n",
SW_Carbon->scenario
);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 265 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L265

Added line #L265 was not covered by tests
}

// Ensure that the desired years were calculated
Expand All @@ -282,9 +277,11 @@ void SW_CBN_read(
year,
SW_Carbon->scenario
);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 280 in src/SW_Carbon.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Carbon.c#L280

Added line #L280 was not covered by tests
}
}

closeFile: { CloseFile(&f, LogInfo); }
}

/**
Expand Down
30 changes: 13 additions & 17 deletions src/SW_Domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
LogError(
LogInfo, LOGERROR, "Invalid key-value pair in %s.", MyFileName
);
return; /* Exit prematurely due to error */
goto closeFile;
}

keyID = key_to_id(key, possibleKeys, NUM_DOM_IN_KEYS);
Expand All @@ -247,7 +247,7 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
}

if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile;
}
}

Expand All @@ -262,7 +262,7 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
MyFileName,
value
);
return; // Exit function prematurely due to error
goto closeFile;
}
strcpy(SW_Domain->DomainType, value);
break;
Expand All @@ -280,31 +280,29 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
y = intRes;

if (y < 0) {
CloseFile(&f, LogInfo);
LogError(
LogInfo,
LOGERROR,
"%s: Negative start year (%d)",
MyFileName,
y
);
return; // Exit function prematurely due to error
goto closeFile;
}
SW_Domain->startyr = yearto4digit((TimeInt) y);
break;
case 5: // End year
y = intRes;

if (y < 0) {
CloseFile(&f, LogInfo);
LogError(
LogInfo,
LOGERROR,
"%s: Negative ending year (%d)",
MyFileName,
y
);
return; // Exit function prematurely due to error
goto closeFile;
}
SW_Domain->endyr = yearto4digit((TimeInt) y);
break;
Expand All @@ -326,7 +324,7 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
"Invalid key-value pair for CRS box in %s.",
MyFileName
);
return; /* Exit prematurely due to error */
goto closeFile;
}

strcpy(SW_Domain->crs_bbox, value);
Expand All @@ -348,7 +346,6 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
y = intRes;

if (y != 1 && y != 2) {
CloseFile(&f, LogInfo);
LogError(
LogInfo,
LOGERROR,
Expand All @@ -357,7 +354,7 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
MyFileName,
y
);
return; // Exit function prematurely due to error
goto closeFile;
}
SW_Domain->SW_SpinUp.mode = y;
break;
Expand Down Expand Up @@ -392,20 +389,18 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
}
}

CloseFile(&f, LogInfo);


// Check if all required input was provided
check_requiredKeys(
hasKeys, requiredKeys, possibleKeys, NUM_DOM_IN_KEYS, LogInfo
);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile;
}

if (SW_Domain->endyr < SW_Domain->startyr) {
LogError(LogInfo, LOGERROR, "%s: Start Year > End Year", MyFileName);
return; // Exit function prematurely due to error
goto closeFile;
}

// Check if start day of year was not found
Expand Down Expand Up @@ -434,12 +429,12 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
// Check bounding box coordinates
if (GT(SW_Domain->min_x, SW_Domain->max_x)) {
LogError(LogInfo, LOGERROR, "Domain.in: bbox x-axis min > max.");
return; // Exit function prematurely due to error
goto closeFile;
}

if (GT(SW_Domain->min_y, SW_Domain->max_y)) {
LogError(LogInfo, LOGERROR, "Domain.in: bbox y-axis min > max.");
return; // Exit function prematurely due to error
goto closeFile;
}

// Check if scope value is out of range
Expand All @@ -452,8 +447,9 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
MyFileName,
SW_Domain->SW_SpinUp.scope
);
return; // Exit function prematurely due to error
}

closeFile: { CloseFile(&f, LogInfo); }
}

/**
Expand Down
38 changes: 14 additions & 24 deletions src/SW_Files.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,15 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
case 20:
PathInfo->InFiles[eOutputDaily] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 132 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L132

Added line #L132 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputDaily], LogInfo);
break;
case 21:
PathInfo->InFiles[eOutputWeekly] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 140 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L140

Added line #L140 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputWeekly], LogInfo);
Expand All @@ -148,8 +146,7 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
case 22:
PathInfo->InFiles[eOutputMonthly] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 149 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L149

Added line #L149 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputMonthly], LogInfo);
Expand All @@ -158,17 +155,15 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
case 23:
PathInfo->InFiles[eOutputYearly] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 158 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L158

Added line #L158 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputYearly], LogInfo);
break;
case 24:
PathInfo->InFiles[eOutputDaily_soil] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 166 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L166

Added line #L166 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputDaily_soil], LogInfo);
Expand All @@ -177,8 +172,7 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
case 25:
PathInfo->InFiles[eOutputWeekly_soil] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 175 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L175

Added line #L175 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputWeekly_soil], LogInfo);
Expand All @@ -187,8 +181,7 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
case 26:
PathInfo->InFiles[eOutputMonthly_soil] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 184 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L184

Added line #L184 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputMonthly_soil], LogInfo);
Expand All @@ -197,8 +190,7 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
case 27:
PathInfo->InFiles[eOutputYearly_soil] = Str_Dup(inbuf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 193 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L193

Added line #L193 was not covered by tests
}
++fileno;
SW_CSV_F_INIT(PathInfo->InFiles[eOutputYearly_soil], LogInfo);
Expand All @@ -217,33 +209,29 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
strcat(buf, inbuf);
PathInfo->InFiles[fileno] = Str_Dup(buf, LogInfo);
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 212 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L212

Added line #L212 was not covered by tests
}
}

// Check if something went wrong in `SW_CSV_F_INIT()`
if (LogInfo->stopRun) {
CloseFile(&f, LogInfo);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 218 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L218

Added line #L218 was not covered by tests
}

lineno++;
}

CloseFile(&f, LogInfo);

if (fileno < eEndFile - 1) {
LogError(
LogInfo, LOGERROR, "Too few files (%d) in %s", fileno, MyFileName
);
return; // Exit function prematurely due to error
goto closeFile;

Check warning on line 228 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L228

Added line #L228 was not covered by tests
}

if (!DirExists(PathInfo->output_prefix)) {
MkDir(PathInfo->output_prefix, LogInfo);
if (LogInfo->stopRun) {
return; // Exit prematurely due to error
goto closeFile;

Check warning on line 234 in src/SW_Files.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Files.c#L232-L234

Added lines #L232 - L234 were not covered by tests
}
}

Expand All @@ -256,6 +244,8 @@ void SW_F_read(PATH_INFO *PathInfo, LOG_INFO *LogInfo) {
LogInfo->logfp = OpenFile(PathInfo->InFiles[eLog], "w", LogInfo);
}
#endif

closeFile: { CloseFile(&f, LogInfo); }
}

void SW_F_deepCopy(PATH_INFO *source, PATH_INFO *dest, LOG_INFO *LogInfo) {
Expand Down
Loading

0 comments on commit 6964cfb

Please sign in to comment.