Commit 2e23f636 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Create a module for parsing functions

parent e5ebe973
Loading
Loading
Loading
Loading

src/Parsers.cpp

0 → 100644
+26 −0
Original line number Diff line number Diff line
/*! \file Parsers.cpp
 */

#include <fstream>
#include <string>
#include "include/List.h"
#include "include/Parsers.h"

std::string *load_file(std::string file_name, int *count = 0) {
  std::fstream input_file(file_name.c_str(), std::ios::in);
  List<std::string> file_lines = List<std::string>();
  std::string line;
  if (input_file.is_open()) {
    getline(input_file, line);
    file_lines.set(0, line);
    while (getline(input_file, line)) {
      file_lines.append(line);
    }
    input_file.close();
  } else {
	  throw FILE_NOT_FOUND_ERROR;
  }
  std::string *array_lines = file_lines.to_array();
  if (count != 0) *count = file_lines.length();
  return array_lines;
}

src/include/Parsers.h

0 → 100644
+34 −0
Original line number Diff line number Diff line
/*! \file Parsers.h
 */

#ifndef INCLUDE_PARSERS_H_
#define INCLUDE_PARSERS_H_

#ifndef FILE_NOT_FOUND_ERROR
//! Error code if a file is not found.
#define FILE_NOT_FOUND_ERROR 21
#endif

/*! \brief Load a text file as a sequence of strings in memory.
 *
 * The configuration of the field expansion code in FORTRAN uses
 * shared memory access and file I/O operations managed by different
 * functions. Although this approach could be theoretically replicated,
 * it is more convenient to handle input and output to distinct files
 * using specific functions. load_file() helps in the task of handling
 * input such as configuration files or text data structures that need
 * to be loaded entirely. The function performs a line-by line scan of
 * the input file and returns an array of strings that can be later
 * parsed and ingested by the concerned code blocks. An optional pointer
 * to integer allows the function to keep track of the number of file
 * lines that were read, if needed.
 *
 * \param file_name: `string` The path of the file to be read.
 * \param count: `int*` Pointer to an integer recording the number of
 * read lines [OPTIONAL, default=NULL].
 * \return array_lines `string*` An array of strings, one for each input
 * file line.
 */
std::string *load_file(std::string file_name, int *count);

#endif /* INCLUDE_PARSERS_H_ */