Leonardus
Loading...
Searching...
No Matches
linesource.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Libray
20#include <iostream>
21#include <fstream>
22#include <string>
23
24// Inc HAA
25#include "error.h"
26
27// Inc Medium
28#include "counter.h"
29
30// IDEA: a class for reading a file and another class for reading from stdin. A third to read interactively?
31
35class LineSource : public Counter<LineSource> {
36 std::string fn_;
37 std::ifstream * inFile_{};
38 bool fromcin_;
40public:
42 explicit LineSource( const std::string & p_fn ) : fn_(p_fn), fromcin_(p_fn=="-") {}
43
45 LineSource( const LineSource& ) = delete;
46
48 LineSource( LineSource&& ) = delete;
49
51 LineSource& operator=( const LineSource& ) = delete;
52
55
57 ~LineSource() { delete inFile_; }
58
60 bool getLine( std::string & p_line ) {
61 if( fromcin_ )
62 return static_cast<bool>( std::getline( std::cin, p_line ) );
63 if( inFile_ == nullptr ) {
64 inFile_ = new (std::nothrow) std::ifstream( fn_, std::ios::in );
65 if( inFile_ == nullptr )
66 panicExit(); /* LCOV_EXCL_LINE */
67 }
68 if( !inFile_->is_open() )
69 inErrExit( cmdline, "can not open file name '" + fn_ + "'" );
70 return static_cast<bool>( std::getline( *inFile_, p_line ) );
71 }
72};
Counter base class.
Definition counter.h:27
Abstraction of input channel.
Definition linesource.h:35
bool fromcin_
Do we read from standard in?
Definition linesource.h:38
std::string fn_
File name.
Definition linesource.h:36
LineSource & operator=(const LineSource &)=delete
Delete copy assignement.
LineSource & operator=(LineSource &&)=delete
Delete move assignement.
bool getLine(std::string &p_line)
Wrapper around getline() to hide input stream.
Definition linesource.h:60
LineSource(const std::string &p_fn)
Ctor.
Definition linesource.h:42
std::ifstream * inFile_
Open, if we read from a file.
Definition linesource.h:37
LineSource(const LineSource &)=delete
Delete the copy ctor.
LineSource(LineSource &&)=delete
Delete the move ctor.
~LineSource()
Dtor.
Definition linesource.h:57
The class Counter.
void panicExit()
Panic exit( EC_PANIC ).
Definition error.cpp:106
void inErrExit(InError p_err, const std::string &p_details, const std::source_location p_location)
Interpreter error message to interpreter cout_ and exit( EC_INTERPRETER / EC_CMDLINE ).
Definition error.cpp:74
Error handling.
@ cmdline
command line usage error
Definition error.h:43