The R Project SVN R

Rev

Rev 32871 | Rev 34099 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 32871 Rev 34073
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1995, 1996	Robert Gentleman and Ross Ihaka
3
 *  Copyright (C) 1995, 1996	Robert Gentleman and Ross Ihaka
4
 *  Copyright (C) 2000--2002	The R Development Core Team.
4
 *  Copyright (C) 2000--2005	The R Development Core Team.
5
 *
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
9
 *  (at your option) any later version.
Line 25... Line 25...
25
 *			 and the Encode.. utils.
25
 *			 and the Encode.. utils.
26
 *
26
 *
27
 *  See ./format.c	 for the  format_FOO_  functions used below.
27
 *  See ./format.c	 for the  format_FOO_  functions used below.
28
 */
28
 */
29
 
29
 
30
/* <UTF8> char here is handled as a whole */
30
/* <UTF8> char here is handled as a whole, 
-
 
31
   but lengths are used as display widths */
31
 
32
 
32
#ifdef HAVE_CONFIG_H
33
#ifdef HAVE_CONFIG_H
33
#include <config.h>
34
#include <config.h>
34
#endif
35
#endif
35
 
36
 
36
#include "Defn.h"
37
#include "Defn.h"
37
#include "Print.h"
38
#include "Print.h"
38
 
39
 
-
 
40
/* We need display width of a string */
-
 
41
 
-
 
42
 
-
 
43
#ifdef SUPPORT_MBCS
-
 
44
 
-
 
45
#ifdef HAVE_ALLOCA_H
-
 
46
#include <alloca.h>
-
 
47
#endif
-
 
48
#if !HAVE_DECL_ALLOCA
-
 
49
extern char *alloca(size_t);
-
 
50
#endif
-
 
51
 
-
 
52
# include <wchar.h>
-
 
53
# include <wctype.h>
-
 
54
#if !HAVE_DECL_WCSWIDTH
-
 
55
extern int wcswidth(const wchar_t *s, size_t n);
-
 
56
#endif
-
 
57
#endif
-
 
58
 
-
 
59
static int strwidth(const char *str)
-
 
60
{
-
 
61
#ifdef SUPPORT_MBCS
-
 
62
    if(mbcslocale) {
-
 
63
	int nc = mbstowcs(NULL, str, 0);
-
 
64
#ifdef HAVE_WCSWIDTH
-
 
65
	wchar_t *wc;
-
 
66
	if(nc == 0) return 0;
-
 
67
	wc = (wchar_t *) alloca((nc+1) * sizeof(wchar_t));
-
 
68
	if(!wc) error(_("allocation failure in strwidth"));
-
 
69
	mbstowcs(wc, str, nc + 1);
-
 
70
	return wcswidth(wc, 2147483647);
-
 
71
#else
-
 
72
	return nc;
-
 
73
#endif
-
 
74
    } else
-
 
75
#endif
-
 
76
	return strlen(str);
-
 
77
}
-
 
78
 
-
 
79
 
39
static void printLogicalMatrix(SEXP sx, int offset, int r, int c,
80
static void printLogicalMatrix(SEXP sx, int offset, int r, int c,
40
			       SEXP rl, SEXP cl, char *rn, char *cn)
81
			       SEXP rl, SEXP cl, char *rn, char *cn)
