Mercurial > hg > xemacs-beta
comparison src/lstream.c @ 665:fdefd0186b75
[xemacs-hg @ 2001-09-20 06:28:42 by ben]
The great integral types renaming.
The purpose of this is to rationalize the names used for various
integral types, so that they match their intended uses and follow
consist conventions, and eliminate types that were not semantically
different from each other.
The conventions are:
-- All integral types that measure quantities of anything are
signed. Some people disagree vociferously with this, but their
arguments are mostly theoretical, and are vastly outweighed by
the practical headaches of mixing signed and unsigned values,
and more importantly by the far increased likelihood of
inadvertent bugs: Because of the broken "viral" nature of
unsigned quantities in C (operations involving mixed
signed/unsigned are done unsigned, when exactly the opposite is
nearly always wanted), even a single error in declaring a
quantity unsigned that should be signed, or even the even more
subtle error of comparing signed and unsigned values and
forgetting the necessary cast, can be catastrophic, as
comparisons will yield wrong results. -Wsign-compare is turned
on specifically to catch this, but this tends to result in a
great number of warnings when mixing signed and unsigned, and
the casts are annoying. More has been written on this
elsewhere.
-- All such quantity types just mentioned boil down to EMACS_INT,
which is 32 bits on 32-bit machines and 64 bits on 64-bit
machines. This is guaranteed to be the same size as Lisp
objects of type `int', and (as far as I can tell) of size_t
(unsigned!) and ssize_t. The only type below that is not an
EMACS_INT is Hashcode, which is an unsigned value of the same
size as EMACS_INT.
-- Type names should be relatively short (no more than 10
characters or so), with the first letter capitalized and no
underscores if they can at all be avoided.
-- "count" == a zero-based measurement of some quantity. Includes
sizes, offsets, and indexes.
-- "bpos" == a one-based measurement of a position in a buffer.
"Charbpos" and "Bytebpos" count text in the buffer, rather than
bytes in memory; thus Bytebpos does not directly correspond to
the memory representation. Use "Membpos" for this.
-- "Char" refers to internal-format characters, not to the C type
"char", which is really a byte.
-- For the actual name changes, see the script below.
I ran the following script to do the conversion. (NOTE: This script
is idempotent. You can safely run it multiple times and it will
not screw up previous results -- in fact, it will do nothing if
nothing has changed. Thus, it can be run repeatedly as necessary
to handle patches coming in from old workspaces, or old branches.)
There are two tags, just before and just after the change:
`pre-integral-type-rename' and `post-integral-type-rename'. When
merging code from the main trunk into a branch, the best thing to
do is first merge up to `pre-integral-type-rename', then apply the
script and associated changes, then merge from
`post-integral-type-change' to the present. (Alternatively, just do
the merging in one operation; but you may then have a lot of
conflicts needing to be resolved by hand.)
Script `fixtypes.sh' follows:
----------------------------------- cut ------------------------------------
files="*.[ch] s/*.h m/*.h config.h.in ../configure.in Makefile.in.in ../lib-src/*.[ch] ../lwlib/*.[ch]"
gr Memory_Count Bytecount $files
gr Lstream_Data_Count Bytecount $files
gr Element_Count Elemcount $files
gr Hash_Code Hashcode $files
gr extcount bytecount $files
gr bufpos charbpos $files
gr bytind bytebpos $files
gr memind membpos $files
gr bufbyte intbyte $files
gr Extcount Bytecount $files
gr Bufpos Charbpos $files
gr Bytind Bytebpos $files
gr Memind Membpos $files
gr Bufbyte Intbyte $files
gr EXTCOUNT BYTECOUNT $files
gr BUFPOS CHARBPOS $files
gr BYTIND BYTEBPOS $files
gr MEMIND MEMBPOS $files
gr BUFBYTE INTBYTE $files
gr MEMORY_COUNT BYTECOUNT $files
gr LSTREAM_DATA_COUNT BYTECOUNT $files
gr ELEMENT_COUNT ELEMCOUNT $files
gr HASH_CODE HASHCODE $files
----------------------------------- cut ------------------------------------
`fixtypes.sh' is a Bourne-shell script; it uses 'gr':
----------------------------------- cut ------------------------------------
#!/bin/sh
# Usage is like this:
# gr FROM TO FILES ...
# globally replace FROM with TO in FILES. FROM and TO are regular expressions.
# backup files are stored in the `backup' directory.
from="$1"
to="$2"
shift 2
echo ${1+"$@"} | xargs global-replace "s/$from/$to/g"
----------------------------------- cut ------------------------------------
`gr' in turn uses a Perl script to do its real work,
`global-replace', which follows:
----------------------------------- cut ------------------------------------
: #-*- Perl -*-
### global-modify --- modify the contents of a file by a Perl expression
## Copyright (C) 1999 Martin Buchholz.
## Copyright (C) 2001 Ben Wing.
## Authors: Martin Buchholz <martin@xemacs.org>, Ben Wing <ben@xemacs.org>
## Maintainer: Ben Wing <ben@xemacs.org>
## Current Version: 1.0, May 5, 2001
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with XEmacs; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
eval 'exec perl -w -S $0 ${1+"$@"}'
if 0;
use strict;
use FileHandle;
use Carp;
use Getopt::Long;
use File::Basename;
(my $myName = $0) =~ s@.*/@@; my $usage="
Usage: $myName [--help] [--backup-dir=DIR] [--line-mode] [--hunk-mode]
PERLEXPR FILE ...
Globally modify a file, either line by line or in one big hunk.
Typical usage is like this:
[with GNU print, GNU xargs: guaranteed to handle spaces, quotes, etc.
in file names]
find . -name '*.[ch]' -print0 | xargs -0 $0 's/\bCONST\b/const/g'\n
[with non-GNU print, xargs]
find . -name '*.[ch]' -print | xargs $0 's/\bCONST\b/const/g'\n
The file is read in, either line by line (with --line-mode specified)
or in one big hunk (with --hunk-mode specified; it's the default), and
the Perl expression is then evalled with \$_ set to the line or hunk of
text, including the terminating newline if there is one. It should
destructively modify the value there, storing the changed result in \$_.
Files in which any modifications are made are backed up to the directory
specified using --backup-dir, or to `backup' by default. To disable this,
use --backup-dir= with no argument.
Hunk mode is the default because it is MUCH MUCH faster than line-by-line.
Use line-by-line only when it matters, e.g. you want to do a replacement
only once per line (the default without the `g' argument). Conversely,
when using hunk mode, *ALWAYS* use `g'; otherwise, you will only make one
replacement in the entire file!
";
my %options = ();
$Getopt::Long::ignorecase = 0;
&GetOptions (
\%options,
'help', 'backup-dir=s', 'line-mode', 'hunk-mode',
);
die $usage if $options{"help"} or @ARGV <= 1;
my $code = shift;
die $usage if grep (-d || ! -w, @ARGV);
sub SafeOpen {
open ((my $fh = new FileHandle), $_[0]);
confess "Can't open $_[0]: $!" if ! defined $fh;
return $fh;
}
sub SafeClose {
close $_[0] or confess "Can't close $_[0]: $!";
}
sub FileContents {
my $fh = SafeOpen ("< $_[0]");
my $olddollarslash = $/;
local $/ = undef;
my $contents = <$fh>;
$/ = $olddollarslash;
return $contents;
}
sub WriteStringToFile {
my $fh = SafeOpen ("> $_[0]");
binmode $fh;
print $fh $_[1] or confess "$_[0]: $!\n";
SafeClose $fh;
}
foreach my $file (@ARGV) {
my $changed_p = 0;
my $new_contents = "";
if ($options{"line-mode"}) {
my $fh = SafeOpen $file;
while (<$fh>) {
my $save_line = $_;
eval $code;
$changed_p = 1 if $save_line ne $_;
$new_contents .= $_;
}
} else {
my $orig_contents = $_ = FileContents $file;
eval $code;
if ($_ ne $orig_contents) {
$changed_p = 1;
$new_contents = $_;
}
}
if ($changed_p) {
my $backdir = $options{"backup-dir"};
$backdir = "backup" if !defined ($backdir);
if ($backdir) {
my ($name, $path, $suffix) = fileparse ($file, "");
my $backfulldir = $path . $backdir;
my $backfile = "$backfulldir/$name";
mkdir $backfulldir, 0755 unless -d $backfulldir;
print "modifying $file (original saved in $backfile)\n";
rename $file, $backfile;
}
WriteStringToFile ($file, $new_contents);
}
}
----------------------------------- cut ------------------------------------
In addition to those programs, I needed to fix up a few other
things, particularly relating to the duplicate definitions of
types, now that some types merged with others. Specifically:
1. in lisp.h, removed duplicate declarations of Bytecount. The
changed code should now look like this: (In each code snippet
below, the first and last lines are the same as the original, as
are all lines outside of those lines. That allows you to locate
the section to be replaced, and replace the stuff in that
section, verifying that there isn't anything new added that
would need to be kept.)
--------------------------------- snip -------------------------------------
/* Counts of bytes or chars */
typedef EMACS_INT Bytecount;
typedef EMACS_INT Charcount;
/* Counts of elements */
typedef EMACS_INT Elemcount;
/* Hash codes */
typedef unsigned long Hashcode;
/* ------------------------ dynamic arrays ------------------- */
--------------------------------- snip -------------------------------------
2. in lstream.h, removed duplicate declaration of Bytecount.
Rewrote the comment about this type. The changed code should
now look like this:
--------------------------------- snip -------------------------------------
#endif
/* The have been some arguments over the what the type should be that
specifies a count of bytes in a data block to be written out or read in,
using Lstream_read(), Lstream_write(), and related functions.
Originally it was long, which worked fine; Martin "corrected" these to
size_t and ssize_t on the grounds that this is theoretically cleaner and
is in keeping with the C standards. Unfortunately, this practice is
horribly error-prone due to design flaws in the way that mixed
signed/unsigned arithmetic happens. In fact, by doing this change,
Martin introduced a subtle but fatal error that caused the operation of
sending large mail messages to the SMTP server under Windows to fail.
By putting all values back to be signed, avoiding any signed/unsigned
mixing, the bug immediately went away. The type then in use was
Lstream_Data_Count, so that it be reverted cleanly if a vote came to
that. Now it is Bytecount.
Some earlier comments about why the type must be signed: This MUST BE
SIGNED, since it also is used in functions that return the number of
bytes actually read to or written from in an operation, and these
functions can return -1 to signal error.
Note that the standard Unix read() and write() functions define the
count going in as a size_t, which is UNSIGNED, and the count going
out as an ssize_t, which is SIGNED. This is a horrible design
flaw. Not only is it highly likely to lead to logic errors when a
-1 gets interpreted as a large positive number, but operations are
bound to fail in all sorts of horrible ways when a number in the
upper-half of the size_t range is passed in -- this number is
unrepresentable as an ssize_t, so code that checks to see how many
bytes are actually written (which is mandatory if you are dealing
with certain types of devices) will get completely screwed up.
--ben
*/
typedef enum lstream_buffering
--------------------------------- snip -------------------------------------
3. in dumper.c, there are four places, all inside of switch()
statements, where XD_BYTECOUNT appears twice as a case tag. In
each case, the two case blocks contain identical code, and you
should *REMOVE THE SECOND* and leave the first.
author | ben |
---|---|
date | Thu, 20 Sep 2001 06:31:11 +0000 |
parents | b39c14581166 |
children | 943eaba38521 |
comparison
equal
deleted
inserted
replaced
664:6e99cc8c6ca5 | 665:fdefd0186b75 |
---|---|
93 int Lstream_fputc (Lstream *stream, int c) | 93 int Lstream_fputc (Lstream *stream, int c) |
94 int Lstream_fgetc (Lstream *stream) | 94 int Lstream_fgetc (Lstream *stream) |
95 void Lstream_fungetc (Lstream *stream, int c) | 95 void Lstream_fungetc (Lstream *stream, int c) |
96 Function equivalents of the above macros. | 96 Function equivalents of the above macros. |
97 | 97 |
98 Lstream_Data_Count Lstream_read (Lstream *stream, void *data, | 98 Bytecount Lstream_read (Lstream *stream, void *data, |
99 Lstream_Data_Count size) | 99 Bytecount size) |
100 Read SIZE bytes of DATA from the stream. Return the number of | 100 Read SIZE bytes of DATA from the stream. Return the number of |
101 bytes read. 0 means EOF. -1 means an error occurred and no | 101 bytes read. 0 means EOF. -1 means an error occurred and no |
102 bytes were read. | 102 bytes were read. |
103 | 103 |
104 Lstream_Data_Count Lstream_write (Lstream *stream, void *data, | 104 Bytecount Lstream_write (Lstream *stream, void *data, |
105 Lstream_Data_Count size) | 105 Bytecount size) |
106 Write SIZE bytes of DATA to the stream. Return the number of | 106 Write SIZE bytes of DATA to the stream. Return the number of |
107 bytes written. -1 means an error occurred and no bytes were | 107 bytes written. -1 means an error occurred and no bytes were |
108 written. | 108 written. |
109 | 109 |
110 void Lstream_unread (Lstream *stream, void *data, Lstream_Data_Count size) | 110 void Lstream_unread (Lstream *stream, void *data, Bytecount size) |
111 Push back SIZE bytes of DATA onto the input queue. The | 111 Push back SIZE bytes of DATA onto the input queue. The |
112 next call to Lstream_read() with the same size will read the | 112 next call to Lstream_read() with the same size will read the |
113 same bytes back. Note that this will be the case even if | 113 same bytes back. Note that this will be the case even if |
114 there is other pending unread data. | 114 there is other pending unread data. |
115 | 115 |
179 /* Just close. */ | 179 /* Just close. */ |
180 Lstream_close (lstr); | 180 Lstream_close (lstr); |
181 } | 181 } |
182 } | 182 } |
183 | 183 |
184 inline static Memory_Count | 184 inline static Bytecount |
185 aligned_sizeof_lstream (Memory_Count lstream_type_specific_size) | 185 aligned_sizeof_lstream (Bytecount lstream_type_specific_size) |
186 { | 186 { |
187 return ALIGN_SIZE (offsetof (Lstream, data) + lstream_type_specific_size, | 187 return ALIGN_SIZE (offsetof (Lstream, data) + lstream_type_specific_size, |
188 ALIGNOF (max_align_t)); | 188 ALIGNOF (max_align_t)); |
189 } | 189 } |
190 | 190 |
191 static Memory_Count | 191 static Bytecount |
192 sizeof_lstream (const void *header) | 192 sizeof_lstream (const void *header) |
193 { | 193 { |
194 return aligned_sizeof_lstream (((const Lstream *) header)->imp->size); | 194 return aligned_sizeof_lstream (((const Lstream *) header)->imp->size); |
195 } | 195 } |
196 | 196 |
301 /* Attempt to flush out all of the buffered data for writing. */ | 301 /* Attempt to flush out all of the buffered data for writing. */ |
302 | 302 |
303 int | 303 int |
304 Lstream_flush_out (Lstream *lstr) | 304 Lstream_flush_out (Lstream *lstr) |
305 { | 305 { |
306 Lstream_Data_Count num_written; | 306 Bytecount num_written; |
307 | 307 |
308 while (lstr->out_buffer_ind > 0) | 308 while (lstr->out_buffer_ind > 0) |
309 { | 309 { |
310 Lstream_Data_Count size = lstr->out_buffer_ind; | 310 Bytecount size = lstr->out_buffer_ind; |
311 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 311 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
312 Lstream_internal_error ("lstream not open", lstr); | 312 Lstream_internal_error ("lstream not open", lstr); |
313 if (! (lstr->flags & LSTREAM_FL_WRITE)) | 313 if (! (lstr->flags & LSTREAM_FL_WRITE)) |
314 Lstream_internal_error ("lstream not open for writing", lstr); | 314 Lstream_internal_error ("lstream not open for writing", lstr); |
315 if (!lstr->imp->writer) | 315 if (!lstr->imp->writer) |
388 the buffering size. (This is used to deal with the possibility | 388 the buffering size. (This is used to deal with the possibility |
389 that the stream writer might refuse to write any bytes now, e.g. | 389 that the stream writer might refuse to write any bytes now, e.g. |
390 if it's getting EWOULDBLOCK errors. We have to keep stocking them | 390 if it's getting EWOULDBLOCK errors. We have to keep stocking them |
391 up until they can be written, so as to avoid losing data. */ | 391 up until they can be written, so as to avoid losing data. */ |
392 | 392 |
393 static Lstream_Data_Count | 393 static Bytecount |
394 Lstream_adding (Lstream *lstr, Lstream_Data_Count num, int force) | 394 Lstream_adding (Lstream *lstr, Bytecount num, int force) |
395 { | 395 { |
396 Lstream_Data_Count size = num + lstr->out_buffer_ind; | 396 Bytecount size = num + lstr->out_buffer_ind; |
397 | 397 |
398 if (size <= lstr->out_buffer_size) | 398 if (size <= lstr->out_buffer_size) |
399 return num; | 399 return num; |
400 | 400 |
401 /* Maybe chop it down so that we don't buffer more characters | 401 /* Maybe chop it down so that we don't buffer more characters |
413 return size - lstr->out_buffer_ind; | 413 return size - lstr->out_buffer_ind; |
414 } | 414 } |
415 | 415 |
416 /* Like Lstream_write(), but does not handle line-buffering correctly. */ | 416 /* Like Lstream_write(), but does not handle line-buffering correctly. */ |
417 | 417 |
418 static Lstream_Data_Count | 418 static Bytecount |
419 Lstream_write_1 (Lstream *lstr, const void *data, Lstream_Data_Count size) | 419 Lstream_write_1 (Lstream *lstr, const void *data, Bytecount size) |
420 { | 420 { |
421 const unsigned char *p = (const unsigned char *) data; | 421 const unsigned char *p = (const unsigned char *) data; |
422 Lstream_Data_Count off = 0; | 422 Bytecount off = 0; |
423 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 423 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
424 Lstream_internal_error ("lstream not open", lstr); | 424 Lstream_internal_error ("lstream not open", lstr); |
425 if (! (lstr->flags & LSTREAM_FL_WRITE)) | 425 if (! (lstr->flags & LSTREAM_FL_WRITE)) |
426 Lstream_internal_error ("lstream not open for writing", lstr); | 426 Lstream_internal_error ("lstream not open for writing", lstr); |
427 { | 427 { |
428 int couldnt_write_last_time = 0; | 428 int couldnt_write_last_time = 0; |
429 | 429 |
430 while (1) | 430 while (1) |
431 { | 431 { |
432 /* Figure out how much we can add to the buffer */ | 432 /* Figure out how much we can add to the buffer */ |
433 Lstream_Data_Count chunk = Lstream_adding (lstr, size, 0); | 433 Bytecount chunk = Lstream_adding (lstr, size, 0); |
434 if (chunk == 0) | 434 if (chunk == 0) |
435 { | 435 { |
436 if (couldnt_write_last_time) | 436 if (couldnt_write_last_time) |
437 /* Ung, we ran out of space and tried to flush | 437 /* Ung, we ran out of space and tried to flush |
438 the buffer, but it didn't work because the stream | 438 the buffer, but it didn't work because the stream |
473 /* If the stream is not line-buffered, then we can just call | 473 /* If the stream is not line-buffered, then we can just call |
474 Lstream_write_1(), which writes in chunks. Otherwise, we | 474 Lstream_write_1(), which writes in chunks. Otherwise, we |
475 repeatedly call Lstream_putc(), which knows how to handle | 475 repeatedly call Lstream_putc(), which knows how to handle |
476 line buffering. Returns number of bytes written. */ | 476 line buffering. Returns number of bytes written. */ |
477 | 477 |
478 Lstream_Data_Count | 478 Bytecount |
479 Lstream_write (Lstream *lstr, const void *data, Lstream_Data_Count size) | 479 Lstream_write (Lstream *lstr, const void *data, Bytecount size) |
480 { | 480 { |
481 Lstream_Data_Count i; | 481 Bytecount i; |
482 const unsigned char *p = (const unsigned char *) data; | 482 const unsigned char *p = (const unsigned char *) data; |
483 | 483 |
484 if (size == 0) | 484 if (size == 0) |
485 return size; | 485 return size; |
486 if (lstr->buffering != LSTREAM_LINE_BUFFERED) | 486 if (lstr->buffering != LSTREAM_LINE_BUFFERED) |
497 Lstream_was_blocked_p (Lstream *lstr) | 497 Lstream_was_blocked_p (Lstream *lstr) |
498 { | 498 { |
499 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; | 499 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; |
500 } | 500 } |
501 | 501 |
502 static Lstream_Data_Count | 502 static Bytecount |
503 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, | 503 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, |
504 Lstream_Data_Count size) | 504 Bytecount size) |
505 { | 505 { |
506 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 506 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
507 Lstream_internal_error ("lstream not open", lstr); | 507 Lstream_internal_error ("lstream not open", lstr); |
508 if (! (lstr->flags & LSTREAM_FL_READ)) | 508 if (! (lstr->flags & LSTREAM_FL_READ)) |
509 Lstream_internal_error ("lstream not open for reading", lstr); | 509 Lstream_internal_error ("lstream not open for reading", lstr); |
513 return (lstr->imp->reader) (lstr, buffer, size); | 513 return (lstr->imp->reader) (lstr, buffer, size); |
514 } | 514 } |
515 | 515 |
516 /* Assuming the buffer is empty, fill it up again. */ | 516 /* Assuming the buffer is empty, fill it up again. */ |
517 | 517 |
518 static Lstream_Data_Count | 518 static Bytecount |
519 Lstream_read_more (Lstream *lstr) | 519 Lstream_read_more (Lstream *lstr) |
520 { | 520 { |
521 #if 0 | 521 #if 0 |
522 Lstream_Data_Count size_needed | 522 Bytecount size_needed |
523 = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); | 523 = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); |
524 #else | 524 #else |
525 /* If someone requested a larger buffer size, so be it! */ | 525 /* If someone requested a larger buffer size, so be it! */ |
526 Lstream_Data_Count size_needed = | 526 Bytecount size_needed = |
527 max (1, lstr->buffering_size); | 527 max (1, lstr->buffering_size); |
528 #endif | 528 #endif |
529 Lstream_Data_Count size_gotten; | 529 Bytecount size_gotten; |
530 | 530 |
531 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, | 531 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, |
532 size_needed, unsigned char); | 532 size_needed, unsigned char); |
533 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); | 533 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); |
534 lstr->in_buffer_current = max (0, size_gotten); | 534 lstr->in_buffer_current = max (0, size_gotten); |
535 lstr->in_buffer_ind = 0; | 535 lstr->in_buffer_ind = 0; |
536 return size_gotten < 0 ? -1 : size_gotten; | 536 return size_gotten < 0 ? -1 : size_gotten; |
537 } | 537 } |
538 | 538 |
539 Lstream_Data_Count | 539 Bytecount |
540 Lstream_read (Lstream *lstr, void *data, Lstream_Data_Count size) | 540 Lstream_read (Lstream *lstr, void *data, Bytecount size) |
541 { | 541 { |
542 unsigned char *p = (unsigned char *) data; | 542 unsigned char *p = (unsigned char *) data; |
543 Lstream_Data_Count off = 0; | 543 Bytecount off = 0; |
544 Lstream_Data_Count chunk; | 544 Bytecount chunk; |
545 int error_occurred = 0; | 545 int error_occurred = 0; |
546 | 546 |
547 if (size == 0) | 547 if (size == 0) |
548 return 0; | 548 return 0; |
549 | 549 |
572 } | 572 } |
573 | 573 |
574 /* If we need some more, try to get some more from the stream's end */ | 574 /* If we need some more, try to get some more from the stream's end */ |
575 if (size > 0) | 575 if (size > 0) |
576 { | 576 { |
577 Lstream_Data_Count retval = Lstream_read_more (lstr); | 577 Bytecount retval = Lstream_read_more (lstr); |
578 if (retval < 0) | 578 if (retval < 0) |
579 error_occurred = 1; | 579 error_occurred = 1; |
580 if (retval <= 0) | 580 if (retval <= 0) |
581 break; | 581 break; |
582 } | 582 } |
596 character, and bump forward to see if the character is | 596 character, and bump forward to see if the character is |
597 complete. */ | 597 complete. */ |
598 VALIDATE_CHARPTR_BACKWARD (dataend); | 598 VALIDATE_CHARPTR_BACKWARD (dataend); |
599 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off) | 599 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off) |
600 { | 600 { |
601 Lstream_Data_Count newoff = dataend - p; | 601 Bytecount newoff = dataend - p; |
602 /* If not, chop the size down to ignore the last char | 602 /* If not, chop the size down to ignore the last char |
603 and stash it away for next time. */ | 603 and stash it away for next time. */ |
604 Lstream_unread (lstr, dataend, off - newoff); | 604 Lstream_unread (lstr, dataend, off - newoff); |
605 off = newoff; | 605 off = newoff; |
606 } | 606 } |
609 | 609 |
610 return off == 0 && error_occurred ? -1 : off; | 610 return off == 0 && error_occurred ? -1 : off; |
611 } | 611 } |
612 | 612 |
613 void | 613 void |
614 Lstream_unread (Lstream *lstr, const void *data, Lstream_Data_Count size) | 614 Lstream_unread (Lstream *lstr, const void *data, Bytecount size) |
615 { | 615 { |
616 const unsigned char *p = (const unsigned char *) data; | 616 const unsigned char *p = (const unsigned char *) data; |
617 | 617 |
618 /* Make sure buffer is big enough */ | 618 /* Make sure buffer is big enough */ |
619 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, | 619 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, |
728 | 728 |
729 int | 729 int |
730 Lstream_fputc (Lstream *lstr, int c) | 730 Lstream_fputc (Lstream *lstr, int c) |
731 { | 731 { |
732 unsigned char ch = (unsigned char) c; | 732 unsigned char ch = (unsigned char) c; |
733 Lstream_Data_Count retval = Lstream_write_1 (lstr, &ch, 1); | 733 Bytecount retval = Lstream_write_1 (lstr, &ch, 1); |
734 if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') | 734 if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') |
735 return Lstream_flush_out (lstr); | 735 return Lstream_flush_out (lstr); |
736 return retval < 0 ? -1 : 0; | 736 return retval < 0 ? -1 : 0; |
737 } | 737 } |
738 | 738 |
809 | 809 |
810 This is probably reasonable, so I don't think we should change this | 810 This is probably reasonable, so I don't think we should change this |
811 code (it could even be argued that the error might have fixed | 811 code (it could even be argued that the error might have fixed |
812 itself, so we should do the fread() again. */ | 812 itself, so we should do the fread() again. */ |
813 | 813 |
814 static Lstream_Data_Count | 814 static Bytecount |
815 stdio_reader (Lstream *stream, unsigned char *data, Lstream_Data_Count size) | 815 stdio_reader (Lstream *stream, unsigned char *data, Bytecount size) |
816 { | 816 { |
817 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | 817 struct stdio_stream *str = STDIO_STREAM_DATA (stream); |
818 Lstream_Data_Count val = fread (data, 1, size, str->file); | 818 Bytecount val = fread (data, 1, size, str->file); |
819 if (!val && ferror (str->file)) | 819 if (!val && ferror (str->file)) |
820 return -1; | 820 return -1; |
821 return val; | 821 return val; |
822 } | 822 } |
823 | 823 |
824 static Lstream_Data_Count | 824 static Bytecount |
825 stdio_writer (Lstream *stream, const unsigned char *data, | 825 stdio_writer (Lstream *stream, const unsigned char *data, |
826 Lstream_Data_Count size) | 826 Bytecount size) |
827 { | 827 { |
828 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | 828 struct stdio_stream *str = STDIO_STREAM_DATA (stream); |
829 Lstream_Data_Count val = fwrite (data, 1, size, str->file); | 829 Bytecount val = fwrite (data, 1, size, str->file); |
830 if (!val && ferror (str->file)) | 830 if (!val && ferror (str->file)) |
831 return -1; | 831 return -1; |
832 return val; | 832 return val; |
833 } | 833 } |
834 | 834 |
877 | 877 |
878 struct filedesc_stream | 878 struct filedesc_stream |
879 { | 879 { |
880 int fd; | 880 int fd; |
881 int pty_max_bytes; | 881 int pty_max_bytes; |
882 Bufbyte eof_char; | 882 Intbyte eof_char; |
883 int starting_pos; | 883 int starting_pos; |
884 int current_pos; | 884 int current_pos; |
885 int end_pos; | 885 int end_pos; |
886 int chars_sans_newline; | 886 int chars_sans_newline; |
887 unsigned int closing :1; | 887 unsigned int closing :1; |
935 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) | 935 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) |
936 { | 936 { |
937 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); | 937 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); |
938 } | 938 } |
939 | 939 |
940 static Lstream_Data_Count | 940 static Bytecount |
941 filedesc_reader (Lstream *stream, unsigned char *data, Lstream_Data_Count size) | 941 filedesc_reader (Lstream *stream, unsigned char *data, Bytecount size) |
942 { | 942 { |
943 Lstream_Data_Count nread; | 943 Bytecount nread; |
944 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 944 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
945 if (str->end_pos >= 0) | 945 if (str->end_pos >= 0) |
946 size = min (size, (Lstream_Data_Count) (str->end_pos - str->current_pos)); | 946 size = min (size, (Bytecount) (str->end_pos - str->current_pos)); |
947 nread = str->allow_quit ? | 947 nread = str->allow_quit ? |
948 read_allowing_quit (str->fd, data, size) : | 948 read_allowing_quit (str->fd, data, size) : |
949 read (str->fd, data, size); | 949 read (str->fd, data, size); |
950 if (nread > 0) | 950 if (nread > 0) |
951 str->current_pos += nread; | 951 str->current_pos += nread; |
964 return 1; | 964 return 1; |
965 #endif | 965 #endif |
966 return 0; | 966 return 0; |
967 } | 967 } |
968 | 968 |
969 static Lstream_Data_Count | 969 static Bytecount |
970 filedesc_writer (Lstream *stream, const unsigned char *data, | 970 filedesc_writer (Lstream *stream, const unsigned char *data, |
971 Lstream_Data_Count size) | 971 Bytecount size) |
972 { | 972 { |
973 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 973 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
974 Lstream_Data_Count retval; | 974 Bytecount retval; |
975 int need_newline = 0; | 975 int need_newline = 0; |
976 | 976 |
977 /* This function would be simple if it were not for the blasted | 977 /* This function would be simple if it were not for the blasted |
978 PTY max-bytes stuff. Why the hell can't they just have written | 978 PTY max-bytes stuff. Why the hell can't they just have written |
979 the PTY drivers right so this problem doesn't exist? | 979 the PTY drivers right so this problem doesn't exist? |
1023 and flush with an EOF if necessary. Be careful to | 1023 and flush with an EOF if necessary. Be careful to |
1024 keep track of write errors as we go along and look | 1024 keep track of write errors as we go along and look |
1025 out for EWOULDBLOCK. */ | 1025 out for EWOULDBLOCK. */ |
1026 if (str->chars_sans_newline >= str->pty_max_bytes) | 1026 if (str->chars_sans_newline >= str->pty_max_bytes) |
1027 { | 1027 { |
1028 Lstream_Data_Count retval2 = str->allow_quit ? | 1028 Bytecount retval2 = str->allow_quit ? |
1029 write_allowing_quit (str->fd, &str->eof_char, 1) : | 1029 write_allowing_quit (str->fd, &str->eof_char, 1) : |
1030 write (str->fd, &str->eof_char, 1); | 1030 write (str->fd, &str->eof_char, 1); |
1031 | 1031 |
1032 if (retval2 > 0) | 1032 if (retval2 > 0) |
1033 str->chars_sans_newline = 0; | 1033 str->chars_sans_newline = 0; |
1055 /* The need_newline flag is necessary because otherwise when the | 1055 /* The need_newline flag is necessary because otherwise when the |
1056 first byte is a newline, we'd get stuck never writing anything | 1056 first byte is a newline, we'd get stuck never writing anything |
1057 in pty-flushing mode. */ | 1057 in pty-flushing mode. */ |
1058 if (need_newline) | 1058 if (need_newline) |
1059 { | 1059 { |
1060 Bufbyte nl = '\n'; | 1060 Intbyte nl = '\n'; |
1061 Lstream_Data_Count retval2 = str->allow_quit ? | 1061 Bytecount retval2 = str->allow_quit ? |
1062 write_allowing_quit (str->fd, &nl, 1) : | 1062 write_allowing_quit (str->fd, &nl, 1) : |
1063 write (str->fd, &nl, 1); | 1063 write (str->fd, &nl, 1); |
1064 | 1064 |
1065 if (retval2 > 0) | 1065 if (retval2 > 0) |
1066 { | 1066 { |
1138 return str->blocking_error_p; | 1138 return str->blocking_error_p; |
1139 } | 1139 } |
1140 | 1140 |
1141 void | 1141 void |
1142 filedesc_stream_set_pty_flushing (Lstream *stream, int pty_max_bytes, | 1142 filedesc_stream_set_pty_flushing (Lstream *stream, int pty_max_bytes, |
1143 Bufbyte eof_char) | 1143 Intbyte eof_char) |
1144 { | 1144 { |
1145 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 1145 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
1146 str->pty_max_bytes = pty_max_bytes; | 1146 str->pty_max_bytes = pty_max_bytes; |
1147 str->eof_char = eof_char; | 1147 str->eof_char = eof_char; |
1148 str->pty_flushing = 1; | 1148 str->pty_flushing = 1; |
1192 str->obj = string; | 1192 str->obj = string; |
1193 XSETLSTREAM (obj, lstr); | 1193 XSETLSTREAM (obj, lstr); |
1194 return obj; | 1194 return obj; |
1195 } | 1195 } |
1196 | 1196 |
1197 static Lstream_Data_Count | 1197 static Bytecount |
1198 lisp_string_reader (Lstream *stream, unsigned char *data, | 1198 lisp_string_reader (Lstream *stream, unsigned char *data, |
1199 Lstream_Data_Count size) | 1199 Bytecount size) |
1200 { | 1200 { |
1201 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); | 1201 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); |
1202 /* Don't lose if the string shrank past us ... */ | 1202 /* Don't lose if the string shrank past us ... */ |
1203 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); | 1203 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); |
1204 Bufbyte *strstart = XSTRING_DATA (str->obj); | 1204 Intbyte *strstart = XSTRING_DATA (str->obj); |
1205 Bufbyte *start = strstart + offset; | 1205 Intbyte *start = strstart + offset; |
1206 | 1206 |
1207 /* ... or if someone changed the string and we ended up in the | 1207 /* ... or if someone changed the string and we ended up in the |
1208 middle of a character. */ | 1208 middle of a character. */ |
1209 /* Being in the middle of a character is `normal' unless | 1209 /* Being in the middle of a character is `normal' unless |
1210 LSTREAM_NO_PARTIAL_CHARS - mrb */ | 1210 LSTREAM_NO_PARTIAL_CHARS - mrb */ |
1211 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) | 1211 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) |
1212 VALIDATE_CHARPTR_BACKWARD (start); | 1212 VALIDATE_CHARPTR_BACKWARD (start); |
1213 offset = start - strstart; | 1213 offset = start - strstart; |
1214 size = min (size, (Lstream_Data_Count) (str->end - offset)); | 1214 size = min (size, (Bytecount) (str->end - offset)); |
1215 memcpy (data, start, size); | 1215 memcpy (data, start, size); |
1216 str->offset = offset + size; | 1216 str->offset = offset + size; |
1217 return size; | 1217 return size; |
1218 } | 1218 } |
1219 | 1219 |
1227 /* Don't lose if the string shrank past us ... */ | 1227 /* Don't lose if the string shrank past us ... */ |
1228 pos = min (pos, XSTRING_LENGTH (str->obj)); | 1228 pos = min (pos, XSTRING_LENGTH (str->obj)); |
1229 /* ... or if someone changed the string and we ended up in the | 1229 /* ... or if someone changed the string and we ended up in the |
1230 middle of a character. */ | 1230 middle of a character. */ |
1231 { | 1231 { |
1232 Bufbyte *strstart = XSTRING_DATA (str->obj); | 1232 Intbyte *strstart = XSTRING_DATA (str->obj); |
1233 Bufbyte *start = strstart + pos; | 1233 Intbyte *start = strstart + pos; |
1234 VALIDATE_CHARPTR_BACKWARD (start); | 1234 VALIDATE_CHARPTR_BACKWARD (start); |
1235 pos = start - strstart; | 1235 pos = start - strstart; |
1236 } | 1236 } |
1237 str->offset = pos; | 1237 str->offset = pos; |
1238 return 0; | 1238 return 0; |
1252 | 1252 |
1253 struct fixed_buffer_stream | 1253 struct fixed_buffer_stream |
1254 { | 1254 { |
1255 const unsigned char *inbuf; | 1255 const unsigned char *inbuf; |
1256 unsigned char *outbuf; | 1256 unsigned char *outbuf; |
1257 Lstream_Data_Count size; | 1257 Bytecount size; |
1258 Lstream_Data_Count offset; | 1258 Bytecount offset; |
1259 }; | 1259 }; |
1260 | 1260 |
1261 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer, | 1261 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer, |
1262 sizeof (struct fixed_buffer_stream)); | 1262 sizeof (struct fixed_buffer_stream)); |
1263 | 1263 |
1264 Lisp_Object | 1264 Lisp_Object |
1265 make_fixed_buffer_input_stream (const void *buf, Lstream_Data_Count size) | 1265 make_fixed_buffer_input_stream (const void *buf, Bytecount size) |
1266 { | 1266 { |
1267 Lisp_Object obj; | 1267 Lisp_Object obj; |
1268 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); | 1268 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); |
1269 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | 1269 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); |
1270 str->inbuf = (const unsigned char *) buf; | 1270 str->inbuf = (const unsigned char *) buf; |
1272 XSETLSTREAM (obj, lstr); | 1272 XSETLSTREAM (obj, lstr); |
1273 return obj; | 1273 return obj; |
1274 } | 1274 } |
1275 | 1275 |
1276 Lisp_Object | 1276 Lisp_Object |
1277 make_fixed_buffer_output_stream (void *buf, Lstream_Data_Count size) | 1277 make_fixed_buffer_output_stream (void *buf, Bytecount size) |
1278 { | 1278 { |
1279 Lisp_Object obj; | 1279 Lisp_Object obj; |
1280 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); | 1280 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); |
1281 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | 1281 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); |
1282 str->outbuf = (unsigned char *) buf; | 1282 str->outbuf = (unsigned char *) buf; |
1283 str->size = size; | 1283 str->size = size; |
1284 XSETLSTREAM (obj, lstr); | 1284 XSETLSTREAM (obj, lstr); |
1285 return obj; | 1285 return obj; |
1286 } | 1286 } |
1287 | 1287 |
1288 static Lstream_Data_Count | 1288 static Bytecount |
1289 fixed_buffer_reader (Lstream *stream, unsigned char *data, | 1289 fixed_buffer_reader (Lstream *stream, unsigned char *data, |
1290 Lstream_Data_Count size) | 1290 Bytecount size) |
1291 { | 1291 { |
1292 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | 1292 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); |
1293 size = min (size, str->size - str->offset); | 1293 size = min (size, str->size - str->offset); |
1294 memcpy (data, str->inbuf + str->offset, size); | 1294 memcpy (data, str->inbuf + str->offset, size); |
1295 str->offset += size; | 1295 str->offset += size; |
1296 return size; | 1296 return size; |
1297 } | 1297 } |
1298 | 1298 |
1299 static Lstream_Data_Count | 1299 static Bytecount |
1300 fixed_buffer_writer (Lstream *stream, const unsigned char *data, | 1300 fixed_buffer_writer (Lstream *stream, const unsigned char *data, |
1301 Lstream_Data_Count size) | 1301 Bytecount size) |
1302 { | 1302 { |
1303 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | 1303 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); |
1304 if (str->offset == str->size) | 1304 if (str->offset == str->size) |
1305 { | 1305 { |
1306 /* If we're at the end, just throw away the data and pretend | 1306 /* If we're at the end, just throw away the data and pretend |
1341 LSTREAM_TYPE_DATA (stream, resizing_buffer) | 1341 LSTREAM_TYPE_DATA (stream, resizing_buffer) |
1342 | 1342 |
1343 struct resizing_buffer_stream | 1343 struct resizing_buffer_stream |
1344 { | 1344 { |
1345 unsigned char *buf; | 1345 unsigned char *buf; |
1346 Lstream_Data_Count allocked; | 1346 Bytecount allocked; |
1347 int max_stored; | 1347 int max_stored; |
1348 int stored; | 1348 int stored; |
1349 }; | 1349 }; |
1350 | 1350 |
1351 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer, | 1351 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer, |
1357 Lisp_Object obj; | 1357 Lisp_Object obj; |
1358 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); | 1358 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); |
1359 return obj; | 1359 return obj; |
1360 } | 1360 } |
1361 | 1361 |
1362 static Lstream_Data_Count | 1362 static Bytecount |
1363 resizing_buffer_writer (Lstream *stream, const unsigned char *data, | 1363 resizing_buffer_writer (Lstream *stream, const unsigned char *data, |
1364 Lstream_Data_Count size) | 1364 Bytecount size) |
1365 { | 1365 { |
1366 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); | 1366 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); |
1367 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); | 1367 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); |
1368 memcpy (str->buf + str->stored, data, size); | 1368 memcpy (str->buf + str->stored, data, size); |
1369 str->stored += size; | 1369 str->stored += size; |
1398 | 1398 |
1399 /*********** write to an unsigned-char dynarr ***********/ | 1399 /*********** write to an unsigned-char dynarr ***********/ |
1400 | 1400 |
1401 /* Note: If you have a dynarr whose type is not unsigned_char_dynarr | 1401 /* Note: If you have a dynarr whose type is not unsigned_char_dynarr |
1402 but which is really just an unsigned_char_dynarr (e.g. its type | 1402 but which is really just an unsigned_char_dynarr (e.g. its type |
1403 is Bufbyte or Extbyte), just cast to unsigned_char_dynarr. */ | 1403 is Intbyte or Extbyte), just cast to unsigned_char_dynarr. */ |
1404 | 1404 |
1405 #define DYNARR_STREAM_DATA(stream) \ | 1405 #define DYNARR_STREAM_DATA(stream) \ |
1406 LSTREAM_TYPE_DATA (stream, dynarr) | 1406 LSTREAM_TYPE_DATA (stream, dynarr) |
1407 | 1407 |
1408 struct dynarr_stream | 1408 struct dynarr_stream |
1420 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); | 1420 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); |
1421 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; | 1421 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; |
1422 return obj; | 1422 return obj; |
1423 } | 1423 } |
1424 | 1424 |
1425 static Lstream_Data_Count | 1425 static Bytecount |
1426 dynarr_writer (Lstream *stream, const unsigned char *data, | 1426 dynarr_writer (Lstream *stream, const unsigned char *data, |
1427 Lstream_Data_Count size) | 1427 Bytecount size) |
1428 { | 1428 { |
1429 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); | 1429 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); |
1430 Dynarr_add_many (str->dyn, data, size); | 1430 Dynarr_add_many (str->dyn, data, size); |
1431 return size; | 1431 return size; |
1432 } | 1432 } |
1464 | 1464 |
1465 DEFINE_LSTREAM_IMPLEMENTATION ("lisp-buffer", lstream_lisp_buffer, | 1465 DEFINE_LSTREAM_IMPLEMENTATION ("lisp-buffer", lstream_lisp_buffer, |
1466 sizeof (struct lisp_buffer_stream)); | 1466 sizeof (struct lisp_buffer_stream)); |
1467 | 1467 |
1468 static Lisp_Object | 1468 static Lisp_Object |
1469 make_lisp_buffer_stream_1 (struct buffer *buf, Bufpos start, Bufpos end, | 1469 make_lisp_buffer_stream_1 (struct buffer *buf, Charbpos start, Charbpos end, |
1470 int flags, const char *mode) | 1470 int flags, const char *mode) |
1471 { | 1471 { |
1472 Lisp_Object obj; | 1472 Lisp_Object obj; |
1473 Lstream *lstr; | 1473 Lstream *lstr; |
1474 struct lisp_buffer_stream *str; | 1474 struct lisp_buffer_stream *str; |
1475 Bufpos bmin, bmax; | 1475 Charbpos bmin, bmax; |
1476 int reading = !strcmp (mode, "r"); | 1476 int reading = !strcmp (mode, "r"); |
1477 | 1477 |
1478 /* Make sure the luser didn't pass "w" in. */ | 1478 /* Make sure the luser didn't pass "w" in. */ |
1479 if (!strcmp (mode, "w")) | 1479 if (!strcmp (mode, "w")) |
1480 abort (); | 1480 abort (); |
1530 XSETLSTREAM (obj, lstr); | 1530 XSETLSTREAM (obj, lstr); |
1531 return obj; | 1531 return obj; |
1532 } | 1532 } |
1533 | 1533 |
1534 Lisp_Object | 1534 Lisp_Object |
1535 make_lisp_buffer_input_stream (struct buffer *buf, Bufpos start, Bufpos end, | 1535 make_lisp_buffer_input_stream (struct buffer *buf, Charbpos start, Charbpos end, |
1536 int flags) | 1536 int flags) |
1537 { | 1537 { |
1538 return make_lisp_buffer_stream_1 (buf, start, end, flags, "r"); | 1538 return make_lisp_buffer_stream_1 (buf, start, end, flags, "r"); |
1539 } | 1539 } |
1540 | 1540 |
1541 Lisp_Object | 1541 Lisp_Object |
1542 make_lisp_buffer_output_stream (struct buffer *buf, Bufpos pos, int flags) | 1542 make_lisp_buffer_output_stream (struct buffer *buf, Charbpos pos, int flags) |
1543 { | 1543 { |
1544 Lisp_Object lstr = make_lisp_buffer_stream_1 (buf, pos, 0, flags, "wc"); | 1544 Lisp_Object lstr = make_lisp_buffer_stream_1 (buf, pos, 0, flags, "wc"); |
1545 | 1545 |
1546 Lstream_set_character_mode (XLSTREAM (lstr)); | 1546 Lstream_set_character_mode (XLSTREAM (lstr)); |
1547 return lstr; | 1547 return lstr; |
1548 } | 1548 } |
1549 | 1549 |
1550 static Lstream_Data_Count | 1550 static Bytecount |
1551 lisp_buffer_reader (Lstream *stream, unsigned char *data, | 1551 lisp_buffer_reader (Lstream *stream, unsigned char *data, |
1552 Lstream_Data_Count size) | 1552 Bytecount size) |
1553 { | 1553 { |
1554 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | 1554 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); |
1555 unsigned char *orig_data = data; | 1555 unsigned char *orig_data = data; |
1556 Bytind start; | 1556 Bytebpos start; |
1557 Bytind end; | 1557 Bytebpos end; |
1558 struct buffer *buf = XBUFFER (str->buffer); | 1558 struct buffer *buf = XBUFFER (str->buffer); |
1559 | 1559 |
1560 if (!BUFFER_LIVE_P (buf)) | 1560 if (!BUFFER_LIVE_P (buf)) |
1561 return 0; /* Fut. */ | 1561 return 0; /* Fut. */ |
1562 | 1562 |
1563 /* NOTE: We do all our operations in Bytind's. | 1563 /* NOTE: We do all our operations in Bytebpos's. |
1564 Keep in mind that SIZE is a value in bytes, not chars. */ | 1564 Keep in mind that SIZE is a value in bytes, not chars. */ |
1565 | 1565 |
1566 start = bi_marker_position (str->start); | 1566 start = bi_marker_position (str->start); |
1567 end = bi_marker_position (str->end); | 1567 end = bi_marker_position (str->end); |
1568 if (!(str->flags & LSTR_IGNORE_ACCESSIBLE)) | 1568 if (!(str->flags & LSTR_IGNORE_ACCESSIBLE)) |
1569 { | 1569 { |
1570 start = bytind_clip_to_bounds (BI_BUF_BEGV (buf), start, | 1570 start = bytebpos_clip_to_bounds (BI_BUF_BEGV (buf), start, |
1571 BI_BUF_ZV (buf)); | 1571 BI_BUF_ZV (buf)); |
1572 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end, | 1572 end = bytebpos_clip_to_bounds (BI_BUF_BEGV (buf), end, |
1573 BI_BUF_ZV (buf)); | 1573 BI_BUF_ZV (buf)); |
1574 } | 1574 } |
1575 | 1575 |
1576 size = min (size, (Lstream_Data_Count) (end - start)); | 1576 size = min (size, (Bytecount) (end - start)); |
1577 end = start + size; | 1577 end = start + size; |
1578 /* We cannot return a partial character. */ | 1578 /* We cannot return a partial character. */ |
1579 VALIDATE_BYTIND_BACKWARD (buf, end); | 1579 VALIDATE_BYTEBPOS_BACKWARD (buf, end); |
1580 | 1580 |
1581 while (start < end) | 1581 while (start < end) |
1582 { | 1582 { |
1583 Bytind ceil; | 1583 Bytebpos ceil; |
1584 Bytecount chunk; | 1584 Bytecount chunk; |
1585 | 1585 |
1586 if (str->flags & LSTR_IGNORE_ACCESSIBLE) | 1586 if (str->flags & LSTR_IGNORE_ACCESSIBLE) |
1587 ceil = BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (buf, start); | 1587 ceil = BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (buf, start); |
1588 else | 1588 else |
1604 | 1604 |
1605 set_bi_marker_position (str->start, end); | 1605 set_bi_marker_position (str->start, end); |
1606 return data - orig_data; | 1606 return data - orig_data; |
1607 } | 1607 } |
1608 | 1608 |
1609 static Lstream_Data_Count | 1609 static Bytecount |
1610 lisp_buffer_writer (Lstream *stream, const unsigned char *data, | 1610 lisp_buffer_writer (Lstream *stream, const unsigned char *data, |
1611 Lstream_Data_Count size) | 1611 Bytecount size) |
1612 { | 1612 { |
1613 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | 1613 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); |
1614 Bufpos pos; | 1614 Charbpos pos; |
1615 struct buffer *buf = XBUFFER (str->buffer); | 1615 struct buffer *buf = XBUFFER (str->buffer); |
1616 | 1616 |
1617 if (!BUFFER_LIVE_P (buf)) | 1617 if (!BUFFER_LIVE_P (buf)) |
1618 return 0; /* Fut. */ | 1618 return 0; /* Fut. */ |
1619 | 1619 |
1651 mark_object (str->start); | 1651 mark_object (str->start); |
1652 mark_object (str->end); | 1652 mark_object (str->end); |
1653 return str->buffer; | 1653 return str->buffer; |
1654 } | 1654 } |
1655 | 1655 |
1656 Bufpos | 1656 Charbpos |
1657 lisp_buffer_stream_startpos (Lstream *stream) | 1657 lisp_buffer_stream_startpos (Lstream *stream) |
1658 { | 1658 { |
1659 return marker_position (LISP_BUFFER_STREAM_DATA (stream)->start); | 1659 return marker_position (LISP_BUFFER_STREAM_DATA (stream)->start); |
1660 } | 1660 } |
1661 | 1661 |