summaryrefslogtreecommitdiff
path: root/xc/test/xsuite/xtest/src/xim/xim_save.c
blob: 68778c3661dd7850752a678965300d16a85d83ea (plain)
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
/* $XConsortium: xim_save.c,v 1.1 94/01/29 15:56:13 rws Exp $ */
/*

Copyright (c) 1993  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.

 *
 * Copyright 1993 by Sun Microsystems, Inc. Mountain View, CA.
 *
 *                   All Rights Reserved
 *
 * Permission  to  use,  copy,  modify,  and  distribute   this
 * software  and  its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright no-
 * tice  appear  in all copies and that both that copyright no-
 * tice and this permission notice appear in  supporting  docu-
 * mentation,  and  that the name of Sun not be used in
 * advertising or publicity pertaining to distribution  of  the
 * software  without specific prior written permission. Sun 
 * makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without any
 * express or implied warranty.
 *
 * SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
 * NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
 * ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
 * PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include    <stdio.h>
#include    <string.h>

#include    "xtest.h"
#include    "Xlib.h"
#include    "Xutil.h"
#include	"Xresource.h"
#include    "xtestlib.h"
#include    "tet_api.h"
#include    "pixval.h"

#include	"ximtest.h"

extern Display *Dsp;

extern  int     tet_thistest;
extern  struct  tet_testlist tet_testlist[];

extern	int     Errnum; /* Number of error record */

/****************************************************************/
/* duplicated in actions.c */
 
static char *keys_style[] = {
    "AREA",
    "CALLBACK",
    "NOTHING",
    "NONE",
    "POSITION",
};
static int nkeys_style = sizeof(keys_style) / sizeof(char *);
static int pe_style_val[] = {
    XIMPreeditArea,
    XIMPreeditCallbacks,
    XIMPreeditNothing,
    XIMPreeditNone,
    XIMPreeditPosition,
};
static int status_style_val[] = {
    XIMStatusArea,
    XIMStatusCallbacks,
    XIMStatusNothing,
    XIMStatusNone,
	0,
};

/****************************************************************/
/* duplicated in response.c */

static char *keys_response[] = {
	"VERSION",
	"X_RELEASE",
	"PREEDIT_STYLE",
	"STATUS_STYLE",
	"RESPONSE",
};
static int nkeys_response = sizeof(keys_response) / sizeof(char *);

static char *keys_cbname[] = {
	"PREEDIT_START",
	"PREEDIT_DONE",
	"PREEDIT_DRAW",
	"PREEDIT_CARET",
	"STATUS_START",
	"STATUS_DONE",
	"STATUS_DRAW",
	"GEOMETRY",
};
static int nkeys_cbname = sizeof(keys_cbname) / sizeof(char *);


static char *keys_ximtext[] = {
	"LENGTH",
	"FEEDBACK",
	"IS_WCHAR",
	"STRING",
};
static int nkeys_ximtext = sizeof(keys_ximtext) / sizeof(char *);


static char *keys_pe_draw[] = {
	"CARET",
	"FIRST",
	"LENGTH",
	"TEXT",
};
static int nkeys_pe_draw = sizeof(keys_pe_draw) / sizeof(char *);


static char *keys_pe_caret[] = {
	"POSITION",
	"DIRECTION",
	"STYLE",
};
static int nkeys_pe_caret = sizeof(keys_pe_caret) / sizeof(char *);


static char *keys_st_draw[] = {
	"TYPE",
	"DATA",
};
static int nkeys_st_draw = sizeof(keys_st_draw) / sizeof(char *);

/***********************************************************/
/* save routines for response routines */

#define MAX_INDENT	70
#define INDENT_STEP	4
static char spaces[MAX_INDENT] = 
/*          1         2         3         4         5         6         7*/
/*01234567890123456789012345678901234567890123456789012345678901234567890*/
 "                                                                      ";
/* this little gizmo works backwards, the indent level is actually */
/* the distance from the end of the spaces string */
/* Hence, an indent_level == MAX_INDENT is no indentation */
/*        an indent_level == 0 is an indentation of MAX_INDENT spaces */ 
static int indent_level = MAX_INDENT-1-INDENT_STEP;

static FILE *xim_save_fp = NULL;

void xim_save_pixmap(fp,px)
	FILE *fp;
	Pixmap px;
{
	fprintf(fp,"%s # Pixmap saving not ready yet\n",
		&spaces[indent_level]);
}

