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

nasa / trick / 20283694683

16 Dec 2025 09:44PM UTC coverage: 55.586% (-0.3%) from 55.934%
20283694683

push

github

web-flow
Add vs support for stl vector, deque, and array containers of supported data types (#1996)

* Added TV GUI side support for vectors

* Initial check-in for variable server to support vector.

* Added accessor functions to ATTRIBUTES to get stl container size and element at index so mm ref_dim can get element at index for a vector as getting element of an array plus bounds check for vector.

* Added support to deque and array as they be treated similar to vector.

* Updated the test due to vs vector support change.

* Added vector/deque/array of TRICK_STRUCTURED data type support.

* Updated to support vector<bool>.

* Updated to support vector/deque/array of *.

* Updated to support vector/deque/array of TRICK_ENUMERATED and display enum name for the stl enum elements for TV; also updated to show bool element as normal bool var for TV.

* Made elements of vector/array/deque to be editable from TV.

* Not to change the stl type to element when not indexing.

* Added special handling for updating vector of bool since std::vector<bool> is optimized to store bits compactly (8 bools per byte) in c++.

* Need to print NULL for the newly added ATTRIBUTES assessor function for filed attributes in io src file.

* Added var_get_stl_size command to vs sesssion and updated tv to show index range as constraint arrays for vector/deque/array when adding a var.

* Added test cases for vector/deque/array and also changed to use its own vs msg type for var_get_stl_size.

* Removed the unused function as it was changed to use clang api instead.

* Updated not to use static temp attr for working on storing vector<boo> to its ref_attr.

---------

Co-authored-by: plherrin <pherring04@gmail.com>

46 of 212 new or added lines in 8 files covered. (21.7%)

2 existing lines in 2 files now uncovered.

12508 of 22502 relevant lines covered (55.59%)

304713.82 hits per line

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

69.72
/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✔
NEW
154
                case TRICK_CHARACTER:
×
155
                case TRICK_UNSIGNED_CHARACTER:
156
                case TRICK_BOOLEAN:
NEW
157
                    _size = sizeof(char);
×
NEW
158
                    break;
×
NEW
159
                case TRICK_SHORT:
×
160
                case TRICK_UNSIGNED_SHORT:
NEW
161
                    _size = sizeof(short);
×
NEW
162
                    break;
×
163
                case TRICK_INTEGER:
1✔
164
                case TRICK_UNSIGNED_INTEGER:
165
                case TRICK_ENUMERATED:
166
                    _size = sizeof(int);
1✔
167
                    break;
1✔
NEW
168
                case TRICK_LONG:
×
169
                case TRICK_UNSIGNED_LONG:
NEW
170
                    _size = sizeof(long);
×
NEW
171
                    break;
×
NEW
172
                case TRICK_LONG_LONG:
×
173
                case TRICK_UNSIGNED_LONG_LONG:
NEW
174
                    _size = sizeof(long long);
×
NEW
175
                    break;
×
NEW
176
                case TRICK_FLOAT:
×
NEW
177
                    _size = sizeof(float);
×
NEW
178
                    break;
×
NEW
179
                case TRICK_DOUBLE:
×
NEW
180
                    _size = sizeof(double);
×
NEW
181
                    break;
×
NEW
182
                case TRICK_STRING:
×
183
                case TRICK_WSTRING:
184
                case TRICK_STRUCTURED:
NEW
185
                    _size = sizeof(void*);
×
NEW
186
                    break;
×
NEW
187
                default:
×
188
                    // Keep existing size for unknown types
NEW
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 if ( _var_info->reference && strstr(_var_info->reference, "].") ) {
8,054✔
196
        // Case 2: Pattern "]." indicates indexing followed by member access (e.g., point_vec[0].x)
197
        // Check if the last "]." is followed only by member names (no more ']')
NEW
198
        const char* last_bracket_dot = strstr(_var_info->reference, "].");
×
199
        // Find the last occurrence of "]."
NEW
200
        const char* temp = last_bracket_dot;
×
NEW
201
        while (temp != NULL && (temp = strstr(temp + 2, "].")) != NULL) {
×
NEW
202
            last_bracket_dot = temp;
×
203
        }
204
        // After the last "].", check if there are no more ']'
NEW
205
        if (strchr(last_bracket_dot + 2, ']') == NULL) {
×
206
            // The final attr is already the member's attributes, so _trick_type and _size are correct.
207
            // Just set the flag to skip follow_address_path() which doesn't understand STL indexing.
208
            // Note: This also matches pointer arrays like ptr_array[0].x, but that's okay - the address
209
            // is already correct and skipping follow_address_path() won't hurt.
NEW
210
            _used_stl_indexing = true;
×
NEW
211
        }
×
212
    } else if ( _var_info->num_index == _var_info->attr->num_index ) {
8,054✔
213
        // single value - nothing else necessary
214
    } else if ( _var_info->attr->index[_var_info->attr->num_index - 1].size != 0 ) {
10✔
215
        // Constrained array
216
        for ( int i = _var_info->attr->num_index-1;  i > _var_info->num_index-1 ; i-- ) {
23✔
217
            _size *= _var_info->attr->index[i].size ;
13✔
218
        }
219
    } else {
220
        // Unconstrained array
221
        if ((_var_info->attr->num_index - _var_info->num_index) > 1 ) {
×
222
            message_publish(MSG_ERROR, "Variable Server Error: var_add(%s) requests more than one dimension of dynamic array.\n", _var_info->reference);
×
223
            message_publish(MSG_ERROR, "Data is not contiguous so returned values are unpredictable.\n") ;
×
224
        }
225
        if ( _var_info->attr->type == TRICK_CHARACTER ) {
×
226
            _trick_type = TRICK_STRING ;
×
227
            _deref = true;
×
228
        } else if ( _var_info->attr->type == TRICK_WCHAR ) {
×
229
            _trick_type = TRICK_WSTRING ;
×
230
            _deref = true;
×
231
        } else {
232
            _deref = true ;
×
233
            _size *= get_size((char*)_address) ;
×
234
        }
235
    }
236
    // handle strings: set a max buffer size, the copy size may vary so will be set in copy_sim_data
237
    if (( _trick_type == TRICK_STRING ) || ( _trick_type == TRICK_WSTRING )) {
8,056✔
238
        _size = MAX_ARRAY_LENGTH ;
6✔
239
    }
240

241
    // Allocate stage and write buffers
242
    _stage_buffer = calloc(_size, 1) ;
8,056✔
243
    _write_buffer = calloc(_size, 1) ;
8,056✔
244

245
    _conversion_factor = cv_get_trivial();
8,056✔
246
    _base_units = _var_info->attr->units;
8,056✔
247
    _requested_units = "";
8,056✔
248
    _name = _var_info->reference;
8,056✔
249

250
    // Done!
251
}
8,056✔
252

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

