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

nasa / trick / 21296718347

23 Jan 2026 06:24PM UTC coverage: 55.589% (+0.009%) from 55.58%
21296718347

Pull #2031

github

web-flow
Merge 050505a97 into e4390df66
Pull Request #2031: Frame log choose buffering

8 of 9 new or added lines in 1 file covered. (88.89%)

172 existing lines in 8 files now uncovered.

12507 of 22499 relevant lines covered (55.59%)

308951.89 hits per line

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

70.63
/trick_source/sim_services/VariableServer/VariableReference.cpp
1
#include <stdlib.h>
2
#include <iostream>
3
#include <udunits2.h>
4
#include <math.h> // for fpclassify
5
#include <iomanip> // for setprecision
6
#include <string.h>
7
#include <sstream>
8

9
#include "trick/VariableReference.hh"
10
#include "trick/memorymanager_c_intf.h"
11
#include "trick/wcs_ext.h"
12
#include "trick/map_trick_units_to_udunits.hh"
13
#include "trick/message_proto.h"
14
#include "trick/message_type.h"
15
#include "trick/UdUnits.hh"
16
#include "trick/bitfield_proto.h"
17
#include "trick/trick_byteswap.h"
18

19

20
// Static variables to be addresses that are known to be the error ref address
21
int Trick::VariableReference::_bad_ref_int = 0 ;
22
int Trick::VariableReference::_do_not_resolve_bad_ref_int = 0 ;
23

24
REF2* Trick::VariableReference::make_error_ref(std::string in_name) {
3✔
25
    REF2* new_ref;
26
    new_ref = (REF2*)calloc(1, sizeof(REF2));
3✔
27
    new_ref->reference = strdup(in_name.c_str()) ;
3✔
28
    new_ref->units = NULL ;
3✔
29
    new_ref->address = (char *)&_bad_ref_int ;
3✔
30
    new_ref->attr = (ATTRIBUTES*)calloc(1, sizeof(ATTRIBUTES)) ;
3✔
31
    new_ref->attr->type = TRICK_NUMBER_OF_TYPES ;
3✔
32
    new_ref->attr->units = (char *)"--" ;
3✔
33
    new_ref->attr->size = sizeof(int) ;
3✔
34
    return new_ref;
3✔
35
}
36

37
REF2* Trick::VariableReference::make_do_not_resolve_ref(std::string in_name) {
1✔
38
    REF2* new_ref;
39
    new_ref = (REF2*)calloc(1, sizeof(REF2));
1✔
40
    new_ref->reference = strdup(in_name.c_str()) ;
1✔
41
    new_ref->units = NULL ;
1✔
42
    new_ref->address = (char *)&_do_not_resolve_bad_ref_int ;
1✔
43
    new_ref->attr = (ATTRIBUTES*)calloc(1, sizeof(ATTRIBUTES)) ;
1✔
44
    new_ref->attr->type = TRICK_NUMBER_OF_TYPES ;
1✔
45
    new_ref->attr->units = (char *)"--" ;
1✔
46
    new_ref->attr->size = sizeof(int) ;
1✔
47
    return new_ref;
1✔
48
}
49

50
// Helper function to deal with time variable
51
REF2* make_time_ref(double * time) {
×
52
    REF2* new_ref;
53
    new_ref = (REF2*)calloc(1, sizeof(REF2));
×
54
    new_ref->reference = strdup("time") ;
×
55
    new_ref->units = strdup("s") ;
×
56
    new_ref->address = (char *)time ;
×
57
    new_ref->attr = (ATTRIBUTES*)calloc(1, sizeof(ATTRIBUTES)) ;
×
58
    new_ref->attr->type = TRICK_DOUBLE ;
×
59
    new_ref->attr->units = strdup("s") ;
×
60
    new_ref->attr->size = sizeof(double) ;
×
61
    return new_ref;
×
62
}
63

64
Trick::VariableReference::VariableReference(std::string var_name, double* time) : _staged(false), _write_ready(false) {
×
65
    if (var_name != "time") {
×
66
        ASSERT(0);
67
    }
68

69
    _var_info = make_time_ref(time);
×
70

71
    // Set up member variables
72
    _address = _var_info->address;
×
73
    _size = _var_info->attr->size ;
×
74
    _deref = false;
×
75

76
    // Deal with weirdness around string vs wstring
77
    _trick_type = _var_info->attr->type ;
×
78

79
    // Allocate stage and write buffers
80
    _stage_buffer = calloc(_size, 1) ;
×
81
    _write_buffer = calloc(_size, 1) ;
×
82

83
    _conversion_factor = cv_get_trivial();
×
84
    _base_units = _var_info->attr->units;
×
85
    _requested_units = "s";
×
86
    _name = _var_info->reference;
×
87
}
×
88

