• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 25456501308

06 May 2026 07:29PM UTC coverage: 55.935% (-0.8%) from 56.7%
25456501308

Pull #2011

github

web-flow
Merge 7ad262960 into 7054e405e
Pull Request #2011: Single-file CI and code style adoption

14612 of 26123 relevant lines covered (55.94%)

462107.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/trick_source/sim_services/MonteCarlo/MonteVarFile.cpp
1
#include <sstream>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <errno.h>
5
#include <limits>
6

7
#include "trick/MonteVarFile.hh"
8
#include "trick/message_proto.h"
9
#include "trick/exec_proto.h"
10

11
Trick::MonteVarFile::MonteVarFile(std::string in_name, std::string in_file_name, unsigned int in_column, std::string in_unit) : input_file_stream(NULL) {
5✔
12
    name = in_name;
5✔
13
    column = in_column;
5✔
14
    unit = in_unit;
5✔
15

16
    set_file_name(in_file_name);
6✔
17
}
6✔
18

19
Trick::MonteVarFile::~MonteVarFile() {
4✔
20
    delete input_file_stream;
4✔
21
}
4✔
22

23
// Composite the various properties of this MonteVarFile.
24
std::string Trick::MonteVarFile::describe_variable() {
×
25
    std::stringstream ss;
×
26

27
    ss << "#NAME:\t\t" << name << "\n"
×
28
       << "#TYPE:\t\tFILE\n"
29
       << "#UNIT:\t\t" << unit << "\n"
×
30
       << "#FILE:\t\t" << file_name << "\n"
×
31
       << "#COLUMN:\t" << column << "\n";
×
32

33
    return ss.str();
×
34
}
×
35

36
std::string Trick::MonteVarFile::get_next_value() {
6✔
37
    // Open the file and seek to the previous position.
38
    input_file_stream->open(file_name.c_str(), std::ifstream::in);
6✔
39
    input_file_stream->seekg(stream_position);
6✔
40

41
    if (input_file_stream->good()) {
6✔
42
        std::string line;
6✔
43
        // Skip the comments and empty lines in the data file.
44
        do {
45
            std::getline(*input_file_stream, line);
10✔
46

47
            if(input_file_stream->eof()) {
10✔
48
                if (line.empty()) {
3✔
49
                    input_file_stream->close();
2✔
50
                    return "EOF";
4✔
51
                } else {
52
                    input_file_stream->seekg(0, input_file_stream->end);
1✔
53
                }
54
            }
55
        }
56
        while(line[0] == '#' || line[0] == '\0');
8✔
57

58
        // Store the current stream position and close the file.
59
        stream_position = input_file_stream->tellg();
4✔
60
        input_file_stream->close();
4✔
61

62
        // Count the number of columns in the input file.
63
        char *token;
64
        unsigned int ntokens = 0;
4✔
65
        char* temp_str = strdup(line.c_str());
4✔
66
        token = strtok(temp_str, " \t");
4✔
67
        while (token != NULL) {
20✔
68
            token = strtok(NULL, " \t");
16✔
69
            ntokens++;
16✔
70
        }
71

72
        // Verify the input column number is valid.
73
        if ((column == 0) || (column > ntokens)) {
4✔
74
            char string[100];
75
            snprintf(string, sizeof(string), "Trick:MonteVarFile An invalid column number %u, valid column numbers are 1 - %u", column, ntokens);
1✔
76
            exec_terminate_with_return(-1, __FILE__, __LINE__, string);
1✔
77
        }
78

79
        // Get the next value.
80
        if(temp_str) {
3✔
81
            free(temp_str);
3✔
82
        }
83
        temp_str = strdup(line.c_str());
3✔
84
        token = strtok(temp_str, " \t");
3✔
85

86
        for(unsigned int i = 1; i < column; i++) {
6✔
87
            // Iterate through each token in the temp_str.
88
            if(token != NULL)
3✔
89
                token = strtok(NULL, " \t");
3✔
90
        }
91

92
        // Return the value as a string.
93
        value = token;
3✔
94
        std::stringstream ss;
3✔
95

96
        if(unit.empty())
3✔
97
            ss << name << " = " << token;
3✔
98
        else
99
            ss << name << " = " << "trick.attach_units(\"" << unit << "\", " << token << ")";
×
100
        if(temp_str) {
3✔
101
            free(temp_str);
3✔
102
            temp_str = nullptr;
3✔
103
        }
104
        return ss.str();
3✔
105

106
    }
6✔
107
    char string[100];
108
    snprintf(string, sizeof(string), "Trick:MonteVarFile the input file \"%s\" is not open for reading", file_name.c_str());
×
109
    exec_terminate_with_return(-1, __FILE__, __LINE__, string);
×
110

111
    return NULL;
×
112
}
113

114
void Trick::MonteVarFile::set_file_name(std::string in_file_name) {
5✔
115
    delete input_file_stream;
5✔
116

117
    input_file_stream = new std::ifstream(in_file_name.c_str(), std::ifstream::in);
5✔
118
    if (input_file_stream->fail()) {
5✔
119
        std::stringstream string_stream;
1✔
120

121
        string_stream << "Error: " << strerror(errno) << std::endl
1✔
122
                      << "       Trick:MonteVarFile input file \"" << in_file_name << "\" failed to open";
1✔
123

124
        exec_terminate_with_return(-1, __FILE__, __LINE__, string_stream.str().c_str());
2✔
125
    }
1✔
126
    input_file_stream->close();
4✔
127
    file_name = in_file_name;
4✔
128
}
4✔
129

130
void Trick::MonteVarFile::set_column(unsigned int in_column) {
×
131
    column = in_column;
×
132
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc