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

nasa / trick / 19943359860

04 Dec 2025 08:45PM UTC coverage: 55.608% (-0.2%) from 55.811%
19943359860

Pull #1996

github

web-flow
Merge 7587cd1c9 into b0776a3f7
Pull Request #1996: Add vs support for stl vector, deque, and array containers of supported data types

35 of 131 new or added lines in 6 files covered. (26.72%)

4 existing lines in 3 files now uncovered.

12425 of 22344 relevant lines covered (55.61%)

252751.04 hits per line

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

70.37
/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 - we indexed into container and stopped at element (e.g., vec[0])
132
    // 2. Pattern "]." in reference - we indexed something then accessed a member (e.g., point_vec[0].x or ptr_array[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
    _used_stl_indexing = false;
8,056✔
138
    if ( _var_info->attr->type == TRICK_STL ) {
8,056✔
139
        // Case 1: Final attr is STL container that we indexed (e.g., vec[0])
140
        // Update type and size to match the element, not the container
141
        _trick_type = _var_info->attr->stl_elem_type;
2✔
142
        _used_stl_indexing = true;
2✔
143

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

184
        // address already points to the element from ref_dim
185
        // Treat as single value - nothing else necessary
186
    } else if ( _var_info->reference && strstr(_var_info->reference, "].") ) {
8,054✔
187
        // Case 2: Pattern "]." indicates indexing followed by member access (e.g., point_vec[0].x)
188
        // The final attr is already the member's attributes, so _trick_type and _size are correct.
189
        // Just set the flag to skip follow_address_path() which doesn't understand STL indexing.
190
        // Note: This also matches pointer arrays like ptr_array[0].x, but that's okay - the address
191
        // is already correct and skipping follow_address_path() won't hurt.
192
        // TODO: if a better way than checking "]." to detect this case is found, update this logic.
NEW
193
        _used_stl_indexing = true;
×
194
    } else if ( _var_info->num_index == _var_info->attr->num_index ) {
8,054✔
195
        // single value - nothing else necessary
196
    } else if ( _var_info->attr->index[_var_info->attr->num_index - 1].size != 0 ) {
10✔
197
        // Constrained array
198
        for ( int i = _var_info->attr->num_index-1;  i > _var_info->num_index-1 ; i-- ) {
23✔
199
            _size *= _var_info->attr->index[i].size ;
13✔
200
        }
201
    } else {
202
        // Unconstrained array
203
        if ((_var_info->attr->num_index - _var_info->num_index) > 1 ) {
×
204
            message_publish(MSG_ERROR, "Variable Server Error: var_add(%s) requests more than one dimension of dynamic array.\n", _var_info->reference);
×
205
            message_publish(MSG_ERROR, "Data is not contiguous so returned values are unpredictable.\n") ;
×
206
        }
207
        if ( _var_info->attr->type == TRICK_CHARACTER ) {
×
208
            _trick_type = TRICK_STRING ;
×
209
            _deref = true;
×
210
        } else if ( _var_info->attr->type == TRICK_WCHAR ) {
×
211
            _trick_type = TRICK_WSTRING ;
×
212
            _deref = true;
×
213
        } else {
214
            _deref = true ;
×
215
            _size *= get_size((char*)_address) ;
×
216
        }
217
    }
218
    // handle strings: set a max buffer size, the copy size may vary so will be set in copy_sim_data
219
    if (( _trick_type == TRICK_STRING ) || ( _trick_type == TRICK_WSTRING )) {
8,056✔
220
        _size = MAX_ARRAY_LENGTH ;
6✔
221
    }
222

223
    // Allocate stage and write buffers
224
    _stage_buffer = calloc(_size, 1) ;
8,056✔
225
    _write_buffer = calloc(_size, 1) ;
8,056✔
226

227
    _conversion_factor = cv_get_trivial();
8,056✔
228
    _base_units = _var_info->attr->units;
8,056✔
229
    _requested_units = "";
8,056✔
230
    _name = _var_info->reference;
8,056✔
231

232
    // Done!
233
}
8,056✔
234

235
Trick::VariableReference::~VariableReference() {
56✔
236
    if (_var_info != NULL) {
56✔
237
        free( _var_info );
56✔
238
        _var_info = NULL;
56✔
239
    }
240
    if (_stage_buffer != NULL) {
56✔
241
        free (_stage_buffer);
56✔
242
        _stage_buffer = NULL;
56✔
243
    }
244
    if (_write_buffer != NULL) {
56✔
245
        free (_write_buffer);
56✔
246
        _write_buffer = NULL;
56✔
247
    }
248
    if (_conversion_factor != NULL) {
56✔
249
        cv_free(_conversion_factor);
56✔
250
    }
251
}
56✔
252

253
std::string Trick::VariableReference::getName() const {
12,021✔
254
    return _name;
12,021✔
255
}
256

257
int Trick::VariableReference::getSizeBinary() const {
4,000✔
258
    return _size;
4,000✔
259
}
260

261
TRICK_TYPE Trick::VariableReference::getType() const {
4✔
262
    return _trick_type;
4✔
263
}
264

265
std::string Trick::VariableReference::getBaseUnits() const {
12✔
266
    return _base_units;
12✔
267
}
268

269
int Trick::VariableReference::setRequestedUnits(std::string units_name) {
10✔
270
    // Some error logging lambdas - these should probably go somewhere else
271
    // But I do kinda like them
272
    auto publish = [](MESSAGE_TYPE type, const std::string& message) {
2✔
273
        std::ostringstream oss;
2✔
274
        oss << "Variable Server: " << message << std::endl;
2✔
275
        message_publish(type, oss.str().c_str());
2✔
276
    };
2✔
277

278
    auto publishError = [&](const std::string& units) {
1✔
279
        std::ostringstream oss;
1✔
280
        oss << "units error for [" << getName() << "] [" << units << "]";
1✔
281
        publish(MSG_ERROR, oss.str());
1✔
282
    };
1✔
283

284
    // If the units_name parameter is "xx", set it to the current units.
285
    if (!units_name.compare("xx")) {
10✔
286
        units_name = getBaseUnits();
×
287
    }
288

289
    // Don't try to convert units for a bad ref
290
    if (_var_info->address == &_bad_ref_int) {
10✔
291
        return -1 ;
×
292
    }
293

294
    // if unitless ('--') then do not convert to udunits 
295
    if (units_name.compare("--")) {
10✔
296
        // Check to see if this is an old style Trick unit that needs to be converted to new udunits
297
        std::string new_units = map_trick_units_to_udunits(units_name) ;
10✔
298
        // Warn if a conversion has taken place
299
        if ( units_name.compare(new_units) ) {
10✔
300
            // TODO: MAKE BETTER SYSTEM FOR ERROR LOGGING
301
            std::ostringstream oss;
×
302
            oss << "[" << getName() << "] old-style units converted from ["
×
303
                << units_name << "] to [" << new_units << "]";
×
304
            publish(MSG_WARNING, oss.str());
×
305
        }
306

307
        // Interpret base unit
308
        ut_unit * from = ut_parse(Trick::UdUnits::get_u_system(), getBaseUnits().c_str(), UT_ASCII) ;
10✔
309
        if ( !from ) {
10✔
310
            std::cout << "Error in interpreting base units" << std::endl;
×
311
            publishError(getBaseUnits());
×
312
            ut_free(from) ;
×
313
            return -1 ;
×
314
        }
315

316
        // Interpret requested unit
317
        ut_unit * to = ut_parse(Trick::UdUnits::get_u_system(), new_units.c_str(), UT_ASCII) ;
10✔
318
        if ( !to ) {
10✔
319
            std::cout << "Error in interpreting requested units" << std::endl;
1✔
320
            publishError(new_units);
1✔
321
            ut_free(from) ;
1✔
322
            ut_free(to) ;
1✔
323
            return -1 ;
1✔
324
        }
325

326
        // Create a converter from the base to the requested
327
        auto new_conversion_factor = ut_get_converter(from, to) ;
9✔
328
        ut_free(from) ;
9✔
329
        ut_free(to) ;
9✔
330
        if ( !new_conversion_factor ) {
9✔
331
            std::ostringstream oss;
1✔
332
            oss << "[" << getName() << "] cannot convert units from [" << getBaseUnits()
1✔
333
                << "] to [" << new_units << "]";
1✔
334
            publish(MSG_ERROR, oss.str());
1✔
335
            return -1 ;
1✔
336
        } else {
337
            _conversion_factor = new_conversion_factor;
8✔
338
        }
339
    
340
        // Set the requested units. This will cause the unit string to be printed in write_value_ascii
341
        _requested_units = new_units;
8✔
342
    }
343
    return 0;
8✔
344
}
345

