Skip to content
Snippets Groups Projects
Commit 2e23f636 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Create a module for parsing functions

parent e5ebe973
No related branches found
No related tags found
No related merge requests found
/*! \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;
}
/*! \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_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment