initial
[prop.git] / lib-src / automata / iolexerbuf.cc
blob4e07a4faabca1861d330e49ff87550089dcec0bf
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung (leunga@cs.nyu.edu)
22 // 1994-1995
23 //////////////////////////////////////////////////////////////////////////////
25 #include <iostream.h>
26 #include <string.h>
27 #include <AD/automata/iolexerbuf.h>
29 //////////////////////////////////////////////////////////////////////////////
30 // Constructor and destructor
31 //////////////////////////////////////////////////////////////////////////////
32 IOLexerBuffer:: IOLexerBuffer() : input(&cin)
33 { allocate_buffer(4096); }
34 IOLexerBuffer:: IOLexerBuffer(istream& in) : input(&in)
35 { allocate_buffer(4096); }
36 IOLexerBuffer::~IOLexerBuffer() {}
38 //////////////////////////////////////////////////////////////////////////////
39 // Allocate the space for a buffer
40 //////////////////////////////////////////////////////////////////////////////
41 void IOLexerBuffer::allocate_buffer(size_t n)
42 { buffer = buffer_limit = cursor = cursor_limit = new char [n+1];
43 buffer_size = n; more_input = true;
44 pinned = false; // buffer is not pinned, can be deleted
47 //////////////////////////////////////////////////////////////////////////////
48 // Method to get new input from the istream
49 //////////////////////////////////////////////////////////////////////////////
50 size_t IOLexerBuffer::read_buffer(char * buf, size_t n)
51 { // Input one line at a time for interactive use.
52 size_t len = input->get(buf, n - 1).gcount();
53 if (input->peek() == '\n') { buf[len++] = input->get(); }
54 buf[len] = '\0';
55 return len;
58 //////////////////////////////////////////////////////////////////////////////
59 // Method to fill the input buffer
60 //////////////////////////////////////////////////////////////////////////////
61 size_t IOLexerBuffer::fill_buffer()
62 { // Shift the remaining contents to the begining of the buffer.
63 size_t left_over = buffer_limit - cursor_limit;
64 memcpy(buffer, cursor_limit, left_over);
65 int count = read_buffer(buffer + left_over,buffer_size - left_over);
66 more_input = count > 0;
67 buffer_limit = buffer + left_over + count;
68 cursor = cursor_limit = buffer;
69 return count;
72 //////////////////////////////////////////////////////////////////////////////
73 // Set/reset the input buffer
74 //////////////////////////////////////////////////////////////////////////////
75 void IOLexerBuffer::set_stream(istream& in)
76 { // Yank back the saved character
77 if (buffer && saved_char >= 0) *cursor_limit = saved_char;
78 saved_char = -1;
79 // Reset the buffer
80 cursor = cursor_limit = buffer;
81 input = &in;