Skip to content
Open
Changes from all commits
Commits
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
13 changes: 9 additions & 4 deletions rffit.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ int main(int argc,char *argv[])
float t,f,vtca,foffset=0.0;
char c,nfdtca[32]="2014-01-01T00:00:00",nfd[32]="2014-01-01T00:00:00",nfdepoch[32]="2014-01-01T00:00:00";
int mode=0,posn=0,click=0;
char *catalog=NULL,*datafile=NULL,filename[64],string[64],bstar[10]=" 00000-0";
char *catalog=NULL,*datafile=NULL,filename[64],default_filename[64],string[64],bstar[10]=" 00000-0";
int arg=0,nobs=0;
FILE *fp,*std,*fpres;
char line0[72],line1[72],line2[72];
Expand Down Expand Up @@ -1066,9 +1066,14 @@ int main(int argc,char *argv[])

// Save
if (c=='S') {
printf("%s_%.3f_%04d_%05d.dat\n",nfdtca,d.ffit/1000.0,d.p[0].site_id,satno);
printf("Save highlighted points, provide filename: ");
status=scanf("%s",filename);
snprintf(default_filename,sizeof(default_filename), "%s_%.3f_%04d_%05d.dat",nfdtca,d.ffit/1000.0,d.p[0].site_id,satno);
printf("Save highlighted points, provide filename [type 'default' for %s]: ",default_filename);
status = scanf("%s",filename);

if (strcmp(filename, "default") == 0 ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

From the PR description, you can just type enter. From the code you have to explicitly type 'default'.
I would prefer the behavior of the description. So if the string is empty, use the proposed default, otherwise the provided filename.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

scanf does not allow for just entering empty string (simply hitting enter) or to be more precise - i did not found a way to just accept empty string - if you have an idea on how to handle both hitting enter to get default value and be able to provide filename then it would be most welcome.

Copy link
Collaborator

Choose a reason for hiding this comment

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

May i propose following improvement:
(it uses fgets() instead of scanf(), which is also robust against accidental overflow)

diff --git a/rffit.c b/rffit.c
index 55dd8c9..fb50a68 100644
--- a/rffit.c
+++ b/rffit.c
@@ -1067,15 +1067,19 @@ int main(int argc,char *argv[])
     // Save
     if (c=='S') {
       snprintf(default_filename,sizeof(default_filename), "%s_%.3f_%04d_%05d.dat",nfdtca,d.ffit/1000.0,d.p[0].site_id,satno);
-      printf("Save highlighted points, provide filename [type 'default' for %s]: ",default_filename);
-      status = scanf("%s",filename);
+      printf("Save highlighted points, provide filename [%s]: ", default_filename);
 
-      if (strcmp(filename, "default") == 0 ) {
-         strcpy(filename,default_filename);
-      }
+      if (fgets(filename, sizeof(filename), stdin)) {
+        // Strip newline
+        filename[strcspn(filename, "\n")] = '\0';
 
-      save_data(xmin,ymin,xmax,ymax,filename);
-      printf("\n================================================================================\n");
+        if (strlen(filename) == 0) {
+          strcpy(filename,default_filename);
+       }
+
+        save_data(xmin,ymin,xmax,ymax,filename);
+        printf("\n================================================================================\n");
+      }
     }
 
     // Unselect

Also i retargeted your PR to the dev branch, so we can test the new features on the dev branch before forwaring main to it.

strcpy(filename,default_filename);
}

save_data(xmin,ymin,xmax,ymax,filename);
printf("\n================================================================================\n");
}
Expand Down