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

nasa / trick / 25870723371

14 May 2026 04:03PM UTC coverage: 56.928% (+0.2%) from 56.774%
25870723371

Pull #2117

github

web-flow
Merge 694a6c711 into af8c162b8
Pull Request #2117: Completed std::wstring support.

24 of 43 new or added lines in 6 files covered. (55.81%)

199 existing lines in 2 files now uncovered.

12974 of 22790 relevant lines covered (56.93%)

298441.05 hits per line

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

71.89
/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) {
11✔
25
    REF2* new_ref;
26
    new_ref = (REF2*)calloc(1, sizeof(REF2));
11✔
27
    new_ref->reference = strdup(in_name.c_str()) ;
11✔
28
    new_ref->units = NULL ;
11✔
29
    new_ref->address = (char *)&_bad_ref_int ;
11✔
30
    new_ref->attr = (ATTRIBUTES*)calloc(1, sizeof(ATTRIBUTES)) ;
11✔
31
    new_ref->attr->type = TRICK_NUMBER_OF_TYPES ;
11✔
32
    new_ref->attr->units = (char *)"--" ;
11✔
33
    new_ref->attr->size = sizeof(int) ;
11✔
34
    return new_ref;
11✔
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,082✔
90

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

98
    // Handle error cases
99
    if ( _var_info == NULL ) {
8,082✔
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());
9✔
103
        _var_info = make_error_ref(var_name);
9✔
104
    } else if ( _var_info->attr ) {
8,073✔
105
        if ( _var_info->attr->type == TRICK_STRUCTURED ) {
8,073✔
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,082✔
122
    _address = _var_info->address;
8,082✔
123
    _size = _var_info->attr->size ;
8,082✔
124
    _deref = false;
8,082✔
125

126
    // Deal with weirdness around string vs wstring
127
    _trick_type = _var_info->attr->type ;
8,082✔
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,082✔
140
    if ( _var_info->attr->type == TRICK_STL && _var_info->reference ) {
8,082✔
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,080✔
204
            _used_stl_indexing = true;
×
205
        }
206

207
        if ( _var_info->num_index == _var_info->attr->num_index ) {
8,080✔
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
216
            if ((_var_info->attr->num_index - _var_info->num_index) > 1 ) {
×
217
                message_publish(MSG_ERROR, "Variable Server Error: var_add(%s) requests more than one dimension of dynamic array.\n", _var_info->reference);
×
218
                message_publish(MSG_ERROR, "Data is not contiguous so returned values are unpredictable.\n") ;
×
219
            }
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 ) {
×
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,082✔
234
        _size = MAX_ARRAY_LENGTH ;
9✔
235
    }
236

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

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

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

249
Trick::VariableReference::~VariableReference() {
79✔
250
    if (_var_info != NULL) {
79✔
251
        free( _var_info );
79✔
252
        _var_info = NULL;
79✔
253
    }
254
    if (_stage_buffer != NULL) {
79✔
255
        free (_stage_buffer);
79✔
256
        _stage_buffer = NULL;
79✔
257
    }
258
    if (_write_buffer != NULL) {
79✔
259
        free (_write_buffer);
79✔
260
        _write_buffer = NULL;
79✔
261
    }
262
    if (_conversion_factor != NULL) {
79✔
263
        cv_free(_conversion_factor);
79✔
264
    }
265
}
79✔
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✔
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✔
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
315
            std::ostringstream oss;
×
316
            oss << "[" << getName() << "] old-style units converted from ["
×
317
                << units_name << "] to [" << new_units << "]";
×
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✔
324
            std::cout << "Error in interpreting base units" << std::endl;
×
325
            publishError(getBaseUnits());
×
326
            ut_free(from) ;
×
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,065✔
361
    _write_ready = false;
8,065✔
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,065✔
367
        REF2 *new_ref = ref_attributes(_var_info->reference);
9✔
368
        if (new_ref != NULL) {
9✔
369
            _var_info = new_ref;
×
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,065✔
381
        _address = follow_address_path(_var_info) ;
15✔
382
        if (_address == NULL) {
15✔
383
            tagAsInvalid();
×
384
        } else if ( validate_address ) {
15✔
385
            validate();
×
386
        } else {
387
            _var_info->address = _address ;
15✔
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,065✔
393
        std::string * str_ptr = (std::string *)_var_info->address ;
8✔
394
        // Get a pointer to the internal character array
395
        _address = (void *)(str_ptr->c_str()) ;
8✔
396
    }
397

398
    // if this variable is a wstring we need to get the raw wide character string out of it.
399
    if ((_trick_type == TRICK_WSTRING) && !_deref)
8,065✔
400
    {
NEW
401
        std::wstring *wstr_ptr = (std::wstring *)_var_info->address;
×
402
        // Get a pointer to the internal wide character array
NEW
403
        _address = (void *)(wstr_ptr->c_str());
×
404
    }
405

406
    // if this variable itself is a pointer, dereference it
407
    if ( _deref ) {
8,065✔
408
        _address = *(void**)_var_info->address ;
×
409
    }
410

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

431
    _staged = true;
8,065✔
432
    return 0;
8,065✔
433
}
434

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

451
    // Everything is fine
452
    return true;
2✔
453
}
454

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

481
int Trick::VariableReference::getSizeAscii() const {
×
482
    std::stringstream ss;
×
483
    writeValueAscii(ss);
×
484
    return ss.str().length();
×
485
}
486

487

488
int Trick::VariableReference::writeValueAscii( std::ostream& out ) const {
4,056✔
489
    // This is copied and modified from vs_format_ascii
490

491
    if (!isWriteReady()) {
4,056✔
492
        return -1;
1✔
493
    }
494

495
    // local_type is set to the type of the attribute, but if it's a STL type, we need to use the element type.
496
    TRICK_TYPE local_type = _trick_type;
4,055✔
497

498
    if (_trick_type == TRICK_STL) {
4,055✔
499
        // If the variable is an STL type, use the STL element type for writting value
500
        local_type = _var_info->attr->stl_elem_type;
×
501
    }
502

503
    int bytes_written = 0;
4,055✔
504
    void * buf_ptr = _write_buffer ;
4,055✔
505
    while (bytes_written < _size) {
8,114✔
506
        bytes_written += _var_info->attr->size ;
4,059✔
507

508
        switch (local_type) {
4,059✔
509

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

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

538
                    char temp_buf[len];
1✔
539
                    wcs_to_ncs((wchar_t *) buf_ptr, temp_buf, len);
1✔
540
                    out << temp_buf;
1✔
541
                    bytes_written = _size ;
2✔
542
                }
543
            }
544
            break;
2✔
545

546
        case TRICK_STRING:
5✔
547
            if ((char *) buf_ptr != NULL) {
5✔
548
                write_escaped_string(out, (const char *) buf_ptr);
5✔
549
                bytes_written = _size ;
5✔
550
            } else {
551
                out << '\0';
×
552
            }
553
            break;
5✔
554

555
        case TRICK_WSTRING:
×
556
            if ((wchar_t *) buf_ptr != NULL) {
×
557
                // convert wide char string char string
558
                size_t len = wcs_to_ncs_len( (wchar_t *)buf_ptr) + 1 ;
×
559

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

572
        case TRICK_UNSIGNED_SHORT:
×
573
            out << (unsigned short)cv_convert_double(_conversion_factor,*(unsigned short *)buf_ptr);
×
574
            break;
×
575

576
        case TRICK_INTEGER:
4,019✔
577
        case TRICK_ENUMERATED:
578
            out << (int)cv_convert_double(_conversion_factor,*(int *)buf_ptr);
4,019✔
579
            break;
4,019✔
580

581
        case TRICK_BOOLEAN:
3✔
582
            out << (int)cv_convert_double(_conversion_factor,*(bool *)buf_ptr);
3✔
583
            break;
3✔
584

585
        case TRICK_BITFIELD:
×
586
            out << (GET_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_BITFIELD:
×
590
            out << (GET_UNSIGNED_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size));
×
591
            break;
×
592
            
593
        case TRICK_UNSIGNED_INTEGER:
1✔
594
            out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned int *)buf_ptr);
1✔
595
            break;
1✔
596

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

606
        case TRICK_UNSIGNED_LONG: {
1✔
607
            unsigned long ul = *(unsigned long *)buf_ptr;
1✔
608
            if (_conversion_factor != cv_get_trivial()) {
1✔
609
                ul = (unsigned long)cv_convert_double(_conversion_factor, ul);
1✔
610
            }
611
            out << ul;
1✔
612
            break;
1✔
613
        }
614

615
        case TRICK_FLOAT:
×
616
            out << std::setprecision(8) << cv_convert_float(_conversion_factor,*(float *)buf_ptr);
×
617
            break;
×
618

619
        case TRICK_DOUBLE:
7✔
620
            out << std::setprecision(16) << cv_convert_double(_conversion_factor,*(double *)buf_ptr);
7✔
621
            break;
7✔
622

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

632
        case TRICK_UNSIGNED_LONG_LONG: {
1✔
633
            unsigned long long ull = *(unsigned long long *)buf_ptr;
1✔
634
            if (_conversion_factor != cv_get_trivial()) {
1✔
635
                ull = (unsigned long long)cv_convert_double(_conversion_factor, ull);
1✔
636
            }
637
            out << ull;
1✔
638
            break;
1✔
639
        }
640

641
        case TRICK_NUMBER_OF_TYPES:
10✔
642
            out << "BAD_REF";
10✔
643
            break;
10✔
644

645
        default:{
×
646

647
            break;
×
648
        }
649
        } // end switch
