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(&std::cin
)
33 { allocate_buffer(4096); }
34 IOLexerBuffer:: IOLexerBuffer(std::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();
57 // std::cout << len << std::endl;
58 // std::cout << (int)input->peek() << " " << input->eof() << std::endl;
59 if (input
->peek() == '\n') { /*std::cout << "peeked \\n" << std::endl; */ buf
[len
++] = input
->get(); }
61 // std::cout << "returning " << len << std::endl;
65 //////////////////////////////////////////////////////////////////////////////
66 // Method to fill the input buffer
67 //////////////////////////////////////////////////////////////////////////////
68 size_t IOLexerBuffer::fill_buffer()
69 { // Shift the remaining contents to the begining of the buffer.
70 size_t left_over
= buffer_limit
- cursor_limit
;
71 memcpy(buffer
, cursor_limit
, left_over
);
72 int count
= read_buffer(buffer
+ left_over
,buffer_size
- left_over
);
73 more_input
= count
> 0;
74 buffer_limit
= buffer
+ left_over
+ count
;
75 cursor
= cursor_limit
= buffer
;
79 //////////////////////////////////////////////////////////////////////////////
80 // Set/reset the input buffer
81 //////////////////////////////////////////////////////////////////////////////
82 void IOLexerBuffer::set_stream(std::istream
& in
)
83 { // Yank back the saved character
84 if (buffer
&& saved_char
>= 0) *cursor_limit
= saved_char
;
87 cursor
= cursor_limit
= buffer
;