2
file.c - file access functions
8
#define DEBIAN__HIGHSCORE "/var/games/typespeed"
9
#define DEBIAN__WORDLIST "/usr/share/games/typespeed"
12
int loadwords(char *stringi)
23
chdir(DEBIAN__WORDLIST);
25
if ((wordfile = fopen(stringi, "r")) == NULL) {
28
makkara = malloc(21 * sizeof(char));
29
while ((nakki = getc(wordfile)) != EOF) {
30
if (wordcount>=WORD_MAX) {
31
break; /* don't read more than max words */
37
makkara[nakkimakkara] = 0;
39
/* we don't need wordfilename here.. */
41
/* if (wordcount == -1) {
42
strcpy(wordfilename, makkara);
45
word[wordcount] = malloc(21 * sizeof(char));
46
strcpy(word[wordcount], makkara);
52
if (nakkimakkara < 21) { /*max word length 20.. + end null char */
53
makkara[nakkimakkara] = nakki;
61
if (wordcount < 22) { /*not enough words */
72
for (i = 0; i < wordcount; i++)
79
int choose_wordfile(int kumpi)
82
struct dirent **namelist;
84
int n, k, tot, cpos = 1;
90
char thingie[WORDFILE_MAX][62];
94
filena = malloc(4 * sizeof(char));
95
info = malloc(62 * sizeof(char));
101
chdir(DEBIAN__WORDLIST);
104
n = scandir(".", &namelist, 0, alphasort);
107
printf("scandir failed.\n");
110
what = (int *) malloc((n + 1) * sizeof(int));
112
if (strstr(namelist[n]->d_name, "words.") == namelist[n]->d_name) {
114
if ((fileinfo = fopen(namelist[n]->d_name, "r")) != NULL) {
115
fgets(info, 60, fileinfo);
116
//mvprintw(k + 1, 2, "%d. %s", k, info);
117
strcpy(thingie[k], info);
130
mvprintw(k + 1, 5, "No wordfiles found...");
131
mvprintw(k + 3, 5, "Press Enter...");
132
liima_mvgetnstr(k + 3, 19, filena, 0);
135
mvprintw(2, 5, "Choose a wordfile (UP/DOWN/ENTER):");
137
for (k = 1; k <= 17; k++) {
138
mvprintw(k + 3, 5, "%s", thingie[k]);
141
for (k = 1; k <= tot; k++) {
142
mvprintw(k + 3, 5, "%s", thingie[k]);
146
mvprintw(k + 3, 1, "->");
153
/* and thank's for xmunkki, he made my day and
154
"solved" this, what i didn't get right (or did
155
but i used 2 extra variables.. :) */
172
mvprintw(4, 5, "%s", thingie[k]);
179
if ((cpos < 17) && (cpos < tot)) {
190
mvprintw(20, 5, "%s", thingie[k]);
201
//mvprintw(1,20,"merkk: %d k: %d cpos: %d",merkk,k,cpos);
202
mvprintw(cpos + 4, 1, " ");
203
mvprintw(cpos + 2, 1, " ");
204
mvprintw(cpos + 3, 1, "->");
214
loadhighscores(namelist[what[k]]->d_name);
216
mvprintw(16, 3, "Do you want to RESET this [y/n] ? ");
220
merkk = clower(merkk);
221
} while ((merkk != 'y') && (merkk != 'n'));
225
a = resetscorefile(namelist[what[k]]->d_name);
231
loadhighscores(namelist[what[k]]->d_name);
233
mvprintw(16, 3, "Press any key to continue...");
235
} while ((merkk = getch()) == ERR);
240
a = loadwords(namelist[what[k]]->d_name);
246
mvprintw(3, 5, "Something went wrong. Contact your mother.");
248
} while ((merkk = getch()) == ERR);
250
usedwordfile = malloc(sizeof(namelist[what[k]]->d_name));
251
strcpy(usedwordfile, namelist[what[k]]->d_name);
255
for (k = 0; k < tot; k++) {
258
/* cores if we free this(?) free(namelist); */
265
int resetscorefile(char *sanafile)
273
{700, 600, 500, 400, 300, 200, 100, 50, 0, 0};
275
{8, 7, 6, 5, 4, 3, 2, 1, 0, 0};
277
{7, 6, 5, 4, 3, 2, 1, 0.5, 0, 0};
279
{7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 1, 0, 0};
280
float etyporatio[10] =
281
{10, 10, 10, 10, 10, 10, 10, 10, 0, 0};
283
{"3l33t", "Pro", "VeryGood", "Good", "Average", "NoGood", "ADP-Pro", "Loser", "None", "None"};
287
chdir(DEBIAN__HIGHSCORE);
289
filetys = (char *) malloc(sizeof(char) * (strlen(sanafile) + strlen(".high") + 1));
291
strcat(filetys, "high.");
292
strcat(filetys, sanafile);
294
if ((highfile = fopen(filetys, "w")) != NULL) {
295
for (x = 0; x < 10; x++) {
296
fwrite(&epong[x], sizeof(epong[x]), 1, highfile);
297
fwrite(&ecps[x], sizeof(ecps[x]), 1, highfile);
298
fwrite(&efidelity[x], sizeof(efidelity[x]), 1, highfile);
299
fwrite(&etcps[x], sizeof(etcps[x]), 1, highfile);
300
fwrite(&etyporatio[x], sizeof(etyporatio[x]), 1, highfile);
301
fwrite(efaim[x], sizeof(efaim[x]), 1, highfile);
315
void loadhighscores(char *sanafile)
329
chdir(DEBIAN__HIGHSCORE);
331
filetys = (char *) malloc(sizeof(char) * (strlen(sanafile) + strlen(".high") + 1));
333
strcat(filetys, "high.");
334
strcat(filetys, sanafile);
344
strcpy(high.faim, " ");
346
if ((highfile = fopen(filetys, "r")) != NULL) {
348
"Top10 (score)(name) (level) (cps) (tcps) (typoinfos)");
350
for (x = 1; x <= 10; x++) {
351
fread(&high.pong, sizeof(int), 1, highfile);
352
fread(&high.cps, sizeof(float), 1, highfile);
353
fread(&high.fidelity, sizeof(int), 1, highfile);
354
fread(&high.tcps, sizeof(float), 1, highfile);
355
fread(&high.typoratio, sizeof(float), 1, highfile);
356
fread(high.faim, sizeof(char), 21, highfile);
357
mvprintw(x + 2, 3, "%d.", x);
358
mvprintw(x + 2, 8, "%d", high.pong);
359
mvprintw(x + 2, 15, "%s", high.faim);
361
if (high.fidelity>=0 && high.fidelity<11) mvprintw(x + 2, 40, "%s", rankki[high.fidelity]);
363
now.level = 0; /* Why here?? */
364
mvprintw(x + 2, 50, "%2.3f", high.cps);
365
mvprintw(x + 2, 57, "%2.3f", high.tcps);
366
mvprintw(x + 2, 64, "%2.2f%%", high.typoratio);
367
if (high.pong != 0) {
368
/* typorankkaus(high.typoratio); */
369
mvprintw(x + 2, 71, "%s", typorank[typorankkaus(high.typoratio)]);
371
mvprintw(x + 2, 71, "None");
379
mvprintw(4, 3, "No Highscores");
386
/*int addtop10(char *sanaf, int pointsgot, int levelgot, float speedgot, float truecps, float typosgot) */
387
int addtop10(char *sanaf, stats_struct * stat)
402
filet = (char *) malloc(sizeof(char) * (strlen(sanaf) +
407
chdir(DEBIAN__HIGHSCORE);
411
strcat(filet, "high.");
412
strcat(filet, sanaf);
413
for (x = 0; x < 10; x++) {
416
high.typoratio[x] = 0;
418
high.fidelity[x] = 0;
419
strcpy(high.faim[x], " ");
422
if ((highf = fopen(filet, "r")) == NULL) {
423
resetscorefile(sanaf);
425
if ((highf = fopen(filet, "r")) != NULL) {
426
for (x = 0; x < 10; x++) {
427
fread(&high.pong[x], sizeof(int), 1, highf);
428
fread(&high.cps[x], sizeof(float), 1, highf);
429
fread(&high.fidelity[x], sizeof(int), 1, highf);
430
fread(&high.tcps[x], sizeof(float), 1, highf);
431
fread(&high.typoratio[x], sizeof(float), 1, highf);
432
fread(&high.faim[x], sizeof(char), 21, highf);
436
if ((highf = fopen(filet, "w")) != NULL) {
437
for (x = 0; x < 10; x++) {
438
/* was >= now >, now changed system to get top10 */
439
if ((stat->score > high.pong[x]) && (madeit != 1)) {
441
fwrite(&(stat->score), sizeof(stat->score), 1, highf);
442
fwrite(&(stat->speed), sizeof(stat->speed), 1, highf);
443
fwrite(&(stat->level), sizeof(stat->level), 1, highf);
444
fwrite(&(stat->totalspeed), sizeof(stat->totalspeed), 1, highf);
445
fwrite(&(stat->ratio), sizeof(stat->ratio), 1, highf);
452
"You managed to get to top10!");
453
mvprintw(5, 2, "Enter your name:");
454
liima_mvgetnstr(5, 19, name, 20);
457
fwrite(name, sizeof(name), 1, highf);
460
fwrite(&high.pong[x],
461
sizeof(high.pong[x]), 1, highf);
462
fwrite(&high.cps[x], sizeof(high.cps[x]),
464
fwrite(&high.fidelity[x],
465
sizeof(high.fidelity[x]),
467
fwrite(&high.tcps[x],
468
sizeof(high.tcps[x]), 1, highf);
469
fwrite(&high.typoratio[x],
470
sizeof(high.typoratio[x]),
473
sizeof(high.faim[x]), 1, highf);
477
if ((madeit == 1) && (x == 9)) {
479
fwrite(&high.pong[x],
480
sizeof(high.pong[x]),
485
fwrite(&high.fidelity[x],
486
sizeof(high.fidelity[x]),
488
fwrite(&high.tcps[x],
489
sizeof(high.tcps[x]),
491
fwrite(&high.typoratio[x],
492
sizeof(high.typoratio[x]),
495
sizeof(high.faim[x]),
511
char wordhakemisto[1025];
515
userhome = malloc((2+strlen(getenv("HOME"))+strlen(LOCALCONF))*sizeof(char));
516
strcpy(userhome,getenv("HOME"));
517
strcat(userhome,"/");
518
strcat(userhome,LOCALCONF);
519
hakemisto = malloc(1026 * sizeof(char));
520
getcwd(hakemisto, 1024);
521
conffile = fopen(CONFIGFILE, "r");
522
if (conffile != NULL) {
523
fgets(wordhakemisto,1024,conffile);
524
fgets(tmp,6,conffile);
525
while (iscntrl(wordhakemisto[strlen(wordhakemisto)-1])) wordhakemisto[strlen(wordhakemisto)-1] = '\0';
526
if (!strcmp(tmp, "cheat"))
529
chdir(wordhakemisto);
531
conffile = fopen(userhome, "r");
532
if (conffile != NULL) {
533
fgets(wordhakemisto,1024,conffile);
534
fgets(tmp,6,conffile);
535
while (iscntrl(wordhakemisto[strlen(wordhakemisto)-1])) wordhakemisto[strlen(wordhakemisto)-1] = '\0';
536
if (!strcmp(tmp, "cheat"))
539
chdir(wordhakemisto);
544
int makescorefiles(void) {
546
struct dirent **namelist;
550
chdir(DEBIAN__WORDLIST);
552
n = scandir(".", &namelist, 0, alphasort);
554
printf("scandir failed.\n");
557
printf("\nReseting/Making scorefiles to all found wordfiles...\n\n");
559
if (strstr(namelist[n]->d_name, "words.") == namelist[n]->d_name) {
560
resetscorefile(namelist[n]->d_name);
561
printf("Make scorefile for wordfile: %s\n",namelist[n]->d_name);