Skip to content

Commit

Permalink
Update epanet.c
Browse files Browse the repository at this point in the history
Co-Authored-By: Lew Rossman <[email protected]>
  • Loading branch information
Mariosmsk and LRossman committed Aug 1, 2024
1 parent 5335088 commit 03e889d
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/epanet.c
Original file line number Diff line number Diff line change
Expand Up @@ -4525,16 +4525,18 @@ int DLLEXPORT EN_loadpatternfile(EN_Project p, const char *filename, const char
*/
{
FILE *file;
char line[1024];
char line[MAXLINE+1];
int err = 0;
int i;
int len = 0;
double value;
Spattern *pat;
double *values = NULL;
int CHUNK = 50;

if (!p->Openflag) return 102;

file = fopen(filename, "r");
if (file == NULL) return 102; // Update with new error code
if (file == NULL) return 302;

// Add the new pattern
if ((err = EN_addpattern(p, id)) != 0) {
Expand All @@ -4548,33 +4550,31 @@ int DLLEXPORT EN_loadpatternfile(EN_Project p, const char *filename, const char
return err;
}

pat = &p->network.Pattern[i];
// Free the initial allocation
free(pat->F);
pat->F = NULL;
pat->Length = 0;

// Read pattern values
while (fgets(line, sizeof(line), file) != NULL) {
// Skip comments and empty lines
if (line[0] == ';' || line[0] == '\n') continue;

// Convert line to a double value
value = atof(line);
if (value == 0 && line[0] != '0') continue; // Skip invalid lines

pat->Length++;
pat->F = (double *)realloc(pat->F, pat->Length * sizeof(double));
// Abort if memory allocation error
if (pat->F == NULL) {
fclose(file);
return 101;
// Skip lines that don't contain valid numbers
if (!getfloat(line, &value) continue;

// Resize multiplier array if it's full
if (len % CHUNK == 0) {
values = (double *) realloc(values, (len + CHUNK) * sizeof(double));

// Abort if memory allocation error
if (values == NULL) {
fclose(file);
return 101;
}
}
pat->F[pat->Length - 1] = value;
values[len] = value;
len++;
}

fclose(file);
return 0;

// Transfer multipliers to pattern
err = EN_setpattern(p, i, values, len);
free(values);
return err;
}

int DLLEXPORT EN_deletepattern(EN_Project p, int index)
Expand Down

0 comments on commit 03e889d

Please sign in to comment.