Skip to content

File filereader.h

File List > code_source > templat > include > TempLat > parameters > filereader.h

Go to the documentation of this file

#ifndef TEMPLAT_PARAMETERS_FILEREADER_H
#define TEMPLAT_PARAMETERS_FILEREADER_H

/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
   Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
   Released under the MIT license, see LICENSE.md. */

// File info: Main contributor(s): Adrien Florio, Year: 2019

#include <fstream>
#include "TempLat/util/exception.h"
#include "TempLat/util/stringtrimmer.h"

namespace TempLat
{
  MakeException(FileReaderProblemInputFile);

  class FileReader
  {
  public:
    // Put public methods here. These should change very little over time.
    FileReader() = default;
    const std::string &operator[](int i) const { return vec[i]; }
    size_t size() const { return vec.size(); }
    void operator()(const std::string &str, char comment = '#')
    {
      std::ifstream t;
      t.open(str);
      std::string tmp;
      if (t.good()) {
        while (getline(t, tmp)) {
          StringTrimmer::trim(tmp);
          if (tmp != "" && tmp[0] != comment) vec.push_back(tmp);
        }
      } else
        throw(FileReaderProblemInputFile("There was a problem opening the input file at " + str + ". Abort."));
    }

  private:
    /* Put all member variables and private methods here. These may change arbitrarily. */

    std::vector<std::string> vec;
  };
} // namespace TempLat

#endif