OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
TerminalStream.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: TerminalStream.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: TerminalStream
10 // Implements an input buffer for reading tokens from a terminal.
11 //
12 // ------------------------------------------------------------------------
13 // Class category: Parser
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2001/08/24 19:33:11 $
17 // $Author: jsberg $
18 //
19 // ------------------------------------------------------------------------
20 
21 #include "Parser/TerminalStream.h"
22 
23 #ifdef READLINE
24 // Special version using GNU readline().
25 // This version does not work with I/O redirection.
26 extern "C"
27 {
28 #include <stdio.h>
29 #ifdef __P
30  // Corrects a problem in the readline header files
31 #undef __P
32 #endif
33 #include <readline/readline.h>
34 #include <readline/history.h>
35 }
36 #endif // READLINE
37 #include <iomanip>
38 #include <iostream>
39 
40 
41 // Class TerminalStream
42 // ------------------------------------------------------------------------
43 
44 TerminalStream::TerminalStream(const char program[]):
45  AbsFileStream("standard input") {
46 #ifdef READLINE
47  // Set up the readline() function.
48  rl_readline_name = new char[strlen(program) + 1];
49  strcpy(const_cast<char *>(rl_readline_name), program);
50  rl_initialize();
51 #endif
52 }
53 
54 
56 #ifdef READLINE
57  delete [] rl_readline_name;
58 #endif
59 }
60 
61 
63 #ifdef READLINE
64  char *p = readline("==>");
65  line = std::string(p) + '\n';
66  add_history(p);
67  line += "\n";
68  curr_line++;
69  std::cerr.width(5);
70  std::cerr << curr_line << " " << line;
71  curr_char = 0;
72  return true;
73 #else
74  if(std::cin.eof()) {
75  // We must test for end of file, even on terminal, as it may be rerouted.
76  return false;
77  } else {
78  std::cerr << "==>";
79  std::getline(std::cin, line, '\n');
80  line += "\n";
81  curr_line++;
82  std::cerr.width(5);
83  std::cerr << curr_line << " " << line;
84  curr_char = 0;
85  return true;
86  }
87 #endif
88 }
std::string line
Definition: AbsFileStream.h:63
virtual bool fillLine()
Read next input line.
A stream of input tokens.
Definition: AbsFileStream.h:31
virtual ~TerminalStream()
TerminalStream(const char program[])
Constructor.