2015-02-26 15:02:31 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-05-22 19:07:25 +00:00
|
|
|
#include <errno.h>
|
2015-02-26 15:02:31 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2015-06-01 15:28:29 +00:00
|
|
|
#include "trick/MonteVarFile.hh"
|
|
|
|
#include "trick/message_proto.h"
|
|
|
|
#include "trick/exec_proto.h"
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
Trick::MonteVarFile::MonteVarFile(std::string in_name, std::string in_file_name, unsigned int in_column, std::string in_unit) : input_file_stream(NULL) {
|
2017-10-31 18:17:14 +00:00
|
|
|
name = in_name;
|
|
|
|
column = in_column;
|
|
|
|
unit = in_unit;
|
2016-11-08 09:25:07 +00:00
|
|
|
|
2016-10-03 19:57:11 +00:00
|
|
|
set_file_name(in_file_name);
|
|
|
|
}
|
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
Trick::MonteVarFile::~MonteVarFile() {
|
2016-10-03 19:57:11 +00:00
|
|
|
delete input_file_stream;
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 15:30:28 +00:00
|
|
|
// Composite the various properties of this MonteVarFile.
|
2017-10-16 19:03:48 +00:00
|
|
|
std::string Trick::MonteVarFile::describe_variable() {
|
2017-10-10 15:30:28 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
|
2017-10-31 18:17:14 +00:00
|
|
|
ss << "#NAME:\t\t" << name << "\n"
|
2017-10-12 15:00:54 +00:00
|
|
|
<< "#TYPE:\t\tFILE\n"
|
2017-10-31 18:17:14 +00:00
|
|
|
<< "#UNIT:\t\t" << unit << "\n"
|
|
|
|
<< "#FILE:\t\t" << file_name << "\n"
|
|
|
|
<< "#COLUMN:\t" << column << "\n";
|
2017-10-10 15:30:28 +00:00
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
std::string Trick::MonteVarFile::get_next_value() {
|
2017-10-31 18:17:14 +00:00
|
|
|
// Open the file and seek to the previous position.
|
|
|
|
input_file_stream->open(file_name.c_str(), std::ifstream::in);
|
|
|
|
input_file_stream->seekg(stream_position);
|
2017-10-27 19:13:28 +00:00
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
if (input_file_stream->good()) {
|
2017-10-16 17:56:45 +00:00
|
|
|
std::string line;
|
2017-10-12 15:00:54 +00:00
|
|
|
// Skip the comments and empty lines in the data file.
|
2017-10-16 19:03:48 +00:00
|
|
|
do {
|
2017-10-16 17:56:45 +00:00
|
|
|
std::getline(*input_file_stream, line);
|
2017-10-12 15:00:54 +00:00
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
if(input_file_stream->eof()) {
|
2017-12-13 18:01:31 +00:00
|
|
|
if (line.empty()) {
|
|
|
|
input_file_stream->close();
|
|
|
|
return "EOF";
|
|
|
|
} else {
|
|
|
|
input_file_stream->seekg(0, input_file_stream->end);
|
|
|
|
}
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
2017-10-12 15:00:54 +00:00
|
|
|
}
|
2017-10-16 17:56:45 +00:00
|
|
|
while(line[0] == '#' || line[0] == '\0');
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2017-10-27 19:13:28 +00:00
|
|
|
// Store the current stream position and close the file.
|
2017-10-31 18:17:14 +00:00
|
|
|
stream_position = input_file_stream->tellg();
|
2017-10-27 19:13:28 +00:00
|
|
|
input_file_stream->close();
|
|
|
|
|
2017-10-12 15:00:54 +00:00
|
|
|
// Count the number of columns in the input file.
|
|
|
|
char *token;
|
2015-02-26 15:02:31 +00:00
|
|
|
unsigned int ntokens = 0;
|
2017-10-16 17:56:45 +00:00
|
|
|
char* temp_str = strdup(line.c_str());
|
2017-10-12 15:00:54 +00:00
|
|
|
token = strtok(temp_str, " \t");
|
2017-10-16 19:03:48 +00:00
|
|
|
while (token != NULL) {
|
2017-10-12 15:00:54 +00:00
|
|
|
token = strtok(NULL, " \t");
|
2015-02-26 15:02:31 +00:00
|
|
|
ntokens++;
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:00:54 +00:00
|
|
|
// Verify the input column number is valid.
|
2017-10-16 19:03:48 +00:00
|
|
|
if ((column == 0) || (column > ntokens)) {
|
2015-02-26 15:02:31 +00:00
|
|
|
char string[100];
|
2022-11-15 21:00:05 +00:00
|
|
|
snprintf(string, sizeof(string), "Trick:MonteVarFile An invalid column number %u, valid column numbers are 1 - %u", column, ntokens);
|
2015-02-26 15:02:31 +00:00
|
|
|
exec_terminate_with_return(-1, __FILE__, __LINE__, string);
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:00:54 +00:00
|
|
|
// Get the next value.
|
2018-11-06 17:23:12 +00:00
|
|
|
if(temp_str) {
|
|
|
|
free(temp_str);
|
|
|
|
}
|
2017-10-16 17:56:45 +00:00
|
|
|
temp_str = strdup(line.c_str());
|
2017-10-12 15:00:54 +00:00
|
|
|
token = strtok(temp_str, " \t");
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
for(unsigned int i = 1; i < column; i++) {
|
2017-10-12 15:00:54 +00:00
|
|
|
// Iterate through each token in the temp_str.
|
|
|
|
if(token != NULL)
|
|
|
|
token = strtok(NULL, " \t");
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
2017-10-12 15:00:54 +00:00
|
|
|
|
|
|
|
// Return the value as a string.
|
2017-10-31 18:17:14 +00:00
|
|
|
value = token;
|
2017-10-12 15:00:54 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
if(unit.empty())
|
2017-10-31 18:17:14 +00:00
|
|
|
ss << name << " = " << token;
|
2017-10-12 15:00:54 +00:00
|
|
|
else
|
2017-10-31 18:17:14 +00:00
|
|
|
ss << name << " = " << "trick.attach_units(\"" << unit << "\", " << token << ")";
|
2018-11-06 17:23:12 +00:00
|
|
|
if(temp_str) {
|
|
|
|
free(temp_str);
|
|
|
|
temp_str = nullptr;
|
|
|
|
}
|
2018-10-22 20:11:29 +00:00
|
|
|
return ss.str();
|
2018-11-06 17:23:12 +00:00
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
char string[100];
|
2022-11-15 21:00:05 +00:00
|
|
|
snprintf(string, sizeof(string), "Trick:MonteVarFile the input file \"%s\" is not open for reading", file_name.c_str());
|
2015-02-26 15:02:31 +00:00
|
|
|
exec_terminate_with_return(-1, __FILE__, __LINE__, string);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-03 19:57:11 +00:00
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
void Trick::MonteVarFile::set_file_name(std::string in_file_name) {
|
2016-10-03 19:57:11 +00:00
|
|
|
delete input_file_stream;
|
|
|
|
|
2017-11-22 18:51:34 +00:00
|
|
|
input_file_stream = new std::ifstream(in_file_name.c_str(), std::ifstream::in);
|
2017-10-16 19:03:48 +00:00
|
|
|
if (input_file_stream->fail()) {
|
2016-10-03 19:57:11 +00:00
|
|
|
std::stringstream string_stream;
|
2017-05-22 18:47:25 +00:00
|
|
|
|
|
|
|
string_stream << "Error: " << strerror(errno) << std::endl
|
2017-10-12 15:00:54 +00:00
|
|
|
<< " Trick:MonteVarFile input file \"" << in_file_name << "\" failed to open";
|
2017-05-22 18:47:25 +00:00
|
|
|
|
2016-10-03 19:57:11 +00:00
|
|
|
exec_terminate_with_return(-1, __FILE__, __LINE__, string_stream.str().c_str());
|
|
|
|
}
|
2017-11-22 18:51:34 +00:00
|
|
|
input_file_stream->close();
|
2017-10-31 18:17:14 +00:00
|
|
|
file_name = in_file_name;
|
2016-10-03 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 19:03:48 +00:00
|
|
|
void Trick::MonteVarFile::set_column(unsigned int in_column) {
|
2017-10-31 18:17:14 +00:00
|
|
|
column = in_column;
|
2016-10-03 19:57:11 +00:00
|
|
|
}
|