Program Listing for File string_utils.hpp

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

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <sstream>
#include <string>
#include <vector>

#include "rcppsw/rcppsw.hpp"

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

/*******************************************************************************
 * Non-Member Functions
 ******************************************************************************/
template <typename T>
bool parse_values(std::istream& is, size_t n_fields, T* buf, char delim = '\n') {
  std::vector<std::string> s(n_fields);
  size_t i = 0;

  /*
   * Get actual # fields present in stream
   */
  while (i < n_fields && std::getline(is, s[i], delim)) {
    i++;
  } /* while() */

  if (i == n_fields) {
    is.clear(); /* the istream was read completely */

    /* parse fields */
    for (i = 0; i < n_fields; i++) {
      std::istringstream iss(s[i]);
      iss >> buf[i];
    }
    return true;
  }
  return false;
}

void tokenize(std::string const &str,
              const char* delim,
              std::vector<std::string>& out);

} /* namespace rcppsw::utils */