271
std::string Trick::VariableReference::getName() const {
12,021✔
272
    return _name;
12,021✔
273
}
274

275
int Trick::VariableReference::getSizeBinary() const {
4,000✔
276
    return _size;
4,000✔
277
}
278

279
TRICK_TYPE Trick::VariableReference::getType() const {
4✔
280
    return _trick_type;
4✔
281
}
282

283
std::string Trick::VariableReference::getBaseUnits() const {
12✔
284
    return _base_units;
12✔
285
}
286

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

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

302
    // If the units_name parameter is "xx", set it to the current units.
303
    if (!units_name.compare("xx")) {
10✔
304
        units_name = getBaseUnits();
×
305
    }
306

307
    // Don't try to convert units for a bad ref
308
    if (_var_info->address == &_bad_ref_int) {
10✔
309
        return -1 ;
×
310
    }
311

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

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

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

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

364
int Trick::VariableReference::stageValue(bool validate_address) {
8,039✔
365
    _write_ready = false;
8,039✔
366

367
    // Copy <size> bytes from <address> to staging_point.
368

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

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

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

402
    // if this variable itself is a pointer, dereference it
403
    if ( _deref ) {
8,039✔
404
        _address = *(void**)_var_info->address ;
×
405
    }
406

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

427
    _staged = true;
8,039✔
428
    return 0;
8,039✔
429
}
430

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

447
    // Everything is fine
448
    return true;
2✔
449
}
450

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

