https://mooseframework.inl.gov
FileInputStream.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "FileInputStream.h"
11 
12 #include "MooseError.h"
13 
14 #include <fstream>
15 
16 FileInputStream::FileInputStream(const std::string & filename) : InputStream(), _filename(filename)
17 {
18 }
19 
20 std::shared_ptr<std::istream>
22 {
23  std::shared_ptr<std::istream> stream = std::make_unique<std::ifstream>();
24  addSharedStream(stream);
25 
26  auto & in = *static_cast<std::ifstream *>(stream.get());
27  in.open(_filename.c_str(), std::ios::in | std::ios::binary);
28  if (in.fail())
29  mooseError("Unable to open file ", std::filesystem::absolute(_filename));
30 
31  return stream;
32 }
33 
34 std::optional<std::filesystem::path>
36 {
37  return _filename;
38 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
void addSharedStream(const std::weak_ptr< std::istream > stream) const
Internal method to be called by derived classes to add a shared stream to _shared_streams (so that it...
Definition: InputStream.C:21
Helper class that hands out input streams to an underlying, managed stream of arbitrary type...
Definition: InputStream.h:22
virtual std::optional< std::filesystem::path > getFilename() const override final
Gets the underlying filename, if any.
const std::filesystem::path _filename
The name of the file.
FileInputStream(const std::string &filename)
virtual std::shared_ptr< std::istream > get() const override final
Gets an input stream to the underlying stream.