• 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

42.79
/trick_source/sim_services/Sie/Sie.cpp
1

2
#include <iostream>
3
#include <algorithm>
4
#include <fstream>
5
#include <string.h>
6
#include <cstring>
7

8
#include "trick/Sie.hh"
9
#include "trick/SimObject.hh"
10
#include "trick/attributes.h"
11
#include "trick/reference.h"
12
#include "trick/memorymanager_c_intf.h"
13
#include "trick/exec_proto.hh"
14
#include "trick/command_line_protos.h"
15
#include "trick/MemoryManager.hh"
16
#include "trick/parameter_types.h"
17

18
Trick::Sie * the_sie = NULL ;
19

20
Trick::Sie::Sie() {
187✔
21
    // Call the attribute_map function which will instantiate the singleton maps
22
    class_attr_map = Trick::AttributesMap::attributes_map() ;
187✔
23
    enum_attr_map = Trick::EnumAttributesMap::attributes_map() ;
187✔
24

25
    move_runtime_generation = false;
187✔
26

27
    the_sie = this ;
187✔
28
}
187✔
29

30
void copy_file (const std::string& original_filename, const std::string& copy_filename) {
2✔
31
    std::ifstream original;
2✔
32
    std::ofstream copy;
2✔
33

34
    original.open(original_filename.c_str(), std::ios::binary);
2✔
35
    copy.open(copy_filename.c_str(), std::ios::binary);
2✔
36

37
    copy << original.rdbuf();
2✔
38

39
    original.close();
2✔
40
    copy.close();
2✔
41
}
2✔
42

43
// Helper function for copying S_sie.resource from default dir to output dir
44
void copy_sie_resource() {
2✔
45
    std::string original_sie_filename = std::string(command_line_args_get_default_dir()) + "/" + "S_sie.resource";
4✔
46
    std::string copy_sie_filename = std::string(command_line_args_get_output_dir()) + "/" + "S_sie.resource";
2✔
47
    copy_file(original_sie_filename, copy_sie_filename);
2✔
48
}
2✔
49

50
int Trick::Sie::process_sim_args() {
187✔
51

52
    int argc ;
53
    char ** argv ;
54

55
    argc = command_line_args_get_argc() ;
187✔
56
    argv = command_line_args_get_argv() ;
187✔
57

58
    if (argc >= 2) {
187✔
59
        if (!strcmp(argv[1], "sie")) {
187✔
60
            /* If main is being invoked by the configuration processor (cp) to generate the sie resource file... */
61
            /* Generate the sie resource file */
62
            sie_print_xml();
×
63
            sie_print_json();
×
64

65
            // Silently exit the sim without printing the termination message
66
            exit(0) ;
×
67
        }
68

69
        bool read_only = false;
187✔
70
        bool oo_dir = false;
187✔
71
        bool o_dir = false;
187✔
72

73
        // Otherwise, go through the rest of the sim args and look for --read-only-sim
74
        for (int i = 1; i < argc; i++) {
376✔
75
            if ((strcmp("--read-only-sim", argv[i]) == 0))  {
190✔
76
                // Set this flag to move runtime generation of sie into the output directory 
77
                move_runtime_generation = true;
1✔
78
                read_only = true;
1✔
79
                break;
1✔
80
            }
81
        }
82

83
        // Set oo_dir and o_dir flags accordingly
84
        for (int i = 1; i < argc; i++) {
377✔
85
            if (strncmp("-OO", argv[i], (size_t) 3) == 0) {
190✔
86
                oo_dir = true;
1✔
87
            } else if (strncmp("-O", argv[i], (size_t) 2) == 0) {
189✔
88
                o_dir = true;
×
89
            }
90
        }
91

92
        // If --read-only-sim is provided without either -OO or -O, exit with error message
93
        // For -OO, save S_sie.resource to the output directory with runtime jobs data
94
        if (read_only && !oo_dir && !o_dir) {
187✔
95
            std::cerr << "\nERROR: Missing -O or -OO argument with --read-only-sim flag" << std::endl;
×
96
            exit(1);
×
97
        } else if (oo_dir) {
187✔
98
            copy_sie_resource();
1✔
99
            move_runtime_generation = true;
1✔
100
        }
101
    }
102

103
    return(0) ;
187✔
104
}
105

106
void Trick::Sie::top_level_objects_print(std::ofstream & sie_out) {
×
107
    Trick::VARIABLE_MAP_ITER vit ;
×
108
    int jj ;
109

110
    for ( vit = trick_MM->variable_map_begin() ; vit != trick_MM->variable_map_end() ; ++vit ) {
×
111
        ALLOC_INFO * alloc_info = (*vit).second ;
×
112

113
        if ( alloc_info != NULL ) {
×
114
            sie_out << "  <top_level_object" ;
×
115
            sie_out << "\n   name=\"" << vit->first << "\"" ;
×
116
            sie_out << "\n   type=\"" ;
×
117
            std::string type = trickTypeCharString(alloc_info->type, alloc_info->user_type_name );
×
118
            std::replace(type.begin(), type.end(), ':', '_');
×
119
            sie_out <<  type << "\"\n" ;
×
120
            sie_out << "   alloc_memory_init=\"" << alloc_info->alloced_in_memory_init << "\"";
×
121
            sie_out << ">\n" ;
×
122
            if ( alloc_info->num_index > 0 ) {
×
123
                for (jj = 0; jj < alloc_info->num_index; jj++) {
×
124
                    sie_out << "        <dimension>" << alloc_info->index[jj] << "</dimension>\n" ;
×
125
                }
126
            }
127
            sie_out << "  </top_level_object>\n\n" ;
×
128
        }
×
129
    }
130
}
×
131