89
Trick::VariableReference::VariableReference(std::string var_name) : _staged(false), _write_ready(false) {
8,056✔
90

91
    if (var_name == "time") {
8,056✔
92
        ASSERT(0);
93
    } else {
94
        // get variable attributes from memory manager
95
        _var_info = ref_attributes(var_name.c_str());
8,056✔
96
    }
97

98
    // Handle error cases
99
    if ( _var_info == NULL ) {
8,056✔
100
        // TODO: ERROR LOGGER sendErrorMessage("Variable Server could not find variable %s.\n", var_name);
101
        // PRINTF IS NOT AN ERROR LOGGER @me
102
        message_publish(MSG_ERROR, "Variable Server could not find variable %s.\n", var_name.c_str());
1✔
103
        _var_info = make_error_ref(var_name);
1✔
104
    } else if ( _var_info->attr ) {
8,055✔
105
        if ( _var_info->attr->type == TRICK_STRUCTURED ) {
8,055✔
106
            // sendErrorMessage("Variable Server: var_add cant add \"%s\" because its a composite variable.\n", var_name);
107
            message_publish(MSG_ERROR, "Variable Server: var_add cant add \"%s\" because its a composite variable.\n", var_name.c_str());
1✔
108

109
            free(_var_info);
1✔
110
            _var_info = make_do_not_resolve_ref(var_name);
1✔
111
        }
112
    } else {
113
        // sendErrorMessage("Variable Server: BAD MOJO - Missing ATTRIBUTES.");
114
        message_publish(MSG_ERROR, "Variable Server: BAD MOJO - Missing ATTRIBUTES.");
×
115

116
        free(_var_info);
×
117
        _var_info = make_error_ref(var_name);
×
118
    }
119

120
    // Set up member variables
121
    _var_info->units = NULL;
8,056✔
122
    _address = _var_info->address;
8,056✔
123
    _size = _var_info->attr->size ;
8,056✔
124
    _deref = false;
8,056✔
125

126
    // Deal with weirdness around string vs wstring
127
    _trick_type = _var_info->attr->type ;
8,056✔
128

129
    // Special handling for indexed STL containers (e.g., vec[0], point_vec[0].x)
130
    // We need to detect two cases:
131
    // 1. Final attr is TRICK_STL AND we indexed it (e.g., vec[0]) - update type/size to element
132
    // 2. Pattern "]." in reference - we indexed something then accessed a member (e.g., point_vec[0].x)
133
    // 
134
    // For case 1, we need to update _trick_type and _size to match the element type.
135
    // For case 2, the final attr is already the member's attributes (e.g., double x), so _trick_type
136
    // and _size are already correct - we just need to set the flag to skip follow_address_path().
137
    //
138
    // Important: If reference is just "vec" (no indexing), keep it as TRICK_STL container type.
139
    _used_stl_indexing = false;
8,056✔
140
    if ( _var_info->attr->type == TRICK_STL && _var_info->reference ) {
8,056✔
141
        // Check if the STL container was actually indexed by checking if reference ends with ']'
142
        // e.g., "vec[0]" ends with ']', but "vec" or "xxx[0].vec" do not
143
        size_t len = strlen(_var_info->reference);
2✔
144
        bool stl_was_indexed = (len > 0 && _var_info->reference[len - 1] == ']');
2✔
145

146
        if (stl_was_indexed) {
2✔
147
            // Case 1: Final attr is STL container AND we indexed it (e.g., vec[0])
148
            // Update type and size to match the element, not the container
149
            _trick_type = _var_info->attr->stl_elem_type;
1✔
150
            _used_stl_indexing = true;
1✔
151

152
            // Update _size to match the element type size, not the container size
153
            switch (_trick_type) {
1✔
154
                case TRICK_CHARACTER:
×
155
                case TRICK_UNSIGNED_CHARACTER:
156
                case TRICK_BOOLEAN:
157
                    _size = sizeof(char);
×
158
                    break;
×
159
                case TRICK_SHORT:
×
160
                case TRICK_UNSIGNED_SHORT:
161
                    _size = sizeof(short);
×
162
                    break;
×
163
                case TRICK_INTEGER:
1✔
164
                case TRICK_UNSIGNED_INTEGER:
165
                case TRICK_ENUMERATED:
166
                    _size = sizeof(int);
1✔
167
                    break;
1✔
168
                case TRICK_LONG:
×
169
                case TRICK_UNSIGNED_LONG:
170
                    _size = sizeof(long);
×
171
                    break;
×
172
                case TRICK_LONG_LONG:
×
173
                case TRICK_UNSIGNED_LONG_LONG:
174
                    _size = sizeof(long long);
×
175
                    break;
×
176
                case TRICK_FLOAT:
×
177
                    _size = sizeof(float);
×
178
                    break;
×
179
                case TRICK_DOUBLE:
×
180
                    _size = sizeof(double);
×
181
                    break;
×
182
                case TRICK_STRING:
×
183
                case TRICK_WSTRING:
184
                case TRICK_STRUCTURED:
185
                    _size = sizeof(void*);
×
186
                    break;
×
187
                default:
×
188
                    // Keep existing size for unknown types
189
                    break;
×
190
            }
191

192
            // address already points to the element from ref_dim
193
            // Treat as single value - nothing else necessary
194
        }
2✔
195
    } else {
196
        // Case 2: Not a directly indexed STL container
197
        // Handle mixed STL+pointer cases or regular array size calculations
198

199
        // Check for STL indexing - set flag to skip follow_address_path call
200
        // For mixed STL+pointer cases, this ensures follow_address_path is skipped
201
        // For STL-only cases (no pointers), this flag doesn't change behavior since
202
        // follow_address_path is only called when pointer_present == 1
203
        if ( _var_info->stl_present == 1 ) {
8,054✔
UNCOV
204
            _used_stl_indexing = true;
×
205
        }
206

207
        if ( _var_info->num_index == _var_info->attr->num_index ) {
8,054✔
208
            // single value - nothing else necessary
209
        } else if ( _var_info->attr->index[_var_info->attr->num_index - 1].size != 0 ) {
10✔
210
            // Constrained array
211
            for ( int i = _var_info->attr->num_index-1;  i > _var_info->num_index-1 ; i-- ) {
23✔
212
                _size *= _var_info->attr->index[i].size ;
13✔
213
            }
214
        } else {
215
            // Unconstrained array
UNCOV
216
            if ((_var_info->attr->num_index - _var_info->num_index) > 1 ) {
×
UNCOV
217
                message_publish(MSG_ERROR, "Variable Server Error: var_add(%s) requests more than one dimension of dynamic array.\n", _var_info->reference);
×
UNCOV
218
                message_publish(MSG_ERROR, "Data is not contiguous so returned values are unpredictable.\n") ;
×
219
            }
UNCOV
220
            if ( _var_info->attr->type == TRICK_CHARACTER ) {
×
221
                _trick_type = TRICK_STRING ;
×
222
                _deref = true;
×
223
            } else if ( _var_info->attr->type == TRICK_WCHAR ) {
×
UNCOV
224
                _trick_type = TRICK_WSTRING ;
×
225
                _deref = true;
×
226
            } else {
227
                _deref = true ;
×
228
                _size *= get_size((char*)_address) ;
×
229
            }
230
        }
231
    }
232
    // handle strings: set a max buffer size, the copy size may vary so will be set in copy_sim_data
233
    if (( _trick_type == TRICK_STRING ) || ( _trick_type == TRICK_WSTRING )) {
8,056✔
234
        _size = MAX_ARRAY_LENGTH ;
6✔
235
    }
236

237
    // Allocate stage and write buffers
238
    _stage_buffer = calloc(_size, 1) ;
8,056✔
239
    _write_buffer = calloc(_size, 1) ;
8,056✔
240

241
    _conversion_factor = cv_get_trivial();
8,056✔
242
    _base_units = _var_info->attr->units;
8,056✔
243
    _requested_units = "";
8,056✔
244
    _name = _var_info->reference;
8,056✔
245

246
    // Done!
247
}
8,056✔
248

