File: radsearch.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 (616 lines) | stat: -rw-r--r-- 14,556 bytes parent folder | download | duplicates (3)
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* This file implements kanji 'radical' search widgets.
 * At one point, I intended to make my own radical list, with
 * my own definition of radicals
 * But I have wimped out and decided to go with the "standard" ones.
 *
 * This stuff takes advantage of the 'radkfile' file, included with
 * Jim Breen's xjdic program, and copied here with permission.
 * 
 */


#include <stdio.h>
#include <stdlib.h>

#include <Xos.h>
#include <Xfuncs.h>
#include <Intrinsic.h>
#include <StringDefs.h>

#include <Shell.h>

#include <Xaw/Command.h>
#include <Xaw/Label.h>
#include <Xaw/Form.h>
#include <Xaw/Box.h>
#include <Xaw/AsciiText.h>
#include <cursorfont.h>

#include "defs.h"
#include "externs.h"
#include "game.h"    /* for GUESS_XXX defines */
#include "search.h"
#include "multikanji.h"
#include "searchwidgets.h"
#include "utils.h"
#include "init.h" /* for setup_deletewindow() */

Cursor pointercursor, busycursor;

Widget radical_popup;
Widget radicalinputform;

Widget radsearch_help; /* like a status bar */

int num_radicals=0; /* number of radicals read from file */

/*MAXRADICALS is defined in defs.h now */


XChar2b radicals[MAXRADICALS]; /* char for each radical */
XChar2b radical_array[MAXRADICALS][MAXKRADPOST]; /* kanji containing each rad*/
Widget radbuttons[MAXRADICALS];
CARD8 radtoggle[MAXRADICALS];   /* array to show if radical is selected*/

int numradsactive; /* number of radicals indicated by current select */
XChar2b activelist[MAXKRADPOST];
 /* activelist[] is the intersection of the kanji list from each
  * highlighted radical (eg: if its entry in radtoggle[] is set)
  */

static void setradstatus(char *s)
{
	XtVaSetValues(radsearch_help,XtNlabel,s,NULL);
}


/* Callback for 'clear' button
 * Note that we have to duplicate PART of functionality in here,
 * in subtractRadical()
 */
void ClearRadicals(Widget w, XtPointer data, XtPointer call_data)
{
	int rcount;
	for(rcount=0; rcount<num_radicals; rcount++){
		UnhighlightButton(radbuttons[rcount]);
		XtSetSensitive(radbuttons[rcount],True);
		radtoggle[rcount]=0;
	}
	numradsactive=0;
	printf("DEBUG: ClearRadicals clearing byte at %p\n",
	       &activelist[0].byte1);

	activelist[0].byte1=0;
	setradstatus("No radicals selected");
	ClearAllMulti();
	
}

/* given a radical indexnumber, 
 * take all kanji referenced by that radical and
 * intersect them with the 'active' list.
 *
 * If do_merge==1, make that intersection, the NEW 'active' list.
 *
 * return 1 if meaningful merge possible, or 0 if intersection set ==empty set
 */
static int mergeRadical(int radnum, int do_merge){
	XChar2b newactivelist[MAXKRADPOST];
	XChar2b searchstr[2];
	XChar2b *kradptr;
	int newcount=0;

	if(radnum>=num_radicals){
		puts("ERROR: mergeRadical passed invalid radnum");
		return 0;
	}

	kradptr=radical_array[radnum];
	/* kradptr now points to list of all kanji that use this radical*/


	while(kradptr->byte1!=0){
		if(kstrchr(activelist,*kradptr) != NULL){
			newactivelist[newcount++]=*kradptr;
		}
		kradptr++;
	}

	if(newcount>0){
		if(do_merge==0){
			/* we were just checking to see if it was POSSIBLE*/
			return 1;
		}
		newactivelist[newcount].byte1=0;
		newactivelist[newcount].byte2=0;
#ifdef DEBUG
		{
			XChar2b *tmpptr=activelist;
			int kcount=0;
			printf("kanji in old active list:\n  ");
			while(tmpptr->byte1!=0){
				printf("0x%2x%2x ",
				       tmpptr->byte1,tmpptr->byte2);
				tmpptr++;
				kcount++;
			}
			printf(" (kcount=%d)\n",kcount);
			
			tmpptr=&radical_array[radnum][0];
			kcount=0;
			printf("kanji in radical%d list:\n  ",radnum);
			while(tmpptr->byte1!=0){
				printf("0x%2x%2x ",
				       tmpptr->byte1,tmpptr->byte2);
				tmpptr++;
				kcount++;
			}
			printf(" (kcount=%d)\n",kcount);
		}
#endif

		printf("DEBUG: copying %d bytes from %p to %p\n",
		       (int)((newcount+1)*sizeof(XChar2b)),newactivelist,activelist);

		bcopy(newactivelist,activelist,(newcount+1)*sizeof(XChar2b));
#ifdef DEBUG
		{
			XChar2b *tmpptr=activelist;
			int kcount=0;
			printf("kanji in MERGED active list:\n  ");
			while(tmpptr->byte1!=0){
				printf("0x%2x%2x ",
				       tmpptr->byte1,tmpptr->byte2);
				tmpptr++;
				kcount++;
			}
			printf(" (kcount=%d)\n",kcount);
		}
#endif

		return 1;
	} else {
		return 0;
	}
}

