Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#348 Review usage of fgets in file reading #398

Closed
wants to merge 67 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
38e6af1
Merge pull request #1 from Tlf/master
darvark Jun 23, 2023
19621b8
Add keyword to start tlf in S&P mode
darvark Jun 23, 2023
b294490
Fix formating
darvark Jun 23, 2023
f8a986a
Move setup operation mode to config file
darvark Jun 28, 2023
2d0318e
Update manual and added test for new function
darvark Jun 28, 2023
b3500cf
Fix tests
darvark Jun 28, 2023
2e6e2f6
Post review fixes.
darvark Jun 29, 2023
eddfbfd
Update man pages
darvark Jun 29, 2023
5fe36d5
Move operating_mode initalization to init_variables
darvark Jun 29, 2023
d10b061
Update ToDo. Marked item finished
darvark Jun 29, 2023
a948f90
Initial commit for Issue #348 - removing usage of fgets and switching…
darvark Jul 9, 2023
974d984
restore rules content
darvark Jul 9, 2023
579e53d
restore execute bit
darvark Jul 9, 2023
7a11229
Merge branch 'Tlf:master' into master
darvark Jul 19, 2023
c5a5841
Add keyword to start tlf in S&P mode
darvark Jun 23, 2023
0ce1332
Fix formating
darvark Jun 23, 2023
0a7ef03
Move setup operation mode to config file
darvark Jun 28, 2023
8cf8548
Update manual and added test for new function
darvark Jun 28, 2023
4e58d00
Fix tests
darvark Jun 28, 2023
9bf132c
Post review fixes.
darvark Jun 29, 2023
c0d8c67
Update man pages
darvark Jun 29, 2023
42da065
Move operating_mode initalization to init_variables
darvark Jun 29, 2023
1ec81e9
Update ToDo. Marked item finished
darvark Jun 29, 2023
cb7dce9
Make band limits configurable (#392)
zcsahok Jul 18, 2023
24b4d1a
Initial commit for Issue #348 - removing usage of fgets and switching…
darvark Jul 9, 2023
06d6617
Initial commit for Issue #348 - removing usage of fgets and switching…
darvark Jul 9, 2023
ae33beb
restore rules content
darvark Jul 9, 2023
7fb00c5
restore execute bit
darvark Jul 9, 2023
bc6ca57
Initial commit for Issue #348 - removing usage of fgets and switching…
darvark Jul 9, 2023
35947c7
rebase
darvark Jul 19, 2023
717e2de
redesign usage of getline
darvark Aug 1, 2023
25f74ad
Update usage of getline, free memory and check if getline read something
darvark Aug 2, 2023
e7a65a4
Merge branch 'Tlf:master' into master
darvark Aug 3, 2023
d79be5d
Move setup operation mode to config file
darvark Jun 28, 2023
c3738ac
Fix tests
darvark Jun 28, 2023
4c45c92
Post review fixes.
darvark Jun 29, 2023
5f33659
add errno handling
darvark Aug 3, 2023
c87650d
review fix
darvark Aug 3, 2023
ba4fcc4
fix typo
darvark Aug 3, 2023
386d7e9
Initial commit for Issue #348 - removing usage of fgets and switching…
darvark Jul 9, 2023
667a8ff
restore rules content
darvark Jul 9, 2023
1a67f3a
restore execute bit
darvark Jul 9, 2023
4459401
Initial commit for Issue #348 - removing usage of fgets and switching…
darvark Jul 9, 2023
6d6c384
restore rules content
darvark Jul 9, 2023
8ff132d
restore execute bit
darvark Jul 9, 2023
24449ac
Add keyword to start tlf in S&P mode
darvark Jun 23, 2023
6000ef6
Fix formating
darvark Jun 23, 2023
b956da7
Move setup operation mode to config file
darvark Jun 28, 2023
34ca3b2
Fix tests
darvark Jun 28, 2023
681436e
Post review fixes.
darvark Jun 29, 2023
d7575f9
Initial commit for Issue #348 - removing usage of fgets and switching…
darvark Jul 9, 2023
f25766a
redesign usage of getline
darvark Aug 1, 2023
18b5b58
Update usage of getline, free memory and check if getline read something
darvark Aug 2, 2023
f56f09c
Move setup operation mode to config file
darvark Jun 28, 2023
71f148c
Fix tests
darvark Jun 28, 2023
0d65145
Post review fixes.
darvark Jun 29, 2023
9191912
add errno handling
darvark Aug 3, 2023
7657f96
review fix
darvark Aug 3, 2023
347c23a
fix typo
darvark Aug 3, 2023
e2fd510
Merge branch '348-remove-fgets' of https://github.com/darvark/tlf-sp6…
darvark Aug 3, 2023
0c90128
restore test rules
darvark Aug 3, 2023
67d076f
restore run_tlf.py permissions
darvark Aug 3, 2023
eb7e2ef
fix astyle, add check to errno == ENOMEM
darvark Aug 4, 2023
ca794d3
add missing include errno.h
darvark Aug 4, 2023
487f21e
reformat and add check for memory
darvark Aug 6, 2023
683adb4
Revert "reformat and add check for memory"
darvark Aug 6, 2023
8b6e88c
restore default formating with astyle
darvark Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified scripts/play_vk
100755 → 100644
Empty file.
Empty file modified scripts/soundlog
100755 → 100644
Empty file.
42 changes: 22 additions & 20 deletions src/addmult.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*
*--------------------------------------------------------------*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "addmult.h"
#include "globalvars.h" // Includes glib.h and tlf.h
Expand Down Expand Up @@ -106,7 +106,8 @@ static int addmult_internal(struct qso_t *qso, bool check_only) {

/* -------------- unique call multi -------------- */
else if (unique_call_multi != MULT_NONE) {
mult_index = remember_multi(qso->call, qso->bandindex, unique_call_multi, check_only);
mult_index = remember_multi(qso->call, qso->bandindex, unique_call_multi,
check_only);
}

/* ------------ grid mult (per band) ------------- */
Expand All @@ -121,7 +122,6 @@ static int addmult_internal(struct qso_t *qso, bool check_only) {
check_only);
}


free(stripped_comment);
return mult_index;
}
Expand All @@ -134,8 +134,6 @@ int check_mult(struct qso_t *qso) {
return addmult_internal(qso, true); // check_only mode
}



