Program Listing for File file_parser.hpp

Return to documentation for file (rcppsw/utils/file_parser.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

#include "rcppsw/rcppsw.hpp"
#include "rcppsw/utils/line_parser.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace rcppsw::utils {

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
template <typename T>
class file_parser {
 public:
  explicit file_parser(const std::string& fname) : m_fname(fname) {}

  std::vector<T>* parse_file(char delim) {
    std::vector<T>* tokens_set = new std::vector<T>;
    std::string line;
    std::ifstream infile(m_fname);
    line_parser parser(delim);
    while (std::getline(infile, line)) {
      tokens_set->push_back(parser.parse(line));
    } /* while() */

    return tokens_set;
  }

 private:
  /* clang-format off */
  const std::string& m_fname;
  /* clang-format on */
};

} /* namespace rcppsw::utils */