home *** CD-ROM | disk | FTP | other *** search
/ ftp.uni-stuttgart.de/pub/systems/acorn/ / Acorn.tar / Acorn / acornet / dev / c / gcc / g++lib.spk / h / editbuf < prev    next >
Text File  |  1993-12-07  |  6KB  |  178 lines

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