1 //////////////////////////////////////////////////////////////////////////////
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
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
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)
23 //////////////////////////////////////////////////////////////////////////////
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(); }
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
;
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
;
80 cursor
= cursor_limit
= buffer
;