File: log.c

package info (click to toggle)
kdrill 6.5deb2-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,340 kB
  • sloc: ansic: 7,636; makefile: 47
file content (284 lines) | stat: -rw-r--r-- 6,363 bytes parent folder | download | duplicates (8)
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
/* log.c:
 * This file deals with "wrong guess" logging, and "usefiles".
 * Both are read in at startup, and written out when the program quits.
 *
 * For usefile usage, we are using the pseudo-OO design of encapsulating
 * details in this file. So for this case, static globals are a GOOD THING.
 * It makes it seem like we have an implicit "usefile" object, that can
 * only be accessed through member functions listed in log.h
 */

#include <stdio.h>
#include <Xatom.h>
#include <Xos.h>
#include <Intrinsic.h>
#include <StringDefs.h>
#include <Xfuncs.h>
#include <Shell.h>
#include <Xaw/Command.h>

#include "defs.h"
#include "externs.h"
#include "game.h"
#include "badguess.h"
#include "options.h"

static char logfilename[200];

Boolean useKanji[MAXTRANSLATIONSALLOWED];

/* initlog()
 *    Checks for existance of logfile, and reads in previously missed
 *      count from that if possible.
 */
void 
initlog()
{
	FILE *oldlogfile;
	char inbuf[100];

	GetXtrmString("logfile", "Logfile", logfilename);

	if (access(logfilename, R_OK) != 0)
		return;
	oldlogfile = fopen(logfilename, "r");
	if (oldlogfile == NULL) {
		printf("kdrill: Error... cannot open file %s\n", logfilename);
		return;
	}
	else {
		printf("Reading in %s for previously missed kanji\n",
		       logfilename);
	}

	numberincorrect = 0;
	while (fgets(inbuf, 99, oldlogfile) != NULL) {
		int kindex;
		kindex = xtoi(inbuf);
		if (kindex < MAXTRANSLATIONSALLOWED) {
			if (translations[kindex] != NULL) {
				translations[kindex]->incorrect += 1;
				if (translations[kindex]->incorrect == 1)
					numberincorrect += 1;
				AdjustBadCache(translations[kindex]);
			}
		}
	}
	printf("Restored a total of %d incorrect kanji\n", numberincorrect);
	TallyWrong();
	fclose(oldlogfile);

}

/*
 * This is a callback for the "Log" button, but can be called with
 * NULL arguments, and IS, at the end of the program.
 *
 * MakeLog dumps our error log out to the logfile.
 * Do it in a vaguely "usefile"-compatible format, in case the player
 * wants _repeated_ drilling on the chars missed.
 */
void 
MakeLog(Widget w, XtPointer client_data, XtPointer call_data)
{
	int kcounter;
	char statbuf[200];
	int errors = 0;
	FILE *logfile;

	if (numberincorrect == 0) {
		sprintf(statbuf, "Clearing log file \"%.190s\"", logfilename);
		setstatus(statbuf);
		/* DEUBG: print same message to stdout*/
		printf("%s\n", statbuf);

		unlink(logfilename);
		return;
	}

	logfile = fopen(logfilename, "w");
	if (logfile == NULL) {
		if (numberincorrect > 0) {
			sprintf(statbuf, "Error.. cannot open \"%.190s\"",
				logfilename);
			setstatus(statbuf);
		}
		return;
	}
	for (kcounter = lowestkanji; kcounter <= highestkanji; kcounter++) {
		if (translations[kcounter] == NULL)
			continue;
		if (translations[kcounter]->incorrect > 0) {
			int loop = translations[kcounter]->incorrect;
			while (loop-- > 0) {
				fprintf(logfile, "%x \n", kcounter);
			}
			errors++;
		}

	}

	fclose(logfile);
	if (errors > 0) {
		sprintf(statbuf, "Saved %d kanji to \"%.190s\"", errors, logfilename);
		setstatus(statbuf);
		printf("%s\n", statbuf);
	}
}