/* Given a SINGLE kanji, look up all radicals that we know of, that
 * are used in it.
 * Length of radlist array should be MAXRADICALS+1,
 * although in reality, there probably wont be more than 5 radicals involved.
 */
void FindRadicals(XChar2b kanji, XChar2b *radlist){
	int rcount;
	XChar2b *radscan;
	for(rcount=0; rcount<MAXRADICALS;rcount++){
		radscan=radical_array[rcount];
		while(radscan->byte1 !=0){
			if((radscan->byte1==kanji.byte1) &&
			   (radscan->byte2==kanji.byte2)) {
				radlist->byte1=radicals[rcount].byte1;
				radlist->byte2=radicals[rcount].byte2;
				radlist++;
			}
			radscan++;
		}
	}
}


/* given a radical indexnumber, 
 * take all kanji referenced by that radical and add to 'active' list.
 * return 1 if okay, 0 if we tried to merge, and failed.
 */
static int addRadical(int radnum)
{
	int radcount;
	XChar2b *radptr;
	if((radnum>num_radicals)||(radnum<0)){
		puts("ERROR: addRadical passed invalid number");
		return 0;
	}

	if(numradsactive==0){
		bcopy(radical_array[radnum], activelist,
		       MAXKRADPOST*sizeof(XChar2b));
	} else {
		if(!mergeRadical(radnum, 1)){
			setradstatus("no combination possible");
			return 0;
		}
	}


	/* mask out buttons that no longer can be used */
	/* skip one that was just pressed, and any that are highlighted*/
	for(radcount=0; radcount <num_radicals; radcount++){
		if((radcount!=radnum)&&(radtoggle[radcount]==0)){
			if(!mergeRadical(radcount,0)){
				XtSetSensitive(radbuttons[radcount],False);
			} else {
				XtSetSensitive(radbuttons[radcount],True);
			}
		}
	}

	/* Now just tally up active list, and we're done */
	radptr=activelist; radcount=0;
	while(radptr->byte1!=0){
		radcount++;
		radptr++;
	}
	
	{
		char countbuf[50];
		sprintf(countbuf,"%d kanji possible",radcount);
		setradstatus(countbuf);
		numradsactive=radcount;
	}

	return 1;
}


/* Need to just recalculate intersection of all currently active
 * radicals, except the one passed in.
 * Make sure to update numradsactive
 * Logic in here must match ClearRadicals()
 */
static void subtractRadical(int radnum)
{
	int rcount,totalactive;

	radtoggle[radnum]=0;
	numradsactive=0;
	activelist[0].byte1=0;

	/* Kinda brute-force, but cant think of "elegant" way to do this */

	for(rcount=0,totalactive=0; rcount<num_radicals; rcount++){
		if(radtoggle[rcount]){
			addRadical(rcount);
			totalactive++;
		}
	}

	/*
	 * Note: addRadical() will have updated numradsactive for us
	 */

	if(totalactive==0){
		/* Hmm. we must have UNclicked the only used radical.*/
		/* This will ensure all buttons are set to active again */
		ClearRadicals(NULL,NULL,NULL);
	}

}