/* -------------------------------------------------------------------*/

void addmult_lan(void) {
Expand Down Expand Up @@ -195,7 +193,6 @@ void addmult_lan(void) {

}


/* lookup n-th position in list of possible mults and
* return pointer to data structure */
possible_mult_t *get_mult_base(int n) {
Expand Down Expand Up @@ -269,7 +266,6 @@ gint cmp_size(char **a, char **b) {
return g_strcmp0(t1->name, t2->name);
}


/* parse a mult line and add data to database
*
* multiline consists of either
Expand Down Expand Up @@ -336,16 +332,17 @@ void add_mult_line(char *line) {
int init_and_load_multipliers(void) {

FILE *cfp;
char s_inputbuffer[2000] = "";
char *s_inputbuffer = NULL;
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);
}
mults_possible = g_ptr_array_new_with_free_func(free_possible_mult);


if (strlen(multsfile) == 0) {
return 0;
}
Expand All @@ -364,29 +361,34 @@ int init_and_load_multipliers(void) {
return 0; // couldn't open file
}

while (fgets(s_inputbuffer, sizeof(s_inputbuffer), cfp) != NULL) {
while ((read = getline(&s_inputbuffer, &s_inputbuffer_len, cfp)) != -1) {
if (s_inputbuffer_len > 0) {
if (errno == ENOMEM) {
fprintf(stderr, "Error in: %s:%d", __FILE__, __LINE__);
perror("RuntimeError: ");
exit(EXIT_FAILURE);
}
/* strip leading and trailing whitespace */
g_strstrip(s_inputbuffer);

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

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

add_mult_line(s_inputbuffer);

}

fclose(cfp);

if (s_inputbuffer > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing a pointer and 0. It works, but using the typical check

if (s_inputbuffer != NULL) 

would be clearer.

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);

return get_mult_count();
}


