1
0
mirror of https://github.com/abakh/nbsdgames.git synced 2024-12-04 14:46:22 -05:00

Write scores to a tmp file first, then rename it over the original score file.

This commit is contained in:
twistdroach 2023-02-03 00:27:32 -05:00
parent 3fc638064a
commit 191f818c93

View File

@ -20,6 +20,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "config.h" #include "config.h"
#define FOPEN_FAIL -10 #define FOPEN_FAIL -10
#define ENV_VAR_OR_USERNAME (getenv("NB_PLAYER")?getenv("NB_PLAYER"):getenv("USER")) #define ENV_VAR_OR_USERNAME (getenv("NB_PLAYER")?getenv("NB_PLAYER"):getenv("USER"))
#define MAXPATHSIZE 1000
FILE* score_file; FILE* score_file;
byte score_write(const char* path, long wscore, byte save_to_num){// only saves the top 10, returns the place in the chart byte score_write(const char* path, long wscore, byte save_to_num){// only saves the top 10, returns the place in the chart
score_file=fopen(path,"r"); score_file=fopen(path,"r");
@ -36,6 +37,9 @@ byte score_write(const char* path, long wscore, byte save_to_num){// only saves
#endif #endif
char name_buff[save_to_num_][60]; char name_buff[save_to_num_][60];
long score_buff[save_to_num_]; long score_buff[save_to_num_];
char tmp_path[MAXPATHSIZE + 8] = {0};
strcpy(tmp_path, path);
strcat(tmp_path, ".XXXXXX");
memset(name_buff,0,save_to_num_*60*sizeof(char) ); memset(name_buff,0,save_to_num_*60*sizeof(char) );
memset(score_buff,0,save_to_num_*sizeof(long) ); memset(score_buff,0,save_to_num_*sizeof(long) );
@ -52,8 +56,8 @@ byte score_write(const char* path, long wscore, byte save_to_num){// only saves
memset(scanned_name,0,60); memset(scanned_name,0,60);
scanned_score=0; scanned_score=0;
} }
score_file = fopen(path,"w+") ;//will get rid of the previous text FILE* tmp_score_file = fdopen(mkstemp(tmp_path), "w");
if(!score_file){ if(!tmp_score_file){
return FOPEN_FAIL; return FOPEN_FAIL;
} }
byte scores_count=location;//if 5 scores were scanned, it is 5. the number of scores it reached byte scores_count=location;//if 5 scores were scanned, it is 5. the number of scores it reached
@ -62,32 +66,41 @@ byte score_write(const char* path, long wscore, byte save_to_num){// only saves
for(byte i=0;i<=scores_count && i<save_to_num_-wrote_it;++i){ for(byte i=0;i<=scores_count && i<save_to_num_-wrote_it;++i){
if(!wrote_it && (i>=scores_count || wscore>=score_buff[i]) ){ if(!wrote_it && (i>=scores_count || wscore>=score_buff[i]) ){
fprintf(score_file,"%s : %ld\n",ENV_VAR_OR_USERNAME,wscore); fprintf(tmp_score_file,"%s : %ld\n",ENV_VAR_OR_USERNAME,wscore);
ret=i; ret=i;
wrote_it=1; wrote_it=1;
} }
if(i<save_to_num_-wrote_it && i<scores_count){ if(i<save_to_num_-wrote_it && i<scores_count){
fprintf(score_file,"%s : %ld\n",name_buff[i],score_buff[i]); fprintf(tmp_score_file,"%s : %ld\n",name_buff[i],score_buff[i]);
} }
} }
fflush(score_file); fflush(tmp_score_file);
fclose(score_file);
if (rename(tmp_path, path) < 0) {
return FOPEN_FAIL;
}
fclose(tmp_score_file);
score_file=fopen(path,"r");
if (!score_file) {
return FOPEN_FAIL;
}
return ret; return ret;
} }
byte fallback_to_home(const char* name,long wscore,byte save_to_num){// only saves the top 10, returns the place in the chart byte fallback_to_home(const char* name,long wscore,byte save_to_num){// only saves the top 10, returns the place in the chart
byte ret; byte ret;
char full_path[1000]={0}; char full_path[MAXPATHSIZE]={0};
if(getenv("NB_SCORES_DIR")){ if(getenv("NB_SCORES_DIR")){
snprintf(full_path,1000,"%s/%s",getenv("NB_SCORES_DIR"),name); snprintf(full_path,MAXPATHSIZE,"%s/%s",getenv("NB_SCORES_DIR"),name);
ret=score_write(full_path,wscore,save_to_num); ret=score_write(full_path,wscore,save_to_num);
if(ret==FOPEN_FAIL){ if(ret==FOPEN_FAIL){
return ret;//do not fallback this return ret;//do not fallback this
} }
} }
snprintf(full_path,1000,"%s/%s",SCORES_DIR,name); snprintf(full_path,MAXPATHSIZE,"%s/%s",SCORES_DIR,name);
ret=score_write(full_path,wscore,save_to_num); ret=score_write(full_path,wscore,save_to_num);
if(ret==FOPEN_FAIL){ if(ret==FOPEN_FAIL){
snprintf(full_path,1000,"%s/.%s",getenv("HOME"),name); snprintf(full_path,MAXPATHSIZE,"%s/.%s",getenv("HOME"),name);
ret=score_write(full_path,wscore,save_to_num); ret=score_write(full_path,wscore,save_to_num);
} }
return ret; return ret;