forked from Typeaway14/TYRBO-on-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_content.c
597 lines (539 loc) · 14.9 KB
/
type_content.c
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//This file contains the definitions of the functions defined in type_content.h
//Typing related functions can be found here
//For further details about a particular function, refer to documentation in respective header files.
#include<stdio.h>
#ifdef _WIN32
#include<windows.h>//includes windows.h that contains functions like Sleep()
#include<conio.h>//includes conio.h that contains functions like getch()
#elif __linux__
#include<sys/ioctl.h>
#include<unistd.h>//usleep()
#include <termios.h>
#endif
#include<string.h> //includes string.h that contains string manipulation functions like strcmp() strlen()
#include<time.h>
#include<stdlib.h>
// #include"menu.h"
#include"type_content.h"//includes the user defined type_content.h header file
// #include"user_manage.h"//includes the user defined user_manage.h header file
#include"tc.h"
// SCORE scores[100];
void caps_check();
int score_n;
FILE *score_fp;
//Function to return random strings from a file
//with the number of lines of the file as a parameter
char* rand_string(char* fname,int charno,char* sent)
{
//Opening the file in read form
FILE *fp=fopen(fname,"r");
//Display that Openeing of the file failed if the file has NULL content
if(fp==NULL)
{
printf("failed");
}
//Opening the file in read form to count the number of lines
FILE *fpcount=fopen(fname,"r");
//int variable to count number of lines
int lines=0;
//Loop to count number of lines
for(;fgets(sent,300,fpcount);)
{
//Incrementing number of lines
lines++;
}
//seeds the random number generator used by the function rand with
//seed as time(NULL) returns the integer value to be used as seed
srand(time(NULL));
//int variable r to store random variable in an iteration
int r=0;
//Loop to iterate until r is greater than 0
while(!(r>0))
{
//r is assigned to a random number from 0 to number of lines in fpcount
r=rand() % lines;
}
//Loop to iterate until fp's contents are not all read and
//Iteration count is lesser than the random variable
for(int i=0;fp!=NULL && i<r;i++)
{
fgets(sent,charno,fp);
}
//closing file fp
fclose(fp);
//Returning the random strings
return sent;
}
//Function to copy code from the file and copy it onto the
//place to be typed on
void type_launch(char* diff,char* sent,char gmode)
{
strcpy(sent,rand_string(diff,300,sent));
int size=strlen(sent)-1;
type_disp(sent,size,gmode);
}
//Function to return a character based on a random parameter
//ranging from 0 to 3
char rand_mode()
{
//seeds the random number generator used by the function rand with
//seed as time(NULL) returns the integer value to be used as seed
srand(time(NULL));
//int variable r to store random variable in an iteration
int r=0;
//Loop to iterate until r is greater than 0
while(!(r>0))
{
//r is assigned to a random number from 0 to 3 of lines in fpcount
r=rand() % 3;
}
//Conditional statement to return character based on random number from 0-3
switch(r)
{
case 1:
return 'n';
case 2:
return 's';
case 3:
return 'b';
default:
return 'n';
}
}
//Function to display type utilizing type_input function
int type_disp( char* p,int size, char gmode)
{
TC_CLRSCR();
char tmp[size];
strcpy(tmp,p);
int x=0,y=0,rows=0,columns=0;
coord_details(&rows,&columns,&x,&y,size);
int ycopy=y;
TC_CLRSCR();
TC_MOVE_CURSOR(x,y);
if(p==NULL)
return -1;
for(int i=1;*p;i++)
{
if(i==columns-1)
{
i=1;
ycopy++;
TC_MOVE_CURSOR(x,ycopy);
strcat(tmp," ");
}
//
printf("%c\xDB",*(p++));
//
printf("\b \b");
}
TC_MOVE_CURSOR(x,y);
return type_input(tmp,size,gmode);
}
//Function to check typing input
int type_input(char* p,int size,char gmode)
{
#ifdef _WIN32
CLEAR_INSTREAM;
#elif __linux__
//system("kendi.sh 0");
#endif
printf("\e[?25l");
clock_t t;
char ch='a';
int BBscore=0;
int tmp=1;
int count=0;
int streak=0;
int x=0,y=0,rows=0,columns=0;
coord_details(&rows,&columns,&x,&y,size);
FILE *fp;
fp=fopen("resources/art/BB_Launch.txt","r");
char bball=fgetc(fp);
int b=1;
for(;((*(p+1))!='\0')&&(ch=getch());)
{
caps_check();
if(tmp)
{
t=clock();
tmp--;
}
if(ch==*p)
{
TC_MOVE_CURSOR(x,y);
printf("%s\b",p+1);
p+=1;
count+=1;
trimTrailing(p);
streak++;
}
else
{
if(gmode=='z')
return 0;
if(gmode=='s')
{
size=count;
break;
}
handle_wrong_case(fp,&b,&streak,&count,p,x,y,0);
}
if((gmode=='b') && (streak>0) && (streak%5==0))
{
TC_MOVE_CURSOR(0,b);
for(;bball!='\n';)
{
printf("%c",bball);
bball=fgetc(fp);
}
b++;
bball=fgetc(fp);
if(streak==65)
{
if(bball_dunk())
{
#ifdef __linux__
// system("kendi.sh 1");
#endif
art_disp("resources/art/BB_Dunk.txt");
BBscore+=25;
}
else
{
#ifdef __linux__
// system("kendi.sh 1");
#endif
art_disp("resources/art/OOF.txt");
BBscore+=5;
}
#ifdef _WIN32
Sleep(1000);
#elif __linux__
usleep(1000000);
#endif
#ifdef _WIN32
CLEAR_INSTREAM;
#elif __linux__
// clear_instream();
#endif
TC_CLRSCR();
caps_check();
ungetc('\n',stdin);
ch=getc(stdin);
count--;
handle_wrong_case(fp,&b,&streak,&count,p,x,y,1);
TC_CLRSCR();
caps_check();
TC_MOVE_CURSOR(x,y);
printf("%s",p);
#ifdef __linux__
// system("kendi.sh 0");
#endif
}
}
}
if(gmode!='z')
{
TC_CLRSCR();
t = clock() - t;
float time_taken = ((float)t)/CLOCKS_PER_SEC; // calculate the elapsed time
#ifdef __linux__
time_taken= ((time_taken)*1000)/2;
#endif
score(time_taken,count,size,gmode,BBscore);
}
return 1;
}
//Function to find score of typing speed test
void score(float time_taken,int count,int size,char gmode,int BBscore)
{
//Displaying the text using escape sequence escape character
printf("\e[?25l");
//Moving cursor from 0 to 0
TC_MOVE_CURSOR(0,0);
//Increasing the count received through the argument
count++;
//Finding word per minute
float wpm=((size/5)/(time_taken/60));
//Finding accuracy of the typing
float acc=((float)size/(float)count)*100;
//Finding net word per minute
float netwpm=wpm*(acc/100);
if(gmode!='s')
{
//Displaying details of time taken to type, words per minute
//and words per minute after checking accuracy
printf("\nTime Taken: %s%.2fs%s\nCharacters: %s%d/%d%s",TC_GRN,time_taken,TC_NRM,TC_GRN,++count,size,TC_NRM);
printf("\nYour WPM was: %s%.1f%s",TC_GRN,wpm,TC_NRM);
printf("\nYour net WPM was: %s%.1f%s",TC_GRN,netwpm,TC_NRM);
//conditional statement for 100% accuracy
if(acc==100.0)
{
printf("\nYour accuracy was: %s",TC_CYN);
}
//conditional statement for >=95.0% accuracy
else if(acc>=95.0)
{
printf("\nYour accuracy was: %s",TC_MAG);
}
//conditional statement for >=90.0 && <95.0 accuracy
else if(acc>=90.0 && acc<95.0)
{
printf("\nYour accuracy was: %s",TC_GRN);
}
//conditional statement for >=80.0 && acc<90.0 accuracy
else if(acc>=80.0 && acc<90.0)
{
printf("\nYour accuracy was: %s",TC_YEL);
}
//conditional statement for <80 accuracy
else
{
printf("\nYour accuracy was: %s",TC_RED);
}
printf("%.1f%c%s",acc,'%',TC_NRM);
}
//Conditional statements based on the random characters from function rand_mode
if(gmode=='n')
{
// score_save(1,&wpm,&acc,&netwpm,-1);
}
if(gmode=='s')
{
TC_CLRSCR();
TC_MOVE_CURSOR(0,0);
printf("\nTime Taken: %s%.2fs%s\nCharacters: %s%d%s",TC_GRN,time_taken,TC_NRM,TC_GRN,--count,TC_NRM);
}
else if(gmode=='b')
{
printf("\nYour Score was: %s%d%s",TC_GRN,BBscore,TC_NRM);
// score_save(2,&wpm,&acc,&netwpm,BBscore);
}
//To continue after results
printf("\n%sPress any key to continue%s",TC_YEL,TC_NRM);
//Delaying the script execution based on operating system being
//Windows or Linux
#ifdef _WIN32
Sleep(1500);
#elif __linux__
usleep(1500000);
#endif
getch();
}
//Function to encounter spaces, tabs and next lines
void trimTrailing(char * str)
{
printf("\e[?25l");
int index, i;
index = -1;//Set default index
i = 0;//Find last index of non-white space character
while(str[i] != '\0')
{
if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n')
{
index= i;
}
i++;
}
str[index + 2] = '\0';//Mark next character to last non-white space character as NULL
}
//Function to check wrong cases and highlight them red
int handle_wrong_case(FILE* fp,int* b,int* streak,int* count,char* p,int x,int y,int color)
{
TC_CLRSCR();
caps_check();
fseek(fp,0,SEEK_SET);
*b=0;
TC_MOVE_CURSOR(x,y);
if(*p==' ')
{
printf("%s%c%s%s\b",TC_RED,'_',TC_NRM,p+1);
*streak=0;
*count+=1;
return 1;
}
printf("%s%c%s%s\b",TC_RED,*(p),TC_NRM,p+1);
if(color)
{
TC_MOVE_CURSOR(x,y);
printf("%s\b",p);
}
*streak=0;
*count+=1;
return 1;
}
//Function to display the text chosen randomly from the database of files
void art_disp(char *filename)
{
printf("\e[?25l");
FILE *fp;
fp=fopen(filename,"r");
char ch[200];
//fscanf(fp,"%c",&ch);
TC_CLRSCR();
TC_MOVE_CURSOR(0,0);
while(fgets(ch,200,fp)!=NULL)
{
printf("%s%s%s",TC_GRN,ch,TC_NRM);
#ifdef _WIN32
Sleep(16);
#elif __linux__
usleep(16000);
#endif
}
// while(ch!=EOF)
// {
// ch=fgetc(fp);
// // Sleep(1);
// printf("%s%c%s",TC_GRN,ch,TC_NRM);
// }
fclose(fp);
}
//Function to find whether
int bball_dunk()
{
#ifdef __linux__
// system("kendi.sh 1");
#endif
//Opening file resources/Dunk_words.csv in read mode
FILE *frand;
frand=fopen("resources/Dunk_words.csv","r");
srand(time(NULL));
int r=0;
//Loop to iterate until r is not greater than zero
while(!(r>0))
{
//r is assigned to a random number from 0 to 3 of lines in fpcount
r=rand() % 213;
}
char ch[1500];
char tmp[6];
// char tmp2[6];
//Conditional statement to check if resources file is not equal to NULL
if(fgets(ch,1500,frand)==NULL)
{
TC_CLRSCR();
printf("Something went wrong..\nReturning back to home page..\n");
#ifdef _WIN32
Sleep(200);
#elif __linux__
usleep(200000);
#endif
}
//Converting string to token
char* tmp2=strtok(ch,",");
//Checking if token is NULL and copying value to tmp
if(tmp2!=NULL)
{
strcpy(tmp,tmp2);
}
//If token is not null printing message that something went wrong and delaying
else
{
TC_CLRSCR();
printf("Something went wrong..\nReturning back to home page..\n");
#ifdef _WIN32
Sleep(200);
#elif __linux__
usleep(200000);
#endif
}
//Loop to iterate from 0 to random value - 1
for(int i=0;i<=r-1;i++)
{
tmp2=strtok(NULL,",");
if(tmp2!=NULL)
{
strcpy(tmp,tmp2);
}
else
{
TC_CLRSCR();
printf("Something went wrong..\nReturning back to home page..\n");
#ifdef _WIN32
Sleep(200);
#elif __linux__
usleep(200000);
#endif
}
}
TC_CLRSCR();
art_disp("resources/art/BASKETBALL.txt");
#ifdef _WIN32
Sleep(750);
#elif __linux__
usleep(750000);
#endif
fclose(frand);
return type_disp(tmp,5,'z');
}
//Defing conditions for Windows OS to find Caps
void caps_check()
{
int rows=0,columns=0;
termsize(&rows,&columns);
#ifdef _WIN32
if (GetKeyState(VK_CAPITAL) & 1)
#endif
#ifdef __linux__
FILE* p = popen("xset -q | grep \"Caps Lock\" | cut -d\" \" -f10", "r");
if (!p) {
printf("INSTALL xset");
}
char result[8];
if (fgets(result, 8, p) == NULL) {
printf("SOMETHING WENT WRONG");
}
pclose(p);
if (result[0] == 'o' && result[1] == 'n')
#endif
{
TC_MOVE_CURSOR((columns-16)/2,(rows/2)+4);
printf("CAPS LOCK IS ON");
}
//Defining conditions for Linux OS to Caps
else
{
TC_MOVE_CURSOR((columns-16)/2,(rows/2)+4);
printf(" ");
}
}
#ifdef __linux__
char getch(void)
{
char buf = 0;
struct termios old = {0};
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
// printf("%c\n", buf);
return buf;
}
#endif
// void score_save(int mode, float *wpm, float *acc, float *netwpm, int BBscore)
// {
// open_scorefile();
// strcpy(scores[score_n].date,__DATE__);
// strcpy(scores[score_n].time,__TIME__);
// scores[score_n].gmode=mode;
// scores[score_n].wpm=*wpm;
// scores[score_n].accuracy=*acc;
// scores[score_n].netwpm=*netwpm;
// scores[score_n].BBscore=BBscore;
// score_n++;
// // open_scorefile();
// write_score(mode,wpm,acc,netwpm,BBscore);
// }