/***********************************************
 * Now the Usefile section                     *
 ***********************************************/

int usefilecount = 0;

int 
NumInUsefile()
{
	return usefilecount;
}
/* If you're nice, use "False" or "True" as val */
void 
SetUseKanji(int kindex, int val)
{
	int oldval = useKanji[kindex];
	useKanji[kindex] = val;

	if (oldval == 0 && val == 1)
		usefilecount++;

	if (oldval == 1 && val == 0)
		usefilecount--;

}
int 
InUsefile(int kindex)
{
	return useKanji[kindex];
}

/* Save "usefile" entries, if appropriate */
void 
SaveUsefile()
{
	int kcount = 0;
	char statbuf[200];
	FILE *fptr;

	if(usefilecount <=0) {
		unlink(usefilename);
		return;
	}
	
	if (usefilecount < HAVE_AT_LEAST) {
		sprintf(statbuf, "WARNING: too few entries for usefile. Still saving to \"%.190s\"",
			usefilename);
	}

	fptr = fopen(usefilename, "w");
	if (fptr == NULL) {
		sprintf(statbuf, "Error.. cannot open \"%.190s\"",
			logfilename);
		setstatus(statbuf);
		perror(statbuf);
		return;
	}

	for (kcount = lowestkanji; kcount <= highestkanji; kcount++) {
		if (InUsefile(kcount) == 0)
			continue;
		fprintf(fptr, "%x\n", kcount);
	}

	fclose(fptr);
}


/* getusefile:
 *    Subroutine for initusefile().
 *      sets global FILE*usefile.
 *      If possible, will get it from the user's home directory,
 *      otherwise, tries current directory
 *      This is NOT an exported routine: initusefile() is
 */
FILE *
getusefile()
{
	FILE *fptr;
	char fullname[100];

	GetXtrmString("usefile", "Usefile", usefilename);

#ifdef DEBUG
	puts("");
	printf("usefile from resources is \"%s\"\n", usefilename);
#endif

	/*if(usefilename == NULL) return NULL; */
	fullname[0] = '\0';
	if (strncmp(usefilename, "~/", 2) == 0) {
		sprintf(fullname, "%s%s", homedir, &usefilename[1]);
		strcpy(usefilename, fullname);
	}

	if ((fptr = fopen(usefilename, "r")) == NULL) {
		printf("Usefile %s does not exist. Using entire dictionary...\n",
		       usefilename);
	}
	else {
		printf("using \"%s\" to abridge dictionary\n", usefilename);
		if (!useUsefile)
			puts("(-nousefile option used. Will not abridge, unless option is changed)");
	}
	return fptr;
}

/* inituse:
 *    revamped from 2.9.3 version.
 *      Read in the "usefile", which should consist of
 *      a list of hex SJIS indexes.
 *      This sets useKanji[] appropriately.
 *
 *      by default, zero out all entries.
 *      Then set only entries mentioned in file to 1 instead of 0
 *
 *      Note that whether or not to pay attention to useKanji[], is
 *      a separate variable, useUsefile
 */

void 
initusefile()
{
	FILE *fptr;
	char inbuf[100];
	int count = 0;


	/*first set all to UNREADABLE, for now */
	bzero(useKanji, sizeof(Boolean[MAXTRANSLATIONSALLOWED]));

	fptr = getusefile();

	if (fptr == NULL) {
		useUsefile = False;
		SetXtrmBoolean("nousefile", True);
		UpdateUsefileL();
		return;
	}

	while (fgets(inbuf, 99, fptr) != NULL) {
		int usechar;
		usechar = xtoi(inbuf);
		if (usechar < MAXTRANSLATIONSALLOWED) {
			SetUseKanji(usechar, True);
			count++;
		}
	}
	printf("read %d entries in usefile\n", count);
	if ((count < HAVE_AT_LEAST) && useUsefile) {
		printf("Warning: Usefile exists with less than %d kanji.\n",
		       HAVE_AT_LEAST);
	}

}