/* Authored by abakh To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include "config.h" #define FOPEN_FAIL -10 #define ENV_VAR_OR_USERNAME (getenv("NB_PLAYER")?getenv("NB_PLAYER"):getenv("USER")) #define MAXPATHSIZE 1000 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 score_file=fopen(path,"r"); if(!score_file){ score_file=fopen(path,"w"); if(!score_file){ return FOPEN_FAIL; } } #ifdef NO_VLA #define save_to_num_ 10 #else //such a dirty cpp hack byte save_to_num_=save_to_num; #endif char name_buff[save_to_num_][60]; 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(score_buff,0,save_to_num_*sizeof(long) ); long scanned_score =0; char scanned_name[60]={0}; byte location=0; while( fscanf(score_file,"%59s : %ld\n",scanned_name,&scanned_score) == 2 && location=scores_count || wscore>=score_buff[i]) ){ fprintf(tmp_score_file,"%s : %ld\n",ENV_VAR_OR_USERNAME,wscore); ret=i; wrote_it=1; } if(i