OpenASIP  2.0
LineReader.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file LineReader.hh
26  *
27  * Declaration of LineReader class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
31  * @note reviewed 2 June 2004 by jm, pj, tr, jn
32  * @note rating: red
33  */
34 
35 #ifndef TTA_LINE_READER_HH
36 #define TTA_LINE_READER_HH
37 
38 #include <cstdio>
39 #include <string>
40 #include <iostream>
41 #include <deque>
42 #include <fstream>
43 
44 #include "Exception.hh"
45 
46 /**
47  * Abstract base class for line readers.
48  *
49  * LineReader's purpose is to handle reading from command line and to make it
50  * possible for the user to edit the input given to program.
51  */
52 class LineReader {
53 public:
54  LineReader(
55  std::istream& iStream = std::cin, std::ostream& oStream = std::cout);
56  virtual ~LineReader();
57 
58  virtual void initialize(
59  std::string defPrompt = "",
60  FILE* in = stdin,
61  FILE* out = stdout,
62  FILE* err = stderr) = 0;
63 
64  virtual std::string readLine(std::string prompt = "") = 0;
65 
66  virtual std::ostream& outputStream();
67 
68  virtual char charQuestion(
69  std::string question, std::string allowedChars,
70  bool caseSensitive = false, char defaultAnswer = '\0') = 0;
71 
72  bool confirmation(
73  std::string question, char defaultAnswer = 'n', char yesChar = 'y',
74  char noChar = 'n');
75 
76  virtual void setSaveInputHistoryToFile(bool flag);
77  virtual bool saveInputHistoryToFile() const;
78  virtual void setInputHistoryLog(const std::string& historyFilename);
79  virtual void setInputHistoryLength(std::size_t length);
80  virtual size_t inputHistoryMaxLength() const;
81  virtual std::size_t inputsInHistory() const;
82  virtual std::string inputHistoryEntry(std::size_t age) const;
83 
84 protected:
85  void setInitialized();
86  bool initialized() const;
87  void putInInputHistory(const std::string& inputLine);
88 
89 private:
90  /// Copying not allowed.
91  LineReader(const LineReader&);
92  /// Assignment not allowed.
94  /// Flag indicating whether LineReader is initialized.
96  /// The input history.
97  std::deque<std::string> inputHistory_;
98  /// The maximum size for input history.
99  std::size_t inputHistorySize_;
100  /// Should the history be appended in a file?
102  /// The output stream to write the command history to.
103  std::ofstream* historyFile_;
104  /// The filename to write the command history to.
105  std::string historyFilename_;
106  /// The input stream.
107  std::istream& iStream_;
108  /// The output stream.
109  std::ostream& oStream_;
110 };
111 
112 #define DEFAULT_INPUT_HISTORY_SIZE 50
113 
114 #include "LineReader.icc"
115 
116 #endif
LineReader::inputHistory_
std::deque< std::string > inputHistory_
The input history.
Definition: LineReader.hh:97
Exception.hh
LineReader::operator=
LineReader & operator=(const LineReader &)
Assignment not allowed.
LineReader::putInInputHistory
void putInInputHistory(const std::string &inputLine)
Definition: LineReader.cc:184
LineReader::setInputHistoryLog
virtual void setInputHistoryLog(const std::string &historyFilename)
Definition: LineReader.cc:124
LineReader::saveHistoryToFile_
bool saveHistoryToFile_
Should the history be appended in a file?
Definition: LineReader.hh:101
LineReader::setInitialized
void setInitialized()
LineReader::charQuestion
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')=0
LineReader::inputHistoryMaxLength
virtual size_t inputHistoryMaxLength() const
LineReader::readLine
virtual std::string readLine(std::string prompt="")=0
LineReader::~LineReader
virtual ~LineReader()
Definition: LineReader.cc:58
LineReader::historyFilename_
std::string historyFilename_
The filename to write the command history to.
Definition: LineReader.hh:105
LineReader::inputHistorySize_
std::size_t inputHistorySize_
The maximum size for input history.
Definition: LineReader.hh:99
LineReader::inputsInHistory
virtual std::size_t inputsInHistory() const
Definition: LineReader.cc:150
LineReader::setSaveInputHistoryToFile
virtual void setSaveInputHistoryToFile(bool flag)
Definition: LineReader.cc:114
LineReader::oStream_
std::ostream & oStream_
The output stream.
Definition: LineReader.hh:109
LineReader::LineReader
LineReader(std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
Definition: LineReader.cc:48
LineReader::inputHistoryEntry
virtual std::string inputHistoryEntry(std::size_t age) const
Definition: LineReader.cc:165
LineReader::initialized
bool initialized() const
LineReader::setInputHistoryLength
virtual void setInputHistoryLength(std::size_t length)
Definition: LineReader.cc:140
LineReader::initialized_
bool initialized_
Flag indicating whether LineReader is initialized.
Definition: LineReader.hh:95
LineReader::outputStream
virtual std::ostream & outputStream()
Definition: LineReader.cc:102
LineReader
Definition: LineReader.hh:52
LineReader::initialize
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)=0
LineReader::iStream_
std::istream & iStream_
The input stream.
Definition: LineReader.hh:107
LineReader::saveInputHistoryToFile
virtual bool saveInputHistoryToFile() const
LineReader::confirmation
bool confirmation(std::string question, char defaultAnswer='n', char yesChar='y', char noChar='n')
Definition: LineReader.cc:79
LineReader::historyFile_
std::ofstream * historyFile_
The output stream to write the command history to.
Definition: LineReader.hh:103
LineReader.icc