/** initialize mults scoring
*
* empties multis[] array, set the number of multis and multscore per band to 0.
Expand Down
8 changes: 4 additions & 4 deletions src/addspot.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void add_local_spot(void) {
static struct qso_t *find_last_qso() {
int i = NR_QSOS;
while (i > 0) {
struct qso_t *last_qso = g_ptr_array_index(qso_array, i-1);
struct qso_t *last_qso = g_ptr_array_index(qso_array, i - 1);
if (!last_qso->is_comment) {
return last_qso;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ static gchar *prepare_spot(void) {
freq_t spot_freq;
if (get_spot_data(&spot_call, &spot_freq)) {
spot_line = g_strdup_printf("DX %.1f %s ", spot_freq / 1000.,
spot_call);
spot_call);
g_free(spot_call);
} else {
beep();
Expand Down Expand Up @@ -206,7 +206,7 @@ static bool complete_spot(gchar **line) {
wmove(stdscr, LINES - 1, strlen(buffer) + 2);
}

g_free (*line);
g_free(*line);
*line = g_strdup(buffer);

return c != ESCAPE;
Expand All @@ -225,5 +225,5 @@ void add_cluster_spot(void) {
g_free(line);
}
}
g_free (spot_line);
g_free(spot_line);
}
54 changes: 27 additions & 27 deletions src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ static void sr_start(void);
void sr_stop();

void sound_setup_default(void) {
if (vk_record_cmd) g_free (vk_record_cmd);
if (vk_record_cmd) g_free(vk_record_cmd);
vk_record_cmd = g_strdup("rec -r 8000 $1 -q");

if (vk_play_cmd) g_free (vk_play_cmd);
if (vk_play_cmd) g_free(vk_play_cmd);
vk_play_cmd = g_strdup("play_vk $1");

if (soundlog_record_cmd) g_free (soundlog_record_cmd);
if (soundlog_record_cmd) g_free(soundlog_record_cmd);
soundlog_record_cmd = g_strdup("soundlog");

if (soundlog_play_cmd) g_free (soundlog_play_cmd);
if (soundlog_play_cmd) g_free(soundlog_play_cmd);
soundlog_play_cmd = g_strdup("play -q $1 2> /dev/null");

if (soundlog_dir) g_free (soundlog_dir);
if (soundlog_dir) g_free(soundlog_dir);
soundlog_dir = g_strdup("./soundlogs");
}

Expand All @@ -91,7 +91,7 @@ void sound_setup_default(void) {
*/
bool is_sr_running() {
char *lockfile = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S,
".VRlock", NULL);
".VRlock", NULL);
bool exists = (access(lockfile, F_OK) == 0);
g_free(lockfile);
return exists;
Expand All @@ -110,7 +110,7 @@ static void recordmenue(void) {
mvaddstr(6, 20, "F1 ... F12, S, C: Record Messages");

mvprintw(9, 20, "1; %s contest recorder",
is_sr_running() ? "Stop" : "Start");
is_sr_running() ? "Stop" : "Start");

mvaddstr(10, 20, "2: List and Play contest file");
mvaddstr(12, 20, "ESC: Exit sound recorder function");
Expand Down Expand Up @@ -273,10 +273,10 @@ static char *expand_directory(const char *dir) {
}