void xim_save_wcstr(fp,pwc,cnt)
	FILE *fp;
	wchar_t *pwc;
	int cnt;
{
	int i;

	for(i=0;i<cnt;i++)
	{
		if((i % 10) == 0)
			fprintf(fp,"%s", &spaces[indent_level]);
		fprintf(fp,"0x%02x ",*pwc);
		pwc++;
		if(((i+1) % 10) == 0)
			fprintf(fp,"\n");
	}
}

void xim_save_mbstr(fp,pmb,cnt)
	FILE *fp;
	unsigned char *pmb;
	int cnt;
{
	int i;

	for(i=0;i<cnt;i++)
	{
		if((i % 10) == 0)
			fprintf(fp,"%s", &spaces[indent_level]);
		fprintf(fp,"0x%02x ",*pmb);
		pmb++;
		if(((i+1) % 10) == 0)
			fprintf(fp,"\n");
	}
}

void xim_save_feedback(fp,pfb,cnt)
	FILE *fp;
	XIMFeedback *pfb;
	int cnt;
{
	int i;

	for(i=0;i<cnt;i++)
	{
		if((i % 10) == 0)
			fprintf(fp,"%s", &spaces[indent_level]);
		fprintf(fp,"0x%02x ",(unsigned int)*pfb);
		pfb++;
		if(((i+1) % 10) == 0)
			fprintf(fp,"\n");
	}
}

void xim_save_ximtext(fp,pt)
	FILE *fp;
	XIMText *pt;
{
	int num;

	if(pt == NULL)
	{
		report("No XIMText data to save");
		return;
	}

	num = (int)pt->length;

	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_ximtext[XIM_TEXT_LENGTH], num);

	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_ximtext[XIM_TEXT_IS_WCHAR], pt->encoding_is_wchar);


	fprintf(fp,"%s%s {\n", &spaces[indent_level],
		keys_ximtext[XIM_TEXT_FEEDBACK]);

	indent_level -= INDENT_STEP;
	xim_save_feedback(fp,pt->feedback,num);
	indent_level += INDENT_STEP;
	fprintf(fp,"\n%s}\n",&spaces[indent_level]);


	fprintf(fp,"%s%s {\n", &spaces[indent_level],
		keys_ximtext[XIM_TEXT_STRING]);

	indent_level -= INDENT_STEP;
	if(pt->encoding_is_wchar)
		xim_save_wcstr(fp,pt->string.wide_char,num);
	else
		xim_save_mbstr(fp,pt->string.multi_byte,num);
	indent_level += INDENT_STEP;
	fprintf(fp,"\n%s}\n",&spaces[indent_level]);

	fprintf(fp,"\n");
}

void xim_save_pe_draw(fp,pd)
	FILE *fp;
	XIMPreeditDrawCallbackStruct *pd;
{
	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_pe_draw[PE_DRAW_CARET], pd->caret);

	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_pe_draw[PE_DRAW_FIRST], pd->chg_first);

	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_pe_draw[PE_DRAW_LENGTH], pd->chg_length);

	fprintf(fp,"%s%s {\n",&spaces[indent_level],
		keys_pe_draw[PE_DRAW_TEXT]);

	indent_level -= INDENT_STEP;
	xim_save_ximtext(fp,pd->text);
	indent_level += INDENT_STEP;

	fprintf(fp,"%s}\n",&spaces[indent_level]);

	fprintf(fp,"\n");
}


void xim_save_pe_caret(fp,pd)
	FILE *fp;
	XIMPreeditCaretCallbackStruct *pd;
{
	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_pe_caret[PE_CARET_POSITION], pd->position);

	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_pe_caret[PE_CARET_DIRECTION], pd->direction);

	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_pe_caret[PE_CARET_STYLE], pd->style);
}


void xim_save_st_draw(fp,pd)
	FILE *fp;
	XIMStatusDrawCallbackStruct *pd;
{
	fprintf(fp,"%s%s %d\n", &spaces[indent_level],
		keys_st_draw[ST_DRAW_TYPE], pd->type);

	fprintf(fp,"%s%s {\n", &spaces[indent_level],
		keys_st_draw[ST_DRAW_DATA]);

	indent_level -= INDENT_STEP;
	if(pd->type == XIMTextType)
		xim_save_ximtext(fp,pd->data.text);
	else
		xim_save_pixmap(fp,pd->data.bitmap);
	indent_level += INDENT_STEP;

	fprintf(fp,"%s}\n",&spaces[indent_level]);
}

