This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfiles.h
executable file
·464 lines (439 loc) · 12.4 KB
/
files.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//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 <copy and save user_speed.info file if you would need it in future>\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 <command mkuser \"username\"> 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
{
time_t timer = time(NULL);
char time_nw[18];
strcpy(time_nw, ctime(&timer));
short i=0;
/*
*removing trailing newline
*/
while(i<=200){
if(time_nw[i]=='\n'){
time_nw[i] = '\0';
break;
}
i++;
}
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 -------------------+\n",time_nw);//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
*/
char get_user_name(char *user_name)
{
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);
while((name_ch=getc(fp))!='\n'&&name_ch!=EOF){
//putchar(name_ch);
user_name[i] = name_ch;
i++;
}
user_name[i]='\0';
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)
{
char *user_name;
get_user_name(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, <try again>");
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");
}
}
}
else printf("%s\n", "Soft reset successful");
}
/*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);
}
}
/*
*Limit the size of speed file to 10KB; occurs after 429 rounds
*/
extern void lmt_pg_size()//limit page size
{
FILE *fp;
char user_name[81];
if((fp=fopen("./speed/user_speed.info","r"))==NULL){
fprintf(stderr, "%s\n", "Fatal Error, Some files are missing");
exit(EXIT_FAILURE);
}
fseek(fp,0,SEEK_END);
if(ftell(fp) >= 100000){
fprintf(stderr, "%s\n", "speed.info file in ~/cmtypist/speed has grown too large, copy and save if you wish to preserve it\n");
printf("\n:The speed.info file will be cleared, continue: [y/n]:");
if(get_only_char()!='n'){
get_user_name(user_name);
if((fp = freopen("./speed/user_speed.info","w+", fp))==NULL){
fprintf(stderr, "%s\n", "unable to reset speed.info file, alternatively run 'cmdtypist rest raw to fix this'\n");
exit(EXIT_FAILURE);
}
rewind(fp);
fputs(user_name,fp);
fputc('\n',fp);
rewind(fp);
}
}
if(fclose(fp)){
fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n");
exit(EXIT_FAILURE);
}
}
/**
* @param void
* performs a test to make sure cmdtypist has been installed to home directory
* installed? true: it changes the current directory the the home/cmdTypist directory
* false: it gives a warning message with possible fixes and continues
*/
extern void redirect(void)
{
char idir[62] = "";
if(getenv("HOME") != NULL)
{
strncpy(idir,getenv("HOME"), 50);
strcat(idir, "/cmdTypist");//installation directory
printf("%s\n", idir);
}
// exit(0);
FILE *fp;
//call script that verifies if program is installed to $home/cmdTypist
char test_dir[60] = "";
strncpy(test_dir, idir, 50);
strcat(test_dir, "/files.h");
if((fp=fopen(test_dir,"r")) == NULL)
{
fprintf(stderr, "%s\n", "Program not installed to $HOME/cmdTypist directory\n");
fprintf(stderr, "\t- %s\n", "Navigate to cmdtypist directory manually, before running it\n");
fprintf(stderr, "\t- %s\n", "OR uninstall and reinstall a fresh copy\n");
return;
}else{
//system(idir); //navigate to installation directory
//change directory
if(chdir(idir) == -1){
fprintf(stderr, "%s\n", "Error changing directory to $HOME/cmdTypist");
exit(EXIT_FAILURE);
}
}
if(fclose(fp)){
fprintf(stderr, "%s\n", "Fatal Error, Unable to close some files\n");
exit(EXIT_FAILURE);
}
}