477
int Trick::VariableReference::getSizeAscii() const {
×
478
    std::stringstream ss;
×
479
    writeValueAscii(ss);
×
480
    return ss.str().length();
×
481
}
482

483

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

487
    if (!isWriteReady()) {
4,030✔
488
        return -1;
1✔
489
    }
490

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

494
    if (_trick_type == TRICK_STL) {
4,029✔
495
        // If the variable is an STL type, use the STL element type for writting value
NEW
496
        local_type = _var_info->attr->stl_elem_type;
×
497
    }
498

499
    int bytes_written = 0;
4,029✔
500
    void * buf_ptr = _write_buffer ;
4,029✔
501
    while (bytes_written < _size) {
8,062✔
502
        bytes_written += _var_info->attr->size ;
4,033✔
503

504
        switch (local_type) {
4,033✔
505

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

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

534
                    char temp_buf[len];
1✔
535
                    wcs_to_ncs((wchar_t *) buf_ptr, temp_buf, len);
1✔
536
                    out << temp_buf;
1✔
537
                    bytes_written = _size ;
2✔
538
                }
539
            }
540
            break;
2✔
541

542
        case TRICK_STRING:
2✔
543
            if ((char *) buf_ptr != NULL) {
2✔
544
                write_escaped_string(out, (const char *) buf_ptr);
2✔
545
                bytes_written = _size ;
2✔
546
            } else {
547
                out << '\0';
×
548
            }
549
            break;
2✔
550

551
        case TRICK_WSTRING:
×
552
            if ((wchar_t *) buf_ptr != NULL) {
×
553
                // convert wide char string char string
554
                size_t len = wcs_to_ncs_len( (wchar_t *)buf_ptr) + 1 ;
×
555

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

568
        case TRICK_UNSIGNED_SHORT:
×
569
            out << (unsigned short)cv_convert_double(_conversion_factor,*(unsigned short *)buf_ptr);
×
570
            break;
×
571

572
        case TRICK_INTEGER:
4,012✔
573
        case TRICK_ENUMERATED:
574
            out << (int)cv_convert_double(_conversion_factor,*(int *)buf_ptr);
4,012✔
575
            break;
4,012✔
576

577
        case TRICK_BOOLEAN:
1✔
578
            out << (int)cv_convert_double(_conversion_factor,*(bool *)buf_ptr);
1✔
579
            break;
1✔
580

581
        case TRICK_BITFIELD:
×
582
            out << (GET_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_BITFIELD:
×
586
            out << (GET_UNSIGNED_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size));
×
587
            break;
×
588
            
589
        case TRICK_UNSIGNED_INTEGER:
×
590
            out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned int *)buf_ptr);
×
591
            break;
×
592

593
        case TRICK_LONG: {
1✔
594
            long l = *(long *)buf_ptr;
1✔
595
            if (_conversion_factor != cv_get_trivial()) {
1✔
596
                l = (long)cv_convert_double(_conversion_factor, l);
1✔
597
            }
598
            out << l;
1✔
599
            break;
1✔
600
        }
601

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

611
        case TRICK_FLOAT:
×
612
            out << std::setprecision(8) << cv_convert_float(_conversion_factor,*(float *)buf_ptr);
×
613
            break;
×
614

615
        case TRICK_DOUBLE:
5✔
616
            out << std::setprecision(16) << cv_convert_double(_conversion_factor,*(double *)buf_ptr);
5✔
617
            break;
5✔
618

619
        case TRICK_LONG_LONG: {
1✔
620
            long long ll = *(long long *)buf_ptr;
1✔
621
            if (_conversion_factor != cv_get_trivial()) {
1✔
622
                ll = (long long)cv_convert_double(_conversion_factor, ll);
1✔
623
            }
624
            out << ll;
1✔
625
            break;
1✔
626
        }
627

628
        case TRICK_UNSIGNED_LONG_LONG: {
1✔
629
            unsigned long long ull = *(unsigned long long *)buf_ptr;
1✔
630
            if (_conversion_factor != cv_get_trivial()) {
1✔
631
                ull = (unsigned long long)cv_convert_double(_conversion_factor, ull);
1✔
632
            }
633
            out << ull;
1✔
634
            break;
1✔
635
        }
636