249
Trick::VariableReference::~VariableReference() {
56✔
250
    if (_var_info != NULL) {
56✔
251
        free( _var_info );
56✔
252
        _var_info = NULL;
56✔
253
    }
254
    if (_stage_buffer != NULL) {
56✔
255
        free (_stage_buffer);
56✔
256
        _stage_buffer = NULL;
56✔
257
    }
258
    if (_write_buffer != NULL) {
56✔
259
        free (_write_buffer);
56✔
260
        _write_buffer = NULL;
56✔
261
    }
262
    if (_conversion_factor != NULL) {
56✔
263
        cv_free(_conversion_factor);
56✔
264
    }
265
}
56✔
266

267
std::string Trick::VariableReference::getName() const {
12,021✔
268
    return _name;
12,021✔
269
}
270

271
int Trick::VariableReference::getSizeBinary() const {
4,000✔
272
    return _size;
4,000✔
273
}
274

275
TRICK_TYPE Trick::VariableReference::getType() const {
4✔
276
    return _trick_type;
4✔
277
}
278

279
std::string Trick::VariableReference::getBaseUnits() const {
12✔
280
    return _base_units;
12✔
281
}
282

283
int Trick::VariableReference::setRequestedUnits(std::string units_name) {
10✔
284
    // Some error logging lambdas - these should probably go somewhere else
285
    // But I do kinda like them
286
    auto publish = [](MESSAGE_TYPE type, const std::string& message) {
2✔
287
        std::ostringstream oss;
2✔
288
        oss << "Variable Server: " << message << std::endl;
2✔
289
        message_publish(type, oss.str().c_str());
2✔
290
    };
2✔
291

292
    auto publishError = [&](const std::string& units) {
1✔
293
        std::ostringstream oss;
1✔
294
        oss << "units error for [" << getName() << "] [" << units << "]";
1✔
295
        publish(MSG_ERROR, oss.str());
1✔
296
    };
1✔
297

298
    // If the units_name parameter is "xx", set it to the current units.
299
    if (!units_name.compare("xx")) {
10✔
UNCOV
300
        units_name = getBaseUnits();
×
301
    }
302

303
    // Don't try to convert units for a bad ref
304
    if (_var_info->address == &_bad_ref_int) {
10✔
UNCOV
305
        return -1 ;
×
306
    }
307

308
    // if unitless ('--') then do not convert to udunits 
309
    if (units_name.compare("--")) {
10✔
310
        // Check to see if this is an old style Trick unit that needs to be converted to new udunits
311
        std::string new_units = map_trick_units_to_udunits(units_name) ;
10✔
312
        // Warn if a conversion has taken place
313
        if ( units_name.compare(new_units) ) {
10✔
314
            // TODO: MAKE BETTER SYSTEM FOR ERROR LOGGING
UNCOV
315
            std::ostringstream oss;
×
UNCOV
316
            oss << "[" << getName() << "] old-style units converted from ["
×
UNCOV
317
                << units_name << "] to [" << new_units << "]";
×
UNCOV
318
            publish(MSG_WARNING, oss.str());
×
319
        }
320

321
        // Interpret base unit
322
        ut_unit * from = ut_parse(Trick::UdUnits::get_u_system(), getBaseUnits().c_str(), UT_ASCII) ;
10✔
323
        if ( !from ) {
10✔
UNCOV
324
            std::cout << "Error in interpreting base units" << std::endl;
×
UNCOV
325
            publishError(getBaseUnits());
×
UNCOV
326
            ut_free(from) ;
×
UNCOV
327
            return -1 ;
×
328
        }
329

330
        // Interpret requested unit
331
        ut_unit * to = ut_parse(Trick::UdUnits::get_u_system(), new_units.c_str(), UT_ASCII) ;
10✔
332
        if ( !to ) {
10✔
333
            std::cout << "Error in interpreting requested units" << std::endl;
1✔
334
            publishError(new_units);
1✔
335
            ut_free(from) ;
1✔
336
            ut_free(to) ;
1✔
337
            return -1 ;
1✔
338
        }
339

340
        // Create a converter from the base to the requested
341
        auto new_conversion_factor = ut_get_converter(from, to) ;
9✔
342
        ut_free(from) ;
9✔
343
        ut_free(to) ;
9✔
344
        if ( !new_conversion_factor ) {
9✔
345
            std::ostringstream oss;
1✔
346
            oss << "[" << getName() << "] cannot convert units from [" << getBaseUnits()
1✔
347
                << "] to [" << new_units << "]";
1✔
348
            publish(MSG_ERROR, oss.str());
1✔
349
            return -1 ;
1✔
350
        } else {
351
            _conversion_factor = new_conversion_factor;
8✔
352
        }
353
    
354
        // Set the requested units. This will cause the unit string to be printed in write_value_ascii
355
        _requested_units = new_units;
8✔
356
    }
357
    return 0;
8✔
358
}
359

