📄 editbuf.cpp
字号:
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 0int 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 0static 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;}#endifedit_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); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -