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

nasa / trick / 16920142303

12 Aug 2025 08:29PM UTC coverage: 55.882% (-0.04%) from 55.926%
16920142303

Pull #1940

github

web-flow
Merge 15f142fae into 1d9fa0795
Pull Request #1940: Updated the TV app to have a shorter timeout when attempting to connect to the var server.

12350 of 22100 relevant lines covered (55.88%)

253670.34 hits per line

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

83.59
/trick_source/sim_services/DataRecord/DRAscii.cpp
1
 
2
/*
3
PURPOSE:
4
    (Data record in ascii format.)
5
PROGRAMMERS:
6
     (((Robert W. Bailey) (LinCom Corp) (3/96) (SES upgrades)
7
     ((Alex Lin) (NASA) (April 2009) (--) (c++ port)))
8
*/
9

10
#include <iostream>
11
#include <stdlib.h>
12
#include <string.h>
13

14
#include "trick/DRAscii.hh"
15
#include "trick/command_line_protos.h"
16
#include "trick/memorymanager_c_intf.h"
17
#include "trick/message_proto.h"
18
#include "trick/message_type.h"
19
#include "trick/bitfield_proto.h"
20

21
Trick::DRAscii::DRAscii( std::string in_name, Trick::DR_Type dr_type ) : Trick::DataRecordGroup( in_name, dr_type ) {
24✔
22

23
    ascii_float_format = "%20.8g" ;
24✔
24
    ascii_double_format = "%20.16g" ;
24✔
25
    delimiter = ",";
24✔
26
    register_group_with_mm(this, "Trick::DRAscii") ;
24✔
27
}
24✔
28

29
int Trick::DRAscii::format_specific_header( std::fstream & out_st ) {
30✔
30
    out_st << " is in ASCII" << std::endl ;
30✔
31
    return(0) ;
30✔
32
}
33

34
/**
35
@details
36
-# If the #delimiter is not empty and not a comma then set the file extension to ".txt"
37
-# Else set the file extension to ".csv"
38
-# Allocate enough memory to hold #record_size of records in memory
39
-# Open the log file
40
   -# Return an error if the open failed.
41
-# Write out the title line of the log file.  The title line includes the names of
42
   recorded variables and the units of measurement separated by the #delimiter
43
-# Declare the recording group to the memory manager so that the group can be checkpointed
44
   and restored
45
*/
46
int Trick::DRAscii::format_specific_init() {
30✔
47

48
    unsigned int jj ;
49
    std::streampos before_write;
30✔
50

51
    /* Store log information in csv/txt file */
52
    if ( ! delimiter.empty()  &&  delimiter.compare(",") != 0 ) {
30✔
53
        file_name.append(".txt");
×
54
    } else {
55
        file_name.append(".csv");
30✔
56
    }
57

58
    /* Calculate a "worst case" for space used for 1 record. */
59
    writer_buff_size = record_size * rec_buffer.size();
30✔
60
    writer_buff = (char *)calloc(1 , writer_buff_size) ;
30✔
61

62
    /* This loop touches all of the memory locations in the allocation forcing the
63
       system to actually do the allocation */
64
    for ( jj= 0 ; jj < record_size * rec_buffer.size() ; jj += 1024 ) {
60✔
65
        writer_buff[jj] = 1 ;
30✔
66
    }
67
    writer_buff[record_size * rec_buffer.size() - 1] = 1 ;
30✔
68

69
    out_stream.open(file_name.c_str(), std::fstream::out | std::fstream::app ) ;
30✔
70
    if ( !out_stream || !out_stream.good() ) {
30✔
71
        message_publish(MSG_ERROR, "Can't open Data Record file %s.\n", file_name.c_str()) ;
×
72
        record = false ;
×
73
        return -1 ;
×
74
    }
75
    before_write = out_stream.tellp();
30✔
76
    // Write out the title line of the recording file
77
    /* Start with the 1st item in the buffer which should be "sys.exec.out.time" */
78
    out_stream << rec_buffer[0]->ref->reference ;
30✔
79
    if ( rec_buffer[0]->ref->attr->units != NULL ) {
30✔
80
        if ( rec_buffer[0]->ref->attr->mods & TRICK_MODS_UNITSDASHDASH ) {
30✔
81
            out_stream << " {--}" ;
×
82
        } else {
83
            out_stream << " {" << rec_buffer[0]->ref->attr->units << "}" ;
30✔
84
        }
85
    }
86
    
87
    /* Write out specified recorded parameters */
88
    for (jj = 1; jj < rec_buffer.size() ; jj++) {
200✔
89
        out_stream << delimiter << rec_buffer[jj]->ref->reference ;
170✔
90

91
        if ( rec_buffer[jj]->ref->attr->units != NULL ) {
170✔
92
            if ( rec_buffer[jj]->ref->attr->mods & TRICK_MODS_UNITSDASHDASH ) {
170✔
93
                out_stream << " {--}" ;
×
94
            } else {
95
                out_stream << " {" << rec_buffer[jj]->ref->attr->units << "}" ;
170✔
96
            }
97
        }
98
    }
99
    out_stream << std::endl ;
30✔
100
    total_bytes_written += out_stream.tellp() - before_write;
30✔
101
    return(0) ;
30✔
102
}
103

104
/**
105
@details
106
-# While there is data in memory that has not been written to disk
107
   -# Write out the time to a temporary #writer_buff
108
   -# Write out each of the other parameter values preceded by the delimiter to the temporary #writer_buff
109
   -# Write #writer_buff to the output file
110
-# Flush the output file stream
111
-# Return the number of bytes written
112
*/
113
int Trick::DRAscii::format_specific_write_data(unsigned int writer_offset) {
49,047✔
114
    unsigned int ii ;
115
    char *buf;
116

117
    buf = writer_buff ;
49,047✔
118

119
    /* Write out the first parameters (time) */
120
    copy_data_ascii_item(rec_buffer[0], writer_offset, buf );
49,047✔
121
    buf += strlen(buf);
49,047✔
122

123
    /* Write out all other parameters */
124
    for (ii = 1; ii < rec_buffer.size() ; ii++) {
370,191✔
125
        strcat(buf, delimiter.c_str() );
321,144✔
126
        buf += delimiter.length() ;
321,144✔
127
        copy_data_ascii_item(rec_buffer[ii], writer_offset, buf );
321,144✔
128
        buf += strlen(buf);
321,144✔
129
    }
130

131
    out_stream << writer_buff << std::endl ;
49,047✔
132

133
    /*! Flush the output */
134
    out_stream.flush() ;
49,047✔
135
    /*! +1 for endl */
136
    return(strlen(writer_buff) + 1) ;
49,047✔
137
}
138

139
/**
140
@details
141
-# Close the output file stream
142
*/
143
int Trick::DRAscii::format_specific_shutdown() {
30✔
144

145
    if ( inited ) {
30✔
146
        out_stream.close() ;
30✔
147
    }
148
    return(0) ;
30✔
149
}
150

151
int Trick::DRAscii::set_ascii_float_format( std::string in_float_format ) {
×
152
    ascii_float_format = in_float_format ;
×
153
    return(0) ;
×
154
}
155

156
int Trick::DRAscii::set_ascii_double_format( std::string in_double_format ) {
×
157
    ascii_double_format = in_double_format ;
×
158
    return(0) ;
×
159
}
160

161
int Trick::DRAscii::set_delimiter( std::string in_delimiter ) {
×
162
    delimiter = in_delimiter ;
×
163
    return(0) ;
×
164
}
165

166
int Trick::DRAscii::set_single_prec_only( bool in_single_prec_only ) {
16✔
167
    Trick::DataRecordGroup::set_single_prec_only(in_single_prec_only) ;
16✔
168
    if( single_prec_only ) {
16✔
169
        ascii_double_format = "%20.8g";
×
170
    }
171
    else {
172
        ascii_double_format = "%20.16g";
16✔
173
    }
174
    return(0) ;
16✔
175
}
176

177
int Trick::DRAscii::copy_data_ascii_item( Trick::DataRecordBuffer * DI, int item_num, char *buf ) {
370,191✔
178

179
    char *address = 0;
370,191✔
180

181
    unsigned long bf;
182
    int sbf;
183

184
    address = DI->buffer + (item_num * DI->ref->attr->size) ;
370,191✔
185

186
    size_t writer_buf_spare = writer_buff + writer_buff_size - buf;
370,191✔
187

188
    switch (DI->ref->attr->type) {
370,191✔
189
        case TRICK_CHARACTER:
11✔
190
            snprintf(buf, writer_buf_spare, "%c", *((char *) address));
11✔
191
            break;
11✔
192

193
        case TRICK_UNSIGNED_CHARACTER:
11✔
194
            snprintf(buf, writer_buf_spare, "%u", *((unsigned char *) address));
11✔
195
            break;
11✔
196

197
        case TRICK_BOOLEAN:
15✔
198
            snprintf(buf, writer_buf_spare, "%u", *((bool *) address));
15✔
199
            break;
15✔
200

201
        case TRICK_STRING:
×
202
            snprintf(buf, writer_buf_spare, "%s", *((char **) address));
×
203
            break;
×
204

205
        case TRICK_SHORT:
11✔
206
            snprintf(buf, writer_buf_spare, "%d", *((short *) address));
11✔
207
            break;
11✔
208

209
        case TRICK_UNSIGNED_SHORT:
11✔
210
            snprintf(buf, writer_buf_spare, "%u", *((unsigned short *) address));
11✔
211
            break;
11✔
212

213
        case TRICK_ENUMERATED:
1,987✔
214
        case TRICK_INTEGER:
215
            snprintf(buf, writer_buf_spare, "%d", *((int *) address));
1,987✔
216
            break;
1,987✔
217

218
        case TRICK_UNSIGNED_INTEGER:
11✔
219
            snprintf(buf, writer_buf_spare, "%u", *((unsigned int *) address));
11✔
220
            break;
11✔
221

222
        case TRICK_LONG:
11✔
223
            snprintf(buf, writer_buf_spare, "%ld", *((long *) address));
11✔
224
            break;
11✔
225

226
        case TRICK_UNSIGNED_LONG:
11✔
227
            snprintf(buf, writer_buf_spare, "%lu", *((unsigned long *) address));
11✔
228
            break;
11✔
229

230
        case TRICK_FLOAT:
11✔
231
            snprintf(buf, writer_buf_spare, ascii_float_format.c_str() , *((float *) address));
11✔
232
            break;
11✔
233

234
        case TRICK_DOUBLE:
367,771✔
235
            snprintf(buf, writer_buf_spare, ascii_double_format.c_str() , *((double *) address));
367,771✔
236
            break;
367,771✔
237

238
        case TRICK_BITFIELD:
154✔
239
            sbf = GET_BITFIELD(address, DI->ref->attr->size, DI->ref->attr->index[0].start, DI->ref->attr->index[0].size);
154✔
240
            snprintf(buf, writer_buf_spare, "%d", sbf);
154✔
241
            break;
154✔
242

243
        case TRICK_UNSIGNED_BITFIELD:
154✔
244
            bf = GET_UNSIGNED_BITFIELD(address, DI->ref->attr->size, DI->ref->attr->index[0].start, DI->ref->attr->index[0].size);
154✔
245
            snprintf(buf, writer_buf_spare, "%lu", bf);
154✔
246
            break;
154✔
247

248
        case TRICK_LONG_LONG:
11✔
249
            snprintf(buf, writer_buf_spare, "%lld", *((long long *) address));
11✔
250
            break;
11✔
251

252
        case TRICK_UNSIGNED_LONG_LONG:
11✔
253
            snprintf(buf, writer_buf_spare, "%llu", *((unsigned long long *) address));
11✔
254
            break;
11✔
255
        default:
×
256
            break;
×
257
    }
258

259
    return(0) ;
370,191✔
260
}
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