346
int Trick::VariableReference::stageValue(bool validate_address) {
8,039✔
347
    _write_ready = false;
8,039✔
348

349
    // Copy <size> bytes from <address> to staging_point.
350

351
    // Try to recreate connection if it has been broken
352
    if (_var_info->address == &_bad_ref_int) {
8,039✔
353
        REF2 *new_ref = ref_attributes(_var_info->reference);
1✔
354
        if (new_ref != NULL) {
1✔
355
            _var_info = new_ref;
×
356
            _address = _var_info->address;
×
357
            // _requested_units = "";
358
        }
359
    }
360

361
    // if there's a pointer somewhere in the address path, follow it in case pointer changed
362
    // BUT: if we've indexed into an STL container, then the address already points to the correct location
363
    // and follow_address_path would incorrectly recalculate it (it doesn't understand STL indexing)
364
    // Use the _used_stl_indexing flag that was set during construction when we detected STL indexing
365
    // (when num_index > attr->num_index and attr->type == TRICK_STL)
366
    if ( _var_info->pointer_present == 1 && !_used_stl_indexing ) {
8,039✔
367
        _address = follow_address_path(_var_info) ;
×
368
        if (_address == NULL) {
×
369
            tagAsInvalid();
×
370
        } else if ( validate_address ) {
×
371
            validate();
×
372
        } else {
373
            _var_info->address = _address ;
×
374
        }
375
    }
376

377
    // if this variable is a string we need to get the raw character string out of it.
378
    if (( _trick_type == TRICK_STRING ) && !_deref) {
8,039✔
379
        std::string * str_ptr = (std::string *)_var_info->address ;
5✔
380
        // Get a pointer to the internal character array
381
        _address = (void *)(str_ptr->c_str()) ;
5✔
382
    }
383

384
    // if this variable itself is a pointer, dereference it
385
    if ( _deref ) {
8,039✔
386
        _address = *(void**)_var_info->address ;
×
387
    }
388

389
    // handle c++ string and char*
390
    if ( _trick_type == TRICK_STRING ) {
8,039✔
391
        if (_address == NULL) {
5✔
392
            _size = 0 ;
×
393
        } else {
394
            _size = strlen((char*)_address) + 1 ;
5✔
395
        }
396
    }
397
    // handle c++ wstring and wchar_t*
398
    if ( _trick_type == TRICK_WSTRING ) {
8,039✔
399
        if (_address == NULL) {
×
400
            _size = 0 ;
×
401
        } else {
402
            _size = wcslen((wchar_t *)_address) * sizeof(wchar_t);
×
403
        }
404
    }
405
    if(_address != NULL) {
8,039✔
406
        memcpy( _stage_buffer , _address , _size ) ;
8,039✔
407
    }
408

409
    _staged = true;
8,039✔
410
    return 0;
8,039✔
411
}
412

