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 36 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
2 changes: 1 addition & 1 deletion ToDo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Planned for later versions
list of most used RST values which will then be used for scrolling,
e.g. CHANGE_RST=339,479,599 or similar.
[x] Have a template for cabrillo file header (suggested by Joop PG4I)
[ ] Add a keyword to startup TLF in S&P mode (should be an easy one)
[x] Add a keyword to startup TLF in S&P mode (should be an easy one)
[ ] switch between ESM and CT mode during contest (Nate N0NB)
[x] Send Morse via Hamlib for radios that support it.
Tnx Christoph DF7CB and others.
Expand Down
Empty file modified scripts/play_vk
100755 → 100644
Empty file.
Empty file modified scripts/soundlog
100755 → 100644
Empty file.
9 changes: 9 additions & 0 deletions share/logcfg.dat
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ CALL=NOCALL
#
#################################
# #
# OPERATING MODE #
# #
#################################
# Can be set to CQ, S&P
OPERATING_MODE=CQ
#
#
#################################
# #
# Time offset from UTC #
# #
#################################
Expand Down
61 changes: 30 additions & 31 deletions src/addmult.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*
*--------------------------------------------------------------*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -36,7 +35,6 @@
#include "utils.h"
#include "bands.h"


GPtrArray *mults_possible;

/*
Expand Down Expand Up @@ -106,7 +104,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 +120,6 @@ static int addmult_internal(struct qso_t *qso, bool check_only) {
check_only);
}


free(stripped_comment);
return mult_index;
}
Expand All @@ -134,8 +132,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 +191,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 +264,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,57 +330,62 @@ 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);
/* 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 (fgets(s_inputbuffer, sizeof(s_inputbuffer), cfp) != NULL) {

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

while ((read = getline(&s_inputbuffer, &s_inputbuffer_len, cfp)) != -1) {
if (s_inputbuffer_len > 0) {
darvark marked this conversation as resolved.
Show resolved Hide resolved
/* 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 {
perror("RuntimeError: ");
Copy link
Member

Choose a reason for hiding this comment

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

This (and the other uses of perror()) is not too informative. If the (un)expected error occurs, you will get a message:

RuntimeError: 

You should consider to use fprintf(stderr, "RuntimeError: %s:%ld", __FILE__, __LINE__)

Copy link
Contributor Author

@darvark darvark Aug 3, 2023

Choose a reason for hiding this comment

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

According to documentation message will be: for code example:

int main()
{
    perror( NULL );
    FILE *fp = fopen( "nonexisting.file", "rb" );
    if( !fp )
         perror( "RuntimeError: " );
    else
         fclose( fp );
   
    return 0;
}

"RuntimeError: No such file or directory" but if fprintf is a standard I can change it. Eventually add that additional error message.

obraz

Copy link
Member

Choose a reason for hiding this comment

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

According to documentation message will be: for code example:

Oh, that's new for me - thanks.

Then that would be fine for me.

but if fprintf is a standard I can change it.

I don't believe there is a "standard" way. In the (Tlf's) code above you check the result of memory allocation. Theoretically you will get an error message something like "Not enough memory" - without any context (eg. in which file and which function triggered the error). This is why I prefer the fprintf() with that way, but I do not consider it as "standard".

As I wrote, this would be fine for me. (With one note: this string is enough for argument: "RuntimeError" - no need the : and extra space)

exit(EXIT_FAILURE);
}
}

fclose(cfp);

if (s_inputbuffer_len > 0)
Copy link
Member

Choose a reason for hiding this comment

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

It would be nicer to check the pointer itself, as you do it here:

    if (s_inputbuffer != NULL) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh my mistake, vscode automatically suggested that variable. Fixed in in comming commit

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
Loading