360
int Trick::VariableReference::stageValue(bool validate_address) {
8,039✔
361
    _write_ready = false;
8,039✔
362

363
    // Copy <size> bytes from <address> to staging_point.
364

365
    // Try to recreate connection if it has been broken
366
    if (_var_info->address == &_bad_ref_int) {
8,039✔
367
        REF2 *new_ref = ref_attributes(_var_info->reference);
1✔
368
        if (new_ref != NULL) {
1✔
UNCOV
369
            _var_info = new_ref;
×
UNCOV
370
            _address = _var_info->address;
×
371
            // _requested_units = "";
372
        }
373
    }
374

375
    // if there's a pointer somewhere in the address path, follow it in case pointer changed
376
    // BUT: if we've indexed into an STL container, then the address already points to the correct location
377
    // and follow_address_path would incorrectly recalculate it (it doesn't understand STL indexing)
378
    // Use the _used_stl_indexing flag that was set during construction when we detected STL indexing
379
    // (when num_index > attr->num_index and attr->type == TRICK_STL)
380
    if ( _var_info->pointer_present == 1 && !_used_stl_indexing ) {
8,039✔
UNCOV
381
        _address = follow_address_path(_var_info) ;
×
UNCOV
382
        if (_address == NULL) {
×
UNCOV
383
            tagAsInvalid();
×
UNCOV
384
        } else if ( validate_address ) {
×
385
            validate();
×
386
        } else {
387
            _var_info->address = _address ;
×
388
        }
389
    }
390

391
    // if this variable is a string we need to get the raw character string out of it.
392
    if (( _trick_type == TRICK_STRING ) && !_deref) {
8,039✔
393
        std::string * str_ptr = (std::string *)_var_info->address ;
5✔
394
        // Get a pointer to the internal character array
395
        _address = (void *)(str_ptr->c_str()) ;
5✔
396
    }
397

398
    // if this variable itself is a pointer, dereference it
399
    if ( _deref ) {
8,039✔
UNCOV
400
        _address = *(void**)_var_info->address ;
×
401
    }
402

403
    // handle c++ string and char*
404
    if ( _trick_type == TRICK_STRING ) {
8,039✔
405
        if (_address == NULL) {
5✔
UNCOV
406
            _size = 0 ;
×
407
        } else {
408
            _size = strlen((char*)_address) + 1 ;
5✔
409
        }
410
    }
411
    // handle c++ wstring and wchar_t*
412
    if ( _trick_type == TRICK_WSTRING ) {
8,039✔
UNCOV
413
        if (_address == NULL) {
×
UNCOV
414
            _size = 0 ;
×
415
        } else {
UNCOV
416
            _size = wcslen((wchar_t *)_address) * sizeof(wchar_t);
×
417
        }
418
    }
419
    if(_address != NULL) {
8,039✔
420
        memcpy( _stage_buffer , _address , _size ) ;
8,039✔
421
    }
422

423
    _staged = true;
8,039✔
424
    return 0;
8,039✔
425
}
426

