File: utils.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 (403 lines) | stat: -rw-r--r-- 8,134 bytes parent folder | download | duplicates (7)
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
/* utils.c:
 * helper routines that dont really belong anywhere else
 */


#include <stdio.h>

#include <Intrinsic.h>
#include <IntrinsicP.h>

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

#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>

#include "defs.h"
#include "externs.h"

/* Is a widget "mapped'? */
/* We need this because we do our own popupshell type management */
/* and we also try to handle WM_DELETE messages elegantly */
Boolean isMapped(Widget w){
	XWindowAttributes wattrs;
	XWindowAttributes *wptr=&wattrs;
	Window win;

	if(w==NULL)
		return False;

	win=XtWindow(w);
	if(win==0)
		return False;
	
	XGetWindowAttributes(display, win, wptr);

	if( (wptr->map_state)==IsUnmapped){
		return False;
	} else {
		return True;
	}
}

/* return 1 if a pipe, 0 if not */
int isapipe(FILE *fp){
	struct stat statbuf;
	int fd = fileno(fp);
	if(fstat(fd, &statbuf)==0){
		switch(errno){
		   case ENOENT:
			return 1;/* pipe, under solaris */
		   default:
			/* until I hear otherwise, I'll assume any OTHER
			 * kind of error is also meaning "pipe"
			 */
			return 1;
		}
	}

	return 0;
}

/* Generic Beep routine that we can call from anywhere.
 * This way we don't have to check for enabled bell every time in the code.
 * We can just put Beep() whereever we feel like it.
 */
void Beep(){
	if(doBell)
		XBell(display,100);
}

/* Since there is no standard util to convert hex ascii to int,...
 *  have to supply our own..
 *  It isn't incredibly efficient.. let's hope the compiler is smart.
 *
 * Note that we take char* as a hex string, EVEN IF it does not
 * start with 0x
 */
int xtoi(char * s)
{
	int out=0;
	sscanf(s,"%x",&out);
	return out;
}

/* nextword:
 *	Goes to first whitespace, then sets pointer to
 *	beginning of non-white-space.
 *
 *	Returns 1 on success, 0 on fail
 */
int nextword(unsigned char **stringp)
{
	while(!isspace(**stringp)){
		if(stringp == '\0')
			return 0;
		*stringp +=1;
	}
	/* now on space */
	while(isspace(**stringp)){
		if(stringp == '\0')
			return 0;
		*stringp +=1;
	}
	return 1;
	
}

/* nextchar:
 * returns pointer to next non-whitespace char
*/
unsigned char *nextchar(unsigned char *c)
{
	while(isspace(*c)){
		if(*c == '\0') break;
		c++;
	}
	return c;
}

XChar2b *dup_16(XChar2b *kanabuffer){
	int pronun_len;
	XChar2b *ret_str;
	
	pronun_len = strlen((char *) kanabuffer);

	ret_str = (XChar2b *) malloc(sizeof(char) * (pronun_len+4));
	if(ret_str== NULL){
		fprintf(stderr,"Not enough memory to read in dictionary\n");
		exit(0);
	}
	bcopy(kanabuffer, ret_str, sizeof(char) * (pronun_len+1));
	return ret_str;
}


/* random debugging util? */
void printline(unsigned char *s)
{
	while(*s){
		putchar(*s++);
	}
	putchar('\n');
}

FILE * open_compressed(char *dictname){
	FILE *fp;
	char command_string[100];
	int namelen;/* length of filename, and flag */
	int extlen;

	if(access(dictname,R_OK)!= 0){
		return NULL;
	}

#ifdef UNCOMPRESS
	namelen = strlen(dictname);
	extlen = strlen(UNCOMPRESSEXT);
	if(strncmp(&dictname[namelen-extlen],UNCOMPRESSEXT,extlen) != 0 ){
		namelen = 0;/* flag for later on */
		fp = fopen(dictname,"r");
	} else {
		sprintf(command_string,"%s %s",UNCOMPRESS,dictname);
		fp = (FILE *) popen(command_string,"r");
	}
#else
	fp = fopen(dictname,"r");
#endif /* UNCOMPRESS */
	if(fp == NULL){
		perror("Cannot open translation file");
		fprintf(stderr,"We were attempting to open %s\n",dictname);
#ifdef UNCOMPRESS
		if(namelen >0)
			fprintf(stderr,"Using uncompression method \"%s\"\n",
				UNCOMPRESS);
#endif
		return NULL;
	}
	return fp;

}



TRANSLATION FindField(TRANSLATION startK,
		      Boolean (*testfunc )(TRANSLATION,void *),
		      void * funcdata)
{


	/*static TRANSLATION startpoint= NULL;
	 * in case we later want
	 * to use this..
	 */

	TRANSLATION checkpoint;


	checkpoint = translations[lowestkanji];

	while(checkpoint != NULL) {
		if(testfunc(checkpoint,funcdata)){
			break;
		}
		checkpoint = checkpoint->nextk;
	}
	

	return checkpoint;
}


/*
 *	set the text field of a textwigdet to the passed integer value.
 */