/* callback for when user clicks on an individual radical button */
void SelectRadical(Widget w, XtPointer data, XtPointer call_data)
{
	TRANSLATION kanjiptr;
	int rcount=0;


	while(rcount<num_radicals){
		if(w==radbuttons[rcount]){
			break;
		}
		rcount++;
	}
	if(rcount==num_radicals){
		puts("Error: unknown radicalbutton called SelectRadical?");
		return;
	}
#ifdef DEBUG
	printf("SelectRadical called by radical %d\n",rcount);
#endif

	/* Over slow links, or in debug mode, the operations
	 * after this can be veeerrryyyy sslllloooowwww...
	 */
	XtVaSetValues(radicalinputform, XtNcursor, busycursor,NULL);

	if(radtoggle[rcount]){
		UnhighlightButton(radbuttons[rcount]);
		subtractRadical(rcount);
	} else {
		if(!addRadical(rcount)){
			return;
		}
		HighlightButton(radbuttons[rcount]);
		radtoggle[rcount]=1;
	}


	XtVaSetValues(radicalinputform, XtNcursor, pointercursor,NULL);

	/* potentially, we could have a really looong active list.
	 * dont bother even trying to display, if longer than multilist
	 * would display anyway
	 */
	if(numradsactive>=getMultiMax()){
		return;
	}

	/* Now fill out the "multikanji" popup window, with an entry
	 * for each "active" kanji that is potentially a match with
	 * the radicals the user has selected so far.
	 */
	
	ClearAllMulti();

	for(rcount=0;rcount<numradsactive;rcount++){
		int kindex=(activelist[rcount].byte1 <<8) +
		            activelist[rcount].byte2;

		if((kindex <MINKANJIALLOWED) ||
		   (kindex >MAXKANJIALLOWED)){
			printf("SelectRadical: ERROR: kanji 0x%x in activelist out of range\n",
			       kindex);
			continue;
		} 

		kanjiptr=translations[kindex];
		if(kanjiptr==NULL){
			printf("SelectRadical: ERROR: no entry for kanji 0x%x (position %d)\n",
			       kindex, rcount);
		} else {
			AddMultiTranslation(kanjiptr);
		}
	}
	SetMultiMode(GUESS_KANJI);
	ShowMulti();
}



XChar2b fakelabel[2]={{0x24, 0x22}, {0x0, 0x0}};

/*
 * Internal routine, to create the buttons for the
 * kanji radical search popup.
 * Should have:
 *  -  Buttons for all 250 radicals
 *  -  a 'clear' button
 *  -  a 'search' button
 *  -  a status label.
 *
 */
void makeradicalinput(Widget parent)
{
	Widget radform,bottomform;
	Widget clearbutton;
	int radcount;
	XChar2b buttonlabel[2];
	buttonlabel[1].byte1=0;
	buttonlabel[1].byte2=0;

	radform=XtVaCreateManagedWidget("radinputform", boxWidgetClass,parent,
		XtNwidth,600,
			XtNleft,XawChainLeft,
			XtNright,XawChainRight,
			XtNtop, XawChainTop,
			XtNbottom, XawChainBottom,
		NULL);

	for(radcount=0;radcount<num_radicals;radcount++){
		char radbname[10];
		sprintf(radbname,"radb%d\n",radcount);
		buttonlabel[0].byte1=radicals[radcount].byte1;
		buttonlabel[0].byte2=radicals[radcount].byte2;
		radbuttons[radcount]=XtVaCreateManagedWidget(radbname,
		        commandWidgetClass, radform,
			XtNlabel,buttonlabel,
			XtNencoding, XawTextEncodingChar2b,
			XtNfont, smallkfont,
		        NULL);

		XtAddCallback(radbuttons[radcount], XtNcallback,
				      SelectRadical, NULL);
	}

	bottomform=XtVaCreateManagedWidget("radcommandform", formWidgetClass,
		parent,
		XtNfromVert,radform,
		XtNborderWidth,0,
			XtNleft,XawChainLeft,
			XtNright,XawChainRight,
			XtNtop, XawChainBottom,
			XtNbottom, XawChainBottom,
		NULL);

	clearbutton=XtVaCreateManagedWidget("radclear",
	        commandWidgetClass, bottomform,
		XtNlabel,"clear",
		XtNleft, XawChainLeft,
		XtNright, XawChainLeft,
	        NULL);

	radsearch_help=XtVaCreateManagedWidget("radsearchhelp",
		labelWidgetClass, bottomform,
		XtNlabel,"                 Radical Search                   ",
		XtNfromHoriz,clearbutton,
		XtNleft, XawChainRight,
		XtNright, XawChainRight,
	        NULL);
	
	XtAddCallback(clearbutton, XtNcallback,
				      ClearRadicals, NULL);

}

/* Exported routine */
/* Lets other functions know if radical search is available or not.
 * (just in case the radkfile is not present)
 */ 
int HaveRadicals()
{
	if(num_radicals>0) return 1;
	return 0;
}

/* exported routine */
/* Call HaveRadicals() if you want to know if it
 * succeeded or not
 */
