1 /* Copyright (C) 2008 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 /* Exercise `scm_c_read ()' and the port type API. Verify assumptions that
20 can be made by port type implementations. */
31 /* Size of our port's internal buffer. */
32 #define PORT_BUFFER_SIZE 1024
34 /* Return a new port of type PORT_TYPE. */
36 make_port (scm_t_bits port_type
)
42 c_buffer
= scm_gc_calloc (PORT_BUFFER_SIZE
, "custom-port-buffer");
44 port
= scm_new_port_table_entry (port_type
);
46 /* Associate C_BUFFER with PORT, for test purposes. */
47 SCM_SETSTREAM (port
, (scm_t_bits
) c_buffer
);
49 /* Use C_BUFFER as PORT's internal buffer. */
50 c_port
= SCM_PTAB_ENTRY (port
);
51 c_port
->read_pos
= c_port
->read_buf
= (unsigned char *) c_buffer
;
52 c_port
->read_end
= (unsigned char *) c_buffer
+ PORT_BUFFER_SIZE
;
53 c_port
->read_buf_size
= PORT_BUFFER_SIZE
;
55 /* Mark PORT as open and readable. */
56 SCM_SET_CELL_TYPE (port
, port_type
| SCM_OPN
| SCM_RDNG
);
61 /* Read one byte from PORT. */
66 scm_t_port
*c_port
= SCM_PTAB_ENTRY (port
);
68 /* Make sure that C_PORT's internal buffer wasn't changed behind our back.
69 See http://lists.gnu.org/archive/html/guile-devel/2008-11/msg00042.html
70 for an example where this assumption matters. */
71 assert (c_port
->read_buf
== (unsigned char *) SCM_STREAM (port
));
72 assert (c_port
->read_buf_size
== PORT_BUFFER_SIZE
);
74 if (c_port
->read_pos
>= c_port
->read_end
)
77 result
= (int) *c_port
->read_pos
++;
82 /* Return true (non-zero) if BUF contains only zeros. */
84 zeroed_buffer_p (const char *buf
, size_t len
)
88 for (i
= 0; i
< len
; i
++)
100 scm_t_bits port_type
;
101 char buffer
[PORT_BUFFER_SIZE
+ (PORT_BUFFER_SIZE
/ 2)];
102 size_t read
, last_read
;
104 port_type
= scm_make_port_type ("custom-input-port", fill_input
, NULL
);
105 port
= make_port (port_type
);
110 last_read
= scm_c_read (port
, &buffer
[read
], 123);
111 assert (last_read
<= 123);
112 assert (zeroed_buffer_p (&buffer
[read
], last_read
));
116 while (last_read
> 0 && read
< sizeof (buffer
));
118 /* We shouldn't be able to read more than what's in PORT's buffer. */
119 assert (read
== PORT_BUFFER_SIZE
);
126 main (int argc
, char *argv
[])
128 scm_with_guile (do_start
, NULL
);