| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* write_array.d - file interface |
| 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 <ecl/ecl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
|
| 18 |
static void |
| 19 |
write_array_inner(bool vector, cl_object x, cl_object stream) |
| 20 |
{ |
| 21 |
cl_env_ptr env = ecl_process_env(); |
| 22 |
const cl_index *adims; |
| 23 |
cl_index subscripts[ECL_ARRAY_RANK_LIMIT]; |
| 24 |
cl_fixnum n, j, m, k, i; |
| 25 |
cl_fixnum print_length; |
| 26 |
cl_fixnum print_level; |
| 27 |
bool readably = ecl_print_readably(); |
| 28 |
|
| 29 |
if (vector) { |
| 30 |
adims = &x->vector.fillp; |
| 31 |
n = 1; |
| 32 |
} else { |
| 33 |
adims = x->array.dims; |
| 34 |
n = x->array.rank; |
| 35 |
} |
| 36 |
if (readably) { |
| 37 |
print_length = MOST_POSITIVE_FIXNUM; |
| 38 |
print_level = MOST_POSITIVE_FIXNUM; |
| 39 |
} else { |
| 40 |
if (!ecl_print_array()) { |
| 41 |
writestr_stream(vector? "#<vector " : "#<array ", stream); |
| 42 |
_ecl_write_addr((void *)x, stream); |
| 43 |
ecl_write_char('>', stream); |
| 44 |
return; |
| 45 |
} |
| 46 |
print_level = ecl_print_level(); |
| 47 |
print_length = ecl_print_length(); |
| 48 |
} |
| 49 |
ecl_write_char('#', stream); |
| 50 |
if (print_level == 0) |
| 51 |
return; |
| 52 |
if (readably) { |
| 53 |
ecl_write_char('A', stream); |
| 54 |
ecl_write_char('(', stream); |
| 55 |
si_write_object(ecl_elttype_to_symbol(ecl_array_elttype(x)), stream); |
| 56 |
ecl_write_char(' ', stream); |
| 57 |
if (n > 0) { |
| 58 |
ecl_write_char('(', stream); |
| 59 |
for (j=0; j<n; j++) { |
| 60 |
si_write_object(ecl_make_fixnum(adims[j]), stream); |
| 61 |
if (j < n-1) |
| 62 |
ecl_write_char(' ', stream); |
| 63 |
} |
| 64 |
ecl_write_char(')', stream); |
| 65 |
} else { |
| 66 |
si_write_object(ECL_NIL, stream); |
| 67 |
} |
| 68 |
ecl_write_char(' ', stream); |
| 69 |
} else if (!vector) { |
| 70 |
_ecl_write_fixnum(n, stream); |
| 71 |
ecl_write_char('A', stream); |
| 72 |
} |
| 73 |
if (print_level >= n) { |
| 74 |
/* We can write the elements of the array */ |
| 75 |
print_level -= n; |
| 76 |
ecl_bds_bind(env, @'*print-level*', ecl_make_fixnum(print_level)); |
| 77 |
} else { |
| 78 |
/* The elements of the array are not printed */ |
| 79 |
n = print_level; |
| 80 |
print_level = -1; |
| 81 |
} |
| 82 |
for (j = 0; j < n; j++) |
| 83 |
subscripts[j] = 0; |
| 84 |
for (m = 0, j = 0;;) { |
| 85 |
for (i = j; i < n; i++) { |
| 86 |
if (subscripts[i] == 0) { |
| 87 |
ecl_write_char('(', stream); |
| 88 |
if (adims[i] == 0) { |
| 89 |
ecl_write_char(')', stream); |
| 90 |
j = i-1; |
| 91 |
k = 0; |
| 92 |
goto INC; |
| 93 |
} |
| 94 |
} |
| 95 |
if (subscripts[i] > 0) |
| 96 |
ecl_write_char(' ', stream); |
| 97 |
if (subscripts[i] >= print_length) { |
| 98 |
writestr_stream("...)", stream); |
| 99 |
k=adims[i]-subscripts[i]; |
| 100 |
subscripts[i] = 0; |
| 101 |
for (j = i+1; j < n; j++) |
| 102 |
k *= adims[j]; |
| 103 |
j = i-1; |
| 104 |
goto INC; |
| 105 |
} |
| 106 |
} |
| 107 |
/* FIXME: This conses! */ |
| 108 |
if (print_level >= 0) |
| 109 |
si_write_object(ecl_aref_unsafe(x, m), stream); |
| 110 |
else |
| 111 |
ecl_write_char('#', stream); |
| 112 |
j = n-1; |
| 113 |
k = 1; |
| 114 |
|
| 115 |
INC: |
| 116 |
while (j >= 0) { |
| 117 |
if (++subscripts[j] < adims[j]) |
| 118 |
break; |
| 119 |
subscripts[j] = 0; |
| 120 |
ecl_write_char(')', stream); |
| 121 |
--j; |
| 122 |
} |
| 123 |
if (j < 0) |
| 124 |
break; |
| 125 |
m += k; |
| 126 |
} |
| 127 |
if (print_level >= 0) { |
| 128 |
ecl_bds_unwind1(env); |
| 129 |
} |
| 130 |
if (readably) { |
| 131 |
ecl_write_char(')', stream); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
_ecl_write_array(cl_object x, cl_object stream) |
| 137 |
{ |
| 138 |
write_array_inner(0, x, stream); |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
_ecl_write_vector(cl_object x, cl_object stream) |
| 143 |
{ |
| 144 |
write_array_inner(1, x, stream); |
| 145 |
} |
| 146 |
|
| 147 |
#ifdef ECL_UNICODE |
| 148 |
void |
| 149 |
_ecl_write_string(cl_object x, cl_object stream) |
| 150 |
{ |
| 151 |
if (!ecl_print_escape() && !ecl_print_readably()) { |
| 152 |
si_do_write_sequence(x, stream, ecl_make_fixnum(0), ECL_NIL); |
| 153 |
} else { |
| 154 |
cl_index ndx, ndx_start; |
| 155 |
ecl_write_char('"', stream); |
| 156 |
for (ndx = ndx_start = 0; ndx < x->string.fillp; ndx++) { |
| 157 |
ecl_character c = x->string.self[ndx]; |
| 158 |
if (c == '"' || c == '\\') { |
| 159 |
si_do_write_sequence(x, stream, ecl_make_fixnum(ndx_start), ecl_make_fixnum(ndx)); |
| 160 |
ecl_write_char('\\', stream); |
| 161 |
ndx_start = ndx; |
| 162 |
} |
| 163 |
} |
| 164 |
si_do_write_sequence(x, stream, ecl_make_fixnum(ndx_start), ECL_NIL); |
| 165 |
ecl_write_char('"', stream); |
| 166 |
} |
| 167 |
} |
| 168 |
#endif |
| 169 |
|
| 170 |
void |
| 171 |
_ecl_write_base_string(cl_object x, cl_object stream) |
| 172 |
{ |
| 173 |
if (!ecl_print_escape() && !ecl_print_readably()) { |
| 174 |
si_do_write_sequence(x, stream, ecl_make_fixnum(0), ECL_NIL); |
| 175 |
} else { |
| 176 |
cl_index ndx, ndx_start; |
| 177 |
ecl_write_char('"', stream); |
| 178 |
for (ndx = ndx_start = 0; ndx < x->base_string.fillp; ndx++) { |
| 179 |
ecl_character c = x->base_string.self[ndx]; |
| 180 |
if (c == '"' || c == '\\') { |
| 181 |
si_do_write_sequence(x, stream, ecl_make_fixnum(ndx_start), ecl_make_fixnum(ndx)); |
| 182 |
ecl_write_char('\\', stream); |
| 183 |
ndx_start = ndx; |
| 184 |
} |
| 185 |
} |
| 186 |
si_do_write_sequence(x, stream, ecl_make_fixnum(ndx_start), ECL_NIL); |
| 187 |
ecl_write_char('"', stream); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void |
| 192 |
_ecl_write_bitvector(cl_object x, cl_object stream) |
| 193 |
{ |
| 194 |
if (!ecl_print_array() && !ecl_print_readably()) { |
| 195 |
writestr_stream("#<bit-vector ", stream); |
| 196 |
_ecl_write_addr((void *)x, stream); |
| 197 |
ecl_write_char('>', stream); |
| 198 |
} else { |
| 199 |
cl_index ndx; |
| 200 |
writestr_stream("#*", stream); |
| 201 |
for (ndx = 0; ndx < x->vector.fillp; ndx++) |
| 202 |
if (x->vector.self.bit[(ndx+x->vector.offset)/8] & (0200 >> (ndx+x->vector.offset)%8)) |
| 203 |
ecl_write_char('1', stream); |
| 204 |
else |
| 205 |
ecl_write_char('0', stream); |
| 206 |
} |
| 207 |
} |