fix segv and iostream problems
[prop.git] / lib-src / automata / iolexerbuf.cc
blob630de1b1e918c19eb3485e0a48674ae5fee91599
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>
26 #include <string.h>
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();
53 if (0 == len)
55 input->clear();
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(); }
60 buf[len] = '\0';
61 // std::cout << "returning " << len << std::endl;
62 return len;
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;
76 return count;
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;
85 saved_char = -1;
86 // Reset the buffer
87 cursor = cursor_limit = buffer;
88 input = &in;