2
* Copyright (C) 2011 Tommi Maekitalo
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* As a special exception, you may use this file as part of a free
10
* software library without restriction. Specifically, if other files
11
* instantiate templates or use macros or inline functions from this
12
* file, or you compile this file and link it with other files to
13
* produce an executable, this file does not by itself cause the
14
* resulting executable to be covered by the GNU General Public
15
* License. This exception does not however invalidate any other
16
* reasons why the executable file might be covered by the GNU Library
17
* General Public License.
19
* This library is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22
* Lesser General Public License for more details.
24
* You should have received a copy of the GNU Lesser General Public
25
* License along with this library; if not, write to the Free Software
26
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29
#ifndef CXXTOOLS_STDIN_H
30
#define CXXTOOLS_STDIN_H
35
#include <cxxtools/arg.h>
40
* Helper class for redirecting input to stdin or file using command line switch.
42
* Using this class it is easy to provide a command line switch to the user
43
* to read input from a file or stdin.
47
* int main(int argc, char* argv[])
49
* cxxtools::ArgIn in(argc, argv, 'i');
53
* std::cout << d*2 << std::endl;
58
* This program reads data from stdin or a file, when provided using the
59
* -i option and doubles the value
62
* int main(int argc, char* argv[])
64
* cxxtools::ArgIn in(argc, argv);
68
* std::cout << d*2 << std::endl;
73
* This program reads data from stdin or a file, when provided as a
74
* parameter and doubles the value. This is similar to the <> operator
78
class ArgIn : public std::istream
82
void doInit(const Arg<const char*>& ifile)
91
buf = std::cin.rdbuf();
96
/// Constructor processes short options like: -i something.
97
ArgIn(int& argc, char* argv[], char option)
100
Arg<const char*> ifile(argc, argv, option);
104
/// Constructor processes long options like: --input something".
105
ArgIn(int& argc, char* argv[], const char* option)
108
Arg<const char*> ifile(argc, argv, option);
112
/// Constructor processes normal parameters.
113
ArgIn(int& argc, char* argv[])
116
Arg<const char*> ifile(argc, argv);
120
/// returns true, if the input is read from a file.
121
bool redirected() const
123
return rdbuf() != std::cin.rdbuf();
128
#endif // CXXTOOLS_STDIN_H