home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / lib / g++-include / editbuf.h < prev    next >
C/C++ Source or Header  |  1994-12-22  |  7KB  |  184 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License.
  24.  
  25. Written by Per Bothner (bothner@cygnus.com). */
  26.  
  27. #ifndef _EDITBUF_H
  28. #define _EDITBUF_H
  29. #ifdef __GNUG__
  30. #pragma interface
  31. #endif
  32. #include <stdio.h>
  33. #include <fstream.h>
  34.  
  35. typedef unsigned long mark_pointer;
  36. // At some point, it might be nice to parameterize this code
  37. // in terms of buf_char.
  38. typedef /*unsigned*/ char buf_char;
  39.  
  40. // Logical pos from start of buffer (does not count gap).
  41. typedef long buf_index;
  42.             
  43. // Pos from start of buffer, possibly including gap_size.
  44. typedef long buf_offset; 
  45.  
  46. #if 0
  47. struct buf_cookie {
  48.     FILE *file;
  49.     struct edit_string *str;
  50.     struct buf_cookie *next;
  51.     buf_index tell();
  52. };
  53. #endif
  54.  
  55. struct edit_buffer;
  56. struct edit_mark;
  57.  
  58. // A edit_string is defined as the region between the 'start' and 'end' marks.
  59. // Normally (always?) 'start->insert_before()' should be false,
  60. // and 'end->insert_before()' should be true.
  61.  
  62. struct edit_string {
  63.     struct edit_buffer *buffer; // buffer that 'start' and 'end' belong to
  64.     struct edit_mark *start, *end;
  65.     int length() const; // count of buf_chars currently in string
  66.     edit_string(struct edit_buffer *b,
  67.               struct edit_mark *ms, struct edit_mark *me)
  68.     { buffer = b; start = ms; end = me; }
  69. /* Make a fresh, contiguous copy of the data in STR.
  70.    Assign length of STR to *LENP.
  71.    (Output has extra NUL at out[*LENP].) */
  72.     buf_char *copy_bytes(int *lenp) const;
  73. //    FILE *open_file(char *mode);
  74.     void assign(struct edit_string *src); // copy bytes from src to this
  75. };
  76.  
  77. struct edit_streambuf : public streambuf {
  78.     friend edit_buffer;
  79.     edit_string *str;
  80.     edit_streambuf* next; // Chain of edit_streambuf's for a edit_buffer.
  81.     short _mode;
  82.     edit_streambuf(edit_string* bstr, int mode);
  83.     ~edit_streambuf();
  84.     virtual int underflow();
  85.     virtual int overflow(int c = EOF);
  86.     virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
  87.     void flush_to_buffer();
  88.     void flush_to_buffer(edit_buffer* buffer);
  89.     int _inserting;
  90.     int inserting() { return _inserting; }
  91.     void inserting(int i) { _inserting = i; }
  92. //    int delete_chars(int count, char* cut_buf); Not implemented.
  93.     int truncate();
  94.     int is_reading() { return gptr() != NULL; }
  95.     buf_char* current() { return is_reading() ? gptr() : pptr(); }
  96.     void set_current(char *p, int is_reading);
  97.   protected:
  98.     void disconnect_gap_from_file(edit_buffer* buffer);
  99. };
  100.  
  101. // A 'edit_mark' indicates a position in a buffer.
  102. // It is "attached" the text (rather than the offset).
  103. // There are two kinds of mark, which have different behavior
  104. // when text is inserted at the mark:
  105. // If 'insert_before()' is true the mark will be adjusted to be
  106. // *after* the new text.
  107.  
  108. struct edit_mark {
  109.     struct edit_mark *chain;
  110.     mark_pointer _pos;
  111.     inline int insert_before() { return _pos & 1; }
  112.     inline unsigned long index_in_buffer(struct edit_buffer *buffer)
  113.     { return _pos >> 1; }
  114.     inline buf_char *ptr(struct edit_buffer *buf);
  115.     buf_index tell();
  116.     edit_mark() { }
  117.     edit_mark(struct edit_string *str, long delta);
  118.     edit_buffer *buffer();
  119.     ~edit_mark();
  120. };
  121.  
  122. // A 'edit_buffer' consists of a sequence of buf_chars (the data),
  123. // a list of edit_marks pointing into the data, and a list of FILEs
  124. // also pointing into the data.
  125. // A 'edit_buffer' coerced to a edit_string is the string of
  126. // all the buf_chars in the buffer.
  127.  
  128. // This implementation uses a conventional buffer gap (as in Emacs).
  129. // The gap start is defined by de-referencing a (buf_char**).
  130. // This is because sometimes a FILE is inserting into the buffer,
  131. // so rather than having each putc adjust the gap, we use indirection
  132. // to have the gap be defined as the write pointer of the FILE.
  133. // (This assumes that putc adjusts a pointer (as in GNU's libc), not an index.)
  134.  
  135. struct edit_buffer {
  136.     buf_char *data; /* == emacs buffer_text.p1+1 */
  137.     buf_char *_gap_start;
  138.     edit_streambuf* _writer; // If non-NULL, currently writing stream
  139.     inline buf_char *gap_start()
  140.     { return _writer ? _writer->pptr() : _gap_start; }
  141.     buf_offset __gap_end_pos; // size of part 1 + size of gap
  142.     /* int gap; implicit: buf_size - size1 - size2 */
  143.     int buf_size;
  144.     struct edit_streambuf *files;
  145.     struct edit_mark start_mark;
  146.     struct edit_mark end_mark;
  147.     edit_buffer();
  148.     inline buf_offset gap_end_pos() { return __gap_end_pos; }
  149.     inline struct edit_mark *start_marker() { return &start_mark; }
  150.     inline struct edit_mark *end_marker() { return &end_mark; }
  151. /* these should be protected, ultimately */
  152.     buf_index tell(edit_mark*);
  153.     buf_index tell(buf_char*);
  154.     inline buf_char *gap_end() { return data + gap_end_pos(); }
  155.     inline int gap_size() { return gap_end() - gap_start(); }
  156.     inline int size1() { return gap_start() - data; }
  157.     inline int size2() { return buf_size - gap_end_pos(); }
  158.     inline struct edit_mark * mark_list() { return &start_mark; }
  159.     void make_gap (buf_offset);
  160.     void move_gap (buf_offset pos);
  161.     void move_gap (buf_char *pos) { move_gap(pos - data); }
  162.     void gap_left (int pos);
  163.     void gap_right (int pos);
  164.     void adjust_markers(mark_pointer low, mark_pointer high,
  165.             int amount, buf_char *old_data);
  166.     void delete_range(buf_index from, buf_index to);
  167.     void delete_range(struct edit_mark *start, struct edit_mark *end);
  168. };
  169.  
  170. extern buf_char * bstr_copy(struct edit_string *str, int *lenp);
  171.  
  172. // Convert a edit_mark to a (buf_char*)
  173.  
  174. inline buf_char *edit_mark::ptr(struct edit_buffer *buf)
  175.     { return buf->data + index_in_buffer(buf); }
  176.  
  177. inline void edit_streambuf::flush_to_buffer()
  178. {
  179.     edit_buffer* buffer = str->buffer;
  180.     if (buffer->_writer == this) flush_to_buffer(buffer);
  181. }
  182. #endif /* !_EDITBUF_H*/
  183.  
  184.