void InitRadicals()
{
#define RADBUFLEN 8192

	/* I'm not particularly proud of the internals of this thing.
	 * Unfortunately, the file format doesnt easily lend itself
	 * to a better approach, that I can think of.
	 * Oh well, memory is a lot cheaper these days. Sigh.
	 */
	char radfile[MAXLINELEN]; /* path to "radkfile" */
	CARD8 linebuf[RADBUFLEN];
	int cur_radnum=-1;
	int kradpos=0;
	FILE *radfp;
	
	GetXtrmString("radkfile","Radkfile",radfile);

	numradsactive=0;
	bzero(radical_array,MAXRADICALS*MAXKRADPOST*sizeof(XChar2b));
	bzero(radtoggle,MAXRADICALS);

	radfp=fopen(radfile,"r");
	if(radfp==NULL){
		num_radicals=0;
		return;
	}
	printf("Opened radfile %s\n",radfile);

	while(cur_radnum<MAXRADICALS){
		int linepos=0;
		if(fgets((void*)linebuf, RADBUFLEN, radfp)==NULL){
			break;
		}
		if(linebuf[0]=='#'){
			continue;
		}
		if(linebuf[0]=='$'){
			cur_radnum++;
			kradpos=0;

			/* save the kanji that we want to display,
			 * for the button representing radical #{cur_radnum}
			 */
			radicals[cur_radnum].byte1=linebuf[2]&0x7f;
			radicals[cur_radnum].byte2=linebuf[3]&0x7f;

			continue;
		}
		/* otherwise, must be radical list line.
		 * A list of kanji that match the current radical.
		 * Except if file is corrupted, of course
		 * Note that linepos gets incremented TWICE.
		 * But, unless file gets corrupted, we should never get
		 * anywhere near RADBUFLEN
		 */

		linepos=0;
		for(; linepos<RADBUFLEN && linebuf[linepos]!='\n'; linepos+=2) {
			radical_array[cur_radnum][kradpos].byte1=
			      linebuf[linepos]&0x7f;
			radical_array[cur_radnum][kradpos].byte2=
			      linebuf[linepos+1]&0x7f;
			kradpos++;
		}
		if(linepos >= RADBUFLEN){
			fprintf(stderr,"ERROR: radkfile %s corrupted!\n",
				radfile);
			printf("linepos=%d,kradpos=%d\n",linepos,kradpos);
			exit(1);
		}
	}

	if(cur_radnum==MAXRADICALS){
		fprintf(stderr,"ERROR:InitRadicals exceeded MAXRADICALS\n");
		/* Need to increase the #define */
		num_radicals=0;
		return;
	}
	num_radicals=cur_radnum;

	printf("%d radicals read from radkfile\n",num_radicals);

}


/* exported routine */
void MakeRadicalinputPopup()
{
	if(num_radicals==0) {
		return;
	}

	pointercursor=XCreateFontCursor(display, XC_X_cursor);
	busycursor=XCreateFontCursor(display, XC_clock);


	radical_popup = XtVaCreatePopupShell("kdrill_radicalsearch ",
		topLevelShellWidgetClass,
		search_popup,
		NULL);

	radicalinputform = XtVaCreateManagedWidget("radicalinputform",
					     formWidgetClass,
					     radical_popup,
			XtNleft,XawChainLeft,
			XtNright,XawChainRight,
			XtNtop, XawChainTop,
			XtNbottom, XawChainBottom,
					     NULL);

	/* make all the substuff */
	makeradicalinput(radicalinputform);

}

/*
 * Exported routine:
 * 
 * It pops up the kanji radical search window
 */
void ShowRadicalinput(Widget w,XtPointer client_data, XtPointer call_data)
{
	static int radicals_up = -1;

	static Position rel_x,rel_y;
	Position x,y;

	if(num_radicals==0){
		setstatus("ERROR: Radical file radkfile not present");
		return;
	}

	if(radicals_up==-1){
		/* first time init.. */
		/*rel_x = GetXtNumber("radical_popupx","Search_popupx");*/
		/*rel_y = GetXtNumber("radical_popupy","Search_popupy");*/
		rel_x = 10;
		rel_y = 10;
		radicals_up=0;
	}
	if(isMapped(radical_popup)==False){
		XtTranslateCoords(search_popup,rel_x,rel_y,&x,&y);
		XtVaSetValues(radical_popup,
		      XtNx,x,
		      XtNy,y,
		      NULL);
		XtPopup(radical_popup,XtGrabNone);
		if(radicals_up==0){
			setup_deletewindow(radical_popup);
			radicals_up=1;
		}
		
		setstatus("Bringing up radical input window...");
	} else {
		XtPopdown(radical_popup);
	}
}