650

651
        if (bytes_written < _size) {
4,059✔
652
        // if returning an array, continue array as comma separated values
653
            out << ",";
4✔
654
            buf_ptr = (void*) ((long)buf_ptr + _var_info->attr->size) ;
4✔
655
        }
656
    } //end while
657

658
    if (_requested_units != "") {
4,055✔
659
        if ( _var_info->attr->mods & TRICK_MODS_UNITSDASHDASH ) {
8✔
660
            out << " {--}";
×
661
        } else {
662
            out << " {" << _requested_units << "}";
8✔
663
        }
664
    }
665

666
    return 0;
4,055✔
667
}
668

669
void Trick::VariableReference::tagAsInvalid () {
2✔
670
    std::string save_name(getName()) ;
2✔
671
    free(_var_info) ;
2✔
672
    _var_info = make_error_ref(save_name) ;
2✔
673
    _address = _var_info->address ;
2✔
674
}
2✔
675

676

677
int Trick::VariableReference::prepareForWrite() {
8,065✔
678
    if (!_staged) {
8,065✔
679
        return 1;
1✔
680
    }
681

682
    void * temp_p = _stage_buffer;
8,064✔
683
    _stage_buffer = _write_buffer;
8,064✔
684
    _write_buffer = temp_p;
8,064✔
685

686
    _staged = false;
8,064✔
687
    _write_ready = true;
8,064✔
688
    return 0;
8,064✔
689
}
690

