Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:54:06

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/io/FileOrConsole.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <fstream>
0010 #include <iostream>
0011 #include <string>
0012 
0013 #include "corecel/Assert.hh"
0014 #include "corecel/Macros.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Construct an input from an existing file, or stdin if the filename is "-".
0021  */
0022 class FileOrStdin
0023 {
0024   public:
0025     // Construct with a filename
0026     explicit inline FileOrStdin(std::string filename);
0027 
0028     //! Implicitly cast to the opened stream
0029     operator std::istream&() { return inf_.is_open() ? inf_ : std::cin; }
0030 
0031     //! Get the filename or renamed placeholder
0032     std::string const& filename() const { return filename_; }
0033 
0034   private:
0035     std::string filename_;
0036     std::ifstream inf_;
0037 };
0038 
0039 //---------------------------------------------------------------------------//
0040 /*!
0041  * Construct an output to a new file, or stdout if the filename is "-".
0042  */
0043 class FileOrStdout
0044 {
0045   public:
0046     // Construct with a filename
0047     explicit inline FileOrStdout(std::string filename);
0048 
0049     //! Implicitly cast to the opened stream
0050     operator std::ostream&() { return outf_.is_open() ? outf_ : std::cout; }
0051 
0052     //! Get the filename or renamed placeholder
0053     std::string const& filename() const { return filename_; }
0054 
0055   private:
0056     std::string filename_;
0057     std::ofstream outf_;
0058 };
0059 
0060 //---------------------------------------------------------------------------//
0061 // INLINE DEFINITIONS
0062 //---------------------------------------------------------------------------//
0063 /*!
0064  * Construct with filename.
0065  */
0066 FileOrStdin::FileOrStdin(std::string filename) : filename_{std::move(filename)}
0067 {
0068     CELER_VALIDATE(!filename_.empty(),
0069                    << "empty filename is not valid for input");
0070     if (filename_ == "-")
0071     {
0072         filename_ = "<stdin>";
0073         return;
0074     }
0075     // Open the specified file
0076     inf_.open(filename_);
0077     CELER_VALIDATE(inf_, << "failed to open '" << filename_ << "'");
0078 }
0079 
0080 //---------------------------------------------------------------------------//
0081 /*!
0082  * Construct with filename.
0083  */
0084 FileOrStdout::FileOrStdout(std::string filename)
0085     : filename_{std::move(filename)}
0086 {
0087     CELER_VALIDATE(!filename_.empty(),
0088                    << "empty filename is not valid for output");
0089     if (filename_ == "-")
0090     {
0091         filename_ = "<stdout>";
0092         return;
0093     }
0094 
0095     // Open the specified file
0096     outf_.open(filename_);
0097     CELER_VALIDATE(outf_, << "failed to open '" << filename_ << "'");
0098 }
0099 
0100 //---------------------------------------------------------------------------//
0101 }  // namespace celeritas