Merge pull request #488 from nasa/NonNumericValues

Non-numerical values can now be used in Monte Carlo data files.
This commit is contained in:
Christopher LaChance 2017-10-17 08:40:29 -05:00 committed by GitHub
commit c8f916e818
3 changed files with 41 additions and 48 deletions

View File

@ -73,9 +73,6 @@ namespace Trick {
/** The input file stream. */
std::ifstream *input_file_stream; /**< \n trick_units(--) */
/** A character buffer. */
char *buffer; /**< \n trick_units(--) */
public:
/**
* Constructs a MonteVarFile with the specified name, file name, column, and units.

View File

@ -8,25 +8,20 @@
#include "trick/message_proto.h"
#include "trick/exec_proto.h"
Trick::MonteVarFile::MonteVarFile(std::string in_name, std::string in_file_name, unsigned int in_column, std::string in_unit) :
input_file_stream(NULL)
{
Trick::MonteVarFile::MonteVarFile(std::string in_name, std::string in_file_name, unsigned int in_column, std::string in_unit) : input_file_stream(NULL) {
this->name = in_name;
this->column = in_column;
this->unit = in_unit;
set_file_name(in_file_name);
buffer = new char[4096];
}
Trick::MonteVarFile::~MonteVarFile() {
delete input_file_stream;
delete buffer;
}
// Composite the various properties of this MonteVarFile.
std::string Trick::MonteVarFile::describe_variable()
{
std::string Trick::MonteVarFile::describe_variable() {
std::stringstream ss;
ss << "#NAME:\t\t" << this->name << "\n"
@ -40,54 +35,55 @@ std::string Trick::MonteVarFile::describe_variable()
std::string Trick::MonteVarFile::get_next_value() {
if (input_file_stream->good()) {
double file_value;
char *current_position = buffer;
std::string line;
// Skip the comments and empty lines in the data file.
do {
input_file_stream->getline(buffer, 4096);
file_value = strtold(buffer, &current_position);
std::getline(*input_file_stream, line);
if(input_file_stream->eof()) {
input_file_stream->close();
return "EOF";
}
} while (file_value == 0 && current_position == buffer && input_file_stream->good());
}
while(line[0] == '#' || line[0] == '\0');
// Count the number of columns in the input file
// Count the number of columns in the input file.
char *token;
unsigned int ntokens = 0;
char temp_str[4096];
strcpy(temp_str, buffer) ;
char* temp_str = strdup(line.c_str());
token = strtok(temp_str, " \t");
while (token != NULL) {
token = strtok(NULL, " \t");
ntokens++;
}
// Verify the input column number is valid
// Verify the input column number is valid.
if ((column == 0) || (column > ntokens)) {
char string[100];
sprintf(string, "Trick:MonteVarFile An invalid column number %d, valid column numbers are 1 - %d", column, ntokens);
exec_terminate_with_return(-1, __FILE__, __LINE__, string);
}
if (current_position != buffer) {
for (unsigned int i = 1; i < column; ++i) {
file_value = strtold(current_position, &current_position);
// Get the next value.
temp_str = strdup(line.c_str());
token = strtok(temp_str, " \t");
for(unsigned int i = 1; i < column; i++) {
// Iterate through each token in the temp_str.
if(token != NULL)
token = strtok(NULL, " \t");
}
std::stringstream string_stream;
string_stream.precision(std::numeric_limits<double>::digits10);
string_stream << file_value ;
value = string_stream.str();
string_stream.str("");
if (unit.empty()) {
string_stream << name << " = " << file_value ;
}
else {
string_stream << name << " = " << "trick.attach_units(\"" << unit << "\", " << file_value
<< ")";
}
return string_stream.str() ;
}
// Return the value as a string.
this->value = token;
std::stringstream ss;
if(unit.empty())
ss << this->name << " = " << token;
else
ss << this->name << " = " << "trick.attach_units(\"" << unit << "\", " << token << ")";
return ss.str();
}
char string[100];
sprintf(string, "Trick:MonteVarFile the input file \"%s\" is not open for reading", file_name.c_str());
@ -114,4 +110,3 @@ void Trick::MonteVarFile::set_file_name(std::string in_file_name) {
void Trick::MonteVarFile::set_column(unsigned int in_column) {
this->column = in_column;
}

View File

@ -14,9 +14,10 @@ std::string Trick::MonteVarFixed::describe_variable()
{
std::stringstream ss;
ss << "#NAME:\t" << this->name << "\n"
<< "#TYPE:\tFIXED\n"
<< "#UNIT:\t" << this->unit << "\n";
ss << "#NAME:\t\t" << this->name << "\n"
<< "#TYPE:\t\tFIXED\n"
<< "#UNIT:\t\t" << this->unit << "\n"
<< "#VALUE:\t\t" << this->value << "\n";
return ss.str();
}