void xim_save_cb(fp,pstk)
	FILE *fp;	
	cbstk_def *pstk;
{
	int i;

	if(fp == NULL)
		return;

	for(i=0;i<pstk->top;i++)
	{
		if(pstk->stack[i] < 0 || pstk->stack[i] >= CB_MAX)
		{
			report("Invalid Callback index %d",pstk->stack[i]);
			continue;
		}

		if(pstk->data[i] == NULL)
		{
			fprintf(fp,"%s%s\n",
				&spaces[indent_level],keys_cbname[pstk->stack[i]]);
		}
		else
		{
			fprintf(fp,"%s%s {\n",
				&spaces[indent_level],keys_cbname[pstk->stack[i]]);
			indent_level -= INDENT_STEP;
		}
		switch(pstk->stack[i])
		{
			case CB_PE_DRAW:
				xim_save_pe_draw(fp,pstk->data[i]);
				break;
			case CB_PE_CARET:
				xim_save_pe_caret(fp,pstk->data[i]);
				break;
			case CB_ST_DRAW:
				xim_save_st_draw(fp,pstk->data[i]);
				break;
			default:
				/* nothing to record, no data */
				if(pstk->data[i] != NULL)
				{
					report("Callback, %s, on has data on the stack - none expected",
						keys_cbname[pstk->stack[i]]);
				}
				break;
		}
		if(pstk->data[i] != NULL)
		{
			indent_level += INDENT_STEP;
			fprintf(fp,"%s}\n",&spaces[indent_level]);
		}
	}
}

void xim_save_response(pstk)
	cbstk_def *pstk;
{
	FILE *fp;

	if(xim_save_fp == NULL)
		return;

	fp = xim_save_fp;

	fprintf(fp,"%s%s {\n",&spaces[indent_level],
		keys_response[RESPONSE_KEY_RESPONSE]);

	indent_level -= INDENT_STEP;
	xim_save_cb(xim_save_fp,pstk);
	indent_level += INDENT_STEP;

	fprintf(fp,"%s}\n",&spaces[indent_level]);

	fprintf(fp,"\n");
}

Bool xim_save_get_style(style,pe,st)
	XIMStyle style;
	int *pe;
	int *st;
{
	int i;

	*pe = -1;
	*st = -1;

	for(i=0;i<nkeys_style;i++)
	{
		if(style & pe_style_val[i])
			*pe = i;
		if(style & status_style_val[i])
			*st = i;
	}
	
	if(*pe == -1 || *st == -1)
		return(False);
}

void xim_save_header(fp,style)
	FILE *fp;
	XIMStyle style;
{
	int pe,st;

	/* version */
	fprintf(fp,"%s %s\n",
		keys_response[RESPONSE_KEY_VERSION],
		RESPONSE_VERSION);

	/* XRelease */
	fprintf(fp,"%s %s\n",
		keys_response[RESPONSE_KEY_XRELEASE],
		RESPONSE_XRELEASE);

	if(!xim_save_get_style(style,&pe,&st))
		report("Unknown Style, 0x%x",style);
	else
	{
		/* Preedit Style */
		fprintf(fp,"%s %s\n",
			keys_response[RESPONSE_KEY_PE_STYLE],
			keys_style[pe]);
		
		/* Status Style */
		fprintf(fp,"%s %s\n",
			keys_response[RESPONSE_KEY_STATUS_STYLE],
			keys_style[st]);
	}

	/* a little space */
	fprintf(fp,"\n");

	/* Start the indentor */
	indent_level = MAX_INDENT - 1 - INDENT_STEP ;
}

Bool xim_save_open(plocale,style)
	char *plocale;
	XIMStyle style;
{
	int testnum;
	char fname[MAXFNAME];
	char *pext;

	/* decide if we are saving as a master or for comparison */
	if(ximconfig.save_im == 0)
		pext = IM_FNAME_DATA;
	else
		pext = IM_FNAME_SAVE;

	/* build a file name */
	/* form is im<test#>.<locale>.response.im_sav */
    testnum = tet_testlist[tet_thistest-1].icref;
	sprintf(fname,"%s%d.%s.%s.%s",
		IM_FNAME_PREFIX,
		testnum,
		plocale,
		IM_FNAME_RESPONSE,
		pext);

	/* figure out which file to open */
	xim_save_fp = fopen(fname,"w");
	if(xim_save_fp == NULL)
	{
		report("Could not open %s to save responses",fname);
		return(False);
	}

	/* write the header */
	fprintf(xim_save_fp,
		"# Response file, %s, saved automatically\n",fname);
	xim_save_header(xim_save_fp,style);

	return(True);
}

void xim_save_close()
{
	if(xim_save_fp != NULL)
		fclose(xim_save_fp);
	xim_save_fp = NULL;
}