#include <config.h>
#include "lisp.h"
-#include <limits.h>
#include "buffer.h"
#include "insdel.h"
/* Functions are as follows:
-Lstream *Lstream_new (Lstream_implementation *imp, CONST char *mode)
+Lstream *Lstream_new (Lstream_implementation *imp, const char *mode)
Allocate and return a new Lstream. This function is not
really meant to be called directly; rather, each stream type
should provide its own stream creation function, which
0 on success, -1 on error.
int Lstream_getc (Lstream *stream)
- Read one byte from the stream. This is a macro and so it
- is very efficient. The STREAM argument is evaluated more
- than once. Return value is -1 for EOF or error.
+ Read one byte from the stream and returns it as an unsigned
+ char cast to an int, or EOF on end of file or error.
+ This is a macro and so it is very efficient. The STREAM
+ argument is evaluated more than once.
void Lstream_ungetc (Lstream *stream, int c)
- Push one byte back onto the input queue. This will be the
- next byte read from the stream. Any number of bytes can be
- pushed back and will be read in the reverse order they were
- pushed back -- most recent first. (This is necessary for
- consistency -- if there are a number of bytes that have been
- unread and I read and unread a byte, it needs to be the first
- to be read again.) This is a macro and so it is very
- efficient. The C argument is only evaluated once but the
- STREAM argument is evaluated more than once.
+ Push one byte back onto the input queue, cast to unsigned char.
+ This will be the next byte read from the stream. Any number
+ of bytes can be pushed back and will be read in the reverse
+ order they were pushed back -- most recent first. (This is
+ necessary for consistency -- if there are a number of bytes
+ that have been unread and I read and unread a byte, it needs
+ to be the first to be read again.) This is a macro and so it
+ is very efficient. The C argument is only evaluated once but
+ the STREAM argument is evaluated more than once.
int Lstream_fputc (Lstream *stream, int c)
int Lstream_fgetc (Lstream *stream)
}
}
+inline static size_t
+aligned_sizeof_lstream (size_t lstream_type_specific_size)
+{
+ return ALIGN_SIZE (offsetof (Lstream, data) + lstream_type_specific_size,
+ ALIGNOF (max_align_t));
+}
+
static size_t
-sizeof_lstream (CONST void *header)
+sizeof_lstream (const void *header)
{
- CONST Lstream *lstr = (CONST Lstream *) header;
- return sizeof (*lstr) + lstr->imp->size - 1;
+ return aligned_sizeof_lstream (((const Lstream *) header)->imp->size);
}
DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("stream", lstream,
}
}
-static CONST Lstream_implementation *lstream_types[32];
+static const Lstream_implementation *lstream_types[32];
static Lisp_Object Vlstream_free_list[32];
static int lstream_type_count;
Lstream *
-Lstream_new (CONST Lstream_implementation *imp, CONST char *mode)
+Lstream_new (const Lstream_implementation *imp, const char *mode)
{
Lstream *p;
int i;
assert (lstream_type_count < countof (lstream_types));
lstream_types[lstream_type_count] = imp;
Vlstream_free_list[lstream_type_count] =
- make_lcrecord_list (sizeof (*p) + imp->size - 1,
+ make_lcrecord_list (aligned_sizeof_lstream (imp->size),
&lrecord_lstream);
lstream_type_count++;
}
p = XLSTREAM (allocate_managed_lcrecord (Vlstream_free_list[i]));
/* Zero it out, except the header. */
- memset ((char *) p + sizeof (p->header), 0,
- sizeof (*p) - sizeof (p->header) + imp->size - 1);
+ memset ((char *) p + sizeof (p->header), '\0',
+ aligned_sizeof_lstream (imp->size) - sizeof (p->header));
p->imp = imp;
Lstream_set_buffering (p, LSTREAM_BLOCK_BUFFERED, 0);
p->flags = LSTREAM_FL_IS_OPEN;
#define Lstream_internal_error(reason, lstr) \
Lstream_signal_simple_error ("Internal error: " reason, lstr)
-static void Lstream_signal_simple_error (CONST char *reason, Lstream *lstr)
+static void Lstream_signal_simple_error (const char *reason, Lstream *lstr)
{
Lisp_Object obj;
XSETLSTREAM (obj, lstr);
character at the end. We need to spit back that
incomplete character. */
{
- CONST unsigned char *data = lstr->out_buffer;
- CONST unsigned char *dataend = data + size - 1;
+ const unsigned char *data = lstr->out_buffer;
+ const unsigned char *dataend = data + size - 1;
assert (size > 0); /* safety check ... */
/* Optimize the most common case. */
if (!BYTE_ASCII_P (*dataend))
/* Like Lstream_write(), but does not handle line-buffering correctly. */
static ssize_t
-Lstream_write_1 (Lstream *lstr, CONST void *data, size_t size)
+Lstream_write_1 (Lstream *lstr, const void *data, size_t size)
{
- CONST unsigned char *p = (CONST unsigned char *) data;
+ const unsigned char *p = (const unsigned char *) data;
ssize_t off = 0;
if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
Lstream_internal_error ("lstream not open", lstr);
line buffering. Returns number of bytes written. */
ssize_t
-Lstream_write (Lstream *lstr, CONST void *data, size_t size)
+Lstream_write (Lstream *lstr, const void *data, size_t size)
{
size_t i;
- CONST unsigned char *p = (CONST unsigned char *) data;
+ const unsigned char *p = (const unsigned char *) data;
if (size == 0)
return size;
return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0;
}
-static int
+static ssize_t
Lstream_raw_read (Lstream *lstr, unsigned char *buffer, size_t size)
{
if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
/* It's quite possible for us to get passed an incomplete
character at the end. We need to spit back that
incomplete character. */
- CONST unsigned char *dataend = p + off - 1;
+ const unsigned char *dataend = p + off - 1;
/* Optimize the most common case. */
if (!BYTE_ASCII_P (*dataend))
{
}
void
-Lstream_unread (Lstream *lstr, CONST void *data, size_t size)
+Lstream_unread (Lstream *lstr, const void *data, size_t size)
{
- CONST unsigned char *p = (CONST unsigned char *) data;
+ const unsigned char *p = (const unsigned char *) data;
/* Make sure buffer is big enough */
DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size,
sizeof (struct stdio_stream));
static Lisp_Object
-make_stdio_stream_1 (FILE *stream, int flags, CONST char *mode)
+make_stdio_stream_1 (FILE *stream, int flags, const char *mode)
{
Lisp_Object obj;
Lstream *lstr = Lstream_new (lstream_stdio, mode);
}
static ssize_t
-stdio_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+stdio_writer (Lstream *stream, const unsigned char *data, size_t size)
{
struct stdio_stream *str = STDIO_STREAM_DATA (stream);
size_t val = fwrite (data, 1, size, str->file);
ignored when writing); -1 for unlimited. */
static Lisp_Object
make_filedesc_stream_1 (int filedesc, int offset, int count, int flags,
- CONST char *mode)
+ const char *mode)
{
Lisp_Object obj;
Lstream *lstr = Lstream_new (lstream_filedesc, mode);
}
static ssize_t
-filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+filedesc_writer (Lstream *stream, const unsigned char *data, size_t size)
{
struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
ssize_t retval;
if (str->pty_flushing)
{
/* To make life easy, only send out one line at the most. */
- CONST unsigned char *ptr;
+ const unsigned char *ptr;
- ptr = (CONST unsigned char *) memchr (data, '\n', size);
+ ptr = (const unsigned char *) memchr (data, '\n', size);
if (ptr)
need_newline = 1;
else
struct fixed_buffer_stream
{
- CONST unsigned char *inbuf;
+ const unsigned char *inbuf;
unsigned char *outbuf;
size_t size;
size_t offset;
sizeof (struct fixed_buffer_stream));
Lisp_Object
-make_fixed_buffer_input_stream (CONST void *buf, size_t size)
+make_fixed_buffer_input_stream (const void *buf, size_t size)
{
Lisp_Object obj;
Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r");
}
static ssize_t
-fixed_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+fixed_buffer_writer (Lstream *stream, const unsigned char *data, size_t size)
{
struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream);
if (str->offset == str->size)
return 0;
}
-CONST unsigned char *
+const unsigned char *
fixed_buffer_input_stream_ptr (Lstream *stream)
{
assert (stream->imp == lstream_fixed_buffer);
}
static ssize_t
-resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+resizing_buffer_writer (Lstream *stream, const unsigned char *data, size_t size)
{
struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream);
DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char);
}
static ssize_t
-dynarr_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+dynarr_writer (Lstream *stream, const unsigned char *data, size_t size)
{
struct dynarr_stream *str = DYNARR_STREAM_DATA (stream);
Dynarr_add_many (str->dyn, data, size);
static Lisp_Object
make_lisp_buffer_stream_1 (struct buffer *buf, Bufpos start, Bufpos end,
- int flags, CONST char *mode)
+ int flags, const char *mode)
{
Lisp_Object obj;
Lstream *lstr;
}
static ssize_t
-lisp_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+lisp_buffer_writer (Lstream *stream, const unsigned char *data, size_t size)
{
struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream);
Bufpos pos;
void
vars_of_lstream (void)
{
+ INIT_LRECORD_IMPLEMENTATION (lstream);
+
reinit_vars_of_lstream ();
}