413
bool Trick::VariableReference::validate() {
3✔
414
    // The address is not NULL.
415
    // Should be called by VariableServer Session if validateAddress is on.
416
    // check the memory manager if the address falls into
417
    // any of the memory blocks it knows of.  Don't do this if we have a std::string or
418
    // wstring type, or we already are pointing to a bad ref.
419
    if ( (_trick_type != TRICK_STRING) and
9✔
420
            (_trick_type != TRICK_WSTRING) and
3✔
421
            (_var_info->address != &_bad_ref_int) and
8✔
422
            (get_alloc_info_of(_address) == NULL) ) {
2✔
423
        
424
        // This variable is broken, make it into an error ref
425
        tagAsInvalid();
1✔
426
        return false;
1✔
427
    }
428

429
    // Everything is fine
430
    return true;
2✔
431
}
432

433
static void write_escaped_string( std::ostream& os, const char* s) {
4✔
434
    for (int ii=0 ; ii<strlen(s) ; ii++) {
41✔
435
        if (isprint(s[ii])) {
37✔
436
            os << s[ii];
29✔
437
        } else {
438
            switch ((s)[ii]) {
8✔
439
                case '\n': os << "\\n"; break;
2✔
440
                case '\t': os << "\\t"; break;
1✔
441
                case '\b': os << "\\b"; break;
1✔
442
                case '\a': os << "\\a"; break;
1✔
443
                case '\f': os << "\\f"; break;
1✔
444
                case '\r': os << "\\n"; break;
1✔
445
                case '\v': os << "\\v"; break;
1✔
446
                case '\"': os << "\\\""; break;
×
447
                default  : {
×
448
                    // Replicating behavior from original vs_format_ascii
449
                    char temp_s[6];
450
                    sprintf(temp_s, "\\x%02x", s[ii]);
×
451
                    os << temp_s ; 
×
452
                    break;
×
453
                }
454
            }
455
        }
456
    }
457
}
4✔
458

459
int Trick::VariableReference::getSizeAscii() const {
×
460
    std::stringstream ss;
×
461
    writeValueAscii(ss);
×
462
    return ss.str().length();
×
463
}
464

465

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

469
    if (!isWriteReady()) {
4,030✔
470
        return -1;
1✔
471
    }
472

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

476
    if (_trick_type == TRICK_STL) {
4,029✔
477
        // If the variable is an STL type, use the STL element type for writting value
NEW
478
        local_type = _var_info->attr->stl_elem_type;
×
479
    }
480

481
    int bytes_written = 0;
4,029✔
482
    void * buf_ptr = _write_buffer ;
4,029✔
483
    while (bytes_written < _size) {
8,062✔
484
        bytes_written += _var_info->attr->size ;
4,033✔
485

486
        switch (local_type) {
4,033✔
487

488
        case TRICK_CHARACTER:
2✔
489
            if (_var_info->attr->num_index == _var_info->num_index) {
2✔
490
                // Single char
491
                out << (int)cv_convert_double(_conversion_factor, *(char *)buf_ptr);
1✔
492
            } else {
493
                // All but last dim specified, leaves a char array 
494
                write_escaped_string(out, (const char *) buf_ptr);
1✔
495
                bytes_written = _size ;
1✔
496
            }
497
            break;
2✔
498
        case TRICK_UNSIGNED_CHARACTER:
2✔
499
            if (_var_info->attr->num_index == _var_info->num_index) {
2✔
500
                // Single char
501
                out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned char *)buf_ptr);
1✔
502
            } else {
503
                // All but last dim specified, leaves a char array 
504
                write_escaped_string(out, (const char *) buf_ptr);
1✔
505
                bytes_written = _size ;
1✔
506
            }
507
            break;
2✔
508

509
        case TRICK_WCHAR:{
2✔
510
                if (_var_info->attr->num_index == _var_info->num_index) {
2✔
511
                    out << *(wchar_t *) buf_ptr;
1✔
512
                } else {
513
                    // convert wide char string char string
514
                    size_t len = wcs_to_ncs_len((wchar_t *)buf_ptr) + 1 ;
2✔
515

516
                    char temp_buf[len];
1✔
517
                    wcs_to_ncs((wchar_t *) buf_ptr, temp_buf, len);
1✔
518
                    out << temp_buf;
1✔
519
                    bytes_written = _size ;
2✔
520
                }
521
            }