637
        case TRICK_NUMBER_OF_TYPES:
2✔
638
            out << "BAD_REF";
2✔
639
            break;
2✔
640

641
        default:{
×
642

643
            break;
×
644
        }
645
        } // end switch
646

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

654
    if (_requested_units != "") {
4,029✔
655
        if ( _var_info->attr->mods & TRICK_MODS_UNITSDASHDASH ) {
8✔
656
            out << " {--}";
×
657
        } else {
658
            out << " {" << _requested_units << "}";
8✔
659
        }
660
    }
661

662
    return 0;
4,029✔
663
}
664

665
void Trick::VariableReference::tagAsInvalid () {
2✔
666
    std::string save_name(getName()) ;
2✔
667
    free(_var_info) ;
2✔
668
    _var_info = make_error_ref(save_name) ;
2✔
669
    _address = _var_info->address ;
2✔
670
}
2✔
671

672

673
int Trick::VariableReference::prepareForWrite() {
8,039✔
674
    if (!_staged) {
8,039✔
675
        return 1;
1✔
676
    }
677

678
    void * temp_p = _stage_buffer;
8,038✔
679
    _stage_buffer = _write_buffer;
8,038✔
680
    _write_buffer = temp_p;
8,038✔
681

682
    _staged = false;
8,038✔
683
    _write_ready = true;
8,038✔
684
    return 0;
8,038✔
685
}
686

687
bool Trick::VariableReference::isStaged() const {
8,003✔
688
    return _staged;
8,003✔
689
}
690

691
bool Trick::VariableReference::isWriteReady() const {
4,035✔
692
    return _write_ready;
4,035✔
693
}
694

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

702
    return 0;
4,000✔
703
}
704

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

712
    return 0;
4,000✔
713
}
714

715
int Trick::VariableReference::writeNameBinary( std::ostream& out, bool byteswap ) const {
4,001✔
716
    std::string name = getName();
4,001✔
717
    out.write(name.c_str(), name.size());
4,001✔
718

719
    return 0;
8,002✔
720
}
721

722
int Trick::VariableReference::writeNameLengthBinary( std::ostream& out, bool byteswap ) const {
4,001✔
723
    int name_size = getName().size();
4,001✔
724
    if (byteswap) {
4,001✔
725
        name_size = trick_byteswap_int(name_size);
×
726
    }
727

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

730
    return 0;
4,001✔
731
}
732

733

734
void Trick::VariableReference::byteswap_var (char * out, char * in) const {
1✔
735
    byteswap_var(out, in, *this);
1✔
736
}
1✔
737

738

739
void Trick::VariableReference::byteswap_var (char * out, char * in, const VariableReference& ref) {
9✔
740
    ATTRIBUTES * attr = ref._var_info->attr;
9✔
741
    int array_size = 1;
9✔
742

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

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

756
        case 2: {
1✔
757
            short * short_in = reinterpret_cast<short *> (in);
1✔
758
            short * short_out = reinterpret_cast<short *> (out);
1✔
759

760
            for (int j = 0; j < array_size; j++) {
2✔
761
                short_out[j] = trick_byteswap_short(short_in[j]);
1✔
762
            }
763
            break;
1✔
764
        }
765

766
        case 4: {
4✔
767
            int * int_in = reinterpret_cast<int *> (in);
4✔
768
            int * int_out = reinterpret_cast<int *> (out);
4✔
769

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

780
            for (int j = 0; j < array_size; j++) {
8✔
781
                double_out[j] = trick_byteswap_double(double_in[j]);
6✔
782
            }
783
            break;
2✔
784
        }
785
    }
786
}
9✔
787

788

789

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

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

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

812
    if (local_type ==  TRICK_NUMBER_OF_TYPES) {
4,006✔
813
        // TRICK_NUMBER_OF_TYPES is an error case
814
        int temp_zero = 0 ;
×
815
        out.write((char *)(&temp_zero), _size);
×
816
        return _size;
×
817
    }
818

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

829
    return _size;
4,006✔
830
    
831
}  
832

833
std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableReference& ref) {
6✔
834
    s << "      \"" << ref.getName() << "\"";
6✔
835
    return s;
6✔
836
}
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

© 2025 Coveralls, Inc