/* strip audio file suffix */
static char* strip_suffix(char * filename) {
static char *strip_suffix(char *filename) {
GRegex *regex = g_regex_new("\\.au$", 0, 0, NULL);
char *stripped_name = g_regex_replace(regex, filename, -1 , 0,
"", 0, NULL);
char *stripped_name = g_regex_replace(regex, filename, -1, 0,
"", 0, NULL);
g_regex_unref(regex);
return stripped_name;
}
Expand Down Expand Up @@ -312,7 +312,7 @@ static int sr_listfiles() {
g_free(printname);

i += 10;
if (i > 60) {
if (i > 60) {
i = 10;
j++;
}
Expand Down Expand Up @@ -340,8 +340,8 @@ static void sr_start(void) {
IGNORE(system("echo " " > ~/.VRlock"));

char *command = g_strconcat("mkdir -p ", soundlog_dir, "; ",
"cd ", soundlog_dir, "; ",
soundlog_record_cmd, " >/dev/null 2>/dev/null &", NULL);
"cd ", soundlog_dir, "; ",
soundlog_record_cmd, " >/dev/null 2>/dev/null &", NULL);

IGNORE(system(command));
g_free(command);
Expand All @@ -360,15 +360,15 @@ void sr_stop() {
static char *prepare_playback_command(char *filename) {
char *file = g_strconcat(filename, ".au", NULL);

GRegex *regex = g_regex_new("\\$1", 0, 0 , NULL);
GRegex *regex = g_regex_new("\\$1", 0, 0, NULL);
char *play_command = g_regex_replace(regex, soundlog_play_cmd, -1, 0,
file, 0, NULL);
g_regex_unref(regex);
file, 0, NULL);
g_regex_unref(regex);
g_free(file);

char *full_command = g_strconcat("cd ", soundlog_dir, "; ",
play_command, NULL);
g_free (play_command);
play_command, NULL);
g_free(play_command);

return full_command;
}
Expand All @@ -384,14 +384,14 @@ static void vk_do_record(int message_nr) {
move(17, 20);
refreshp();

GRegex *regex = g_regex_new("\\$1", 0, 0 , NULL);
GRegex *regex = g_regex_new("\\$1", 0, 0, NULL);
char *command = g_regex_replace(regex, vk_record_cmd, -1, 0,
ph_message[message_nr], 0, NULL);
ph_message[message_nr], 0, NULL);
g_regex_unref(regex);

/* let the command run in background so we can stop recording by
* <esc> key later */
char *reccommand = g_strconcat( command, " &", NULL);
char *reccommand = g_strconcat(command, " &", NULL);

IGNORE(system(reccommand));
g_free(command);
Expand All @@ -413,11 +413,11 @@ void *play_thread(void *ptr) {

pthread_detach(pthread_self());

vk_running=true;
vk_running = true;

GRegex *regex = g_regex_new("\\$1", 0, 0, NULL);
char *playcommand = g_regex_replace(regex, vk_play_cmd, -1, 0,
audiofile, 0, NULL);
audiofile, 0, NULL);
g_regex_unref(regex);
g_free(ptr);

Expand All @@ -441,7 +441,7 @@ void *play_thread(void *ptr) {
netkeyer(K_PTT, "0"); // ptt off
}

vk_running= false;
vk_running = false;

return NULL;
}
Expand All @@ -467,8 +467,8 @@ void vk_play_file(char *audiofile) {

/* play sound in separate thread so it can be killed from the main one */
if (pthread_create(&vk_thread, NULL, play_thread, (void *)file) != 0) {
g_free(file);
TLF_LOG_INFO("could not start sound thread!");
g_free(file);
TLF_LOG_INFO("could not start sound thread!");
}
}

Expand All @@ -480,7 +480,7 @@ void vk_stop() {

/* check if playing VK message is finished */
bool is_vk_finished() {
return (vk_running == false);
return (vk_running == false);
}


10 changes: 5 additions & 5 deletions src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

#include <stdbool.h>

extern char* vk_record_cmd;
extern char* vk_play_cmd;
extern char* soundlog_record_cmd;
extern char* soundlog_play_cmd;
extern char* soundlog_dir;
extern char *vk_record_cmd;
extern char *vk_play_cmd;
extern char *soundlog_record_cmd;
extern char *soundlog_play_cmd;
extern char *soundlog_dir;

void sound_setup_default(void);
void record(void);
Expand Down
Loading