Skip to content

Commit

Permalink
Update usage of getline, free memory and check if getline read something
Browse files Browse the repository at this point in the history
  • Loading branch information
darvark committed Aug 2, 2023
1 parent 717e2de commit 25f74ad
Show file tree
Hide file tree
Showing 13 changed files with 435 additions and 358 deletions.
42 changes: 26 additions & 16 deletions src/addmult.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,48 +331,58 @@ int init_and_load_multipliers(void) {

FILE *cfp;
char *s_inputbuffer = NULL;
size_t s_inputbuffer_len = 2000;
size_t s_inputbuffer_len = 0;
char *mults_location;
ssize_t read;

if (mults_possible) {
/* free old array if exists */
g_ptr_array_free(mults_possible, TRUE);
/* free old array if exists */
g_ptr_array_free(mults_possible, TRUE);
}
mults_possible = g_ptr_array_new_with_free_func(free_possible_mult);

if (strlen(multsfile) == 0) {
return 0;
return 0;
}

mults_location = find_available(multsfile);

if ((cfp = fopen(mults_location, "r")) == NULL) {
mvprintw(9, 0, "Error opening multiplier file %s.\n", multsfile);
refreshp();
sleep(5);
mvprintw(9, 0, "Error opening multiplier file %s.\n", multsfile);
refreshp();
sleep(5);
}

g_free(mults_location);

if (cfp == NULL) {
return 0; // couldn't open file
return 0; // couldn't open file
}

while ((read = getline(&s_inputbuffer, &s_inputbuffer_len, cfp)) != -1) {
/* strip leading and trailing whitespace */
g_strstrip(s_inputbuffer);

/* drop comments starting with '#' and empty lines */
if (*s_inputbuffer == '#' || *s_inputbuffer == '\0') {
continue;
if (read != -1) {
if (s_inputbuffer_len > 0) {
/* strip leading and trailing whitespace */
g_strstrip(s_inputbuffer);

/* drop comments starting with '#' and empty lines */
if (*s_inputbuffer == '#' || *s_inputbuffer == '\0') {
continue;
}

add_mult_line(s_inputbuffer);
}
}
else {
printf("RuntimeError: %ld", read);
exit(1);
}

add_mult_line(s_inputbuffer);

}

fclose(cfp);
if (s_inputbuffer_len > 0)
free(s_inputbuffer);
/* do not rely on the order in the mult file but sort it here */
g_ptr_array_sort(mults_possible, (GCompareFunc)cmp_size);

Expand Down
97 changes: 52 additions & 45 deletions src/bandmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void bmdata_read_file() {
int timediff, last_bm_save_time, fc, read;
char *line = NULL;
char *token;
size_t line_len = 50;
size_t line_len = 0;
static bool bmdata_parsed = false;

if (bmdata_parsed)
Expand All @@ -145,50 +145,57 @@ void bmdata_read_file() {
if (timediff < 0)
timediff = 0;

while ((read = getline(&line, &line_len, fp)) != -1) {
spot *entry = g_new0(spot, 1);
fc = 0;
token = strtok(line, ";");
while (token != NULL) {
switch (fc) {
case 0: entry -> call = g_strdup(token);
break;
case 1: sscanf(token, "%d", &entry->freq);
break;
case 2: sscanf(token, "%hhd", &entry->mode);
break;
case 3: // re-evaluate band index
entry->bandindex = freq2bandindex(entry->freq);
break;
case 4: sscanf(token, "%c", &entry->node);
break;
case 5: sscanf(token, "%u", &entry->timeout);
break;
case 6: // dupe is transient, not read back
entry->dupe = false;
break;
case 7: sscanf(token, "%d", &entry->cqzone);
break;
case 8: sscanf(token, "%d", &entry->ctynr);
break;
case 9: entry->pfx = g_strdup(token);
break;
case 10: // mult is transient, not read back
entry->mult = false;
break;
}
fc++;
token = strtok(NULL, ";");
}
if (entry->timeout > timediff) {
entry->timeout -= timediff; /* remaining time */
allspots = g_list_insert_sorted(allspots, entry, (GCompareFunc)cmp_freq);
} else {
free_spot(entry);
}
}
}
fclose(fp);
while ((read = getline(&line, &line_len, fp)) != -1) {
if (line_len > 0) {
spot *entry = g_new0(spot, 1);
fc = 0;
token = strtok(line, ";");
while (token != NULL) {
switch (fc) {
case 0: entry -> call = g_strdup(token);
break;
case 1: sscanf(token, "%d", &entry->freq);
break;
case 2: sscanf(token, "%hhd", &entry->mode);
break;
case 3: // re-evaluate band index
entry->bandindex = freq2bandindex(entry->freq);
break;
case 4: sscanf(token, "%c", &entry->node);
break;
case 5: sscanf(token, "%u", &entry->timeout);
break;
case 6: // dupe is transient, not read back
entry->dupe = false;
break;
case 7: sscanf(token, "%d", &entry->cqzone);
break;
case 8: sscanf(token, "%d", &entry->ctynr);
break;
case 9: entry->pfx = g_strdup(token);
break;
case 10: // mult is transient, not read back
entry->mult = false;
break;
}
fc++;
token = strtok(NULL, ";");
}

if (entry->timeout > timediff) {
entry->timeout -= timediff; /* remaining time */
allspots = g_list_insert_sorted(allspots, entry, (GCompareFunc)cmp_freq);
} else {
free_spot(entry);
}
}
}
}

if (line != NULL)
free(line);

fclose(fp);
}
}

Expand Down
49 changes: 27 additions & 22 deletions src/cabrillo_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,35 +579,40 @@ static int process_cabrillo_template_file(const char *file_name) {
}

char* logline = NULL;
size_t read_len = 0;
int read;

int result = PARSE_OK;

while ((read = getline(&logline, (size_t*)MAX_CABRILLO_LEN, fp)) != 1) {
g_strstrip(logline);
if (skip_template_line(logline)) {
continue; // skip it
}
char **fields = g_strsplit(logline, ":", 2);
g_strstrip(fields[0]);
if (g_strv_length(fields) == 2) {
g_strstrip(fields[1]);
}

int rc = add_cabrillo_field(fields[0], fields[1]);

if (rc != PARSE_OK) {
error_details = g_strdup_printf("unknown tag '%s'", fields[0]);
result = PARSE_ERROR;
}

g_strfreev(fields);

if (result != PARSE_OK) {
break;
while ((read = getline(&logline, &read_len, fp)) != 1) {
if (read_len > 0) {
g_strstrip(logline);
if (skip_template_line(logline)) {
continue; // skip it
}
char **fields = g_strsplit(logline, ":", 2);
g_strstrip(fields[0]);
if (g_strv_length(fields) == 2) {
g_strstrip(fields[1]);
}

int rc = add_cabrillo_field(fields[0], fields[1]);

if (rc != PARSE_OK) {
error_details = g_strdup_printf("unknown tag '%s'", fields[0]);
result = PARSE_ERROR;
}

g_strfreev(fields);

if (result != PARSE_OK) {
break;
}
}
}

if (logline != NULL)
free(logline);
fclose(fp);

return result;
Expand Down
Loading

0 comments on commit 25f74ad

Please sign in to comment.