427
bool Trick::VariableReference::validate() {
3✔
428
    // The address is not NULL.
429
    // Should be called by VariableServer Session if validateAddress is on.
430
    // check the memory manager if the address falls into
431
    // any of the memory blocks it knows of.  Don't do this if we have a std::string or
432
    // wstring type, or we already are pointing to a bad ref.
433
    if ( (_trick_type != TRICK_STRING) and
9✔
434
            (_trick_type != TRICK_WSTRING) and
3✔
435
            (_var_info->address != &_bad_ref_int) and
8✔
436
            (get_alloc_info_of(_address) == NULL) ) {
2✔
437
        
438
        // This variable is broken, make it into an error ref
439
        tagAsInvalid();
1✔
440
        return false;
1✔
441
    }
442

443
    // Everything is fine
444
    return true;
2✔
445
}
446

447
static void write_escaped_string( std::ostream& os, const char* s) {
4✔
448
    for (int ii=0 ; ii<strlen(s) ; ii++) {
41✔
449
        if (isprint(s[ii])) {
37✔
450
            os << s[ii];
29✔
451
        } else {
452
            switch ((s)[ii]) {
8✔
453
                case '\n': os << "\\n"; break;
2✔
454
                case '\t': os << "\\t"; break;
1✔
455
                case '\b': os << "\\b"; break;
1✔
456
                case '\a': os << "\\a"; break;
1✔
457
                case '\f': os << "\\f"; break;
1✔
458
                case '\r': os << "\\n"; break;
1✔
459
                case '\v': os << "\\v"; break;
1✔
UNCOV
460
                case '\"': os << "\\\""; break;
×
UNCOV
461
                default  : {
×
462
                    // Replicating behavior from original vs_format_ascii
463
                    char temp_s[6];
464
                    sprintf(temp_s, "\\x%02x", s[ii]);
×
465
                    os << temp_s ; 
×
UNCOV
466
                    break;
×
467
                }
468
            }
469
        }
470
    }
471
}
4✔
472

UNCOV
473
int Trick::VariableReference::getSizeAscii() const {
×
UNCOV
474
    std::stringstream ss;
×
UNCOV
475
    writeValueAscii(ss);
×
UNCOV
476
    return ss.str().length();
×
477
}
478

479