522
            break;
2✔
523

524
        case TRICK_STRING:
2✔
525
            if ((char *) buf_ptr != NULL) {
2✔
526
                write_escaped_string(out, (const char *) buf_ptr);
2✔
527
                bytes_written = _size ;
2✔
528
            } else {
529
                out << '\0';
×
530
            }
531
            break;
2✔
532

533
        case TRICK_WSTRING:
×
534
            if ((wchar_t *) buf_ptr != NULL) {
×
535
                // convert wide char string char string
536
                size_t len = wcs_to_ncs_len( (wchar_t *)buf_ptr) + 1 ;
×
537

538
                char temp_buf[len];
×
539
                wcs_to_ncs(  (wchar_t *) buf_ptr, temp_buf, len);
×
540
                out << temp_buf;
×
541
                bytes_written = _size ;
×
542
            } else {
543
                out << '\0';
×
544
            }
545
            break;
×
546
        case TRICK_SHORT:
1✔
547
            out << (short)cv_convert_double(_conversion_factor,*(short *)buf_ptr);
1✔
548
            break;
1✔
549

550
        case TRICK_UNSIGNED_SHORT:
×
551
            out << (unsigned short)cv_convert_double(_conversion_factor,*(unsigned short *)buf_ptr);
×
552
            break;
×
553

554
        case TRICK_INTEGER:
4,012✔
555
        case TRICK_ENUMERATED:
556
            out << (int)cv_convert_double(_conversion_factor,*(int *)buf_ptr);
4,012✔
557
            break;
4,012✔
558

559
        case TRICK_BOOLEAN:
1✔
560
            out << (int)cv_convert_double(_conversion_factor,*(bool *)buf_ptr);
1✔
561
            break;
1✔
562

563
        case TRICK_BITFIELD:
×
564
            out << (GET_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size));
×
565
            break;
×
566

567
        case TRICK_UNSIGNED_BITFIELD:
×
568
            out << (GET_UNSIGNED_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size));
×
569
            break;
×
570
            
571
        case TRICK_UNSIGNED_INTEGER:
×
572
            out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned int *)buf_ptr);
×
573
            break;
×
574

575
        case TRICK_LONG: {
1✔
576
            long l = *(long *)buf_ptr;
1✔
577
            if (_conversion_factor != cv_get_trivial()) {
1✔
578
                l = (long)cv_convert_double(_conversion_factor, l);
1✔
579
            }
580
            out << l;
1✔
581
            break;
1✔
582
        }
583

584
        case TRICK_UNSIGNED_LONG: {
1✔
585
            unsigned long ul = *(unsigned long *)buf_ptr;
1✔
586
            if (_conversion_factor != cv_get_trivial()) {
1✔
587
                ul = (unsigned long)cv_convert_double(_conversion_factor, ul);
1✔
588
            }
589
            out << ul;
1✔
590
            break;
1✔
591
        }
592

593
        case TRICK_FLOAT:
×
594
            out << std::setprecision(8) << cv_convert_float(_conversion_factor,*(float *)buf_ptr);
×
595
            break;
×
596

597
        case TRICK_DOUBLE:
5✔
598
            out << std::setprecision(16) << cv_convert_double(_conversion_factor,*(double *)buf_ptr);
5✔
599
            break;
5✔
600

601
        case TRICK_LONG_LONG: {
1✔
602
            long long ll = *(long long *)buf_ptr;
1✔
603
            if (_conversion_factor != cv_get_trivial()) {
1✔
604
                ll = (long long)cv_convert_double(_conversion_factor, ll);
1✔
605
            }
606
            out << ll;
1✔
607
            break;
1✔
608
        }
609

