1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
|
/* This is part of libio/iostream, providing -*- C++ -*- input/output.
Copyright (C) 1993 Free Software Foundation
This file is part of the GNU IO Library. This library 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 library 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 this library; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
As a special exception, if you link this library with files
compiled with a GNU compiler to produce an executable, this does not cause
the resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why
the executable file might be covered by the GNU General Public License.
Written by Per Bothner (bothner@cygnus.com). */
#ifdef __GNUG__
#pragma implementation
#endif
#include "libioP.h"
#include "editbuf.h"
#include <stddef.h>
#include <stdlib.h>
/* NOTE: Some of the code here is taken from GNU emacs */
/* Hence this file falls under the GNU License! */
// Invariants for edit_streambuf:
// An edit_streambuf is associated with a specific edit_string,
// which again is a sub-string of a specific edit_buffer.
// An edit_streambuf is always in either get mode or put mode, never both.
// In get mode, gptr() is the current position,
// and pbase(), pptr(), and epptr() are all NULL.
// In put mode, pptr() is the current position,
// and eback(), gptr(), and egptr() are all NULL.
// Any edit_streambuf that is actively doing insertion (as opposed to
// replacing) // must have its pptr() pointing to the start of the gap.
// Only one edit_streambuf can be actively inserting into a specific
// edit_buffer; the edit_buffer's _writer field points to that edit_streambuf.
// That edit_streambuf "owns" the gap, and the actual start of the
// gap is the pptr() of the edit_streambuf; the edit_buffer::_gap_start pointer
// will only be updated on an edit_streambuf::overflow().
int edit_streambuf::truncate()
{
str->buffer->delete_range(str->buffer->tell((buf_char*)pptr()),
str->buffer->tell(str->end));
return 0;
}
#ifdef OLD_STDIO
inline void disconnect_gap_from_file(edit_buffer* buffer, FILE* fp)
{
if (buffer->gap_start_ptr != &fp->__bufp)
return;
buffer->gap_start_normal = fp->__bufp;
buffer->gap_start_ptr = &buffer->gap_start_normal;
}
#endif
void edit_streambuf::flush_to_buffer(edit_buffer* buffer)
{
if (pptr() > buffer->_gap_start && pptr() < buffer->gap_end())
buffer->_gap_start = pptr();
}
void edit_streambuf::disconnect_gap_from_file(edit_buffer* buffer)
{
if (buffer->_writer != this) return;
flush_to_buffer(buffer);
setp(pptr(),pptr());
buffer->_writer = NULL;
}
buf_index edit_buffer::tell(buf_char *ptr)
{
if (ptr <= gap_start())
return ptr - data;
else
return ptr - gap_end() + size1();
}
#if 0
buf_index buf_cookie::tell()
{
return str->buffer->tell(file->__bufp);
}
#endif
buf_index edit_buffer::tell(edit_mark*mark)
{
return tell(data + mark->index_in_buffer(this));
}
// adjust the position of the gap
void edit_buffer::move_gap(buf_offset pos)
{
if (pos < size1())
gap_left (pos);
else if (pos > size1())
gap_right (pos);
}
void edit_buffer::gap_left (int pos)
{
register buf_char *to, *from;
register int i;
int new_s1;
i = size1();
from = gap_start();
to = from + gap_size();
new_s1 = size1();
/* Now copy the characters. To move the gap down,
copy characters up. */
for (;;)
{
/* I gets number of characters left to copy. */
i = new_s1 - pos;
if (i == 0)
break;
#if 0
/* If a quit is requested, stop copying now.
Change POS to be where we have actually moved the gap to. */
if (QUITP)
{
pos = new_s1;
break;
}
#endif
/* Move at most 32000 chars before checking again for a quit. */
if (i > 32000)
i = 32000;
new_s1 -= i;
while (--i >= 0)
*--to = *--from;
}
/* Adjust markers, and buffer data structure, to put the gap at POS.
POS is where the loop above stopped, which may be what was specified
or may be where a quit was detected. */
adjust_markers (pos << 1, size1() << 1, gap_size(), data);
#ifndef OLD_STDIO
_gap_start = data + pos;
#else
if (gap_start_ptr == &gap_start_normal)
gap_start_normal = data + pos;
#endif
__gap_end_pos = to - data;
/* QUIT;*/
}
void edit_buffer::gap_right (int pos)
{
register buf_char *to, *from;
register int i;
int new_s1;
i = size1();
to = gap_start();
from = i + gap_end();
new_s1 = i;
/* Now copy the characters. To move the gap up,
copy characters down. */
while (1)
{
/* I gets number of characters left to copy. */
i = pos - new_s1;
if (i == 0)
break;
#if 0
/* If a quit is requested, stop copying now.
Change POS to be where we have actually moved the gap to. */
if (QUITP)
{
pos = new_s1;
break;
}
#endif
/* Move at most 32000 chars before checking again for a quit. */
if (i > 32000)
i = 32000;
new_s1 += i;
while (--i >= 0)
*to++ = *from++;
}
adjust_markers ((size1() + gap_size()) << 1, (pos + gap_size()) << 1,
- gap_size(), data);
#ifndef OLD_STDIO
_gap_start = data+pos;
#else
if (gap_start_ptr == &gap_start_normal)
gap_start_normal = data + pos;
#endif
__gap_end_pos = from - data;
/* QUIT;*/
}
/* make sure that the gap in the current buffer is at least k
characters wide */
void edit_buffer::make_gap(buf_offset k)
{
register buf_char *p1, *p2, *lim;
buf_char *old_data = data;
int s1 = size1();
if (gap_size() >= k)
return;
/* Get more than just enough */
if (buf_size > 1000) k += 2000;
else k += /*200;*/ 20; // for testing!
p1 = (buf_char *) realloc (data, s1 + size2() + k);
if (p1 == 0)
abort(); /*memory_full ();*/
k -= gap_size(); /* Amount of increase. */
/* Record new location of text */
data = p1;
/* Transfer the new free space from the end to the gap
by shifting the second segment upward */
p2 = data + buf_size;
p1 = p2 + k;
lim = p2 - size2();
while (lim < p2)
*--p1 = *--p2;
/* Finish updating text location data */
__gap_end_pos += k;
#ifndef OLD_STDIO
_gap_start = data + s1;
#else
if (gap_start_ptr == &gap_start_normal)
gap_start_normal = data + s1;
#endif
/* adjust markers */
adjust_markers (s1 << 1, (buf_size << 1) + 1, k, old_data);
buf_size += k;
}
/* Add `amount' to the position of every marker in the current buffer
whose current position is between `from' (exclusive) and `to' (inclusive).
Also, any markers past the outside of that interval, in the direction
of adjustment, are first moved back to the near end of the interval
and then adjusted by `amount'. */
void edit_buffer::adjust_markers(register mark_pointer low,
register mark_pointer high,
int amount, buf_char *old_data)
{
register struct edit_mark *m;
register mark_pointer mpos;
/* convert to mark_pointer */
amount <<= 1;
if (_writer)
_writer->disconnect_gap_from_file(this);
for (m = mark_list(); m != NULL; m = m->chain)
{
mpos = m->_pos;
if (amount > 0)
{
if (mpos > high && mpos < high + amount)
mpos = high + amount;
}
else
{
if (mpos > low + amount && mpos <= low)
mpos = low + amount;
}
if (mpos > low && mpos <= high)
mpos += amount;
m->_pos = mpos;
}
// Now adjust files
edit_streambuf *file;
for (file = files; file != NULL; file = file->next) {
mpos = file->current() - old_data;
if (amount > 0)
{
if (mpos > high && mpos < high + amount)
mpos = high + amount;
}
else
{
if (mpos > low + amount && mpos <= low)
mpos = low + amount;
}
if (mpos > low && mpos <= high)
mpos += amount;
char* new_pos = data + mpos;
file->set_current(new_pos, file->is_reading());
}
}
#if 0
stdio_
__off == index at start of buffer (need only be valid after seek ? )
__buf ==
if read/read_delete/overwrite mode:
__endp <= min(*gap_start_ptr, edit_string->end->ptr(buffer))
if inserting:
must have *gap_start_ptr == __bufp && *gap_start_ptr+gap == __endp
file->edit_string->end->ptr(buffer) == *gap_start_ptr+end
if write_mode:
if before gap
#endif
int edit_streambuf::underflow()
{
if (!(_mode & ios::in))
return EOF;
struct edit_buffer *buffer = str->buffer;
if (!is_reading()) { // Must switch from put to get mode.
disconnect_gap_from_file(buffer);
set_current(pptr(), 1);
}
buf_char *str_end = str->end->ptr(buffer);
retry:
if (gptr() < egptr()) {
return *gptr();
}
if ((buf_char*)gptr() == str_end)
return EOF;
if (str_end <= buffer->gap_start()) {
setg(eback(), gptr(), str_end);
goto retry;
}
if (gptr() < buffer->gap_start()) {
setg(eback(), gptr(), buffer->gap_start());
goto retry;
}
if (gptr() == buffer->gap_start()) {
disconnect_gap_from_file(buffer);
// fp->__offset += fp->__bufp - fp->__buffer;
setg(buffer->gap_end(), buffer->gap_end(), str_end);
}
else
setg(eback(), gptr(), str_end);
goto retry;
}
int edit_streambuf::overflow(int ch)
{
if (_mode == ios::in)
return EOF;
struct edit_buffer *buffer = str->buffer;
flush_to_buffer(buffer);
if (ch == EOF)
return 0;
if (is_reading()) { // Must switch from get to put mode.
set_current(gptr(), 0);
}
buf_char *str_end = str->end->ptr(buffer);
retry:
if (pptr() < epptr()) {
*pptr() = ch;
pbump(1);
return (unsigned char)ch;
}
if ((buf_char*)pptr() == str_end || inserting()) {
/* insert instead */
if (buffer->_writer)
buffer->_writer->flush_to_buffer(); // Redundant?
buffer->_writer = NULL;
if (pptr() >= buffer->gap_end())
buffer->move_gap(pptr() - buffer->gap_size());
else
buffer->move_gap(pptr());
buffer->make_gap(1);
setp(buffer->gap_start(), buffer->gap_end());
buffer->_writer = this;
*pptr() = ch;
pbump(1);
return (unsigned char)ch;
}
if (str_end <= buffer->gap_start()) {
// Entire string is left of gap.
setp(pptr(), str_end);
}
else if (pptr() < buffer->gap_start()) {
// Current pos is left of gap.
setp(pptr(), buffer->gap_start());
goto retry;
}
else if (pptr() == buffer->gap_start()) {
// Current pos is at start of gap; move to end of gap.
// disconnect_gap_from_file(buffer);
setp(buffer->gap_end(), str_end);
// __offset += __bufp - __buffer;
}
else {
// Otherwise, current pos is right of gap.
setp(pptr(), str_end);
}
goto retry;
}
void edit_streambuf::set_current(char *new_pos, int reading)
{
if (reading) {
setg(new_pos, new_pos, new_pos);
setp(NULL, NULL);
}
else {
setg(NULL, NULL, NULL);
setp(new_pos, new_pos);
}
}
// Called by fseek(fp, pos, whence) if fp is bound to a edit_buffer.
streampos edit_streambuf::seekoff(streamoff offset, _seek_dir dir,
int /* =ios::in|ios::out*/)
{
struct edit_buffer *buffer = str->buffer;
disconnect_gap_from_file(buffer);
buf_index cur_pos = buffer->tell((buf_char*)current());;
buf_index start_pos = buffer->tell(str->start);
buf_index end_pos = buffer->tell(str->end);
switch (dir) {
case ios::beg:
offset += start_pos;
break;
case ios::cur:
offset += cur_pos;
break;
case ios::end:
offset += end_pos;
break;
}
if (offset < start_pos || offset > end_pos)
return EOF;
buf_char *new_pos = buffer->data + offset;
buf_char* gap_start = buffer->gap_start();
if (new_pos > gap_start) {
buf_char* gap_end = buffer->gap_end();
new_pos += gap_end - gap_start;
if (new_pos >= buffer->data + buffer->buf_size) abort(); // Paranoia.
}
set_current(new_pos, is_reading());
return EOF;
}
#if 0
int buf_seek(void *arg_cookie, fpos_t * pos, int whence)
{
struct buf_cookie *cookie = arg_cookie;
FILE *file = cookie->file;
struct edit_buffer *buffer = cookie->str->buffer;
buf_char *str_start = cookie->str->start->ptr(buffer);
disconnect_gap_from_file(buffer, cookie->file);
fpos_t cur_pos, new_pos;
if (file->__bufp <= *buffer->gap_start_ptr
|| str_start >= buffer->__gap_end)
cur_pos = str_start - file->__bufp;
else
cur_pos =
(*buffer->gap_start_ptr - str_start) + (file->__bufp - __gap_end);
end_pos = ...;
switch (whence) {
case SEEK_SET:
new_pos = *pos;
break;
case SEEK_CUR:
new_pos = cur_pos + *pos;
break;
case SEEK_END:
new_pos = end_pos + *pos;
break;
}
if (new_pos > end_pos) {
seek to end_pos;
insert_nulls(new_pos - end_pos);
return;
}
if (str_start + new_pos <= *gap_start_ptr &* *gap_start_ptr < end) {
__buffer = str_start;
__off = 0;
__bufp = str_start + new_pos;
file->__get_limit =
*buffer->gap_start_ptr; /* what if gap_start_ptr == &bufp ??? */
} else if () {
}
*pos = new_pos;
}
#endif
/* Delete characters from `from' up to (but not incl) `to' */
void edit_buffer::delete_range (buf_index from, buf_index to)
{
register int numdel;
if ((numdel = to - from) <= 0)
return;
/* Make sure the gap is somewhere in or next to what we are deleting */
if (from > size1())
gap_right (from);
if (to < size1())
gap_left (to);
/* Relocate all markers pointing into the new, larger gap
to point at the end of the text before the gap. */
adjust_markers ((to + gap_size()) << 1, (to + gap_size()) << 1,
- numdel - gap_size(), data);
__gap_end_pos = to + gap_size();
_gap_start = data + from;
}
void edit_buffer::delete_range(struct edit_mark *start, struct edit_mark *end)
{
delete_range(tell(start), tell(end));
}
void buf_delete_chars(struct edit_buffer *, struct edit_mark *, size_t)
{
abort();
}
edit_streambuf::edit_streambuf(edit_string* bstr, int mode)
{
_mode = mode;
str = bstr;
edit_buffer* buffer = bstr->buffer;
next = buffer->files;
buffer->files = this;
char* buf_ptr = bstr->start->ptr(buffer);
_inserting = 0;
// setb(buf_ptr, buf_ptr, 0);
set_current(buf_ptr, !(mode & ios::out+ios::trunc+ios::app));
if (_mode & ios::trunc)
truncate();
if (_mode & ios::ate)
seekoff(0, ios::end);
}
// Called by fclose(fp) if fp is bound to a edit_buffer.
#if 0
static int buf_close(void *arg)
{
register struct buf_cookie *cookie = arg;
struct edit_buffer *buffer = cookie->str->buffer;
struct buf_cookie **ptr;
for (ptr = &buffer->files; *ptr != cookie; ptr = &(*ptr)->next) ;
*ptr = cookie->next;
disconnect_gap_from_file(buffer, cookie->file);
free (cookie);
return 0;
}
#endif
edit_streambuf::~edit_streambuf()
{
if (_mode == ios::out)
truncate();
// Unlink this from list of files associated with bstr->buffer.
edit_streambuf **ptr = &str->buffer->files;
for (; *ptr != this; ptr = &(*ptr)->next) { }
*ptr = next;
disconnect_gap_from_file(str->buffer);
}
edit_buffer::edit_buffer()
{
buf_size = /*200;*/ 15; /* for testing! */
data = (buf_char*)malloc(buf_size);
files = NULL;
#ifndef OLD_STDIO
_gap_start = data;
_writer = NULL;
#else
gap_start_normal = data;
gap_start_ptr = &gap_start_normal;
#endif
__gap_end_pos = buf_size;
start_mark.chain = &end_mark;
start_mark._pos = 0;
end_mark.chain = NULL;
end_mark._pos = 2 * buf_size + 1;
}
// Allocate a new mark, which is adjusted by 'delta' bytes from 'this'.
// Restrict new mark to lie within 'str'.
edit_mark::edit_mark(struct edit_string *str, long delta)
{
struct edit_buffer *buf = str->buffer;
chain = buf->start_mark.chain;
buf->start_mark.chain = this;
mark_pointer size1 = buf->size1() << 1;
int gap_size = buf->gap_size() << 1;
delta <<= 1;
// check if new and old marks are opposite sides of gap
if (_pos <= size1 && _pos + delta > size1)
delta += gap_size;
else if (_pos >= size1 + gap_size && _pos + delta < size1 + gap_size)
delta -= gap_size;
_pos = _pos + delta;
if (_pos < str->start->_pos & ~1)
_pos = (str->start->_pos & ~ 1) + (_pos & 1);
else if (_pos >= str->end->_pos)
_pos = (str->end->_pos & ~ 1) + (_pos & 1);
}
// A (slow) way to find the buffer a mark belongs to.
edit_buffer * edit_mark::buffer()
{
struct edit_mark *mark;
for (mark = this; mark->chain != NULL; mark = mark->chain) ;
// Assume that the last mark on the chain is the end_mark.
return (edit_buffer *)((char*)mark - offsetof(edit_buffer, end_mark));
}
edit_mark::~edit_mark()
{
// Must unlink mark from chain of owning buffer
struct edit_buffer *buf = buffer();
if (this == &buf->start_mark || this == &buf->end_mark) abort();
edit_mark **ptr;
for (ptr = &buf->start_mark.chain; *ptr != this; ptr = &(*ptr)->chain) ;
*ptr = this->chain;
}
int edit_string::length() const
{
ptrdiff_t delta = end->ptr(buffer) - start->ptr(buffer);
if (end->ptr(buffer) <= buffer->gap_start() ||
start->ptr(buffer) >= buffer->gap_end())
return delta;
return delta - buffer->gap_size();
}
buf_char * edit_string::copy_bytes(int *lenp) const
{
char *new_str;
int len1, len2;
buf_char *start1, *start2;
start1 = start->ptr(buffer);
if (end->ptr(buffer) <= buffer->gap_start()
|| start->ptr(buffer) >= buffer->gap_end()) {
len1 = end->ptr(buffer) - start1;
len2 = 0;
start2 = NULL; // To avoid a warning from g++.
}
else {
len1 = buffer->gap_start() - start1;
start2 = buffer->gap_end();
len2 = end->ptr(buffer) - start2;
}
new_str = (char*)malloc(len1 + len2 + 1);
memcpy(new_str, start1, len1);
if (len2 > 0) memcpy(new_str + len1, start2, len2);
new_str[len1+len2] = '\0';
*lenp = len1+len2;
return new_str;
}
// Replace the buf_chars in 'this' with ones from 'src'.
// Equivalent to deleting this, then inserting src, except tries
// to leave marks in place: Marks whose offset from the start
// of 'this' is less than 'src->length()' will still have the
// same offset in 'this' when done.
void edit_string::assign(struct edit_string *src)
{
edit_streambuf dst_file(this, ios::out);
if (buffer == src->buffer /*&& ???*/) { /* overly conservative */
int src_len;
buf_char *new_str;
new_str = src->copy_bytes(&src_len);
dst_file.sputn(new_str, src_len);
free (new_str);
} else {
edit_streambuf src_file(src, ios::in);
for ( ; ; ) {
int ch = src_file.sbumpc();
if (ch == EOF) break;
dst_file.sputc(ch);
}
}
}
|