480
int Trick::VariableReference::writeValueAscii( std::ostream& out ) const {
4,030✔
481
    // This is copied and modified from vs_format_ascii
482

483
    if (!isWriteReady()) {
4,030✔
484
        return -1;
1✔
485
    }
486

487
    // local_type is set to the type of the attribute, but if it's a STL type, we need to use the element type.
488
    TRICK_TYPE local_type = _trick_type;
4,029✔
489

490
    if (_trick_type == TRICK_STL) {
4,029✔
491
        // If the variable is an STL type, use the STL element type for writting value
UNCOV
492
        local_type = _var_info->attr->stl_elem_type;
×
493
    }
494

495
    int bytes_written = 0;
4,029✔
496
    void * buf_ptr = _write_buffer ;
4,029✔
497
    while (bytes_written < _size) {
8,062✔
498
        bytes_written += _var_info->attr->size ;
4,033✔
499

500
        switch (local_type) {
4,033✔
501

502
        case TRICK_CHARACTER:
2✔
503
            if (_var_info->attr->num_index == _var_info->num_index) {
2✔
504
                // Single char
505
                out << (int)cv_convert_double(_conversion_factor, *(char *)buf_ptr);
1✔
506
            } else {
507
                // All but last dim specified, leaves a char array 
508
                write_escaped_string(out, (const char *) buf_ptr);
1✔
509
                bytes_written = _size ;
1✔
510
            }
511
            break;
2✔
512
        case TRICK_UNSIGNED_CHARACTER:
2✔
513
            if (_var_info->attr->num_index == _var_info->num_index) {
2✔
514
                // Single char
515
                out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned char *)buf_ptr);
1✔
516
            } else {
517
                // All but last dim specified, leaves a char array 
518
                write_escaped_string(out, (const char *) buf_ptr);
1✔
519
                bytes_written = _size ;
1✔
520
            }
521
            break;
2✔
522

523
        case TRICK_WCHAR:{
2✔
524
                if (_var_info->attr->num_index == _var_info->num_index) {
2✔
525
                    out << *(wchar_t *) buf_ptr;
1✔
526
                } else {
527
                    // convert wide char string char string
528
                    size_t len = wcs_to_ncs_len((wchar_t *)buf_ptr) + 1 ;
2✔
529

530
                    char temp_buf[len];
1✔
531
                    wcs_to_ncs((wchar_t *) buf_ptr, temp_buf, len);
1✔
532
                    out << temp_buf;
1✔
533
                    bytes_written = _size ;
2✔
534
                }
535
            }
536
            break;
2✔
537

538
        case TRICK_STRING:
2✔
539
            if ((char *) buf_ptr != NULL) {
2✔
540
                write_escaped_string(out, (const char *) buf_ptr);
2✔
541
                bytes_written = _size ;
2✔
542
            } else {
UNCOV
543
                out << '\0';
×
544
            }
545
            break;
2✔
546

547
        case TRICK_WSTRING:
×
UNCOV
548
            if ((wchar_t *) buf_ptr != NULL) {
×
549
                // convert wide char string char string
UNCOV
550
                size_t len = wcs_to_ncs_len( (wchar_t *)buf_ptr) + 1 ;
×
551

552
                char temp_buf[len];
×
UNCOV
553
                wcs_to_ncs(  (wchar_t *) buf_ptr, temp_buf, len);
×
554
                out << temp_buf;
×
UNCOV
555
                bytes_written = _size ;
×
556
            } else {
557
                out << '\0';
×
558
            }
559
            break;
×
560
        case TRICK_SHORT:
1✔
561
            out << (short)cv_convert_double(_conversion_factor,*(short *)buf_ptr);
1✔
562
            break;
1✔
563

UNCOV
564
        case TRICK_UNSIGNED_SHORT:
×
UNCOV
565
            out << (unsigned short)cv_convert_double(_conversion_factor,*(unsigned short *)buf_ptr);
×
UNCOV
566
            break;
×
567

568
        case TRICK_INTEGER:
4,012✔
569
        case TRICK_ENUMERATED:
570
            out << (int)cv_convert_double(_conversion_factor,*(int *)buf_ptr);
4,012✔
571
            break;
4,012✔
572

573
        case TRICK_BOOLEAN:
1✔
574
            out << (int)cv_convert_double(_conversion_factor,*(bool *)buf_ptr);
1✔
575
            break;
1✔
576

UNCOV
577
        case TRICK_BITFIELD:
×
UNCOV
578
            out << (GET_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size));
×
UNCOV
579
            break;
×
580

581
        case TRICK_UNSIGNED_BITFIELD:
×
582
            out << (GET_UNSIGNED_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size));
×
583
            break;
×
584
            
585
        case TRICK_UNSIGNED_INTEGER:
×
586
            out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned int *)buf_ptr);
×
587
            break;
×
588

589
        case TRICK_LONG: {
1✔
590
            long l = *(long *)buf_ptr;
1✔
591
            if (_conversion_factor != cv_get_trivial()) {
1✔
592
                l = (long)cv_convert_double(_conversion_factor, l);
1✔
593
            }
594
            out << l;
1✔
595
            break;
1✔
596
        }
597

598
        case TRICK_UNSIGNED_LONG: {
1✔
599
            unsigned long ul = *(unsigned long *)buf_ptr;
1✔
600
            if (_conversion_factor != cv_get_trivial()) {
1✔
601
                ul = (unsigned long)cv_convert_double(_conversion_factor, ul);
1✔
602
            }
603
            out << ul;
1✔
604
            break;
1✔
605
        }