132
void Trick::Sie::runtime_objects_print(std::fstream & sie_out) {
1✔
133
    Trick::VARIABLE_MAP_ITER vit ;
1✔
134
    int jj ;
135

136
    for ( vit = trick_MM->variable_map_begin() ; vit != trick_MM->variable_map_end() ; ++vit ) {
28✔
137
        ALLOC_INFO * alloc_info = (*vit).second ;
27✔
138

139
        if ( alloc_info != NULL && alloc_info->alloced_in_memory_init == 0) {
27✔
140
            sie_out << "  <top_level_object" ;
5✔
141
            sie_out << "\n   name=\"" << vit->first << "\"" ;
5✔
142
            sie_out << "\n   type=\"" ;
5✔
143
            std::string type = trickTypeCharString(alloc_info->type, alloc_info->user_type_name );
5✔
144
            std::replace(type.begin(), type.end(), ':', '_');
5✔
145
            sie_out <<  type << "\"\n" ;
5✔
146
            sie_out << "   alloc_memory_init=\"" << alloc_info->alloced_in_memory_init << "\"";
5✔
147
            sie_out << ">\n" ;
5✔
148
            if ( alloc_info->num_index > 0 ) {
5✔
149
                for (jj = 0; jj < alloc_info->num_index; jj++) {
×
150
                    sie_out << "        <dimension>" << alloc_info->index[jj] << "</dimension>\n" ;
×
151
                }
152
            }
153
            sie_out << "  </top_level_object>\n\n" ;
5✔
154
        }
5✔
155
    }
156
}
1✔
157

158
void Trick::Sie::top_level_objects_json(std::ofstream & sie_out) {
×
159
    Trick::VARIABLE_MAP_ITER vit ;
×
160
    int jj ;
161
    sie_out << "  \"top_level_objects\": [\n";
×
162
    for ( vit = trick_MM->variable_map_begin() ; vit != trick_MM->variable_map_end() ; ++vit ) {
×
163
        ALLOC_INFO * alloc_info = (*vit).second ;
×
164

165
        if ( alloc_info != NULL ) {
×
166
            sie_out << "    {\n" ;
×
167
            sie_out << "      \"name\": \"" << vit->first << "\",\n" ;
×
168
            sie_out << "      \"type\": \"" ;
×
169
            std::string type = trickTypeCharString(alloc_info->type, alloc_info->user_type_name );
×
170
            std::replace(type.begin(), type.end(), ':', '_') ;
×
171
            sie_out <<  type << "\",\n" ;
×
172
            sie_out << "      \"alloc_memory_init\": \"" << alloc_info->alloced_in_memory_init << "\"";
×
173
            if ( alloc_info->num_index > 0 ) {
×
174
                sie_out << ",\n        \"dimensions\": [" ;
×
175
                for (jj = 0; jj < alloc_info->num_index - 1; jj++) {
×
176
                    sie_out << " \"" << alloc_info->index[jj] << "\"," ;
×
177
                }
178
                sie_out << " \"" << alloc_info->index[alloc_info->num_index - 1] << "\" " ;
×
179
                sie_out << "]\n" ;
×
180
                } else {
181
                    sie_out << '\n' ;
×
182
                }
183
            sie_out << "    }" ;
×
184
            if(std::next(vit, 1) != trick_MM->variable_map_end()) {
×
185
                sie_out << ',' ;
×
186
            }
187
            sie_out << '\n';
×
188
        }
×
189
    }
190
    sie_out << "  ]\n";
×
191
}
×
192

193
void Trick::Sie::sie_print_xml() {
×
194
    std::ofstream sie_out ;
×
195
    std::string file_name = std::string(get_runtime_sie_dir()) + "/" + "S_sie.resource" ;
×
196
    sie_out.open(file_name.c_str()) ;
×
197
    sie_out << "<?xml version=\"1.0\"?>\n\n" ;
×
198
    sie_out << "<sie>\n\n" ;
×
199
    class_attr_map->print_xml(sie_out) ;
×
200
    enum_attr_map->print_xml(sie_out) ;
×
201
    top_level_objects_print(sie_out) ;
×
202
    sie_out << "</sie>\n" ;
×
203
    sie_out.close() ;
×
204
}
×
205