691
bool Trick::VariableReference::isStaged() const {
8,029✔
692
    return _staged;
8,029✔
693
}
694

695
bool Trick::VariableReference::isWriteReady() const {
4,061✔
696
    return _write_ready;
4,061✔
697
}
698

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

706
    return 0;
4,000✔
707
}
708

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

716
    return 0;
4,000✔
717
}
718

719
int Trick::VariableReference::writeNameBinary( std::ostream& out, bool byteswap ) const {
4,001✔
720
    std::string name = getName();
4,001✔
721
    out.write(name.c_str(), name.size());
4,001✔
722

723
    return 0;
8,002✔
724
}
725

726
int Trick::VariableReference::writeNameLengthBinary( std::ostream& out, bool byteswap ) const {
4,001✔
727
    int name_size = getName().size();
4,001✔
728
    if (byteswap) {
4,001✔
729
        name_size = trick_byteswap_int(name_size);
×
730
    }
731

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

734
    return 0;
4,001✔
735
}
736

737

738
void Trick::VariableReference::byteswap_var (char * out, char * in) const {
1✔
739
    byteswap_var(out, in, *this);
1✔
740
}
1✔
741

742

743
void Trick::VariableReference::byteswap_var (char * out, char * in, const VariableReference& ref) {
9✔
744
    ATTRIBUTES * attr = ref._var_info->attr;
9✔
745
    int array_size = 1;
9✔
746

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

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

760
        case 2: {
1✔
761
            short * short_in = reinterpret_cast<short *> (in);
1✔
762
            short * short_out = reinterpret_cast<short *> (out);
1✔
763

764
            for (int j = 0; j < array_size; j++) {
2✔
765
                short_out[j] = trick_byteswap_short(short_in[j]);
1✔
766
            }
767
            break;
1✔
768
        }
769

770
        case 4: {
4✔
771
            int * int_in = reinterpret_cast<int *> (in);
4✔
772
            int * int_out = reinterpret_cast<int *> (out);
4✔
773

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

784
            for (int j = 0; j < array_size; j++) {
8✔
785
                double_out[j] = trick_byteswap_double(double_in[j]);
6✔
786
            }
787
            break;
2✔
788
        }
789
    }
790
}
9✔
791

792

793

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

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

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

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

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

833
    return _size;
4,006✔
834
    
835
}  
836

837
std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableReference& ref) {
6✔
838
    s << "      \"" << ref.getName() << "\"";
6✔
839
    return s;
6✔
840
}
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