606

UNCOV
607
        case TRICK_FLOAT:
×
UNCOV
608
            out << std::setprecision(8) << cv_convert_float(_conversion_factor,*(float *)buf_ptr);
×
UNCOV
609
            break;
×
610

611
        case TRICK_DOUBLE:
5✔
612
            out << std::setprecision(16) << cv_convert_double(_conversion_factor,*(double *)buf_ptr);
5✔
613
            break;
5✔
614

615
        case TRICK_LONG_LONG: {
1✔
616
            long long ll = *(long long *)buf_ptr;
1✔
617
            if (_conversion_factor != cv_get_trivial()) {
1✔
618
                ll = (long long)cv_convert_double(_conversion_factor, ll);
1✔
619
            }
620
            out << ll;
1✔
621
            break;
1✔
622
        }
623

624
        case TRICK_UNSIGNED_LONG_LONG: {
1✔
625
            unsigned long long ull = *(unsigned long long *)buf_ptr;
1✔
626
            if (_conversion_factor != cv_get_trivial()) {
1✔
627
                ull = (unsigned long long)cv_convert_double(_conversion_factor, ull);
1✔
628
            }
629
            out << ull;
1✔
630
            break;
1✔
631
        }
632

633
        case TRICK_NUMBER_OF_TYPES:
2✔
634
            out << "BAD_REF";
2✔
635
            break;
2✔
636

UNCOV
637
        default:{
×
638

UNCOV
639
            break;
×
640
        }
641
        } // end switch
642

643
        if (bytes_written < _size) {
4,033✔
644
        // if returning an array, continue array as comma separated values
645
            out << ",";
4✔
646
            buf_ptr = (void*) ((long)buf_ptr + _var_info->attr->size) ;
4✔
647
        }
648
    } //end while
649

650
    if (_requested_units != "") {
4,029✔
651
        if ( _var_info->attr->mods & TRICK_MODS_UNITSDASHDASH ) {
8✔
UNCOV
652
            out << " {--}";
×
653
        } else {
654
            out << " {" << _requested_units << "}";
8✔
655
        }
656
    }
657

658
    return 0;
4,029✔
659
}
660

661
void Trick::VariableReference::tagAsInvalid () {
2✔
662
    std::string save_name(getName()) ;
2✔
663
    free(_var_info) ;
2✔
664
    _var_info = make_error_ref(save_name) ;
2✔
665
    _address = _var_info->address ;
2✔
666
}
2✔
667

668

669
int Trick::VariableReference::prepareForWrite() {
8,039✔
670
    if (!_staged) {
8,039✔
671
        return 1;
1✔
672
    }
673

674
    void * temp_p = _stage_buffer;
8,038✔
675
    _stage_buffer = _write_buffer;
8,038✔
676
    _write_buffer = temp_p;
8,038✔
677

678
    _staged = false;
8,038✔
679
    _write_ready = true;
8,038✔
680
    return 0;
8,038✔
681
}
682

683
bool Trick::VariableReference::isStaged() const {
8,003✔
684
    return _staged;
8,003✔
685
}
686

687
bool Trick::VariableReference::isWriteReady() const {
4,035✔
688
    return _write_ready;
4,035✔
689
}
690

691
int Trick::VariableReference::writeTypeBinary( std::ostream& out, bool byteswap ) const {
4,000✔
692
    int local_type = _trick_type;
4,000✔
693
    if (byteswap) {
4,000✔
UNCOV
694
        local_type = trick_byteswap_int(local_type);
×
695
    }
696
    out.write(const_cast<const char *>(reinterpret_cast<char *>(&local_type)), sizeof(int));
4,000✔
697

698
    return 0;
4,000✔
699
}
700

701
int Trick::VariableReference::writeSizeBinary( std::ostream& out, bool byteswap ) const {
4,000✔
702
    int local_size = _size;
4,000✔
703
    if (byteswap) {
4,000✔
UNCOV
704
        local_size = trick_byteswap_int(local_size);
×
705
    }
706
    out.write(const_cast<const char *>(reinterpret_cast<char *>(&local_size)), sizeof(int));
4,000✔
707

708
    return 0;
4,000✔
709
}
710

711
int Trick::VariableReference::writeNameBinary( std::ostream& out, bool byteswap ) const {
4,001✔
712
    std::string name = getName();
4,001✔
713
    out.write(name.c_str(), name.size());
4,001✔
714

715
    return 0;
8,002✔
716
}
717