610
        case TRICK_UNSIGNED_LONG_LONG: {
1✔
611
            unsigned long long ull = *(unsigned long long *)buf_ptr;
1✔
612
            if (_conversion_factor != cv_get_trivial()) {
1✔
613
                ull = (unsigned long long)cv_convert_double(_conversion_factor, ull);
1✔
614
            }
615
            out << ull;
1✔
616
            break;
1✔
617
        }
618

619
        case TRICK_NUMBER_OF_TYPES:
2✔
620
            out << "BAD_REF";
2✔
621
            break;
2✔
622

623
        default:{
×
624

625
            break;
×
626
        }
627
        } // end switch
628

629
        if (bytes_written < _size) {
4,033✔
630
        // if returning an array, continue array as comma separated values
631
            out << ",";
4✔
632
            buf_ptr = (void*) ((long)buf_ptr + _var_info->attr->size) ;
4✔
633
        }
634
    } //end while
635

636
    if (_requested_units != "") {
4,029✔
637
        if ( _var_info->attr->mods & TRICK_MODS_UNITSDASHDASH ) {
8✔
638
            out << " {--}";
×
639
        } else {
640
            out << " {" << _requested_units << "}";
8✔
641
        }
642
    }
643

644
    return 0;
4,029✔
645
}
646

647
void Trick::VariableReference::tagAsInvalid () {
2✔
648
    std::string save_name(getName()) ;
2✔
649
    free(_var_info) ;
2✔
650
    _var_info = make_error_ref(save_name) ;
2✔
651
    _address = _var_info->address ;
2✔
652
}
2✔
653

654

655
int Trick::VariableReference::prepareForWrite() {
8,039✔
656
    if (!_staged) {
8,039✔
657
        return 1;
1✔
658
    }
659

660
    void * temp_p = _stage_buffer;
8,038✔
661
    _stage_buffer = _write_buffer;
8,038✔
662
    _write_buffer = temp_p;
8,038✔
663

664
    _staged = false;
8,038✔
665
    _write_ready = true;
8,038✔
666
    return 0;
8,038✔
667
}
668

669
bool Trick::VariableReference::isStaged() const {
8,003✔
670
    return _staged;
8,003✔
671
}
672

673
bool Trick::VariableReference::isWriteReady() const {
4,035✔
674
    return _write_ready;
4,035✔
675
}
676

677
int Trick::VariableReference::writeTypeBinary( std::ostream& out, bool byteswap ) const {
4,000✔
678
    int local_type = _trick_type;
4,000✔
679
    if (byteswap) {
4,000✔
680
        local_type = trick_byteswap_int(local_type);
×
681
    }
682
    out.write(const_cast<const char *>(reinterpret_cast<char *>(&local_type)), sizeof(int));
4,000✔
683

684
    return 0;
4,000✔
685
}
686

687
int Trick::VariableReference::writeSizeBinary( std::ostream& out, bool byteswap ) const {
4,000✔
688
    int local_size = _size;
4,000✔
689
    if (byteswap) {
4,000✔
690
        local_size = trick_byteswap_int(local_size);
×
691
    }
692
    out.write(const_cast<const char *>(reinterpret_cast<char *>(&local_size)), sizeof(int));
4,000✔
693

694
    return 0;
4,000✔
695
}
696

697
int Trick::VariableReference::writeNameBinary( std::ostream& out, bool byteswap ) const {
4,001✔
698
    std::string name = getName();
4,001✔
699
    out.write(name.c_str(), name.size());
4,001✔
700

701
    return 0;
8,002✔
702
}
703

704
int Trick::VariableReference::writeNameLengthBinary( std::ostream& out, bool byteswap ) const {
4,001✔
705
    int name_size = getName().size();
4,001✔
706
    if (byteswap) {
4,001✔
707
        name_size = trick_byteswap_int(name_size);
×
708
    }
709

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

712
    return 0;
4,001✔
713
}
714

715

716
void Trick::VariableReference::byteswap_var (char * out, char * in) const {
1✔
717
    byteswap_var(out, in, *this);
1✔
718
}
1✔
719

720

