//files.h: file manipulations /* Program title: CMDTYPIST Author: Chiatiah Calson License: GPL 3 or later versions Date and Time: 5 July 2017 @ 10:40PM Program Size: 2.8 */ void test_new_user(char *name)//used to create new user name { if(first_time()!=0)//if a user exist { //char ch='z';//setting a value so that the value initialized by default is never y or n printf("\nWARNING: All saved data will be erased \n" "Do you want to continue: [y/n]:"); if(get_only_char()=='n') exit(EXIT_FAILURE); } confirm_user_exist(); strcat(upper_case(name),"\n"); if(strlen(name)<3) { fprintf(stderr, "%s %s\n", name,"too short, cannot be a user name");//exiting when a wrong value is entered exit(EXIT_FAILURE); } FILE *fp; if((fp=fopen("./speed/user_speed.info","w"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } rewind(fp); fprintf(fp, "%s\n", name); fget_ignore_newline(name); printf("\n%s %s\n",name,"written with success"); if(fclose(fp)) { fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n"); exit(EXIT_FAILURE); } } /*Ensures atleast one user account exist*/ extern void user_test(void) { FILE *fp; if((fp=fopen("./speed/user_speed.info","r"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } rewind(fp); char ch=getc(fp); if(ch==EOF)//checking if at the beginning of a character there is a charater, if not exist { fprintf(stderr, "%s\n", "No user exists yet, use to add new user"); exit(EXIT_FAILURE); } if(fclose(fp)) { fprintf(stderr, "%s\n", "Unable to close lesson file"); exit(EXIT_FAILURE); } } void write_user_speed(unsigned int session_time,int wrongly_typed, float typed)//Elapsed time is time taken to type { FILE* fp; char time_conversion[36];//stores converted time by seconds_hms() if((fp=fopen("./speed/user_speed.info","r+"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } float raw_speed=0;//holds the speed when not ajusted. float adj_speed=0;//holds the adjusted speed make_current(fp); fseek(fp,0L,SEEK_END);//moving to end of file fprintf(fp, "\t+------------------%s %s %s----------------------+\n", __DATE__,"at",__TIME__);//printing date and time when the test is done seconds_to_hms(session_time,time_conversion); //fprintf(fp, "\t|%d%s TIME: %s\n",wrongly_typed,(wrongly_typed==0)? "(NO Error)":(wrongly_typed==1)? "ERROR":"ERRORS",time_conversion); raw_speed= ((float)typed*60.0f)/(float)session_time; adj_speed= (float) (((float)typed-wrongly_typed)*60.0f)/(float)session_time; fprintf(fp, "\t|ERROR(s):%u TIME: %s SPEED: ~%.3fWPM Accuracy: %.2f%%\n",wrongly_typed,time_conversion,raw_speed/5.0f,(adj_speed/raw_speed)*100.0f); fprintf(fp, "\t|%s%.2fCPM ~= %.2fWPM \n", "AVERAGE SPEED: ",adj_speed,adj_speed/5.0); fprintf(fp, "%s\n\n", "\t+---------------------------------------------------------------+"); if(fclose(fp)) { fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n"); exit(EXIT_FAILURE); } } /*Reads user information already opened by main, writes to it the name with which the user wants to create an account with. Also reads system time and date and wite agains that user name*/ void accounts_create(void) { FILE *fp; if((fp=fopen("user.info","a+"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } strcpy(user_info,user_name); strcat(user_info,__DATE__); strcat(user_info,__TIME__); user_info[strlen(user_info)]='\n'; user_info[strlen(user_info)]='\0'; fprintf(fp, "%s", user_info); if(fclose(fp)) { fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n"); exit(EXIT_FAILURE); } } /*Reads pariticular user information and stores in a global static variable user_info using the fact that the end of each user information ends in new line*/ void get_user_name(void) { char name_ch; int i=0; char ch; FILE *fp; if((fp=fopen("./speed/user_speed.info","r"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } rewind(fp); printf(""GREEN" "); while((name_ch=getc(fp))!='\n'&&name_ch!=EOF) putchar(name_ch); //printf(""RESET"\n"); if(fclose(fp)) { fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n"); exit(EXIT_FAILURE); } } /*Selects the user playing based on the name sent to it via command line*/ long int select_user(void) { FILE *user_ptr; if((user_ptr=fopen("user.info","r+"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } int get_count=0;//used to regulate first user since there is no newline before the first user char ch; rewind(user_ptr); unsigned short int u; int i=0; while(u!=369) { get_user_name(); get_count++; strcpy(temp_name,user_info); u=0; while(u!=369) { if((char)temp_name[u]=='.') break; u++; } temp_name[u+1]='\0'; if(strcmp(temp_name,user_name)==0) { //fseek(user_ptr,select_user(),SEEK_CUR);//Moving back to start reading from the correct position break; } //else; i++; if(i==3000) { fprintf(stderr, "%s\n", "The account name you entered is incorrect, "); exit(EXIT_FAILURE); } } if(get_count==1) rewind(user_ptr); else { fseek(user_ptr,-2L,SEEK_CUR);//Moving backward twice (escape new ling and string terminator) while((ch=fgetc(user_ptr))!='\n'&&ch!=EOF) fseek(user_ptr,-2L,SEEK_CUR);//moving backward twice since the while statement causes a single forward movement //to prevent reading of just one character that leads to infinite loop } fflush(user_ptr);//needed to clear buffer to prevnet ftell from reading wrong values from text files. return ftell(user_ptr); if(fclose(user_ptr)) { fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n"); exit(EXIT_FAILURE); } } //Initial settings of cmdtypist. void reset_default_config(char *raw, int argc_cmd) { write_myown_config(1); write_conf_mode(1); write_conf_block_read(4); sound_config_write(0); write_message_conf(0); if(argc_cmd==3) { if(strcmp(raw,"raw")==0) { printf("%s", "Raw reset, resets everything including speed history; continue? [y/n]:"); if(get_only_char()=='n') exit(EXIT_SUCCESS); else { FILE *fp; if((fp=fopen("./speed/user_speed.info","w+"))==NULL) { fprintf(stderr, "%s\n", "Could not reset, some files are missing"); exit(EXIT_FAILURE); } fclose(fp); if((fp=fopen("my_own.txt","w+"))==NULL) { fprintf(stderr, "%s\n", "Could not reset, some files are missing"); exit(EXIT_FAILURE); } fclose(fp); printf("%s\n", "User settings reset to default"); } } } } /*Function takes the name of a file, and appends .txt to it, then tries to read it from external storage and determine the total number of characters it contains"*/ long int read_file_size(FILE* name_to_read) { char ch; int size=0; rewind(name_to_read); while((ch=getc(name_to_read))!=EOF) size++; return size; } /*Writes the string that is given to it as a parameter, replaces every other string on the line to which it is writing to space and finally writes a newline to the end of the line*/ void write_to_line(char* to_write, FILE* fp) { int i=0; char ch; //while((ch=fgetc(fp))!='\n')//Ensuring the end of file is not reached //{ if((strlen(new_name))>=strlen(user_info))//starts printing in the first position of the line fprintf(fp, "%s", to_write);//write name else { fprintf(fp, "%s", to_write); while((ch=getc(fp))!='\n') { fseek(fp,-1L,SEEK_CUR); fprintf(fp, "%c", ' '); } /*if((ch=getc(fp))!='\n'&&ch!=' ') fseek(fp,-1L,SEEK_CUR);//moving backward one space since getc above had moved one space ahead //so as to write over the data existing. //fprintf(fp, "%s", " "); /*if(ch=='\n') { fseek(fp,-1L,SEEK_CUR); fprintf(fp, "%c", '\n');//writing newline at the end of file since it had been replaced with a space string break;//escaping since the end of the line has been reached } */ } // i++; //} } void swap_modify_name(int length_of_string) { int i=0; //301, is just an exiting value while(i<=301) { int j=0; if(user_info[i]=='.') { while(j<=301) { //strcat(new_name,user_info[j+1]); new_name[length_of_string+j]=user_info[j+i]; if(j==strlen(user_info)-i) { j=300;//value to help for an exit since the length of the sring is never more than 81 i=300; } j++; } } i++; } new_name[strlen(new_name)]='\n'; new_name[strlen(new_name)]='\0'; } /*Opens the user file and over writes former name with new name, uses a new array already containing the new name and writes to it the date and time for the former user, it then writes this new array to the user info file*/ void edit_name(void) { FILE *fp; if((fp=fopen("user.info","r+"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } fseek(fp,select_user(),SEEK_SET); write_to_line(new_name,fp); if(fclose(fp)) { fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n"); exit(EXIT_FAILURE); } } /*used to remove all non standard ascii codes from user text*/ extern void remove_ext_ascii(void) { srand((unsigned)time(NULL)); FILE *fp; if((fp=fopen(file_to_read,"r+"))==NULL) { fprintf(stderr, "%s\n", "Fatal Error, Some files are missing"); exit(EXIT_FAILURE); } rewind(fp); char ch,f; while((ch=getc(fp))!=EOF) { if(ch!='\n') if(!(isascii(ch))) { ch=rand()%15+33; //fprintf(stderr, "%c", ch); //f=getchar(); fseek(fp,-1L,SEEK_CUR); putc(ch,fp); } } if(fclose(fp)) { fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n"); exit(EXIT_FAILURE); } }