206
void Trick::Sie::sie_append_runtime_objs() {
1✔
207
    std::fstream sie_out ;
1✔
208

209
    if (move_runtime_generation) {
1✔
210
        copy_sie_resource();
1✔
211
    }
212

213
    std::string sie_filename = get_runtime_sie_dir() + "/" + "S_sie.resource" ;
1✔
214
    sie_out.open(sie_filename.c_str(), std::fstream::in | std::fstream::out) ;
1✔
215

216
    const char * comment = "<!--\nRuntime Allocations\nDo not edit this comment or file content past this point\n-->\n";
1✔
217
    int comment_len = strlen(comment);
1✔
218

219
    int curr_offset = 1;
1✔
220
    int mem_size = 1000000;
1✔
221
    char * buff = new char[mem_size + 1];
1✔
222
    char * find_str = NULL;
1✔
223

224
    // initial read use the whole buffer
225
    sie_out.get(buff, mem_size, '\0');
1✔
226
    find_str = strstr(buff, comment);
1✔
227

228
    // loop through the file looking for the marker comment.  The comment could straddle 2 reads.  Copy the last bytes of
229
    // the previous read to start the next search.
230
    while ( !find_str && !sie_out.eof() ) {
1✔
231
        curr_offset += mem_size - comment_len - 1;
×
232
        strncpy(buff, &buff[mem_size-comment_len-1], comment_len);
×
233
        sie_out.get(&buff[comment_len], mem_size-comment_len, '\0');
×
234
        find_str = strstr(buff, comment);
×
235
    }
236

237
    if ( !find_str ) {
1✔
238
        std::cerr << "Warning: Cannot add runtime/dynamic allocations to S_sie.resource. S_sie.resource is corrupted, outdated, or missing. Please be sure that SIM_*/S_sie.resource is preserved after build time if needed at runtime for trick-tv or other variable server clients. Please also rerun trick-CP." << std::endl;
×
239
        delete[] buff;
×
240
        return;
×
241
    }
242

243
    // calculate the position of the marker in the file and jump to the point after the comment ends.
244
    int pos = curr_offset + (find_str - buff) + comment_len - 1;
1✔
245
    sie_out.clear();
1✔
246
    sie_out.seekp(pos);
1✔
247

248
    runtime_objects_print(sie_out);
1✔
249
    sie_out << "</sie>\n";
1✔
250
    sie_out.close();
1✔
251
    delete[] buff;
1✔
252
}
1✔
253

254
std::string Trick::Sie::get_runtime_sie_dir() {
1✔
255
    if (move_runtime_generation) {
1✔
256
        return std::string(command_line_args_get_output_dir());
2✔
257
    } else {
258
        return std::string(command_line_args_get_default_dir());
×
259
    }
260
}
261

262
void Trick::Sie::sie_print_json() {
×
263
    std::ofstream sie_out ;
×
264
    std::string file_name = std::string(get_runtime_sie_dir()) + "/" + "S_sie.json" ;
×
265
    sie_out.open(file_name.c_str()) ;
×
266
    sie_out << "{\n" ;
×
267
    class_attr_map->print_json(sie_out) ;
×
268
    enum_attr_map->print_json(sie_out) ;
×
269
    top_level_objects_json(sie_out) ;
×
270
    sie_out << "}\n" ;
×
271
    sie_out.close() ;
×
272
}
×
273

274

275
void Trick::Sie::class_attr_map_print_xml() {
×
276
    std::ofstream sie_out ;
×
277
    std::string file_name = std::string(get_runtime_sie_dir()) + "/" + "S_sie_class.xml" ;
×
278
    sie_out.open(file_name.c_str()) ;
×
279
    sie_out << "<?xml version=\"1.0\"?>\n\n" ;
×
280
    sie_out << "<sie>\n" ;
×
281
    class_attr_map->print_xml(sie_out) ;
×
282
    sie_out << "</sie>\n" ;
×
283
    sie_out.close() ;
×
284
}
×
285

286
void Trick::Sie::enum_attr_map_print_xml() {
×
287
    std::ofstream sie_out ;
×
288
    std::string file_name = std::string(get_runtime_sie_dir()) + "/" + "S_sie_enum.xml" ;
×
289
    sie_out.open(file_name.c_str()) ;
×
290
    sie_out << "<?xml version=\"1.0\"?>\n\n" ;
×
291
    sie_out << "<sie>\n" ;
×
292
    enum_attr_map->print_xml(sie_out) ;
×
293
    sie_out << "</sie>\n" ;
×
294
    sie_out.close() ;
×
295
}
×
296

297
void Trick::Sie::top_level_objects_print_xml() {
×
298
    std::ofstream sie_out ;
×
299
    std::string file_name = std::string(get_runtime_sie_dir()) + "/" + "S_sie_top_level_objects.xml" ;
×
300
    sie_out.open(file_name.c_str()) ;
×
301
    sie_out << "<?xml version=\"1.0\"?>\n\n" ;
×
302
    sie_out << "<sie>\n" ;
×
303
    top_level_objects_print(sie_out) ;
×
304
    sie_out << "</sie>\n" ;
×
305
    sie_out.close() ;
×
306
}
×
307

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