void SetWidgetNumberval(Widget w,int val){
	char tempstr[100];
	if(val == 0)
		tempstr[0] = '\0';
	else
		sprintf(tempstr,"%d",val);
	XtVaSetValues(w,XtNstring,tempstr,NULL);
	return;
}
/*
 *	set the text field of a textwigdet to the passed HEX integer value.
 */
void SetWidgetHexval(Widget w,int val){
	char tempstr[100];
	if(val == 0)
		tempstr[0] = '\0';
	else
		sprintf(tempstr,"%x",val);
	XtVaSetValues(w,XtNstring,tempstr,NULL);
	return;
}


/* GetWidgetNumberval
 *	Get the asciiText from an input widget.
 *	Attempt to look up kanji index
 *	Set asciiText label to null if not found, or
 *	a clean decimal print of index otherwise.
 *
 *	Calls FindIndex, which accepts special prefixes.
 */
int GetWidgetNumberval(Widget w){
	String str;
	int retval=0;


	XtVaGetValues(w,XtNstring,&str,NULL);
	retval=atoi(str);
	SetWidgetNumberval(w,retval);

/*
	if(w == currentkanjiNum){
		retval = xtoi(str);
		SetWidgetHexval(w,retval);
	} else {
		retval = FindIndex(str);
		SetWidgetNumberval(w,retval);
	}
*/
	return (retval);
}

/* GetWidgetHexval
 *	Like GetWidgetNumberval(), except assumes that
 *	the widget value MUST BE A HEX STRING
 *	Get the asciiText from an input widget.
 *	Set asciiText label to null if not found, or
 *	a clean print of index otherwise.
 *
 *	Calls FindIndex, which accepts special prefixes.
 */
int GetWidgetHexval(Widget w){
	String str;
	int retval=0;


	XtVaGetValues(w,XtNstring,&str,NULL);
	retval = xtoi(str);
	SetWidgetHexval(w,retval);

	return (retval);
}

int GetWidgetWidth(Widget w){
	Dimension wwidth;
	XtVaGetValues(w, XtNwidth, &wwidth, NULL);
	return wwidth;
}
int GetWidgetBWidth(Widget w){
	Dimension wwidth;
	XtVaGetValues(w, XtNborderWidth, &wwidth, NULL);
	return wwidth;
}int GetWidgetHeight(Widget w){
	Dimension wheight;
	XtVaGetValues(w, XtNheight, &wheight, NULL);
	return wheight;
}

/* Note that this sets borderwidth to 0!! */
void WidenWidget(Widget w, int newwidth){
	int height, bwidth;

	height=GetWidgetHeight(w);
	bwidth=GetWidgetBWidth(w);
	
#ifdef NONO
	/*XtResizeWidget(w, newwidth, height, bwidth);*/
#else
	printf("Resizing some window to width %d\n",newwidth);
	XtVaSetValues(w,XtNwidth,newwidth,NULL);
	XtResizeWindow(w);
#endif

}




/* We take a tip from java, and use the PARENT to determine background
 * and foreground.
 *
 * This takes a button, and sets it to "highlighted", reguardless
 * of what it was before.
 *
 * UNFORTUNATELY... foreground never seems to work. so we'll fake
 * it with black. sigh.
 *
 * See SetWhiteOnBlack() comments about bug in Xaw, and not using this
 * from a callback
 */
void HighlightButton(Widget button)
{
	Pixel fg, bg;
	XtVaGetValues(XtParent(button), XtNforeground, &fg,NULL);
	XtVaGetValues(XtParent(button), XtNbackground, &bg, NULL);

	fg=black;

	XtVaSetValues(button, XtNforeground, bg,
		      XtNbackground, fg,
		      NULL);
}
/*
 * This takes a button, and sets it to NON-"highlighted", reguardless
 * of what it was before.
 */
void UnhighlightButton(Widget button)
{
	Pixel fg, bg;
	XtVaGetValues(XtParent(button), XtNforeground, &fg,NULL);
	XtVaGetValues(XtParent(button), XtNbackground, &bg, NULL);

	fg=black;

	XtVaSetValues(button, XtNforeground, fg,
		      XtNbackground, bg,
		      NULL);
}

/* Whatever it is now, reverse the colors of a button/widget */
void ReverseButton(Widget button)
{
	Pixel fg, bg;

	XtVaGetValues(button, XtNforeground, &fg,
		      XtNbackground, &bg, NULL);

	XtVaSetValues(button, XtNforeground, bg,
		      XtNbackground, fg,
		      NULL);
}


/*
 * Sets widget to have white text on a black background
 * bug in Xaw.so.6 means can only call at setup time, not in callback
 */
void SetWhiteOnBlack(Widget w){
	XtVaSetValues(w,XtNbackground,black,
		       XtNforeground,white,
		       NULL);
}

/*
 * Sets widget to have black text on a white background
 * bug in Xaw.so.6 means can only call at setup time, not in callback
 */
void SetBlackOnWhite(Widget w){
	XtVaSetValues(w,XtNbackground,white,
		       XtNforeground,black,
		       NULL);
}