41
{
82
{
42
    SEXP sw;
83
    SEXP sw;
43
    int *x, *w;
84
    int *x, *w;
Line 48... Line 89...
48
	formatString(STRING_PTR(rl), r, &rlabw, 0);
89
	formatString(STRING_PTR(rl), r, &rlabw, 0);
49
    else
90
    else
50
	rlabw = IndexWidth(r + 1) + 3;
91
	rlabw = IndexWidth(r + 1) + 3;
51
 
92
 
52
    if (rn) {
93
    if (rn) {
53
	rnw = strlen(rn);
94
	rnw = strwidth(rn);
54
	if ( rnw < rlabw + R_MIN_LBLOFF )
95
	if ( rnw < rlabw + R_MIN_LBLOFF )
55
	    lbloff = R_MIN_LBLOFF;
96
	    lbloff = R_MIN_LBLOFF;
56
	else
97
	else
57
	    lbloff = rnw - rlabw;
98
	    lbloff = rnw - rlabw;
58
 
99
 
Line 66... Line 107...
66
    for (j = 0; j < c; j++) {
107
    for (j = 0; j < c; j++) {
67
	formatLogical(&x[j * r], r, &w[j]);
108
	formatLogical(&x[j * r], r, &w[j]);
68
	if (!isNull(cl)) {
109
	if (!isNull(cl)) {
69
	    if(STRING_ELT(cl, j) == NA_STRING) 
110
	    if(STRING_ELT(cl, j) == NA_STRING) 
70
		clabw = R_print.na_width_noquote;
111
		clabw = R_print.na_width_noquote;
71
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
112
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
72
	} else
113
	} else
73
	    clabw = IndexWidth(j + 1) + 3;
114
	    clabw = IndexWidth(j + 1) + 3;
74
	if (w[j] < clabw)
115
	if (w[j] < clabw)
75
	    w[j] = clabw;
116
	    w[j] = clabw;
76
	w[j] += R_print.gap;
117
	w[j] += R_print.gap;
Line 124... Line 165...
124
	formatString(STRING_PTR(rl), r, &rlabw, 0);
165
	formatString(STRING_PTR(rl), r, &rlabw, 0);
125
    else
166
    else
126
	rlabw = IndexWidth(r + 1) + 3;
167
	rlabw = IndexWidth(r + 1) + 3;
127
 
168
 
128
    if (rn) {
169
    if (rn) {
129
	rnw = strlen(rn);
170
	rnw = strwidth(rn);
130
	if ( rnw < rlabw + R_MIN_LBLOFF )
171
	if ( rnw < rlabw + R_MIN_LBLOFF )
131
	    lbloff = R_MIN_LBLOFF;
172
	    lbloff = R_MIN_LBLOFF;
132
	else
173
	else
133
	    lbloff = rnw - rlabw;
174
	    lbloff = rnw - rlabw;
134
 
175
 
Line 141... Line 182...
141
    for (j = 0; j < c; j++) {
182
    for (j = 0; j < c; j++) {
142
	formatInteger(&x[j * r], r, &w[j]);
183
	formatInteger(&x[j * r], r, &w[j]);
143
	if (!isNull(cl)) {
184
	if (!isNull(cl)) {
144
	    if(STRING_ELT(cl, j) == NA_STRING) 
185
	    if(STRING_ELT(cl, j) == NA_STRING) 
145
		clabw = R_print.na_width_noquote;
186
		clabw = R_print.na_width_noquote;
146
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
187
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
147
	} else
188
	} else
148
	    clabw = IndexWidth(j + 1) + 3;
189
	    clabw = IndexWidth(j + 1) + 3;
149
	if (w[j] < clabw)
190
	if (w[j] < clabw)
150
	    w[j] = clabw;
191
	    w[j] = clabw;
151
	w[j] += R_print.gap;
192
	w[j] += R_print.gap;
Line 200... Line 241...
200
	formatString(STRING_PTR(rl), r, &rlabw, 0);
241
	formatString(STRING_PTR(rl), r, &rlabw, 0);
201
    else
242
    else
202
	rlabw = IndexWidth(r + 1) + 3;
243
	rlabw = IndexWidth(r + 1) + 3;
203
 
244
 
204
    if (rn) {
245
    if (rn) {
205
	rnw = strlen(rn);
246
	rnw = strwidth(rn);
206
	if ( rnw < rlabw + R_MIN_LBLOFF )
247
	if ( rnw < rlabw + R_MIN_LBLOFF )
207
	    lbloff = R_MIN_LBLOFF;
248
	    lbloff = R_MIN_LBLOFF;
208
	else
249
	else
209
	    lbloff = rnw - rlabw;
250
	    lbloff = rnw - rlabw;
210
 
251
 
Line 223... Line 264...
223
    for (j = 0; j < c; j++) {
264
    for (j = 0; j < c; j++) {
224
	formatReal(&x[j * r], r, &w[j], &d[j], &e[j], 0);
265
	formatReal(&x[j * r], r, &w[j], &d[j], &e[j], 0);
225
	if (!isNull(cl)) {
266
	if (!isNull(cl)) {
226
	    if(STRING_ELT(cl, j) == NA_STRING) 
267
	    if(STRING_ELT(cl, j) == NA_STRING) 
227
		clabw = R_print.na_width_noquote;
268
		clabw = R_print.na_width_noquote;
228
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
269
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
229
	} else
270
	} else
230
	    clabw = IndexWidth(j + 1) + 3;
271
	    clabw = IndexWidth(j + 1) + 3;
231
	if (w[j] < clabw)
272
	if (w[j] < clabw)
232
	    w[j] = clabw;
273
	    w[j] = clabw;
233
	w[j] += R_print.gap;
274
	w[j] += R_print.gap;
Line 282... Line 323...
282
	formatString(STRING_PTR(rl), r, &rlabw, 0);
323
	formatString(STRING_PTR(rl), r, &rlabw, 0);
283
    else
324
    else
284
	rlabw = IndexWidth(r + 1) + 3;
325
	rlabw = IndexWidth(r + 1) + 3;
285
 
326
 
286
    if (rn) {
327
    if (rn) {
287
	rnw = strlen(rn);
328
	rnw = strwidth(rn);
288
	if ( rnw < rlabw + R_MIN_LBLOFF )
329
	if ( rnw < rlabw + R_MIN_LBLOFF )
289
	    lbloff = R_MIN_LBLOFF;
330
	    lbloff = R_MIN_LBLOFF;
290
	else
331
	else
291
	    lbloff = rnw - rlabw;
332
	    lbloff = rnw - rlabw;
292
 
333
 
Line 317... Line 358...
317
		      &wr[j], &dr[j], &er[j],
358
		      &wr[j], &dr[j], &er[j],
318
		      &wi[j], &di[j], &ei[j], 0);
359
		      &wi[j], &di[j], &ei[j], 0);
319
	if (!isNull(cl)) {
360
	if (!isNull(cl)) {
320
	    if(STRING_ELT(cl, j) == NA_STRING) 
361
	    if(STRING_ELT(cl, j) == NA_STRING) 
321
		clabw = R_print.na_width_noquote;
362
		clabw = R_print.na_width_noquote;
322
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
363
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
323
	} else
364
	} else
324
	    clabw = IndexWidth(j + 1) + 3;
365
	    clabw = IndexWidth(j + 1) + 3;
325
	w[j] = wr[j] + wi[j] + 2;
366
	w[j] = wr[j] + wi[j] + 2;
326
	if (w[j] < clabw)
367
	if (w[j] < clabw)
327
	    w[j] = clabw;
368
	    w[j] = clabw;
Line 385... Line 426...
385
	formatString(STRING_PTR(rl), r, &rlabw, 0);
426
	formatString(STRING_PTR(rl), r, &rlabw, 0);
386
    else
427
    else
387
	rlabw = IndexWidth(r + 1) + 3;
428
	rlabw = IndexWidth(r + 1) + 3;
388
 
429
 
389
    if (rn) {
430
    if (rn) {
390
	rnw = strlen(rn);
431
	rnw = strwidth(rn);
391
	if ( rnw < rlabw + R_MIN_LBLOFF )
432
	if ( rnw < rlabw + R_MIN_LBLOFF )
392
	    lbloff = R_MIN_LBLOFF;
433
	    lbloff = R_MIN_LBLOFF;
393
	else
434
	else
394
	    lbloff = rnw - rlabw;
435
	    lbloff = rnw - rlabw;
395
 
436
 
Line 402... Line 443...
402
    for (j = 0; j < c; j++) {
443
    for (j = 0; j < c; j++) {
403
	formatString(&x[j * r], r, &w[j], quote);
444
	formatString(&x[j * r], r, &w[j], quote);
404
	if (!isNull(cl)) {
445
	if (!isNull(cl)) {
405
	    if(STRING_ELT(cl, j) == NA_STRING) 
446
	    if(STRING_ELT(cl, j) == NA_STRING) 
406
		clabw = R_print.na_width_noquote;
447
		clabw = R_print.na_width_noquote;
407
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
448
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
408
	} else
449
	} else
409
	    clabw = IndexWidth(j + 1) + 3;
450
	    clabw = IndexWidth(j + 1) + 3;
410
	if (w[j] < clabw)
451
	if (w[j] < clabw)
411
	    w[j] = clabw;
452
	    w[j] = clabw;
412
    }
453
    }