718
int Trick::VariableReference::writeNameLengthBinary( std::ostream& out, bool byteswap ) const {
4,001✔
719
    int name_size = getName().size();
4,001✔
720
    if (byteswap) {
4,001✔
UNCOV
721
        name_size = trick_byteswap_int(name_size);
×
722
    }
723

724
    out.write(const_cast<const char *>(reinterpret_cast<char *>(&name_size)), sizeof(int));
4,001✔
725

726
    return 0;
4,001✔
727
}
728

729

730
void Trick::VariableReference::byteswap_var (char * out, char * in) const {
1✔
731
    byteswap_var(out, in, *this);
1✔
732
}
1✔
733

734

735
void Trick::VariableReference::byteswap_var (char * out, char * in, const VariableReference& ref) {
9✔
736
    ATTRIBUTES * attr = ref._var_info->attr;
9✔
737
    int array_size = 1;
9✔
738

739
    // Determine how many elements are in this array if it is an array
740
    for (int j = 0; j < ref._var_info->attr->num_index; j++) {
16✔
741
        array_size *= attr->index[j].size;
7✔
742
    }
743

744
    switch (attr->size) {
9✔
745
        case 1:
2✔
746
            // If these are just characters, no need to byteswap
747
            for (int j = 0; j < array_size; j++) {
10✔
748
                out[j] = in[j];
8✔
749
            }
750
            break;
2✔
751

752
        case 2: {
1✔
753
            short * short_in = reinterpret_cast<short *> (in);
1✔
754
            short * short_out = reinterpret_cast<short *> (out);
1✔
755

756
            for (int j = 0; j < array_size; j++) {
2✔
757
                short_out[j] = trick_byteswap_short(short_in[j]);
1✔
758
            }
759
            break;
1✔
760
        }
761

762
        case 4: {
4✔
763
            int * int_in = reinterpret_cast<int *> (in);
4✔
764
            int * int_out = reinterpret_cast<int *> (out);
4✔
765

766
            for (int j = 0; j < array_size; j++) {
131✔
767
                int_out[j] = trick_byteswap_int(int_in[j]);
127✔
768
            }
769
            break;
4✔
770
        }
771
        case 8: {
2✔
772
            // We don't actually care if this is double or long, just that it's the right size
773
            double * double_in = reinterpret_cast<double *> (in);
2✔
774
            double * double_out = reinterpret_cast<double *> (out);
2✔
775

776
            for (int j = 0; j < array_size; j++) {
8✔
777
                double_out[j] = trick_byteswap_double(double_in[j]);
6✔
778
            }
779
            break;
2✔
780
        }
781
    }
782
}
9✔
783

784

785

786
int Trick::VariableReference::writeValueBinary( std::ostream& out, bool byteswap ) const {
4,006✔
787
    // local_type is set to the type of the attribute, but if it's a STL type, we need to use the element type.
788
    TRICK_TYPE local_type = _trick_type;
4,006✔
789
    if (local_type == TRICK_STL) {
4,006✔
790
        // If the variable is an STL type, use the STL element type for writing value
UNCOV
791
        local_type = _var_info->attr->stl_elem_type;
×
792
    }
793

794
    if ( local_type == TRICK_BITFIELD ) {
4,006✔
795
        int temp_i = GET_BITFIELD(_write_buffer , _var_info->attr->size ,
×
796
            _var_info->attr->index[0].start, _var_info->attr->index[0].size) ;
UNCOV
797
        out.write((char *)(&temp_i), _size);
×
UNCOV
798
        return _size;
×
799
    }
800

801
    if ( local_type == TRICK_UNSIGNED_BITFIELD ) {
4,006✔
802
        int temp_unsigned = GET_UNSIGNED_BITFIELD(_write_buffer , _var_info->attr->size ,
×
803
                _var_info->attr->index[0].start, _var_info->attr->index[0].size) ;
UNCOV
804
        out.write((char *)(&temp_unsigned), _size);
×
UNCOV
805
        return _size;
×
806
    }
807

808
    if (local_type ==  TRICK_NUMBER_OF_TYPES) {
4,006✔
809
        // TRICK_NUMBER_OF_TYPES is an error case
UNCOV
810
        int temp_zero = 0 ;
×
UNCOV
811
        out.write((char *)(&temp_zero), _size);
×
UNCOV
812
        return _size;
×
813
    }
814

815
    if (byteswap) {
4,006✔
816
        char * byteswap_buf = (char *) calloc (_size, 1);
1✔
817
        byteswap_var(byteswap_buf, (char *) _write_buffer);
1✔
818
        out.write(byteswap_buf, _size);
1✔
819
        free (byteswap_buf);
1✔
820
    }
821
    else {
822
        out.write((char *) _write_buffer, _size);
4,005✔
823
    }
824

825
    return _size;
4,006✔
826
    
827
}  
828

829
std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableReference& ref) {
6✔
830
    s << "      \"" << ref.getName() << "\"";
6✔
831
    return s;
6✔
832
}
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