721
void Trick::VariableReference::byteswap_var (char * out, char * in, const VariableReference& ref) {
9✔
722
    ATTRIBUTES * attr = ref._var_info->attr;
9✔
723
    int array_size = 1;
9✔
724

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

730
    switch (attr->size) {
9✔
731
        case 1:
2✔
732
            // If these are just characters, no need to byteswap
733
            for (int j = 0; j < array_size; j++) {
10✔
734
                out[j] = in[j];
8✔
735
            }
736
            break;
2✔
737

738
        case 2: {
1✔
739
            short * short_in = reinterpret_cast<short *> (in);
1✔
740
            short * short_out = reinterpret_cast<short *> (out);
1✔
741

742
            for (int j = 0; j < array_size; j++) {
2✔
743
                short_out[j] = trick_byteswap_short(short_in[j]);
1✔
744
            }
745
            break;
1✔
746
        }
747

748
        case 4: {
4✔
749
            int * int_in = reinterpret_cast<int *> (in);
4✔
750
            int * int_out = reinterpret_cast<int *> (out);
4✔
751

752
            for (int j = 0; j < array_size; j++) {
131✔
753
                int_out[j] = trick_byteswap_int(int_in[j]);
127✔
754
            }
755
            break;
4✔
756
        }
757
        case 8: {
2✔
758
            // We don't actually care if this is double or long, just that it's the right size
759
            double * double_in = reinterpret_cast<double *> (in);
2✔
760
            double * double_out = reinterpret_cast<double *> (out);
2✔
761

762
            for (int j = 0; j < array_size; j++) {
8✔
763
                double_out[j] = trick_byteswap_double(double_in[j]);
6✔
764
            }
765
            break;
2✔
766
        }
767
    }
768
}
9✔
769

770

771

772
int Trick::VariableReference::writeValueBinary( std::ostream& out, bool byteswap ) const {
4,006✔
773
    // local_type is set to the type of the attribute, but if it's a STL type, we need to use the element type.
774
    TRICK_TYPE local_type = _trick_type;
4,006✔
775
    if (local_type == TRICK_STL) {
4,006✔
776
        // If the variable is an STL type, use the STL element type for writing value
NEW
777
        local_type = _var_info->attr->stl_elem_type;
×
778
    }
779

780
    if ( local_type == TRICK_BITFIELD ) {
4,006✔
781
        int temp_i = GET_BITFIELD(_write_buffer , _var_info->attr->size ,
×
782
            _var_info->attr->index[0].start, _var_info->attr->index[0].size) ;
783
        out.write((char *)(&temp_i), _size);
×
784
        return _size;
×
785
    }
786

787
    if ( local_type == TRICK_UNSIGNED_BITFIELD ) {
4,006✔
788
        int temp_unsigned = GET_UNSIGNED_BITFIELD(_write_buffer , _var_info->attr->size ,
×
789
                _var_info->attr->index[0].start, _var_info->attr->index[0].size) ;
790
        out.write((char *)(&temp_unsigned), _size);
×
791
        return _size;
×
792
    }
793

794
    if (local_type ==  TRICK_NUMBER_OF_TYPES) {
4,006✔
795
        // TRICK_NUMBER_OF_TYPES is an error case
796
        int temp_zero = 0 ;
×
797
        out.write((char *)(&temp_zero), _size);
×
798
        return _size;
×
799
    }
800

801
    if (byteswap) {
4,006✔
802
        char * byteswap_buf = (char *) calloc (_size, 1);
1✔
803
        byteswap_var(byteswap_buf, (char *) _write_buffer);
1✔
804
        out.write(byteswap_buf, _size);
1✔
805
        free (byteswap_buf);
1✔
806
    }
807
    else {
808
        out.write((char *) _write_buffer, _size);
4,005✔
809
    }
810

811
    return _size;
4,006✔
812
    
813
}  
814

815
std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableReference& ref) {
6✔
816
    s << "      \"" << ref.getName() << "\"";
6✔
817
    return s;
6✔
818
}
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