home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / parsestream.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  5KB  |  146 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  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.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef PARSESTREAM_H
  19. #define PARSESTREAM_H
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23. #include "streambuf.h"
  24.  
  25. // A parsebuf is a streambuf optimized for scanning text files.
  26. // It keeps track of line and column numbers.
  27. // It is guaranteed to remember the entire current line,
  28. // as well the '\n'-s on either side of it (if they exist).
  29. // You can arbitrarily seek (or unget) within this extended line.
  30. // Other backward seeks are not supported.
  31. // Normal read semantics are supported (and hence istream operators like >>).
  32.  
  33. class parsebuf : public backupbuf {
  34.   protected:
  35.     fpos_t pos_at_line_start;
  36.     long _line_length;
  37.     unsigned long __line_number;
  38.     char *buf_start;
  39.     char *buf_end;
  40.  
  41.   public:
  42.     parsebuf *chain;
  43.  
  44.     // Return column number (raw - don't handle tabs etc).
  45.     // Retult can be -1, meaning: at '\n' before current line.
  46.     virtual int tell_in_line();
  47.  
  48.     // seek to (raw) column I in current line.
  49.     // Result is new (raw) column position - differs from I if unable to seek.
  50.     // Seek to -1 tries to seek to before previous LF.
  51.     virtual int seek_in_line(int i);
  52.  
  53.     // Note: there is no "current line" initially, until something is read.
  54.  
  55.     // Current line number, starting with 0.
  56.     // If tell_in_line()==-1, then line number of next line.
  57.     int line_number() { return __line_number; }
  58.  
  59.     // Length of current line, not counting either '\n'.
  60.     int line_length() { return _line_length; }
  61.     // Current line - not a copy, so file ops may trash it. 
  62.     virtual char* current_line();
  63.     streampos seekoff(streamoff, _seek_dir, int);
  64.     virtual streambuf* setbuf(char* p, int len);
  65.   protected:
  66.     parsebuf() : backupbuf() { chain= NULL;
  67.     __line_number = 0; pos_at_line_start = 0; _line_length = -1; }
  68.     virtual int pbackfail(int c);
  69. };
  70.  
  71. // A string_parsebuf is a parsebuf whose source is a fixed string.
  72.  
  73. class string_parsebuf : public parsebuf {
  74.   public:
  75.     int do_delete;
  76.     string_parsebuf(char *str, int len, int delete_at_close=0);
  77.     virtual int underflow();
  78.     virtual char* current_line();
  79.     virtual int seek_in_line(int i);
  80.     virtual int tell_in_line();
  81.     char *left() const { return base(); }
  82.     char *right() const { return ebuf(); }
  83. //    streampos seekoff(streamoff, _seek_dir, int);
  84. };
  85.  
  86. // A func_parsebuf calls a given function to get new input.
  87. // Each call returns an entire NUL-terminated line (without the '\n').
  88. // That line has been allocated with malloc(), not new.
  89. // The interface is tailored to the GNU readline library.
  90. // Example:
  91. // char* DoReadLine(void* arg)
  92. // {
  93. //   char *line = readline((char*)arg); /* 'arg' is used as prompt. */
  94. //   if line == NULL) { putc('\n', stderr); return NULL; }
  95. //   if (line[0] != '\0') add_history(line);
  96. //    return line;
  97. // }
  98. // char PromptBuffer[100] = "> ";
  99. // func_parsebuf my_stream(DoReadLine, PromptBuffer);
  100.  
  101. typedef char *(*CharReader)(void *arg);
  102. class istream;
  103.  
  104. class func_parsebuf : public parsebuf {
  105.   public:
  106.     void *arg;
  107.     CharReader read_func;
  108.     int backed_up_to_newline;
  109.     func_parsebuf(CharReader func, void *argm = NULL);
  110.     int underflow();
  111.     virtual int tell_in_line();
  112.     virtual int seek_in_line(int i);
  113.     virtual char* current_line();
  114. };
  115.  
  116. // A general_parsebuf is a parsebuf which gets its input from some
  117. // other streambuf. It explicitly buffers up an entire line.
  118.  
  119. class general_parsebuf : public parsebuf {
  120.   public:
  121.     streambuf *sbuf;
  122.     int delete_buf; // Delete sbuf when destroying this.
  123.     general_parsebuf(streambuf *buf, int delete_arg_buf = 0);
  124.     int underflow();
  125.     virtual int tell_in_line();
  126.     virtual int seek_in_line(int i);
  127.     ~general_parsebuf();
  128.     virtual char* current_line();
  129. };
  130.  
  131. #if 0
  132. class parsestream : public istream {
  133.     streammarker marks[2];
  134.     short _first; // of the two marks; either 0 or 1
  135.     int _lineno;
  136.     int first() { return _first; }
  137.     int second() { return 1-_first; }
  138.     int line_length() { marks[second].delta(marks[first]); }
  139.     int line_length() { marks[second].delta(marks[first]); }
  140.     int seek_in_line(int i);
  141.     int tell_in_line();
  142.     int line_number();
  143. };
  144. #endif
  145. #endif /*!defined(PARSESTREAM_H)*/
  146.