| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* format.d - format |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
#include <float.h> |
| 17 |
#define ECL_INCLUDE_MATH_H |
| 18 |
#include <ecl/ecl.h> |
| 19 |
#include <limits.h> |
| 20 |
#include <ecl/impl/math_fenv.h> |
| 21 |
#include <ecl/internal.h> |
| 22 |
|
| 23 |
#if !defined(ECL_CMU_FORMAT) |
| 24 |
|
| 25 |
#warning "The old version of FORMAT is not ANSI compliant" |
| 26 |
|
| 27 |
#define FMT_MAX_PARAM 8 |
| 28 |
typedef struct format_stack_struct { |
| 29 |
cl_object stream; |
| 30 |
cl_object aux_stream; |
| 31 |
cl_object aux_string; |
| 32 |
cl_index ctl_index, ctl_end; |
| 33 |
cl_object ctl_str; |
| 34 |
cl_object args, current; |
| 35 |
jmp_buf *jmp_buf; |
| 36 |
cl_index indents; |
| 37 |
cl_index spare_spaces; |
| 38 |
cl_index line_length; |
| 39 |
cl_object param[FMT_MAX_PARAM]; |
| 40 |
int nparam; |
| 41 |
} *format_stack; |
| 42 |
|
| 43 |
#if MOST_POSITIVE_FIXNUM_VAL < INT_MAX |
| 44 |
# define FMT_VALUE_UPPER_LIMIT MOST_POSITIVE_FIXNUM |
| 45 |
#else |
| 46 |
# define FMT_VALUE_UPPER_LIMIT INT_MAX |
| 47 |
#endif |
| 48 |
|
| 49 |
#if MOST_NEGATIVE_FIXNUM_VAL > INT_MIN |
| 50 |
# define FMT_VALUE_LOWER_LIMIT MOST_NEGATIVE_FIXNUM |
| 51 |
#else |
| 52 |
# define FMT_VALUE_LOWER_LIMIT INT_MIN |
| 53 |
#endif |
| 54 |
|
| 55 |
|
| 56 |
/******************* COMMON ***************************/ |
| 57 |
|
| 58 |
#define NONE 0 |
| 59 |
#define INT 1 |
| 60 |
#define CHAR 2 |
| 61 |
|
| 62 |
static const char *fmt_big_numeral[] = { |
| 63 |
"thousand", |
| 64 |
"million", |
| 65 |
"billion", |
| 66 |
"trillion", |
| 67 |
"quadrillion", |
| 68 |
"quintillion", |
| 69 |
"sextillion", |
| 70 |
"septillion", |
| 71 |
"octillion" |
| 72 |
}; |
| 73 |
|
| 74 |
static const char *fmt_numeral[] = { |
| 75 |
"zero", "one", "two", "three", "four", |
| 76 |
"five", "six", "seven", "eight", "nine", |
| 77 |
"ten", "eleven", "twelve", "thirteen", "fourteen", |
| 78 |
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen", |
| 79 |
"zero", "ten", "twenty", "thirty", "forty", |
| 80 |
"fifty", "sixty", "seventy", "eighty", "ninety" |
| 81 |
}; |
| 82 |
|
| 83 |
static const char *fmt_ordinal[] = { |
| 84 |
"zeroth", "first", "second", "third", "fourth", |
| 85 |
"fifth", "sixth", "seventh", "eighth", "ninth", |
| 86 |
"tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", |
| 87 |
"fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", |
| 88 |
"zeroth", "tenth", "twentieth", "thirtieth", "fortieth", |
| 89 |
"fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" |
| 90 |
}; |
| 91 |
|
| 92 |
static void format(format_stack, cl_index, cl_index); |
| 93 |
static cl_object doformat(cl_narg narg, cl_object strm, cl_object string, ecl_va_list args, bool in_formatter); |
| 94 |
|
| 95 |
static cl_object |
| 96 |
get_aux_stream(void) |
| 97 |
{ |
| 98 |
cl_env_ptr env = ecl_process_env(); |
| 99 |
cl_object stream; |
| 100 |
|
| 101 |
ECL_WITHOUT_INTERRUPTS_BEGIN(env) { |
| 102 |
if (env->fmt_aux_stream == ECL_NIL) { |
| 103 |
stream = ecl_make_string_output_stream(64, 1); |
| 104 |
} else { |
| 105 |
stream = env->fmt_aux_stream; |
| 106 |
env->fmt_aux_stream = ECL_NIL; |
| 107 |
} |
| 108 |
} ECL_WITHOUT_INTERRUPTS_END; |
| 109 |
return stream; |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
fmt_error(format_stack fmt, const char *s) |
| 114 |
{ |
| 115 |
cl_error(7, @'si::format-error', |
| 116 |
@':format-control', ecl_make_constant_base_string(s,-1), |
| 117 |
@':control-string', fmt->ctl_str, |
| 118 |
@':offset', ecl_make_fixnum(fmt->ctl_index)); |
| 119 |
} |
| 120 |
|
| 121 |
static ecl_character |
| 122 |
tempstr(format_stack fmt, int s) |
| 123 |
{ |
| 124 |
return ecl_char(fmt->aux_string,s); |
| 125 |
} |
| 126 |
|
| 127 |
static ecl_character |
| 128 |
ctl_advance(format_stack fmt) |
| 129 |
{ |
| 130 |
if (fmt->ctl_index >= fmt->ctl_end) |
| 131 |
fmt_error(fmt, "unexpected end of control string"); |
| 132 |
return ecl_char(fmt->ctl_str, fmt->ctl_index++); |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
fmt_go(format_stack fmt, cl_fixnum n) |
| 137 |
{ |
| 138 |
cl_object p; |
| 139 |
if (n < 0) |
| 140 |
fmt_error(fmt, "can't goto"); |
| 141 |
if ((p = ecl_nthcdr(n, fmt->args)) == ECL_NIL) |
| 142 |
fmt_error(fmt, "can't goto"); |
| 143 |
fmt->current = p; |
| 144 |
} |
| 145 |
|
| 146 |
static cl_index |
| 147 |
fmt_index(format_stack fmt) |
| 148 |
{ |
| 149 |
cl_object p = fmt->args, target = fmt->current; |
| 150 |
cl_index n = 0; |
| 151 |
if (target == ECL_NIL) |
| 152 |
return ecl_length(p); |
| 153 |
while (p != fmt->current) { |
| 154 |
p = CDR(p); |
| 155 |
if (p == ECL_NIL) |
| 156 |
fmt_error(fmt, "Overflow"); |
| 157 |
n++; |
| 158 |
} |
| 159 |
return n; |
| 160 |
} |
| 161 |
|
| 162 |
static cl_object |
| 163 |
fmt_back_up(format_stack fmt) |
| 164 |
{ |
| 165 |
fmt_go(fmt, fmt_index(fmt) - 1); |
| 166 |
} |
| 167 |
|
| 168 |
static bool |
| 169 |
fmt_more_args_p(format_stack fmt) |
| 170 |
{ |
| 171 |
return fmt->current != ECL_NIL; |
| 172 |
} |
| 173 |
|
| 174 |
static cl_index |
| 175 |
fmt_args_left(format_stack fmt) |
| 176 |
{ |
| 177 |
return ecl_length(fmt->current); |
| 178 |
} |
| 179 |
|
| 180 |
static cl_object |
| 181 |
fmt_advance(format_stack fmt) |
| 182 |
{ |
| 183 |
cl_object output, l = fmt->current; |
| 184 |
if (l == ECL_NIL) |
| 185 |
fmt_error(fmt, "arguments exhausted"); |
| 186 |
output = CAR(l); |
| 187 |
fmt->current = CDR(l); |
| 188 |
return output; |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
fmt_set_arg_list(format_stack fmt, cl_object l) |
| 193 |
{ |
| 194 |
assert_type_proper_list(l); |
| 195 |
fmt->current = fmt->args = cl_copy_list(l); |
| 196 |
} |
| 197 |
|
| 198 |
static int |
| 199 |
fmt_skip(format_stack fmt) |
| 200 |
{ |
| 201 |
ecl_character c; |
| 202 |
int level = 0; |
| 203 |
|
| 204 |
LOOP: |
| 205 |
if (ctl_advance(fmt) != '~') |
| 206 |
goto LOOP; |
| 207 |
for (;;) |
| 208 |
switch (c = ctl_advance(fmt)) { |
| 209 |
case '\'': |
| 210 |
ctl_advance(fmt); |
| 211 |
|
| 212 |
case ',': |
| 213 |
case '0': case '1': case '2': case '3': case '4': |
| 214 |
case '5': case '6': case '7': case '8': case '9': |
| 215 |
case '+': |
| 216 |
case '-': |
| 217 |
case 'v': case 'V': |
| 218 |
case '#': |
| 219 |
case ':': case '@@': |
| 220 |
continue; |
| 221 |
|
| 222 |
default: |
| 223 |
goto DIRECTIVE; |
| 224 |
} |
| 225 |
|
| 226 |
DIRECTIVE: |
| 227 |
switch (c) { |
| 228 |
case '(': case '[': case '<': case '{': |
| 229 |
level++; |
| 230 |
break; |
| 231 |
|
| 232 |
case ')': case ']': case '>': case '}': |
| 233 |
if (level == 0) |
| 234 |
return(fmt->ctl_index); |
| 235 |
else |
| 236 |
--level; |
| 237 |
break; |
| 238 |
|
| 239 |
case ';': |
| 240 |
if (level == 0) |
| 241 |
return(fmt->ctl_index); |
| 242 |
break; |
| 243 |
} |
| 244 |
goto LOOP; |
| 245 |
} |
| 246 |
|
| 247 |
static void |
| 248 |
ensure_param(format_stack fmt, int n) |
| 249 |
{ |
| 250 |
if (fmt->nparam > n) |
| 251 |
fmt_error(fmt, "too many parameters"); |
| 252 |
while (n-- > fmt->nparam) |
| 253 |
fmt->param[n] = ECL_NIL; |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
fmt_not_colon(format_stack fmt, bool colon) |
| 258 |
{ |
| 259 |
if (colon) |
| 260 |
fmt_error(fmt, "illegal :"); |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
fmt_not_atsign(format_stack fmt, bool atsign) |
| 265 |
{ |
| 266 |
if (atsign) |
| 267 |
fmt_error(fmt, "illegal @@"); |
| 268 |
} |
| 269 |
|
| 270 |
static void |
| 271 |
fmt_not_colon_atsign(format_stack fmt, bool colon, bool atsign) |
| 272 |
{ |
| 273 |
if (colon && atsign) |
| 274 |
fmt_error(fmt, "illegal :@@"); |
| 275 |
} |
| 276 |
|
| 277 |
static cl_object |
| 278 |
set_param(format_stack fmt, int i, int t, cl_object v) |
| 279 |
{ |
| 280 |
if (i >= fmt->nparam || fmt->param[i] == ECL_NIL) |
| 281 |
return v; |
| 282 |
else if ((t != INT && t != CHAR) || |
| 283 |
(t == INT && !cl_integerp(fmt->param[i])) || |
| 284 |
(t == CHAR && !ECL_CHARACTERP(fmt->param[i]))) |
| 285 |
fmt_error(fmt, "illegal parameter type"); |
| 286 |
return fmt->param[i]; |
| 287 |
} |
| 288 |
|
| 289 |
static int |
| 290 |
set_param_positive(format_stack fmt, int i, const char *message) |
| 291 |
{ |
| 292 |
if (i >= fmt->nparam || fmt->param[i] == ECL_NIL) |
| 293 |
return -1; |
| 294 |
else if (cl_integerp(fmt->param[i]) == ECL_NIL) |
| 295 |
fmt_error(fmt, "illegal parameter type"); |
| 296 |
else { |
| 297 |
cl_object p = fmt->param[i]; |
| 298 |
if (ecl_minusp(p)) fmt_error(fmt, message); |
| 299 |
return ecl_to_fix(p); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
static void |
| 304 |
fmt_copy(format_stack fmt_copy, format_stack fmt) |
| 305 |
{ |
| 306 |
*fmt_copy = *fmt; |
| 307 |
} |
| 308 |
|
| 309 |
static void |
| 310 |
fmt_copy1(format_stack fmt_copy, format_stack fmt) |
| 311 |
{ |
| 312 |
fmt_copy->stream = fmt->stream; |
| 313 |
fmt_copy->ctl_str = fmt->ctl_str; |
| 314 |
fmt_copy->ctl_index = fmt->ctl_index; |
| 315 |
fmt_copy->ctl_end = fmt->ctl_end; |
| 316 |
fmt_copy->jmp_buf = fmt->jmp_buf; |
| 317 |
fmt_copy->indents = fmt->indents; |
| 318 |
} |
| 319 |
|
| 320 |
static void |
| 321 |
fmt_prepare_aux_stream(format_stack fmt) |
| 322 |
{ |
| 323 |
fmt->aux_string->base_string.fillp = 0; |
| 324 |
fmt->aux_stream->stream.int0 = ecl_file_column(fmt->stream); |
| 325 |
fmt->aux_stream->stream.int1 = ecl_file_column(fmt->stream); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
static void |
| 330 |
fmt_ascii(format_stack fmt, bool colon, bool atsign) |
| 331 |
{ |
| 332 |
int mincol, colinc, minpad; |
| 333 |
ecl_character padchar; |
| 334 |
cl_object x; |
| 335 |
int l, i; |
| 336 |
|
| 337 |
ensure_param(fmt, 4); |
| 338 |
mincol = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 339 |
colinc = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(1))); |
| 340 |
minpad = ecl_to_fix(set_param(fmt, 2, INT, ecl_make_fixnum(0))); |
| 341 |
padchar = ECL_CHAR_CODE(set_param(fmt, 3, CHAR, ECL_CODE_CHAR(' '))); |
| 342 |
|
| 343 |
fmt_prepare_aux_stream(fmt); |
| 344 |
x = fmt_advance(fmt); |
| 345 |
if (colon && Null(x)) |
| 346 |
writestr_stream("()", fmt->aux_stream); |
| 347 |
else if (mincol == 0 && minpad == 0) { |
| 348 |
ecl_princ(x, fmt->stream); |
| 349 |
return; |
| 350 |
} else |
| 351 |
ecl_princ(x, fmt->aux_stream); |
| 352 |
l = fmt->aux_string->base_string.fillp; |
| 353 |
for (i = minpad; l + i < mincol; i += colinc) |
| 354 |
; |
| 355 |
if (!atsign) { |
| 356 |
ecl_write_string(fmt->aux_string, fmt->stream); |
| 357 |
while (i-- > 0) |
| 358 |
ecl_write_char(padchar, fmt->stream); |
| 359 |
} else { |
| 360 |
while (i-- > 0) |
| 361 |
ecl_write_char(padchar, fmt->stream); |
| 362 |
ecl_write_string(fmt->aux_string, fmt->stream); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
static void |
| 367 |
fmt_S_expression(format_stack fmt, bool colon, bool atsign) |
| 368 |
{ |
| 369 |
int mincol, colinc, minpad; |
| 370 |
ecl_character padchar; |
| 371 |
cl_object x; |
| 372 |
int l, i; |
| 373 |
|
| 374 |
ensure_param(fmt, 4); |
| 375 |
mincol = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 376 |
colinc = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(1))); |
| 377 |
minpad = ecl_to_fix(set_param(fmt, 2, INT, ecl_make_fixnum(0))); |
| 378 |
padchar = ECL_CHAR_CODE(set_param(fmt, 3, CHAR, ECL_CODE_CHAR(' '))); |
| 379 |
|
| 380 |
fmt_prepare_aux_stream(fmt); |
| 381 |
x = fmt_advance(fmt); |
| 382 |
if (colon && Null(x)) |
| 383 |
writestr_stream("()", fmt->aux_stream); |
| 384 |
else if (mincol == 0 && minpad == 0) { |
| 385 |
ecl_prin1(x, fmt->stream); |
| 386 |
return; |
| 387 |
} else |
| 388 |
ecl_prin1(x, fmt->aux_stream); |
| 389 |
l = fmt->aux_string->base_string.fillp; |
| 390 |
for (i = minpad; l + i < mincol; i += colinc) |
| 391 |
; |
| 392 |
if (!atsign) { |
| 393 |
ecl_write_string(fmt->aux_string, fmt->stream); |
| 394 |
while (i-- > 0) |
| 395 |
ecl_write_char(padchar, fmt->stream); |
| 396 |
} else { |
| 397 |
while (i-- > 0) |
| 398 |
ecl_write_char(padchar, fmt->stream); |
| 399 |
ecl_write_string(fmt->aux_string, fmt->stream); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
static void |
| 405 |
fmt_integer(format_stack fmt, cl_object x, bool colon, bool atsign, |
| 406 |
int radix, int mincol, ecl_character padchar, ecl_character commachar) |
| 407 |
{ |
| 408 |
const cl_env_ptr env = ecl_process_env(); |
| 409 |
int l, l1; |
| 410 |
int s; |
| 411 |
|
| 412 |
if (!ECL_FIXNUMP(x) && ecl_t_of(x) != t_bignum) { |
| 413 |
fmt_prepare_aux_stream(fmt); |
| 414 |
ecl_bds_bind(env, @'*print-escape*', ECL_NIL); |
| 415 |
ecl_bds_bind(env, @'*print-base*', ecl_make_fixnum(radix)); |
| 416 |
si_write_object(x, fmt->aux_stream); |
| 417 |
ecl_bds_unwind_n(env, 2); |
| 418 |
l = fmt->aux_string->base_string.fillp; |
| 419 |
mincol -= l; |
| 420 |
while (mincol-- > 0) |
| 421 |
ecl_write_char(padchar, fmt->stream); |
| 422 |
ecl_write_string(fmt->aux_string, fmt->stream); |
| 423 |
return; |
| 424 |
} |
| 425 |
fmt_prepare_aux_stream(fmt); |
| 426 |
ecl_bds_bind(env, @'*print-radix*', ECL_NIL); |
| 427 |
ecl_bds_bind(env, @'*print-base*', ecl_make_fixnum(radix)); |
| 428 |
si_write_object(x, fmt->aux_stream); |
| 429 |
ecl_bds_unwind_n(env, 2); |
| 430 |
l = l1 = fmt->aux_string->base_string.fillp; |
| 431 |
s = 0; |
| 432 |
if (tempstr(fmt, s) == '-') |
| 433 |
--l1; |
| 434 |
mincol -= l; |
| 435 |
if (colon) |
| 436 |
mincol -= (l1 - 1)/3; |
| 437 |
if (atsign && tempstr(fmt, s) != '-') |
| 438 |
--mincol; |
| 439 |
while (mincol-- > 0) |
| 440 |
ecl_write_char(padchar, fmt->stream); |
| 441 |
if (tempstr(fmt, s) == '-') { |
| 442 |
s++; |
| 443 |
ecl_write_char('-', fmt->stream); |
| 444 |
} else if (atsign) |
| 445 |
ecl_write_char('+', fmt->stream); |
| 446 |
while (l1-- > 0) { |
| 447 |
ecl_write_char(tempstr(fmt, s++), fmt->stream); |
| 448 |
if (colon && l1 > 0 && l1%3 == 0) |
| 449 |
ecl_write_char(commachar, fmt->stream); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
static void |
| 454 |
fmt_decimal(format_stack fmt, bool colon, bool atsign) |
| 455 |
{ |
| 456 |
int mincol; |
| 457 |
ecl_character padchar, commachar; |
| 458 |
|
| 459 |
ensure_param(fmt, 3); |
| 460 |
mincol = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 461 |
padchar = ECL_CHAR_CODE(set_param(fmt, 1, CHAR, ECL_CODE_CHAR(' '))); |
| 462 |
commachar = ECL_CHAR_CODE(set_param(fmt, 2, CHAR, ECL_CODE_CHAR(','))); |
| 463 |
fmt_integer(fmt, fmt_advance(fmt), colon, atsign, |
| 464 |
10, mincol, padchar, commachar); |
| 465 |
} |
| 466 |
|
| 467 |
static void |
| 468 |
fmt_binary(format_stack fmt, bool colon, bool atsign) |
| 469 |
{ |
| 470 |
int mincol; |
| 471 |
ecl_character padchar, commachar; |
| 472 |
|
| 473 |
ensure_param(fmt, 3); |
| 474 |
mincol = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 475 |
padchar = ECL_CHAR_CODE(set_param(fmt, 1, CHAR, ECL_CODE_CHAR(' '))); |
| 476 |
commachar = ECL_CHAR_CODE(set_param(fmt, 2, CHAR, ECL_CODE_CHAR(','))); |
| 477 |
fmt_integer(fmt, fmt_advance(fmt), colon, atsign, |
| 478 |
2, mincol, padchar, commachar); |
| 479 |
} |
| 480 |
|
| 481 |
static void |
| 482 |
fmt_octal(format_stack fmt, bool colon, bool atsign) |
| 483 |
{ |
| 484 |
int mincol; |
| 485 |
ecl_character padchar, commachar; |
| 486 |
|
| 487 |
ensure_param(fmt, 3); |
| 488 |
mincol = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 489 |
padchar = ECL_CHAR_CODE(set_param(fmt, 1, CHAR, ECL_CODE_CHAR(' '))); |
| 490 |
commachar = ECL_CHAR_CODE(set_param(fmt, 2, CHAR, ECL_CODE_CHAR(','))); |
| 491 |
fmt_integer(fmt, fmt_advance(fmt), colon, atsign, |
| 492 |
8, mincol, padchar, commachar); |
| 493 |
} |
| 494 |
|
| 495 |
static void |
| 496 |
fmt_hexadecimal(format_stack fmt, bool colon, bool atsign) |
| 497 |
{ |
| 498 |
int mincol; |
| 499 |
ecl_character padchar, commachar; |
| 500 |
|
| 501 |
ensure_param(fmt, 3); |
| 502 |
mincol = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 503 |
padchar = ECL_CHAR_CODE(set_param(fmt, 1, CHAR, ECL_CODE_CHAR(' '))); |
| 504 |
commachar = ECL_CHAR_CODE(set_param(fmt, 2, CHAR, ECL_CODE_CHAR(','))); |
| 505 |
fmt_integer(fmt, fmt_advance(fmt), colon, atsign, |
| 506 |
16, mincol, padchar, commachar); |
| 507 |
} |
| 508 |
|
| 509 |
static void |
| 510 |
fmt_write_numeral(format_stack fmt, int s, int i) |
| 511 |
{ |
| 512 |
writestr_stream(fmt_numeral[tempstr(fmt, s) - '0' + i], fmt->stream); |
| 513 |
} |
| 514 |
|
| 515 |
static void |
| 516 |
fmt_write_ordinal(format_stack fmt, int s, int i) |
| 517 |
{ |
| 518 |
writestr_stream(fmt_ordinal[tempstr(fmt, s) - '0' + i], fmt->stream); |
| 519 |
} |
| 520 |
|
| 521 |
static bool |
| 522 |
fmt_thousand(format_stack fmt, int s, int i, bool b, bool o, int t) |
| 523 |
{ |
| 524 |
if (i == 3 && tempstr(fmt, s) > '0') { |
| 525 |
if (b) |
| 526 |
ecl_write_char(' ', fmt->stream); |
| 527 |
fmt_write_numeral(fmt, s, 0); |
| 528 |
writestr_stream(" hundred", fmt->stream); |
| 529 |
--i; |
| 530 |
s++; |
| 531 |
b = TRUE; |
| 532 |
if (o && (s > t)) |
| 533 |
writestr_stream("th", fmt->stream); |
| 534 |
} |
| 535 |
if (i == 3) { |
| 536 |
--i; |
| 537 |
s++; |
| 538 |
} |
| 539 |
if (i == 2 && tempstr(fmt, s) > '0') { |
| 540 |
if (b) |
| 541 |
ecl_write_char(' ', fmt->stream); |
| 542 |
if (tempstr(fmt, s) == '1') { |
| 543 |
if (o && (s + 2 > t)) |
| 544 |
fmt_write_ordinal(fmt, ++s, 10); |
| 545 |
else |
| 546 |
fmt_write_numeral(fmt, ++s, 10); |
| 547 |
return(TRUE); |
| 548 |
} else { |
| 549 |
if (o && (s + 1 > t)) |
| 550 |
fmt_write_ordinal(fmt, s, 20); |
| 551 |
else |
| 552 |
fmt_write_numeral(fmt, s, 20); |
| 553 |
s++; |
| 554 |
if (tempstr(fmt, s) > '0') { |
| 555 |
ecl_write_char('-', fmt->stream); |
| 556 |
if (o && s + 1 > t) |
| 557 |
fmt_write_ordinal(fmt, s, 0); |
| 558 |
else |
| 559 |
fmt_write_numeral(fmt, s, 0); |
| 560 |
} |
| 561 |
return(TRUE); |
| 562 |
} |
| 563 |
} |
| 564 |
if (i == 2) |
| 565 |
s++; |
| 566 |
if (tempstr(fmt, s) > '0') { |
| 567 |
if (b) |
| 568 |
ecl_write_char(' ', fmt->stream); |
| 569 |
if (o && s + 1 > t) |
| 570 |
fmt_write_ordinal(fmt, s, 0); |
| 571 |
else |
| 572 |
fmt_write_numeral(fmt, s, 0); |
| 573 |
return(TRUE); |
| 574 |
} |
| 575 |
return(b); |
| 576 |
} |
| 577 |
|
| 578 |
static bool |
| 579 |
fmt_nonillion(format_stack fmt, int s, int i, bool b, bool o, int t) |
| 580 |
{ |
| 581 |
int j; |
| 582 |
|
| 583 |
for (; i > 3; i -= j) { |
| 584 |
b = fmt_thousand(fmt, s, j = (i+2)%3+1, b, FALSE, t); |
| 585 |
if (j != 3 || tempstr(fmt, s) != '0' || |
| 586 |
tempstr(fmt, s+1) != '0' || tempstr(fmt, s+2) != '0') { |
| 587 |
ecl_write_char(' ', fmt->stream); |
| 588 |
writestr_stream(fmt_big_numeral[(i - 1)/3 - 1], |
| 589 |
fmt->stream); |
| 590 |
s += j; |
| 591 |
if (o && s > t) |
| 592 |
writestr_stream("th", fmt->stream); |
| 593 |
} else |
| 594 |
s += j; |
| 595 |
} |
| 596 |
return(fmt_thousand(fmt, s, i, b, o, t)); |
| 597 |
} |
| 598 |
|
| 599 |
static void |
| 600 |
fmt_roman(format_stack fmt, int i, int one, int five, int ten, bool colon) |
| 601 |
{ |
| 602 |
int j; |
| 603 |
|
| 604 |
if (i == 0) |
| 605 |
return; |
| 606 |
if ((!colon && i < 4) || (colon && i < 5)) |
| 607 |
for (j = 0; j < i; j++) |
| 608 |
ecl_write_char(one, fmt->stream); |
| 609 |
else if (!colon && i == 4) { |
| 610 |
ecl_write_char(one, fmt->stream); |
| 611 |
ecl_write_char(five, fmt->stream); |
| 612 |
} else if ((!colon && i < 9) || colon) { |
| 613 |
ecl_write_char(five, fmt->stream); |
| 614 |
for (j = 5; j < i; j++) |
| 615 |
ecl_write_char(one, fmt->stream); |
| 616 |
} else if (!colon && i == 9) { |
| 617 |
ecl_write_char(one, fmt->stream); |
| 618 |
ecl_write_char(ten, fmt->stream); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
static void |
| 623 |
fmt_radix(format_stack fmt, bool colon, bool atsign) |
| 624 |
{ |
| 625 |
const cl_env_ptr env = ecl_process_env(); |
| 626 |
int radix, mincol; |
| 627 |
ecl_character padchar, commachar; |
| 628 |
cl_object x; |
| 629 |
int i, j, k; |
| 630 |
int s, t; |
| 631 |
bool b; |
| 632 |
|
| 633 |
if (fmt->nparam == 0) { |
| 634 |
x = fmt_advance(fmt); |
| 635 |
assert_type_integer(x); |
| 636 |
if (atsign) { |
| 637 |
if (ECL_FIXNUMP(x)) |
| 638 |
i = ecl_fixnum(x); |
| 639 |
else |
| 640 |
i = -1; |
| 641 |
if ((!colon && (i <= 0 || i >= 4000)) || |
| 642 |
(colon && (i <= 0 || i >= 5000))) { |
| 643 |
fmt_integer(fmt, x, FALSE, FALSE, 10, 0, ' ', ','); |
| 644 |
return; |
| 645 |
} |
| 646 |
fmt_roman(fmt, i/1000, 'M', '*', '*', colon); |
| 647 |
fmt_roman(fmt, i%1000/100, 'C', 'D', 'M', colon); |
| 648 |
fmt_roman(fmt, i%100/10, 'X', 'L', 'C', colon); |
| 649 |
fmt_roman(fmt, i%10, 'I', 'V', 'X', colon); |
| 650 |
return; |
| 651 |
} |
| 652 |
fmt_prepare_aux_stream(fmt); |
| 653 |
ecl_bds_bind(env, @'*print-radix*', ECL_NIL); |
| 654 |
ecl_bds_bind(env, @'*print-base*', ecl_make_fixnum(10)); |
| 655 |
si_write_object(x, fmt->aux_stream); |
| 656 |
ecl_bds_unwind_n(env, 2); |
| 657 |
s = 0; |
| 658 |
i = fmt->aux_string->base_string.fillp; |
| 659 |
if (i == 1 && tempstr(fmt, s) == '0') { |
| 660 |
writestr_stream("zero", fmt->stream); |
| 661 |
if (colon) |
| 662 |
writestr_stream("th", fmt->stream); |
| 663 |
return; |
| 664 |
} else if (tempstr(fmt, s) == '-') { |
| 665 |
writestr_stream("minus ", fmt->stream); |
| 666 |
--i; |
| 667 |
s++; |
| 668 |
} |
| 669 |
t = fmt->aux_string->base_string.fillp; |
| 670 |
for (; tempstr(fmt, --t) == '0' ;) ; |
| 671 |
for (b = FALSE; i > 0; i -= j) { |
| 672 |
b = fmt_nonillion(fmt, s, j = (i+29)%30+1, b, |
| 673 |
i<=30&&colon, t); |
| 674 |
s += j; |
| 675 |
if (b && i > 30) { |
| 676 |
for (k = (i - 1)/30; k > 0; --k) |
| 677 |
writestr_stream(" nonillion", |
| 678 |
fmt->stream); |
| 679 |
if (colon && s > t) |
| 680 |
writestr_stream("th", fmt->stream); |
| 681 |
} |
| 682 |
} |
| 683 |
return; |
| 684 |
} |
| 685 |
ensure_param(fmt, 4); |
| 686 |
radix = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(10))); |
| 687 |
mincol = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(0))); |
| 688 |
padchar = ECL_CHAR_CODE(set_param(fmt, 2, CHAR, ECL_CODE_CHAR(' '))); |
| 689 |
commachar = ECL_CHAR_CODE(set_param(fmt, 3, CHAR, ECL_CODE_CHAR(','))); |
| 690 |
x = fmt_advance(fmt); |
| 691 |
assert_type_integer(x); |
| 692 |
if (radix < 0 || radix > 36) |
| 693 |
FEerror("~D is illegal as a radix.", 1, ecl_make_fixnum(radix)); |
| 694 |
fmt_integer(fmt, x, colon, atsign, radix, mincol, padchar, commachar); |
| 695 |
} |
| 696 |
|
| 697 |
static void |
| 698 |
fmt_plural(format_stack fmt, bool colon, bool atsign) |
| 699 |
{ |
| 700 |
ensure_param(fmt, 0); |
| 701 |
if (colon) { |
| 702 |
fmt_back_up(fmt); |
| 703 |
} |
| 704 |
if (ecl_eql(fmt_advance(fmt), ecl_make_fixnum(1))) { |
| 705 |
if (atsign) |
| 706 |
ecl_write_char('y', fmt->stream); |
| 707 |
} |
| 708 |
else |
| 709 |
if (atsign) |
| 710 |
writestr_stream("ies", fmt->stream); |
| 711 |
else |
| 712 |
ecl_write_char('s', fmt->stream); |
| 713 |
} |
| 714 |
|
| 715 |
static void |
| 716 |
fmt_character(format_stack fmt, bool colon, bool atsign) |
| 717 |
{ |
| 718 |
cl_object x; |
| 719 |
cl_index i; |
| 720 |
|
| 721 |
ensure_param(fmt, 0); |
| 722 |
x = fmt_advance(fmt); |
| 723 |
x = ecl_check_cl_type(@'format',x,t_character); |
| 724 |
if (!colon && !atsign) { |
| 725 |
ecl_write_char(ECL_CHAR_CODE(x), fmt->stream); |
| 726 |
} else { |
| 727 |
fmt_prepare_aux_stream(fmt); |
| 728 |
ecl_prin1(x, fmt->aux_stream); |
| 729 |
if (!colon && atsign) |
| 730 |
i = 0; |
| 731 |
else |
| 732 |
i = 2; |
| 733 |
for (; i < fmt->aux_string->base_string.fillp; i++) |
| 734 |
ecl_write_char(tempstr(fmt, i), fmt->stream); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
/* The floating point precision is required to make the |
| 739 |
most-positive-long-float printed expression readable. |
| 740 |
If this is too small, then the rounded off fraction, may be too big |
| 741 |
to read */ |
| 742 |
|
| 743 |
/* Maximum number of significant digits required to represent accurately |
| 744 |
* a double or single float. */ |
| 745 |
|
| 746 |
#define LOG10_2 0.30103 |
| 747 |
#define DBL_SIG ((int)(DBL_MANT_DIG * LOG10_2 + 1)) |
| 748 |
#define FLT_SIG ((int)(FLT_MANT_DIG * LOG10_2 + 1)) |
| 749 |
|
| 750 |
/* This is the maximum number of decimal digits that our numbers will have. |
| 751 |
* Notice that we leave some extra margin, to ensure that reading the number |
| 752 |
* again will produce the same floating point number. |
| 753 |
*/ |
| 754 |
#define LDBL_SIG ((int)(LDBL_MANT_DIG * LOG10_2 + 1)) |
| 755 |
#define DBL_MAX_DIGITS (LDBL_SIG + 3) |
| 756 |
#define DBL_EXPONENT_SIZE (1 + 1 + 4) |
| 757 |
|
| 758 |
/* The sinificant digits + the possible sign + the decimal dot. */ |
| 759 |
#define DBL_MANTISSA_SIZE (DBL_MAX_DIGITS + 1 + 1) |
| 760 |
/* Total estimated size that a floating point number can take. */ |
| 761 |
#define DBL_SIZE (DBL_MANTISSA_SIZE + DBL_EXPONENT_SIZE) |
| 762 |
|
| 763 |
#define EXP_STRING "Le" |
| 764 |
#define G_EXP_STRING "Lg" |
| 765 |
#define DBL_TYPE long double |
| 766 |
#define strtod strtold |
| 767 |
extern long double strtold(const char *nptr, char **endptr); |
| 768 |
|
| 769 |
static int |
| 770 |
edit_double(int n, DBL_TYPE d, int *sp, char *s, int *ep) |
| 771 |
{ |
| 772 |
char *exponent, buff[DBL_SIZE + 1]; |
| 773 |
int length; |
| 774 |
|
| 775 |
ECL_WITHOUT_FPE_BEGIN { |
| 776 |
unlikely_if (isnan(d) || !isfinite(d)) { |
| 777 |
FEerror("Can't print a non-number.", 0); |
| 778 |
} |
| 779 |
if (n < -DBL_MAX_DIGITS) |
| 780 |
n = DBL_MAX_DIGITS; |
| 781 |
if (n < 0) { |
| 782 |
DBL_TYPE aux; |
| 783 |
n = -n; |
| 784 |
do { |
| 785 |
sprintf(buff, "%- *.*" EXP_STRING, n + 1 + 1 + DBL_EXPONENT_SIZE, n-1, d); |
| 786 |
aux = strtod(buff, NULL); |
| 787 |
if (n < LDBL_SIG) |
| 788 |
aux = (double) aux; |
| 789 |
if (n < DBL_SIG) |
| 790 |
aux = (float)aux; |
| 791 |
n++; |
| 792 |
} while (d != aux && n <= DBL_MAX_DIGITS); |
| 793 |
n--; |
| 794 |
} else { |
| 795 |
sprintf(buff, "%- *.*" EXP_STRING, DBL_SIZE, |
| 796 |
(n <= DBL_MAX_DIGITS)? (n-1) : (DBL_MAX_DIGITS-1), d); |
| 797 |
} |
| 798 |
exponent = strchr(buff, 'e'); |
| 799 |
|
| 800 |
/* Get the exponent */ |
| 801 |
*ep = strtol(exponent+1, NULL, 10); |
| 802 |
|
| 803 |
/* Get the sign */ |
| 804 |
*sp = (buff[0] == '-') ? -1 : +1; |
| 805 |
|
| 806 |
/* Get the digits of the mantissa */ |
| 807 |
buff[2] = buff[1]; |
| 808 |
|
| 809 |
/* Get the actual number of digits in the mantissa */ |
| 810 |
length = exponent - (buff + 2); |
| 811 |
|
| 812 |
/* The output consists of a string {d1,d2,d3,...,dn} |
| 813 |
with all N digits of the mantissa. If we ask for more |
| 814 |
digits than there are, the last ones are set to zero. */ |
| 815 |
if (n <= length) { |
| 816 |
memcpy(s, buff+2, n); |
| 817 |
} else { |
| 818 |
cl_index i; |
| 819 |
memcpy(s, buff+2, length); |
| 820 |
for (i = length; i < n; i++) |
| 821 |
s[i] = '0'; |
| 822 |
} |
| 823 |
s[n] = '\0'; |
| 824 |
} ECL_WITHOUT_FPE_END; |
| 825 |
|
| 826 |
return length; |
| 827 |
} |
| 828 |
|
| 829 |
static void |
| 830 |
fmt_fix_float(format_stack fmt, bool colon, bool atsign) |
| 831 |
{ |
| 832 |
int w, d, k; |
| 833 |
ecl_character overflowchar, padchar; |
| 834 |
double f; |
| 835 |
int sign; |
| 836 |
char buff[256], *b, buff1[256]; |
| 837 |
int exp; |
| 838 |
int i, j; |
| 839 |
cl_object x; |
| 840 |
int n, m; |
| 841 |
|
| 842 |
b = buff1 + 1; |
| 843 |
|
| 844 |
fmt_not_colon(fmt, colon); |
| 845 |
ensure_param(fmt, 5); |
| 846 |
w = set_param_positive(fmt, 0, "illegal width"); |
| 847 |
d = set_param_positive(fmt, 1, "illegal number of digits"); |
| 848 |
k = ecl_to_fix(set_param(fmt, 2, INT, ecl_make_fixnum(0))); |
| 849 |
overflowchar = ECL_CHAR_CODE(set_param(fmt, 3, CHAR, ECL_CODE_CHAR('\0'))); |
| 850 |
padchar = ECL_CHAR_CODE(set_param(fmt, 4, CHAR, ECL_CODE_CHAR(' '))); |
| 851 |
|
| 852 |
x = fmt_advance(fmt); |
| 853 |
if (ECL_FIXNUMP(x) || |
| 854 |
ecl_t_of(x) == t_bignum || |
| 855 |
ecl_t_of(x) == t_ratio) |
| 856 |
x = ecl_make_single_float(ecl_to_float(x)); |
| 857 |
if (!ECL_REAL_TYPE_P(ecl_t_of(x))) { |
| 858 |
if (fmt->nparam > 1) fmt->nparam = 1; |
| 859 |
fmt_back_up(fmt); |
| 860 |
fmt_decimal(fmt, colon, atsign); |
| 861 |
return; |
| 862 |
} |
| 863 |
if (ecl_t_of(x) == t_doublefloat) |
| 864 |
n = 16; |
| 865 |
else |
| 866 |
n = 7; |
| 867 |
f = ecl_to_double(x); |
| 868 |
edit_double(n, f, &sign, buff, &exp); |
| 869 |
if (exp + k > 100 || exp + k < -100 || d > 100) { |
| 870 |
ecl_prin1(x, fmt->stream); |
| 871 |
return; |
| 872 |
} |
| 873 |
if (d >= 0) |
| 874 |
m = d + exp + k + 1; |
| 875 |
else if (w >= 0) { |
| 876 |
if (exp + k >= 0) |
| 877 |
m = w - 1; |
| 878 |
else |
| 879 |
m = w + exp + k - 2; |
| 880 |
if (sign < 0 || atsign) |
| 881 |
--m; |
| 882 |
if (m == 0) |
| 883 |
m = 1; |
| 884 |
} else |
| 885 |
m = n; |
| 886 |
if (m <= 0) { |
| 887 |
if (m == 0 && buff[0] >= '5') { |
| 888 |
exp++; |
| 889 |
n = m = 1; |
| 890 |
buff[0] = '1'; |
| 891 |
} else |
| 892 |
n = m = 0; |
| 893 |
} else if (m < n) { |
| 894 |
n = m; |
| 895 |
edit_double(n, f, &sign, buff, &exp); |
| 896 |
} |
| 897 |
while (n >= 0) |
| 898 |
if (buff[n - 1] == '0') |
| 899 |
--n; |
| 900 |
else |
| 901 |
break; |
| 902 |
exp += k; |
| 903 |
j = 0; |
| 904 |
if (exp >= 0) { |
| 905 |
for (i = 0; i <= exp; i++) |
| 906 |
b[j++] = i < n ? buff[i] : '0'; |
| 907 |
b[j++] = '.'; |
| 908 |
if (d >= 0) |
| 909 |
for (m = i + d; i < m; i++) |
| 910 |
b[j++] = i < n ? buff[i] : '0'; |
| 911 |
else |
| 912 |
for (; i < n; i++) |
| 913 |
b[j++] = buff[i]; |
| 914 |
} else { |
| 915 |
b[j++] = '.'; |
| 916 |
if (d >= 0) { |
| 917 |
for (i = 0; i < (-exp) - 1 && i < d; i++) |
| 918 |
b[j++] = '0'; |
| 919 |
for (m = d - i, i = 0; i < m; i++) |
| 920 |
b[j++] = i < n ? buff[i] : '0'; |
| 921 |
} else if (n > 0) { |
| 922 |
for (i = 0; i < (-exp) - 1; i++) |
| 923 |
b[j++] = '0'; |
| 924 |
for (i = 0; i < n; i++) |
| 925 |
b[j++] = buff[i]; |
| 926 |
} |
| 927 |
} |
| 928 |
b[j] = '\0'; |
| 929 |
if (w >= 0) { |
| 930 |
if (sign < 0 || atsign) |
| 931 |
--w; |
| 932 |
if (j > w && overflowchar != '\0') { |
| 933 |
w = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 934 |
for (i = 0; i < w; i++) |
| 935 |
ecl_write_char(overflowchar, fmt->stream); |
| 936 |
return; |
| 937 |
} |
| 938 |
if (j < w && d < 0 && b[j-1] == '.') { |
| 939 |
b[j++] = '0'; |
| 940 |
b[j] = '\0'; |
| 941 |
} |
| 942 |
if (j < w && b[0] == '.') { |
| 943 |
*--b = '0'; |
| 944 |
j++; |
| 945 |
} |
| 946 |
for (i = j; i < w; i++) |
| 947 |
ecl_write_char(padchar, fmt->stream); |
| 948 |
} else { |
| 949 |
if (b[0] == '.') { |
| 950 |
*--b = '0'; |
| 951 |
j++; |
| 952 |
} |
| 953 |
if (d < 0 && b[j-1] == '.') { |
| 954 |
b[j++] = '0'; |
| 955 |
b[j] = '\0'; |
| 956 |
} |
| 957 |
} |
| 958 |
if (sign < 0) |
| 959 |
ecl_write_char('-', fmt->stream); |
| 960 |
else if (atsign) |
| 961 |
ecl_write_char('+', fmt->stream); |
| 962 |
writestr_stream(b, fmt->stream); |
| 963 |
} |
| 964 |
|
| 965 |
static int |
| 966 |
fmt_exponent_length(int e) |
| 967 |
{ |
| 968 |
int i; |
| 969 |
|
| 970 |
if (e == 0) |
| 971 |
return(1); |
| 972 |
if (e < 0) |
| 973 |
e = -e; |
| 974 |
for (i = 0; e > 0; i++, e /= 10) |
| 975 |
; |
| 976 |
return(i); |
| 977 |
} |
| 978 |
|
| 979 |
static void |
| 980 |
fmt_exponent1(cl_object stream, int e) |
| 981 |
{ |
| 982 |
if (e == 0) |
| 983 |
return; |
| 984 |
fmt_exponent1(stream, e/10); |
| 985 |
ecl_write_char('0' + e%10, stream); |
| 986 |
} |
| 987 |
|
| 988 |
static void |
| 989 |
fmt_exponent(format_stack fmt, int e) |
| 990 |
{ |
| 991 |
if (e == 0) { |
| 992 |
ecl_write_char('0', fmt->stream); |
| 993 |
return; |
| 994 |
} |
| 995 |
if (e < 0) |
| 996 |
e = -e; |
| 997 |
fmt_exponent1(fmt->stream, e); |
| 998 |
} |
| 999 |
|
| 1000 |
static void |
| 1001 |
fmt_exponential_float(format_stack fmt, bool colon, bool atsign) |
| 1002 |
{ |
| 1003 |
int w, d, e, k; |
| 1004 |
ecl_character overflowchar, padchar, exponentchar; |
| 1005 |
double f; |
| 1006 |
int sign; |
| 1007 |
char buff[256], *b, buff1[256]; |
| 1008 |
int exp; |
| 1009 |
int i, j; |
| 1010 |
cl_object x, y; |
| 1011 |
int n, m; |
| 1012 |
cl_type t; |
| 1013 |
|
| 1014 |
b = buff1 + 1; |
| 1015 |
|
| 1016 |
fmt_not_colon(fmt, colon); |
| 1017 |
ensure_param(fmt, 7); |
| 1018 |
w = set_param_positive(fmt, 0, "illegal width"); |
| 1019 |
d = set_param_positive(fmt, 1, "illegal number of digits"); |
| 1020 |
e = set_param_positive(fmt, 2, "illegal number of digits in exponent"); |
| 1021 |
k = ecl_to_fix(set_param(fmt, 3, INT, ecl_make_fixnum(1))); |
| 1022 |
overflowchar = ECL_CHAR_CODE(set_param(fmt, 4, CHAR, ECL_CODE_CHAR('\0'))); |
| 1023 |
padchar = ECL_CHAR_CODE(set_param(fmt, 5, CHAR, ECL_CODE_CHAR(' '))); |
| 1024 |
exponentchar = ECL_CHAR_CODE(set_param(fmt, 6, CHAR, ECL_CODE_CHAR('\0'))); |
| 1025 |
|
| 1026 |
x = fmt_advance(fmt); |
| 1027 |
if (ECL_FIXNUMP(x) || |
| 1028 |
ecl_t_of(x) == t_bignum || |
| 1029 |
ecl_t_of(x) == t_ratio) |
| 1030 |
x = ecl_make_single_float(ecl_to_float(x)); |
| 1031 |
if (!ECL_REAL_TYPE_P(ecl_t_of(x))) { |
| 1032 |
if (fmt->nparam > 1) fmt->nparam = 1; |
| 1033 |
fmt_back_up(fmt); |
| 1034 |
fmt_decimal(fmt, colon, atsign); |
| 1035 |
return; |
| 1036 |
} |
| 1037 |
if (ecl_t_of(x) == t_doublefloat) |
| 1038 |
n = 16; |
| 1039 |
else |
| 1040 |
n = 7; |
| 1041 |
f = ecl_to_double(x); |
| 1042 |
edit_double(n, f, &sign, buff, &exp); |
| 1043 |
if (d >= 0) { |
| 1044 |
if (k > 0) { |
| 1045 |
if (!(k < d + 2)) |
| 1046 |
fmt_error(fmt, "illegal scale factor"); |
| 1047 |
m = d + 1; |
| 1048 |
} else { |
| 1049 |
if (!(k > -d)) |
| 1050 |
fmt_error(fmt, "illegal scale factor"); |
| 1051 |
m = d + k; |
| 1052 |
} |
| 1053 |
} else if (w >= 0) { |
| 1054 |
if (k > 0) |
| 1055 |
m = w - 1; |
| 1056 |
else |
| 1057 |
m = w + k - 1; |
| 1058 |
if (sign < 0 || atsign) |
| 1059 |
--m; |
| 1060 |
if (e >= 0) |
| 1061 |
m -= e + 2; |
| 1062 |
else |
| 1063 |
m -= fmt_exponent_length(e - k + 1) + 2; |
| 1064 |
} else |
| 1065 |
m = n; |
| 1066 |
if (m <= 0) { |
| 1067 |
if (m == 0 && buff[0] >= '5') { |
| 1068 |
exp++; |
| 1069 |
n = m = 1; |
| 1070 |
buff[0] = '1'; |
| 1071 |
} else |
| 1072 |
n = m = 0; |
| 1073 |
} else if (m < n) { |
| 1074 |
n = m; |
| 1075 |
edit_double(n, f, &sign, buff, &exp); |
| 1076 |
} |
| 1077 |
while (n >= 0) |
| 1078 |
if (buff[n - 1] == '0') |
| 1079 |
--n; |
| 1080 |
else |
| 1081 |
break; |
| 1082 |
exp = exp - k + 1; |
| 1083 |
j = 0; |
| 1084 |
if (k > 0) { |
| 1085 |
for (i = 0; i < k; i++) |
| 1086 |
b[j++] = i < n ? buff[i] : '0'; |
| 1087 |
b[j++] = '.'; |
| 1088 |
if (d >= 0) |
| 1089 |
for (m = i + (d - k + 1); i < m; i++) |
| 1090 |
b[j++] = i < n ? buff[i] : '0'; |
| 1091 |
else |
| 1092 |
for (; i < n; i++) |
| 1093 |
b[j++] = buff[i]; |
| 1094 |
} else { |
| 1095 |
b[j++] = '.'; |
| 1096 |
if (d >= 0) { |
| 1097 |
for (i = 0; i < -k && i < d; i++) |
| 1098 |
b[j++] = '0'; |
| 1099 |
for (m = d - i, i = 0; i < m; i++) |
| 1100 |
b[j++] = i < n ? buff[i] : '0'; |
| 1101 |
} else if (n > 0) { |
| 1102 |
for (i = 0; i < -k; i++) |
| 1103 |
b[j++] = '0'; |
| 1104 |
for (i = 0; i < n; i++) |
| 1105 |
b[j++] = buff[i]; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
b[j] = '\0'; |
| 1109 |
if (w >= 0) { |
| 1110 |
if (sign < 0 || atsign) |
| 1111 |
--w; |
| 1112 |
i = fmt_exponent_length(exp); |
| 1113 |
if (e >= 0) { |
| 1114 |
if (i > e) { |
| 1115 |
if (overflowchar != '\0') |
| 1116 |
goto OVER; |
| 1117 |
else |
| 1118 |
e = i; |
| 1119 |
} |
| 1120 |
w -= e + 2; |
| 1121 |
} else |
| 1122 |
w -= i + 2; |
| 1123 |
if (j > w && overflowchar != '\0') |
| 1124 |
goto OVER; |
| 1125 |
if (j < w && b[0] == '.') { |
| 1126 |
*--b = '0'; |
| 1127 |
j++; |
| 1128 |
} |
| 1129 |
for (i = j; i < w; i++) |
| 1130 |
ecl_write_char(padchar, fmt->stream); |
| 1131 |
} else { |
| 1132 |
if (b[j-1] == '.') { |
| 1133 |
b[j++] = '0'; |
| 1134 |
b[j] = '\0'; |
| 1135 |
} |
| 1136 |
if (d < 0 && b[0] == '.') { |
| 1137 |
*--b = '0'; |
| 1138 |
j++; |
| 1139 |
} |
| 1140 |
} |
| 1141 |
if (sign < 0) |
| 1142 |
ecl_write_char('-', fmt->stream); |
| 1143 |
else if (atsign) |
| 1144 |
ecl_write_char('+', fmt->stream); |
| 1145 |
writestr_stream(b, fmt->stream); |
| 1146 |
y = ecl_symbol_value(@'*read-default-float-format*'); |
| 1147 |
if (exponentchar < 0) { |
| 1148 |
if (y == @'long-float') { |
| 1149 |
t = t_longfloat; |
| 1150 |
} else if (y == @'double-float') { |
| 1151 |
t = t_doublefloat; |
| 1152 |
} else if (y == @'single-float') { |
| 1153 |
t = t_singlefloat; |
| 1154 |
} else { |
| 1155 |
t = t_singlefloat; |
| 1156 |
} |
| 1157 |
if (ecl_t_of(x) == t) |
| 1158 |
exponentchar = 'E'; |
| 1159 |
else if (ecl_t_of(x) == t_singlefloat) |
| 1160 |
exponentchar = 'F'; |
| 1161 |
else if (ecl_t_of(x) == t_longfloat) |
| 1162 |
exponentchar = 'L'; |
| 1163 |
else |
| 1164 |
exponentchar = 'D'; |
| 1165 |
} |
| 1166 |
ecl_write_char(exponentchar, fmt->stream); |
| 1167 |
if (exp < 0) |
| 1168 |
ecl_write_char('-', fmt->stream); |
| 1169 |
else |
| 1170 |
ecl_write_char('+', fmt->stream); |
| 1171 |
if (e >= 0) |
| 1172 |
for (i = e - fmt_exponent_length(exp); i > 0; --i) |
| 1173 |
ecl_write_char('0', fmt->stream); |
| 1174 |
fmt_exponent(fmt, exp); |
| 1175 |
return; |
| 1176 |
|
| 1177 |
OVER: |
| 1178 |
w = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1179 |
for (i = 0; i < w; i++) |
| 1180 |
ecl_write_char(overflowchar, fmt->stream); |
| 1181 |
return; |
| 1182 |
} |
| 1183 |
|
| 1184 |
static void |
| 1185 |
fmt_general_float(format_stack fmt, bool colon, bool atsign) |
| 1186 |
{ |
| 1187 |
int w, d, e, k; |
| 1188 |
ecl_character overflowchar, padchar, exponentchar; |
| 1189 |
int sign, exp; |
| 1190 |
char buff[256]; |
| 1191 |
cl_object x; |
| 1192 |
int n, ee, ww, q, dd; |
| 1193 |
|
| 1194 |
fmt_not_colon(fmt, colon); |
| 1195 |
ensure_param(fmt, 7); |
| 1196 |
w = set_param_positive(fmt, 0, "illegal width"); |
| 1197 |
d = set_param_positive(fmt, 1, "illegal number of digits"); |
| 1198 |
e = set_param_positive(fmt, 2, "illegal number of digits in exponent"); |
| 1199 |
k = ecl_to_fix(set_param(fmt, 3, INT, ecl_make_fixnum(1))); |
| 1200 |
overflowchar = ECL_CHAR_CODE(set_param(fmt, 4, CHAR, ECL_CODE_CHAR('\0'))); |
| 1201 |
padchar = ECL_CHAR_CODE(set_param(fmt, 5, CHAR, ECL_CODE_CHAR(' '))); |
| 1202 |
exponentchar = ECL_CHAR_CODE(set_param(fmt, 6, CHAR, ECL_CODE_CHAR('\0'))); |
| 1203 |
|
| 1204 |
x = fmt_advance(fmt); |
| 1205 |
if (!ECL_REAL_TYPE_P(ecl_t_of(x))) { |
| 1206 |
if (fmt->nparam > 1) fmt->nparam = 1; |
| 1207 |
fmt_back_up(fmt); |
| 1208 |
fmt_decimal(fmt, colon, atsign); |
| 1209 |
return; |
| 1210 |
} |
| 1211 |
if (ecl_t_of(x) == t_doublefloat) |
| 1212 |
q = 16; |
| 1213 |
else |
| 1214 |
q = 7; |
| 1215 |
edit_double(q, ecl_to_double(x), &sign, buff, &exp); |
| 1216 |
n = exp + 1; |
| 1217 |
while (q >= 0) |
| 1218 |
if (buff[q - 1] == '0') |
| 1219 |
--q; |
| 1220 |
else |
| 1221 |
break; |
| 1222 |
if (e >= 0) |
| 1223 |
ee = e + 2; |
| 1224 |
else |
| 1225 |
ee = 4; |
| 1226 |
ww = w - ee; |
| 1227 |
if (d < 0) { |
| 1228 |
d = n < 7 ? n : 7; |
| 1229 |
d = q > d ? q : d; |
| 1230 |
} |
| 1231 |
dd = d - n; |
| 1232 |
if (0 <= dd && dd <= d) { |
| 1233 |
fmt->nparam = 5; |
| 1234 |
fmt->param[0] = ecl_make_fixnum(ww); |
| 1235 |
fmt->param[1] = ecl_make_fixnum(dd); |
| 1236 |
fmt->param[2] = ECL_NIL; |
| 1237 |
fmt->param[3] = fmt->param[4]; |
| 1238 |
fmt->param[4] = fmt->param[5]; |
| 1239 |
fmt_back_up(fmt); |
| 1240 |
fmt_fix_float(fmt, colon, atsign); |
| 1241 |
if (w >= 0) |
| 1242 |
while (ww++ < w) |
| 1243 |
ecl_write_char(padchar, fmt->stream); |
| 1244 |
return; |
| 1245 |
} |
| 1246 |
fmt->param[1] = ecl_make_fixnum(d); |
| 1247 |
fmt_back_up(fmt); |
| 1248 |
fmt_exponential_float(fmt, colon, atsign); |
| 1249 |
} |
| 1250 |
|
| 1251 |
static void |
| 1252 |
fmt_dollars_float(format_stack fmt, bool colon, bool atsign) |
| 1253 |
{ |
| 1254 |
int d, n, w; |
| 1255 |
ecl_character padchar; |
| 1256 |
double f; |
| 1257 |
int sign; |
| 1258 |
char buff[256]; |
| 1259 |
int exp; |
| 1260 |
int q, i; |
| 1261 |
cl_object x; |
| 1262 |
|
| 1263 |
ensure_param(fmt, 4); |
| 1264 |
d = set_param_positive(fmt, 0, "illegal number of digits"); |
| 1265 |
if (d < 0) d = 2; |
| 1266 |
n = set_param_positive(fmt, 1, "illegal number of digits"); |
| 1267 |
if (n < 0) n = 1; |
| 1268 |
w = set_param_positive(fmt, 2, "illegal width"); |
| 1269 |
if (w < 0) w = 0; |
| 1270 |
padchar = ECL_CHAR_CODE(set_param(fmt, 3, CHAR, ECL_CODE_CHAR(' '))); |
| 1271 |
x = fmt_advance(fmt); |
| 1272 |
if (!ECL_REAL_TYPE_P(ecl_t_of(x))) { |
| 1273 |
if (fmt->nparam < 3) |
| 1274 |
fmt->nparam = 0; |
| 1275 |
else { |
| 1276 |
fmt->nparam = 1; |
| 1277 |
fmt->param[0] = fmt->param[2]; |
| 1278 |
} |
| 1279 |
fmt_back_up(fmt); |
| 1280 |
fmt_decimal(fmt, colon, atsign); |
| 1281 |
return; |
| 1282 |
} |
| 1283 |
q = 7; |
| 1284 |
if (ecl_t_of(x) == t_doublefloat) |
| 1285 |
q = 16; |
| 1286 |
f = ecl_to_double(x); |
| 1287 |
edit_double(q, f, &sign, buff, &exp); |
| 1288 |
if ((q = exp + d + 1) > 0) |
| 1289 |
edit_double(q, f, &sign, buff, &exp); |
| 1290 |
exp++; |
| 1291 |
if (w > 100 || exp > 100 || exp < -100) { |
| 1292 |
fmt->nparam = 6; |
| 1293 |
fmt->param[0] = fmt->param[2]; |
| 1294 |
fmt->param[1] = ecl_make_fixnum(d + n - 1); |
| 1295 |
fmt->param[5] = fmt->param[3]; |
| 1296 |
fmt->param[2] = |
| 1297 |
fmt->param[3] = |
| 1298 |
fmt->param[4] = ECL_NIL; |
| 1299 |
fmt_back_up(fmt); |
| 1300 |
fmt_exponential_float(fmt, colon, atsign); |
| 1301 |
} |
| 1302 |
if (exp > n) |
| 1303 |
n = exp; |
| 1304 |
if (sign < 0 || atsign) |
| 1305 |
--w; |
| 1306 |
if (colon) { |
| 1307 |
if (sign < 0) |
| 1308 |
ecl_write_char('-', fmt->stream); |
| 1309 |
else if (atsign) |
| 1310 |
ecl_write_char('+', fmt->stream); |
| 1311 |
while (--w > n + d) |
| 1312 |
ecl_write_char(padchar, fmt->stream); |
| 1313 |
} else { |
| 1314 |
while (--w > n + d) |
| 1315 |
ecl_write_char(padchar, fmt->stream); |
| 1316 |
if (sign < 0) |
| 1317 |
ecl_write_char('-', fmt->stream); |
| 1318 |
else if (atsign) |
| 1319 |
ecl_write_char('+', fmt->stream); |
| 1320 |
} |
| 1321 |
for (i = n - exp; i > 0; --i) |
| 1322 |
ecl_write_char('0', fmt->stream); |
| 1323 |
for (i = 0; i < exp; i++) |
| 1324 |
ecl_write_char((i < q ? buff[i] : '0'), fmt->stream); |
| 1325 |
ecl_write_char('.', fmt->stream); |
| 1326 |
for (d += i; i < d; i++) |
| 1327 |
ecl_write_char((i < q ? buff[i] : '0'), fmt->stream); |
| 1328 |
} |
| 1329 |
|
| 1330 |
static void |
| 1331 |
fmt_percent(format_stack fmt, bool colon, bool atsign) |
| 1332 |
{ |
| 1333 |
int n, i; |
| 1334 |
|
| 1335 |
ensure_param(fmt, 1); |
| 1336 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1))); |
| 1337 |
fmt_not_colon(fmt, colon); |
| 1338 |
fmt_not_atsign(fmt, atsign); |
| 1339 |
while (n-- > 0) { |
| 1340 |
ecl_write_char('\n', fmt->stream); |
| 1341 |
if (n == 0) |
| 1342 |
for (i = fmt->indents; i > 0; --i) |
| 1343 |
ecl_write_char(' ', fmt->stream); |
| 1344 |
} |
| 1345 |
} |
| 1346 |
|
| 1347 |
static void |
| 1348 |
fmt_ampersand(format_stack fmt, bool colon, bool atsign) |
| 1349 |
{ |
| 1350 |
int n; |
| 1351 |
|
| 1352 |
ensure_param(fmt, 1); |
| 1353 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1))); |
| 1354 |
fmt_not_colon(fmt, colon); |
| 1355 |
fmt_not_atsign(fmt, atsign); |
| 1356 |
if (n == 0) |
| 1357 |
return; |
| 1358 |
if (ecl_file_column(fmt->stream) != 0) |
| 1359 |
ecl_write_char('\n', fmt->stream); |
| 1360 |
while (--n > 0) |
| 1361 |
ecl_write_char('\n', fmt->stream); |
| 1362 |
fmt->indents = 0; |
| 1363 |
} |
| 1364 |
|
| 1365 |
static void |
| 1366 |
fmt_bar(format_stack fmt, bool colon, bool atsign) |
| 1367 |
{ |
| 1368 |
int n; |
| 1369 |
|
| 1370 |
ensure_param(fmt, 1); |
| 1371 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1))); |
| 1372 |
fmt_not_colon(fmt, colon); |
| 1373 |
fmt_not_atsign(fmt, atsign); |
| 1374 |
while (n-- > 0) |
| 1375 |
ecl_write_char('\f', fmt->stream); |
| 1376 |
} |
| 1377 |
|
| 1378 |
static void |
| 1379 |
fmt_tilde(format_stack fmt, bool colon, bool atsign) |
| 1380 |
{ |
| 1381 |
int n; |
| 1382 |
|
| 1383 |
ensure_param(fmt, 1); |
| 1384 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1))); |
| 1385 |
fmt_not_colon(fmt, colon); |
| 1386 |
fmt_not_atsign(fmt, atsign); |
| 1387 |
while (n-- > 0) |
| 1388 |
ecl_write_char('~', fmt->stream); |
| 1389 |
} |
| 1390 |
|
| 1391 |
static void |
| 1392 |
fmt_newline(format_stack fmt, bool colon, bool atsign) |
| 1393 |
{ |
| 1394 |
ensure_param(fmt, 0); |
| 1395 |
fmt_not_colon_atsign(fmt, colon, atsign); |
| 1396 |
if (atsign) |
| 1397 |
ecl_write_char('\n', fmt->stream); |
| 1398 |
while (fmt->ctl_index < fmt->ctl_end && isspace(ecl_char(fmt->ctl_str, fmt->ctl_index))) { |
| 1399 |
if (colon) |
| 1400 |
ecl_write_char(ecl_char(fmt->ctl_str, fmt->ctl_index), fmt->stream); |
| 1401 |
fmt->ctl_index++; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
static void |
| 1406 |
fmt_tabulate(format_stack fmt, bool colon, bool atsign) |
| 1407 |
{ |
| 1408 |
int colnum, colinc; |
| 1409 |
int c, i; |
| 1410 |
|
| 1411 |
ensure_param(fmt, 2); |
| 1412 |
fmt_not_colon(fmt, colon); |
| 1413 |
colnum = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1))); |
| 1414 |
colinc = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(1))); |
| 1415 |
if (!atsign) { |
| 1416 |
c = ecl_file_column(fmt->stream); |
| 1417 |
if (c < 0) { |
| 1418 |
writestr_stream(" ", fmt->stream); |
| 1419 |
return; |
| 1420 |
} |
| 1421 |
if (c > colnum && colinc <= 0) |
| 1422 |
return; |
| 1423 |
while (c > colnum) |
| 1424 |
colnum += colinc; |
| 1425 |
for (i = colnum - c; i > 0; --i) |
| 1426 |
ecl_write_char(' ', fmt->stream); |
| 1427 |
} else { |
| 1428 |
for (i = colnum; i > 0; --i) |
| 1429 |
ecl_write_char(' ', fmt->stream); |
| 1430 |
c = ecl_file_column(fmt->stream); |
| 1431 |
if (c < 0 || colinc <= 0) |
| 1432 |
return; |
| 1433 |
colnum = 0; |
| 1434 |
while (c > colnum) |
| 1435 |
colnum += colinc; |
| 1436 |
for (i = colnum - c; i > 0; --i) |
| 1437 |
ecl_write_char(' ', fmt->stream); |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
static void |
| 1442 |
fmt_asterisk(format_stack fmt, bool colon, bool atsign) |
| 1443 |
{ |
| 1444 |
int n; |
| 1445 |
|
| 1446 |
ensure_param(fmt, 1); |
| 1447 |
fmt_not_colon_atsign(fmt, colon, atsign); |
| 1448 |
if (atsign) { |
| 1449 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1450 |
fmt_go(fmt, n); |
| 1451 |
} else if (colon) { |
| 1452 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1))); |
| 1453 |
fmt_go(fmt, fmt_index(fmt) - n); |
| 1454 |
} else { |
| 1455 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1))); |
| 1456 |
while (n-- > 0) |
| 1457 |
fmt_advance(fmt); |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
static void |
| 1462 |
fmt_indirection(format_stack fmt, bool colon, bool atsign) |
| 1463 |
{ |
| 1464 |
cl_object s, l; |
| 1465 |
struct format_stack_struct fmt_old; |
| 1466 |
jmp_buf fmt_jmp_buf0; |
| 1467 |
int up_colon; |
| 1468 |
|
| 1469 |
ensure_param(fmt, 0); |
| 1470 |
fmt_not_colon(fmt, colon); |
| 1471 |
s = fmt_advance(fmt); |
| 1472 |
switch (ecl_t_of(s)) { |
| 1473 |
#ifdef ECL_UNICODE |
| 1474 |
case t_string: |
| 1475 |
#endif |
| 1476 |
case t_base_string: |
| 1477 |
break; |
| 1478 |
default: |
| 1479 |
fmt_error(fmt, "control string expected"); |
| 1480 |
} |
| 1481 |
if (atsign) { |
| 1482 |
fmt_copy(&fmt_old, fmt); |
| 1483 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1484 |
fmt->ctl_str = s; |
| 1485 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) { |
| 1486 |
if (--up_colon) |
| 1487 |
fmt_error(fmt, "illegal ~~:^"); |
| 1488 |
} else |
| 1489 |
format(fmt, 0, s->base_string.fillp); |
| 1490 |
fmt_copy1(fmt, &fmt_old); |
| 1491 |
} else { |
| 1492 |
l = fmt_advance(fmt); |
| 1493 |
fmt_copy(&fmt_old, fmt); |
| 1494 |
fmt_set_arg_list(fmt, l); |
| 1495 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1496 |
fmt->ctl_str = s; |
| 1497 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) { |
| 1498 |
if (--up_colon) |
| 1499 |
fmt_error(fmt, "illegal ~~:^"); |
| 1500 |
} else |
| 1501 |
format(fmt, 0, s->base_string.fillp); |
| 1502 |
fmt_copy(fmt, &fmt_old); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
static void |
| 1507 |
fmt_case(format_stack fmt, bool colon, bool atsign) |
| 1508 |
{ |
| 1509 |
cl_object x; |
| 1510 |
cl_index i; |
| 1511 |
int j; |
| 1512 |
ecl_character c; |
| 1513 |
struct format_stack_struct fmt_old; |
| 1514 |
jmp_buf fmt_jmp_buf0; |
| 1515 |
int up_colon; |
| 1516 |
bool b; |
| 1517 |
|
| 1518 |
x = ecl_make_string_output_stream(64, 1); |
| 1519 |
i = fmt->ctl_index; |
| 1520 |
j = fmt_skip(fmt); |
| 1521 |
if (ecl_char(fmt->ctl_str, --j) != ')' || ecl_char(fmt->ctl_str, --j) != '~') |
| 1522 |
fmt_error(fmt, "~~) expected"); |
| 1523 |
fmt_copy(&fmt_old, fmt); |
| 1524 |
fmt->stream = x; |
| 1525 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1526 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) |
| 1527 |
; |
| 1528 |
else |
| 1529 |
format(fmt, i, j); |
| 1530 |
fmt_copy1(fmt, &fmt_old); |
| 1531 |
x = STRING_OUTPUT_STRING(x); |
| 1532 |
if (!colon && !atsign) |
| 1533 |
for (i = 0; i < x->base_string.fillp; i++) { |
| 1534 |
if (ecl_upper_case_p(c = ecl_char(x, i))) |
| 1535 |
c = ecl_char_downcase(c); |
| 1536 |
ecl_write_char(c, fmt->stream); |
| 1537 |
} |
| 1538 |
else if (colon && !atsign) |
| 1539 |
for (b = TRUE, i = 0; i < x->base_string.fillp; i++) { |
| 1540 |
if (ecl_lower_case_p(c = ecl_char(x, i))) { |
| 1541 |
if (b) |
| 1542 |
c = ecl_char_upcase(c); |
| 1543 |
b = FALSE; |
| 1544 |
} else if (ecl_upper_case_p(c)) { |
| 1545 |
if (!b) |
| 1546 |
c = ecl_char_downcase(c); |
| 1547 |
b = FALSE; |
| 1548 |
} else if (ecl_digitp(c,10) == -1) |
| 1549 |
b = TRUE; |
| 1550 |
ecl_write_char(c, fmt->stream); |
| 1551 |
} |
| 1552 |
else if (!colon && atsign) |
| 1553 |
for (b = TRUE, i = 0; i < x->base_string.fillp; i++) { |
| 1554 |
if (ecl_lower_case_p(c = ecl_char(x, i))) { |
| 1555 |
if (b) |
| 1556 |
c = ecl_char_upcase(c); |
| 1557 |
b = FALSE; |
| 1558 |
} else if (ecl_upper_case_p(c)) { |
| 1559 |
if (!b) |
| 1560 |
c = ecl_char_downcase(c); |
| 1561 |
b = FALSE; |
| 1562 |
} |
| 1563 |
ecl_write_char(c, fmt->stream); |
| 1564 |
} |
| 1565 |
else |
| 1566 |
for (i = 0; i < x->base_string.fillp; i++) { |
| 1567 |
if (ecl_lower_case_p(c = ecl_char(x, i))) |
| 1568 |
c = ecl_char_upcase(c); |
| 1569 |
ecl_write_char(c, fmt->stream); |
| 1570 |
} |
| 1571 |
if (up_colon) |
| 1572 |
ecl_longjmp(*fmt->jmp_buf, up_colon); |
| 1573 |
} |
| 1574 |
|
| 1575 |
static void |
| 1576 |
fmt_conditional(format_stack fmt, bool colon, bool atsign) |
| 1577 |
{ |
| 1578 |
int i, j, k; |
| 1579 |
cl_object x; |
| 1580 |
int n; |
| 1581 |
bool done; |
| 1582 |
struct format_stack_struct fmt_old; |
| 1583 |
|
| 1584 |
fmt_not_colon_atsign(fmt, colon, atsign); |
| 1585 |
if (colon) { |
| 1586 |
ensure_param(fmt, 0); |
| 1587 |
i = fmt->ctl_index; |
| 1588 |
j = fmt_skip(fmt); |
| 1589 |
if (ecl_char(fmt->ctl_str, --j) != ';' || ecl_char(fmt->ctl_str, --j) != '~') |
| 1590 |
fmt_error(fmt, "~~; expected"); |
| 1591 |
k = fmt_skip(fmt); |
| 1592 |
if (ecl_char(fmt->ctl_str, --k) != ']' || ecl_char(fmt->ctl_str, --k) != '~') |
| 1593 |
fmt_error(fmt, "~~] expected"); |
| 1594 |
if (Null(fmt_advance(fmt))) { |
| 1595 |
fmt_copy(&fmt_old, fmt); |
| 1596 |
format(fmt, i, j); |
| 1597 |
fmt_copy1(fmt, &fmt_old); |
| 1598 |
} else { |
| 1599 |
fmt_copy(&fmt_old, fmt); |
| 1600 |
format(fmt, j + 2, k); |
| 1601 |
fmt_copy1(fmt, &fmt_old); |
| 1602 |
} |
| 1603 |
} else if (atsign) { |
| 1604 |
i = fmt->ctl_index; |
| 1605 |
j = fmt_skip(fmt); |
| 1606 |
if (ecl_char(fmt->ctl_str, --j) != ']' || ecl_char(fmt->ctl_str, --j) != '~') |
| 1607 |
fmt_error(fmt, "~~] expected"); |
| 1608 |
if (Null(fmt_advance(fmt))) |
| 1609 |
; |
| 1610 |
else { |
| 1611 |
fmt_back_up(fmt); |
| 1612 |
fmt_copy(&fmt_old, fmt); |
| 1613 |
format(fmt, i, j); |
| 1614 |
fmt_copy1(fmt, &fmt_old); |
| 1615 |
} |
| 1616 |
} else { |
| 1617 |
ensure_param(fmt, 1); |
| 1618 |
if (fmt->nparam == 0) { |
| 1619 |
x = fmt_advance(fmt); |
| 1620 |
if (!ECL_FIXNUMP(x)) |
| 1621 |
fmt_error(fmt, "illegal argument for conditional"); |
| 1622 |
n = ecl_fixnum(x); |
| 1623 |
} else |
| 1624 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1625 |
i = fmt->ctl_index; |
| 1626 |
for (done = FALSE;; --n) { |
| 1627 |
j = fmt_skip(fmt); |
| 1628 |
for (k = j; ecl_char(fmt->ctl_str, --k) != '~';) |
| 1629 |
; |
| 1630 |
if (n == 0) { |
| 1631 |
fmt_copy(&fmt_old, fmt); |
| 1632 |
format(fmt, i, k); |
| 1633 |
fmt_copy1(fmt, &fmt_old); |
| 1634 |
done = TRUE; |
| 1635 |
} |
| 1636 |
i = j; |
| 1637 |
if (ecl_char(fmt->ctl_str, --j) == ']') { |
| 1638 |
if (ecl_char(fmt->ctl_str, --j) != '~') |
| 1639 |
fmt_error(fmt, "~~] expected"); |
| 1640 |
return; |
| 1641 |
} |
| 1642 |
if (ecl_char(fmt->ctl_str, j) == ';') { |
| 1643 |
if (ecl_char(fmt->ctl_str, --j) == '~') |
| 1644 |
continue; |
| 1645 |
if (ecl_char(fmt->ctl_str, j) == ':') |
| 1646 |
goto ELSE; |
| 1647 |
} |
| 1648 |
fmt_error(fmt, "~~; or ~~] expected"); |
| 1649 |
} |
| 1650 |
ELSE: |
| 1651 |
if (ecl_char(fmt->ctl_str, --j) != '~') |
| 1652 |
fmt_error(fmt, "~~:; expected"); |
| 1653 |
j = fmt_skip(fmt); |
| 1654 |
if (ecl_char(fmt->ctl_str, --j) != ']' || ecl_char(fmt->ctl_str, --j) != '~') |
| 1655 |
fmt_error(fmt, "~~] expected"); |
| 1656 |
if (!done) { |
| 1657 |
fmt_copy(&fmt_old, fmt); |
| 1658 |
format(fmt, i, j); |
| 1659 |
fmt_copy1(fmt, &fmt_old); |
| 1660 |
} |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
static void |
| 1665 |
fmt_iteration(format_stack fmt, bool colon, bool atsign) |
| 1666 |
{ |
| 1667 |
int n, i; |
| 1668 |
volatile int j; |
| 1669 |
bool colon_close = FALSE; |
| 1670 |
cl_object l; |
| 1671 |
struct format_stack_struct fmt_old; |
| 1672 |
jmp_buf fmt_jmp_buf0; |
| 1673 |
int up_colon; |
| 1674 |
|
| 1675 |
ensure_param(fmt, 1); |
| 1676 |
n = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(1000000))); |
| 1677 |
i = fmt->ctl_index; |
| 1678 |
j = fmt_skip(fmt); |
| 1679 |
if (ecl_char(fmt->ctl_str, --j) != '}') |
| 1680 |
fmt_error(fmt, "~~} expected"); |
| 1681 |
if (ecl_char(fmt->ctl_str, --j) == ':') { |
| 1682 |
colon_close = TRUE; |
| 1683 |
--j; |
| 1684 |
} |
| 1685 |
if (ecl_char(fmt->ctl_str, j) != '~') |
| 1686 |
fmt_error(fmt, "syntax error"); |
| 1687 |
if (!colon && !atsign) { |
| 1688 |
l = fmt_advance(fmt); |
| 1689 |
fmt_copy(&fmt_old, fmt); |
| 1690 |
fmt_set_arg_list(fmt, l); |
| 1691 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1692 |
if (colon_close) |
| 1693 |
goto L1; |
| 1694 |
while (fmt_more_args_p(fmt)) { |
| 1695 |
L1: |
| 1696 |
if (n-- <= 0) |
| 1697 |
break; |
| 1698 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) { |
| 1699 |
if (--up_colon) |
| 1700 |
fmt_error(fmt, "illegal ~~:^"); |
| 1701 |
break; |
| 1702 |
} |
| 1703 |
format(fmt, i, j); |
| 1704 |
} |
| 1705 |
fmt_copy(fmt, &fmt_old); |
| 1706 |
} else if (colon && !atsign) { |
| 1707 |
int fl = 0; |
| 1708 |
volatile cl_object l0; |
| 1709 |
l0 = fmt_advance(fmt); |
| 1710 |
fmt_copy(&fmt_old, fmt); |
| 1711 |
for (l = l0; !ecl_endp(l); l = CDR(l)) |
| 1712 |
fl += ecl_length(CAR(l)); |
| 1713 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1714 |
if (colon_close) |
| 1715 |
goto L2; |
| 1716 |
while (!ecl_endp(l0)) { |
| 1717 |
L2: |
| 1718 |
if (n-- <= 0) |
| 1719 |
break; |
| 1720 |
l = CAR(l0); |
| 1721 |
l0 = CDR(l0); |
| 1722 |
fmt_set_arg_list(fmt, l); |
| 1723 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) { |
| 1724 |
if (--up_colon) |
| 1725 |
break; |
| 1726 |
else |
| 1727 |
continue; |
| 1728 |
} |
| 1729 |
format(fmt, i, j); |
| 1730 |
} |
| 1731 |
fmt_copy(fmt, &fmt_old); |
| 1732 |
} else if (!colon && atsign) { |
| 1733 |
fmt_copy(&fmt_old, fmt); |
| 1734 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1735 |
if (colon_close) |
| 1736 |
goto L3; |
| 1737 |
while (fmt_more_args_p(fmt)) { |
| 1738 |
L3: |
| 1739 |
if (n-- <= 0) |
| 1740 |
break; |
| 1741 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) { |
| 1742 |
if (--up_colon) |
| 1743 |
fmt_error(fmt, "illegal ~~:^"); |
| 1744 |
break; |
| 1745 |
} |
| 1746 |
format(fmt, i, j); |
| 1747 |
} |
| 1748 |
fmt_copy1(fmt, &fmt_old); |
| 1749 |
} else if (colon && atsign) { |
| 1750 |
if (colon_close) |
| 1751 |
goto L4; |
| 1752 |
while (fmt_more_args_p(fmt)) { |
| 1753 |
L4: |
| 1754 |
if (n-- <= 0) |
| 1755 |
break; |
| 1756 |
l = fmt_advance(fmt); |
| 1757 |
fmt_copy(&fmt_old, fmt); |
| 1758 |
fmt_set_arg_list(fmt, l); |
| 1759 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1760 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) { |
| 1761 |
fmt_copy(fmt, &fmt_old); |
| 1762 |
if (--up_colon) |
| 1763 |
break; |
| 1764 |
else |
| 1765 |
continue; |
| 1766 |
} |
| 1767 |
format(fmt, i, j); |
| 1768 |
fmt_copy(fmt, &fmt_old); |
| 1769 |
} |
| 1770 |
} |
| 1771 |
} |
| 1772 |
|
| 1773 |
static void |
| 1774 |
fmt_justification(format_stack fmt, volatile bool colon, bool atsign) |
| 1775 |
{ |
| 1776 |
int mincol, colinc; |
| 1777 |
ecl_character minpad, padchar; |
| 1778 |
volatile cl_object fields; |
| 1779 |
cl_object p; |
| 1780 |
struct format_stack_struct fmt_old; |
| 1781 |
jmp_buf fmt_jmp_buf0; |
| 1782 |
volatile int i, j, k, l, m, j0, l0; |
| 1783 |
int up_colon; |
| 1784 |
volatile cl_object special = ECL_NIL; |
| 1785 |
volatile int spare_spaces, line_length; |
| 1786 |
|
| 1787 |
ensure_param(fmt, 4); |
| 1788 |
mincol = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1789 |
colinc = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(1))); |
| 1790 |
minpad = ecl_to_fix(set_param(fmt, 2, INT, ecl_make_fixnum(0))); |
| 1791 |
padchar = ECL_CHAR_CODE(set_param(fmt, 3, CHAR, ECL_CODE_CHAR(' '))); |
| 1792 |
|
| 1793 |
fields = ECL_NIL; |
| 1794 |
for (;;) { |
| 1795 |
cl_object this_field = ecl_make_string_output_stream(64, 1); |
| 1796 |
i = fmt->ctl_index; |
| 1797 |
j0 = j = fmt_skip(fmt); |
| 1798 |
while (ecl_char(fmt->ctl_str, --j) != '~') |
| 1799 |
; |
| 1800 |
|
| 1801 |
fmt_copy(&fmt_old, fmt); |
| 1802 |
fmt->jmp_buf = &fmt_jmp_buf0; |
| 1803 |
if ((up_colon = ecl_setjmp(*fmt->jmp_buf))) { |
| 1804 |
if (--up_colon) |
| 1805 |
fmt_error(fmt, "illegal ~~:^"); |
| 1806 |
fmt_copy1(fmt, &fmt_old); |
| 1807 |
while (ecl_char(fmt->ctl_str, --j0) != '>') |
| 1808 |
j0 = fmt_skip(fmt); |
| 1809 |
if (ecl_char(fmt->ctl_str, --j0) != '~') |
| 1810 |
fmt_error(fmt, "~~> expected"); |
| 1811 |
break; |
| 1812 |
} |
| 1813 |
fmt->stream = this_field; |
| 1814 |
format(fmt, i, j); |
| 1815 |
fields = CONS(STRING_OUTPUT_STRING(this_field), fields); |
| 1816 |
fmt_copy1(fmt, &fmt_old); |
| 1817 |
|
| 1818 |
if (ecl_char(fmt->ctl_str, --j0) == '>') { |
| 1819 |
if (ecl_char(fmt->ctl_str, --j0) != '~') |
| 1820 |
fmt_error(fmt, "~~> expected"); |
| 1821 |
break; |
| 1822 |
} else if (ecl_char(fmt->ctl_str, j0) != ';') |
| 1823 |
fmt_error(fmt, "~~; expected"); |
| 1824 |
else if (ecl_char(fmt->ctl_str, --j0) == ':') { |
| 1825 |
if (ecl_length(fields) != 1 || !Null(special)) |
| 1826 |
fmt_error(fmt, "illegal ~~:;"); |
| 1827 |
special = CAR(fields); |
| 1828 |
fields = CDR(fields); |
| 1829 |
for (j = j0; ecl_char(fmt->ctl_str, j) != '~'; --j) |
| 1830 |
; |
| 1831 |
fmt_copy(&fmt_old, fmt); |
| 1832 |
format(fmt, j, j0 + 2); |
| 1833 |
fmt_copy1(fmt, &fmt_old); |
| 1834 |
spare_spaces = fmt->spare_spaces; |
| 1835 |
line_length = fmt->line_length; |
| 1836 |
} else if (ecl_char(fmt->ctl_str, j0) != '~') |
| 1837 |
fmt_error(fmt, "~~; expected"); |
| 1838 |
} |
| 1839 |
/* |
| 1840 |
* Compute the length of items to be output. If the clause ~:; was |
| 1841 |
* found, the first item is not included. |
| 1842 |
*/ |
| 1843 |
fields = cl_nreverse(fields); |
| 1844 |
for (p = fields, l = 0; p != ECL_NIL; p = CDR(p)) |
| 1845 |
l += CAR(p)->base_string.fillp; |
| 1846 |
/* |
| 1847 |
* Count the number of segments that need padding, "M". If the colon |
| 1848 |
* modifier, the first item needs padding. If the @@ modifier is |
| 1849 |
* present, the last modifier also needs padding. |
| 1850 |
*/ |
| 1851 |
m = ecl_length(fields) - 1; |
| 1852 |
if (m <= 0 && !colon && !atsign) { |
| 1853 |
m = 0; |
| 1854 |
colon = TRUE; |
| 1855 |
} |
| 1856 |
if (colon) |
| 1857 |
m++; |
| 1858 |
if (atsign) |
| 1859 |
m++; |
| 1860 |
/* |
| 1861 |
* Count the minimal length in which the text fits. This length must |
| 1862 |
* the smallest integer of the form l = mincol + k * colinc. If the |
| 1863 |
* length exceeds the line length, the text before the ~:; is output |
| 1864 |
* first. |
| 1865 |
*/ |
| 1866 |
l0 = l; |
| 1867 |
l += minpad * m; |
| 1868 |
for (k = 0; mincol + k * colinc < l; k++) |
| 1869 |
; |
| 1870 |
l = mincol + k * colinc; |
| 1871 |
if (special != ECL_NIL && |
| 1872 |
ecl_file_column(fmt->stream) + l + spare_spaces > line_length) |
| 1873 |
ecl_princ(special, fmt->stream); |
| 1874 |
/* |
| 1875 |
* Output the text with the padding segments. The total number of |
| 1876 |
* padchars is kept in "l", and it is shared equally among all segments. |
| 1877 |
*/ |
| 1878 |
l -= l0; |
| 1879 |
for (p = fields; p != ECL_NIL; p = CDR(p)) { |
| 1880 |
if (p != fields || colon) |
| 1881 |
for (j = l / m, l -= j, --m; j > 0; --j) |
| 1882 |
ecl_write_char(padchar, fmt->stream); |
| 1883 |
ecl_princ(CAR(p), fmt->stream); |
| 1884 |
} |
| 1885 |
if (atsign) |
| 1886 |
for (j = l; j > 0; --j) |
| 1887 |
ecl_write_char(padchar, fmt->stream); |
| 1888 |
} |
| 1889 |
|
| 1890 |
static void |
| 1891 |
fmt_up_and_out(format_stack fmt, bool colon, bool atsign) |
| 1892 |
{ |
| 1893 |
int i, j, k; |
| 1894 |
|
| 1895 |
ensure_param(fmt, 3); |
| 1896 |
fmt_not_atsign(fmt, atsign); |
| 1897 |
if (fmt->nparam == 0) { |
| 1898 |
if (!fmt_more_args_p(fmt)) |
| 1899 |
ecl_longjmp(*fmt->jmp_buf, ++colon); |
| 1900 |
} else if (fmt->nparam == 1) { |
| 1901 |
i = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1902 |
if (i == 0) |
| 1903 |
ecl_longjmp(*fmt->jmp_buf, ++colon); |
| 1904 |
} else if (fmt->nparam == 2) { |
| 1905 |
i = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1906 |
j = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(0))); |
| 1907 |
if (i == j) |
| 1908 |
ecl_longjmp(*fmt->jmp_buf, ++colon); |
| 1909 |
} else { |
| 1910 |
i = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1911 |
j = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(0))); |
| 1912 |
k = ecl_to_fix(set_param(fmt, 2, INT, ecl_make_fixnum(0))); |
| 1913 |
if (i <= j && j <= k) |
| 1914 |
ecl_longjmp(*fmt->jmp_buf, ++colon); |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
static void |
| 1919 |
fmt_semicolon(format_stack fmt, bool colon, bool atsign) |
| 1920 |
{ |
| 1921 |
fmt_not_atsign(fmt, atsign); |
| 1922 |
if (!colon) |
| 1923 |
fmt_error(fmt, "~~:; expected"); |
| 1924 |
ensure_param(fmt, 2); |
| 1925 |
fmt->spare_spaces = ecl_to_fix(set_param(fmt, 0, INT, ecl_make_fixnum(0))); |
| 1926 |
fmt->line_length = ecl_to_fix(set_param(fmt, 1, INT, ecl_make_fixnum(72))); |
| 1927 |
} |
| 1928 |
|
| 1929 |
@(defun si::formatter-aux (strm string &rest args) |
| 1930 |
@ |
| 1931 |
@(return doformat(narg, strm, string, args, TRUE)) |
| 1932 |
@) |
| 1933 |
|
| 1934 |
|
| 1935 |
static cl_object |
| 1936 |
doformat(cl_narg narg, cl_object strm, cl_object string, ecl_va_list args, bool in_formatter) |
| 1937 |
{ |
| 1938 |
struct format_stack_struct fmt; |
| 1939 |
jmp_buf fmt_jmp_buf0; |
| 1940 |
int colon; |
| 1941 |
cl_object output = cl_grab_rest_args(args); |
| 1942 |
while(!ecl_stringp(string)) |
| 1943 |
#ifdef ECL_UNICODE |
| 1944 |
string = ecl_type_error(@'format', "argument", string, @'string'); |
| 1945 |
#else |
| 1946 |
string = ecl_type_error(@'format', "argument", string, @'base-string'); |
| 1947 |
#endif |
| 1948 |
fmt.stream = strm; |
| 1949 |
fmt_set_arg_list(&fmt, output); |
| 1950 |
fmt.jmp_buf = &fmt_jmp_buf0; |
| 1951 |
if (ecl_symbol_value(@'si::*indent-formatted-output*') != ECL_NIL) |
| 1952 |
fmt.indents = ecl_file_column(strm); |
| 1953 |
else |
| 1954 |
fmt.indents = 0; |
| 1955 |
fmt.ctl_str = string; |
| 1956 |
fmt.aux_stream = get_aux_stream(); |
| 1957 |
fmt.aux_string = STRING_OUTPUT_STRING(fmt.aux_stream); |
| 1958 |
if ((colon = ecl_setjmp(*fmt.jmp_buf))) { |
| 1959 |
if (--colon) |
| 1960 |
fmt_error(&fmt, "illegal ~~:^"); |
| 1961 |
} else { |
| 1962 |
format(&fmt, 0, string->base_string.fillp); |
| 1963 |
ecl_force_output(strm); |
| 1964 |
} |
| 1965 |
ecl_process_env()->fmt_aux_stream = fmt.aux_stream; |
| 1966 |
if (!in_formatter) |
| 1967 |
output = ECL_NIL; |
| 1968 |
return output; |
| 1969 |
} |
| 1970 |
|
| 1971 |
static void |
| 1972 |
format(format_stack fmt, cl_index start, cl_index end) |
| 1973 |
{ |
| 1974 |
ecl_character c; |
| 1975 |
cl_index i, n; |
| 1976 |
bool colon, atsign; |
| 1977 |
cl_object x; |
| 1978 |
|
| 1979 |
fmt->ctl_index = start; |
| 1980 |
fmt->ctl_end = end; |
| 1981 |
|
| 1982 |
LOOP: |
| 1983 |
if (fmt->ctl_index >= fmt->ctl_end) |
| 1984 |
return; |
| 1985 |
if ((c = ctl_advance(fmt)) != '~') { |
| 1986 |
ecl_write_char(c, fmt->stream); |
| 1987 |
goto LOOP; |
| 1988 |
} |
| 1989 |
n = 0; |
| 1990 |
for (;;) { |
| 1991 |
switch (c = ctl_advance(fmt)) { |
| 1992 |
case ',': |
| 1993 |
fmt->param[n] = ECL_NIL; |
| 1994 |
break; |
| 1995 |
|
| 1996 |
case '+': case '-': |
| 1997 |
case '0': case '1': case '2': case '3': case '4': |
| 1998 |
case '5': case '6': case '7': case '8': case '9': |
| 1999 |
i = fmt->ctl_index - 1; |
| 2000 |
do { |
| 2001 |
c = ctl_advance(fmt); |
| 2002 |
} while (ecl_digitp(c,10) != -1); |
| 2003 |
x = ecl_parse_integer(fmt->ctl_str, i, fmt->ctl_index, &i, 10); |
| 2004 |
INTEGER: |
| 2005 |
/* FIXME! A hack to solve the problem of bignums in arguments */ |
| 2006 |
if (x == OBJNULL || !ecl_numberp(x)) |
| 2007 |
fmt_error(fmt, "integer expected"); |
| 2008 |
if (ecl_number_compare(x, ecl_make_fixnum(FMT_VALUE_UPPER_LIMIT)) > 0) { |
| 2009 |
fmt->param[n] = ecl_make_fixnum(FMT_VALUE_UPPER_LIMIT); |
| 2010 |
} else if (ecl_number_compare(x, ecl_make_fixnum(FMT_VALUE_LOWER_LIMIT)) < 0) { |
| 2011 |
fmt->param[n] = ecl_make_fixnum(FMT_VALUE_LOWER_LIMIT); |
| 2012 |
} else { |
| 2013 |
fmt->param[n] = x; |
| 2014 |
} |
| 2015 |
if (ECL_FIXNUMP(x)) { |
| 2016 |
fmt->param[n] = x; |
| 2017 |
} else if (ecl_plusp(x)) { |
| 2018 |
fmt->param[n] = ecl_make_fixnum(MOST_POSITIVE_FIXNUM); |
| 2019 |
} else { |
| 2020 |
fmt->param[n] = ecl_make_fixnum(MOST_NEGATIVE_FIXNUM); |
| 2021 |
} |
| 2022 |
break; |
| 2023 |
|
| 2024 |
case '\'': |
| 2025 |
fmt->param[n] = ECL_CODE_CHAR(ctl_advance(fmt)); |
| 2026 |
c = ctl_advance(fmt); |
| 2027 |
break; |
| 2028 |
|
| 2029 |
case 'v': case 'V': |
| 2030 |
x = fmt_advance(fmt); |
| 2031 |
c = ctl_advance(fmt); |
| 2032 |
if (ecl_t_of(x) == t_character) { |
| 2033 |
fmt->param[n] = x; |
| 2034 |
} else { |
| 2035 |
goto INTEGER; |
| 2036 |
} |
| 2037 |
break; |
| 2038 |
|
| 2039 |
case '#': |
| 2040 |
fmt->param[n] = ecl_make_fixnum(fmt_args_left(fmt)); |
| 2041 |
c = ctl_advance(fmt); |
| 2042 |
break; |
| 2043 |
|
| 2044 |
default: |
| 2045 |
if (n > 0) |
| 2046 |
fmt_error(fmt, "illegal ,"); |
| 2047 |
else |
| 2048 |
goto DIRECTIVE; |
| 2049 |
} |
| 2050 |
n++; |
| 2051 |
if (n == FMT_MAX_PARAM) |
| 2052 |
fmt_error(fmt, "too many parameters"); |
| 2053 |
if (c != ',') |
| 2054 |
break; |
| 2055 |
} |
| 2056 |
|
| 2057 |
DIRECTIVE: |
| 2058 |
colon = atsign = FALSE; |
| 2059 |
if (c == ':') { |
| 2060 |
colon = TRUE; |
| 2061 |
c = ctl_advance(fmt); |
| 2062 |
} |
| 2063 |
if (c == '@@') { |
| 2064 |
atsign = TRUE; |
| 2065 |
c = ctl_advance(fmt); |
| 2066 |
} |
| 2067 |
fmt->nparam = n; |
| 2068 |
switch (c) { |
| 2069 |
case 'a': case 'A': |
| 2070 |
fmt_ascii(fmt, colon, atsign); |
| 2071 |
break; |
| 2072 |
|
| 2073 |
case 's': case 'S': |
| 2074 |
fmt_S_expression(fmt, colon, atsign); |
| 2075 |
break; |
| 2076 |
|
| 2077 |
case 'd': case 'D': |
| 2078 |
fmt_decimal(fmt, colon, atsign); |
| 2079 |
break; |
| 2080 |
|
| 2081 |
case 'b': case 'B': |
| 2082 |
fmt_binary(fmt, colon, atsign); |
| 2083 |
break; |
| 2084 |
|
| 2085 |
case 'o': case 'O': |
| 2086 |
fmt_octal(fmt, colon, atsign); |
| 2087 |
break; |
| 2088 |
|
| 2089 |
case 'x': case 'X': |
| 2090 |
fmt_hexadecimal(fmt, colon, atsign); |
| 2091 |
break; |
| 2092 |
|
| 2093 |
case 'r': case 'R': |
| 2094 |
fmt_radix(fmt, colon, atsign); |
| 2095 |
break; |
| 2096 |
|
| 2097 |
case 'p': case 'P': |
| 2098 |
fmt_plural(fmt, colon, atsign); |
| 2099 |
break; |
| 2100 |
|
| 2101 |
case 'c': case 'C': |
| 2102 |
fmt_character(fmt, colon, atsign); |
| 2103 |
break; |
| 2104 |
|
| 2105 |
case 'f': case 'F': |
| 2106 |
fmt_fix_float(fmt, colon, atsign); |
| 2107 |
break; |
| 2108 |
|
| 2109 |
case 'e': case 'E': |
| 2110 |
fmt_exponential_float(fmt, colon, atsign); |
| 2111 |
break; |
| 2112 |
|
| 2113 |
case 'g': case 'G': |
| 2114 |
fmt_general_float(fmt, colon, atsign); |
| 2115 |
break; |
| 2116 |
|
| 2117 |
case '$': |
| 2118 |
fmt_dollars_float(fmt, colon, atsign); |
| 2119 |
break; |
| 2120 |
|
| 2121 |
case '%': |
| 2122 |
fmt_percent(fmt, colon, atsign); |
| 2123 |
break; |
| 2124 |
|
| 2125 |
case '&': |
| 2126 |
fmt_ampersand(fmt, colon, atsign); |
| 2127 |
break; |
| 2128 |
|
| 2129 |
case '|': |
| 2130 |
fmt_bar(fmt, colon, atsign); |
| 2131 |
break; |
| 2132 |
|
| 2133 |
case '~': |
| 2134 |
fmt_tilde(fmt, colon, atsign); |
| 2135 |
break; |
| 2136 |
|
| 2137 |
case '\n': |
| 2138 |
case '\r': |
| 2139 |
fmt_newline(fmt, colon, atsign); |
| 2140 |
break; |
| 2141 |
|
| 2142 |
case 't': case 'T': |
| 2143 |
fmt_tabulate(fmt, colon, atsign); |
| 2144 |
break; |
| 2145 |
|
| 2146 |
case '*': |
| 2147 |
fmt_asterisk(fmt, colon, atsign); |
| 2148 |
break; |
| 2149 |
|
| 2150 |
case '?': |
| 2151 |
fmt_indirection(fmt, colon, atsign); |
| 2152 |
break; |
| 2153 |
|
| 2154 |
case '(': |
| 2155 |
fmt_case(fmt, colon, atsign); |
| 2156 |
break; |
| 2157 |
|
| 2158 |
case '[': |
| 2159 |
fmt_conditional(fmt, colon, atsign); |
| 2160 |
break; |
| 2161 |
|
| 2162 |
case '{': |
| 2163 |
fmt_iteration(fmt, colon, atsign); |
| 2164 |
break; |
| 2165 |
|
| 2166 |
case '<': |
| 2167 |
fmt_justification(fmt, colon, atsign); |
| 2168 |
break; |
| 2169 |
|
| 2170 |
case '^': |
| 2171 |
fmt_up_and_out(fmt, colon, atsign); |
| 2172 |
break; |
| 2173 |
|
| 2174 |
case ';': |
| 2175 |
fmt_semicolon(fmt, colon, atsign); |
| 2176 |
break; |
| 2177 |
|
| 2178 |
default: |
| 2179 |
fmt_error(fmt, "illegal directive"); |
| 2180 |
} |
| 2181 |
goto LOOP; |
| 2182 |
} |
| 2183 |
#endif /* !ECL_CMU_FORMAT */ |
| 2184 |
|
| 2185 |
@(defun format (strm string &rest args) |
| 2186 |
cl_object output = ECL_NIL; |
| 2187 |
int null_strm = 0; |
| 2188 |
@ |
| 2189 |
if (Null(strm)) { |
| 2190 |
#ifdef ECL_UNICODE |
| 2191 |
strm = ecl_alloc_adjustable_extended_string(64); |
| 2192 |
#else |
| 2193 |
strm = ecl_alloc_adjustable_base_string(64); |
| 2194 |
#endif |
| 2195 |
null_strm = 1; |
| 2196 |
} else if (strm == ECL_T) { |
| 2197 |
strm = ecl_cmp_symbol_value(the_env, @'*standard-output*'); |
| 2198 |
} |
| 2199 |
if (ecl_stringp(strm)) { |
| 2200 |
output = strm; |
| 2201 |
if (!ECL_ARRAY_HAS_FILL_POINTER_P(output)) { |
| 2202 |
cl_error(7, @'si::format-error', |
| 2203 |
@':format-control', |
| 2204 |
@"Cannot output to a non adjustable string.", |
| 2205 |
@':control-string', string, |
| 2206 |
@':offset', ecl_make_fixnum(0)); |
| 2207 |
} |
| 2208 |
strm = si_make_string_output_stream_from_string(strm); |
| 2209 |
if (null_strm == 0) |
| 2210 |
output = ECL_NIL; |
| 2211 |
} |
| 2212 |
if (!Null(cl_functionp(string))) { |
| 2213 |
cl_apply(3, string, strm, cl_grab_rest_args(args)); |
| 2214 |
} else { |
| 2215 |
#ifdef ECL_CMU_FORMAT |
| 2216 |
_ecl_funcall4(@'si::formatter-aux', strm, string, |
| 2217 |
cl_grab_rest_args(args)); |
| 2218 |
#else |
| 2219 |
doformat(narg, strm, string, args, FALSE); |
| 2220 |
#endif |
| 2221 |
} |
| 2222 |
output = cl_copy_seq(output); |
| 2223 |
@(return output); |
| 2224 |
@) |