@irsdk-node/native 4.1.1 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,997 @@
1
+ /*
2
+ Copyright (c) 2013, iRacing.com Motorsport Simulations, LLC.
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+ * Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+ * Neither the name of iRacing.com Motorsport Simulations nor the
13
+ names of its contributors may be used to endorse or promote products
14
+ derived from this software without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <stdio.h>
29
+ #include <string.h>
30
+ #include <time.h>
31
+
32
+ #include <assert.h>
33
+ #include "irsdk_defines.h"
34
+ #include "yaml_parser.h"
35
+ #include "irsdk_diskclient.h"
36
+
37
+ #pragma warning(disable:4996)
38
+
39
+ irsdkDiskClient::irsdkDiskClient()
40
+ : m_ibtFile(NULL)
41
+ , m_sessionInfoString(NULL)
42
+ , m_varHeaders(NULL)
43
+ , m_varBuf(NULL)
44
+ , m_ibtFileSize(0)
45
+ {
46
+ memset(&m_header, 0, sizeof(m_header));
47
+ memset(&m_diskSubHeader, 0, sizeof(m_diskSubHeader));
48
+ }
49
+
50
+ irsdkDiskClient::irsdkDiskClient(const char *path)
51
+ : m_ibtFile(NULL)
52
+ , m_sessionInfoString(NULL)
53
+ , m_varHeaders(NULL)
54
+ , m_varBuf(NULL)
55
+ , m_ibtFileSize(0)
56
+ {
57
+ memset(&m_header, 0, sizeof(m_header));
58
+ memset(&m_diskSubHeader, 0, sizeof(m_diskSubHeader));
59
+
60
+ openFile(path);
61
+ }
62
+
63
+ bool irsdkDiskClient::openFile(const char *path)
64
+ {
65
+ closeFile();
66
+
67
+ m_ibtFile = fopen(path, "rb");
68
+ if(m_ibtFile)
69
+ {
70
+ // work out file size on disk
71
+ fseek(m_ibtFile, 0L, SEEK_END);
72
+ m_ibtFileSize = ftell(m_ibtFile);
73
+ fseek(m_ibtFile, 0L, SEEK_SET);
74
+
75
+ if(fread(&m_header, 1, sizeof(m_header), m_ibtFile) == sizeof(m_header))
76
+ {
77
+ if(fread(&m_diskSubHeader, 1, sizeof(m_diskSubHeader), m_ibtFile) == sizeof(m_diskSubHeader))
78
+ {
79
+ m_sessionInfoString = new char[m_header.sessionInfoLen];
80
+ if(m_sessionInfoString)
81
+ {
82
+ fseek(m_ibtFile, m_header.sessionInfoOffset, SEEK_SET);
83
+ if(fread(m_sessionInfoString, 1, m_header.sessionInfoLen, m_ibtFile) == (size_t)m_header.sessionInfoLen)
84
+ {
85
+ m_sessionInfoString[m_header.sessionInfoLen-1] = '\0';
86
+
87
+ m_varHeaders = new irsdk_varHeader[m_header.numVars];
88
+ if(m_varHeaders)
89
+ {
90
+ fseek(m_ibtFile, m_header.varHeaderOffset, SEEK_SET);
91
+ size_t len = m_header.numVars * sizeof(irsdk_varHeader);
92
+ if(fread(m_varHeaders, 1, len, m_ibtFile) == len)
93
+ {
94
+ m_varBuf = new char[m_header.bufLen];
95
+ if(m_varBuf)
96
+ {
97
+ fseek(m_ibtFile, m_header.varBuf[0].bufOffset, SEEK_SET);
98
+
99
+ return true;
100
+
101
+ //delete [] m_varBuf;
102
+ //m_varBuf = NULL;
103
+ }
104
+ }
105
+
106
+ delete [] m_varHeaders;
107
+ m_varHeaders = NULL;
108
+ }
109
+ }
110
+
111
+ delete [] m_sessionInfoString;
112
+ m_sessionInfoString = NULL;
113
+ }
114
+ }
115
+ }
116
+ fclose(m_ibtFile);
117
+ m_ibtFile = NULL;
118
+ }
119
+
120
+ return false;
121
+ }
122
+
123
+ void irsdkDiskClient::closeFile()
124
+ {
125
+ if(m_varBuf)
126
+ delete [] m_varBuf;
127
+ m_varBuf = NULL;
128
+
129
+ if(m_varHeaders)
130
+ delete [] m_varHeaders;
131
+ m_varHeaders = NULL;
132
+
133
+ if(m_sessionInfoString)
134
+ delete [] m_sessionInfoString;
135
+ m_sessionInfoString = NULL;
136
+
137
+ if(m_ibtFile)
138
+ fclose(m_ibtFile);
139
+ m_ibtFile = NULL;
140
+
141
+ m_ibtFileSize = 0;
142
+ }
143
+
144
+ bool irsdkDiskClient::getNextData()
145
+ {
146
+ if(m_ibtFile)
147
+ return fread(m_varBuf, 1, m_header.bufLen, m_ibtFile) == (size_t)m_header.bufLen;
148
+
149
+ return false;
150
+ }
151
+
152
+ // return how many variables this .ibt file has in the header
153
+ int irsdkDiskClient::getNumVars()
154
+ {
155
+ if(m_ibtFile)
156
+ return m_header.numVars;
157
+
158
+ return -1;
159
+ }
160
+
161
+ int irsdkDiskClient::getVarIdx(const char *name)
162
+ {
163
+ if(m_ibtFile && name)
164
+ {
165
+ for(int idx=0; idx<m_header.numVars; idx++)
166
+ {
167
+ if(0 == strncmp(name, m_varHeaders[idx].name, IRSDK_MAX_STRING))
168
+ {
169
+ return idx;
170
+ }
171
+ }
172
+ }
173
+
174
+ return -1;
175
+ }
176
+
177
+ irsdk_VarType irsdkDiskClient::getVarType(int idx)
178
+ {
179
+ if(m_ibtFile)
180
+ {
181
+ if(idx >= 0 && idx < m_header.numVars)
182
+ {
183
+ return (irsdk_VarType)m_varHeaders[idx].type;
184
+ }
185
+
186
+ //invalid variable index
187
+ assert(false);
188
+ }
189
+
190
+ return irsdk_char;
191
+ }
192
+
193
+ // get info on the var
194
+ const char* irsdkDiskClient::getVarName(int idx)
195
+ {
196
+ if(m_ibtFile)
197
+ {
198
+ if(idx >= 0 && idx < m_header.numVars)
199
+ {
200
+ return m_varHeaders[idx].name;
201
+ }
202
+
203
+ //invalid variable index
204
+ assert(false);
205
+ }
206
+
207
+ return NULL;
208
+ }
209
+
210
+ const char* irsdkDiskClient::getVarDesc(int idx)
211
+ {
212
+ if(m_ibtFile)
213
+ {
214
+ if(idx >= 0 && idx < m_header.numVars)
215
+ {
216
+ return m_varHeaders[idx].desc;
217
+ }
218
+
219
+ //invalid variable index
220
+ assert(false);
221
+ }
222
+
223
+ return NULL;
224
+ }
225
+
226
+ const char* irsdkDiskClient::getVarUnit(int idx)
227
+ {
228
+ if(m_ibtFile)
229
+ {
230
+ if(idx >= 0 && idx < m_header.numVars)
231
+ {
232
+ return m_varHeaders[idx].unit;
233
+ }
234
+
235
+ //invalid variable index
236
+ assert(false);
237
+ }
238
+
239
+ return NULL;
240
+ }
241
+
242
+ int irsdkDiskClient::getVarCount(int idx)
243
+ {
244
+ if(m_ibtFile)
245
+ {
246
+ if(idx >= 0 && idx < m_header.numVars)
247
+ {
248
+ return m_varHeaders[idx].count;
249
+ }
250
+
251
+ //invalid variable index
252
+ assert(false);
253
+ }
254
+
255
+ return 0;
256
+ }
257
+
258
+ bool irsdkDiskClient::getVarBool(int idx, int entry)
259
+ {
260
+ if(m_ibtFile)
261
+ {
262
+ if(idx >= 0 && idx < m_header.numVars)
263
+ {
264
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
265
+ {
266
+ const char * data = m_varBuf + m_varHeaders[idx].offset;
267
+ switch(m_varHeaders[idx].type)
268
+ {
269
+ // 1 byte
270
+ case irsdk_char:
271
+ case irsdk_bool:
272
+ return (((const char*)data)[entry]) != 0;
273
+ break;
274
+
275
+ // 4 bytes
276
+ case irsdk_int:
277
+ case irsdk_bitField:
278
+ return (((const int*)data)[entry]) != 0;
279
+ break;
280
+
281
+ // test float/double for greater than 1.0 so that
282
+ // we have a chance of this being usefull
283
+ // technically there is no right conversion...
284
+ case irsdk_float:
285
+ return (((const float*)data)[entry]) >= 1.0f;
286
+ break;
287
+
288
+ // 8 bytes
289
+ case irsdk_double:
290
+ return (((const double*)data)[entry]) >= 1.0;
291
+ break;
292
+ }
293
+ }
294
+ else
295
+ {
296
+ // invalid offset
297
+ assert(false);
298
+ }
299
+ }
300
+ else
301
+ {
302
+ //invalid variable index
303
+ assert(false);
304
+ }
305
+ }
306
+
307
+ return false;
308
+ }
309
+
310
+ int irsdkDiskClient::getVarInt(int idx, int entry)
311
+ {
312
+ if(m_ibtFile)
313
+ {
314
+ if(idx >= 0 && idx < m_header.numVars)
315
+ {
316
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
317
+ {
318
+ const char * data = m_varBuf + m_varHeaders[idx].offset;
319
+ switch(m_varHeaders[idx].type)
320
+ {
321
+ // 1 byte
322
+ case irsdk_char:
323
+ case irsdk_bool:
324
+ return (int)(((const char*)data)[entry]);
325
+ break;
326
+
327
+ // 4 bytes
328
+ case irsdk_int:
329
+ case irsdk_bitField:
330
+ return (int)(((const int*)data)[entry]);
331
+ break;
332
+
333
+ case irsdk_float:
334
+ return (int)(((const float*)data)[entry]);
335
+ break;
336
+
337
+ // 8 bytes
338
+ case irsdk_double:
339
+ return (int)(((const double*)data)[entry]);
340
+ break;
341
+ }
342
+ }
343
+ else
344
+ {
345
+ // invalid offset
346
+ assert(false);
347
+ }
348
+ }
349
+ else
350
+ {
351
+ //invalid variable index
352
+ assert(false);
353
+ }
354
+ }
355
+
356
+ return 0;
357
+ }
358
+
359
+ float irsdkDiskClient::getVarFloat(int idx, int entry)
360
+ {
361
+ if(m_ibtFile)
362
+ {
363
+ if(idx >= 0 && idx < m_header.numVars)
364
+ {
365
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
366
+ {
367
+ const char * data = m_varBuf + m_varHeaders[idx].offset;
368
+ switch(m_varHeaders[idx].type)
369
+ {
370
+ // 1 byte
371
+ case irsdk_char:
372
+ case irsdk_bool:
373
+ return (float)(((const char*)data)[entry]);
374
+ break;
375
+
376
+ // 4 bytes
377
+ case irsdk_int:
378
+ case irsdk_bitField:
379
+ return (float)(((const int*)data)[entry]);
380
+ break;
381
+
382
+ case irsdk_float:
383
+ return (float)(((const float*)data)[entry]);
384
+ break;
385
+
386
+ // 8 bytes
387
+ case irsdk_double:
388
+ return (float)(((const double*)data)[entry]);
389
+ break;
390
+ }
391
+ }
392
+ else
393
+ {
394
+ // invalid offset
395
+ assert(false);
396
+ }
397
+ }
398
+ else
399
+ {
400
+ //invalid variable index
401
+ assert(false);
402
+ }
403
+ }
404
+
405
+ return 0.0f;
406
+ }
407
+
408
+ double irsdkDiskClient::getVarDouble(int idx, int entry)
409
+ {
410
+ if(m_ibtFile)
411
+ {
412
+ if(idx >= 0 && idx < m_header.numVars)
413
+ {
414
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
415
+ {
416
+ const char * data = m_varBuf + m_varHeaders[idx].offset;
417
+ switch(m_varHeaders[idx].type)
418
+ {
419
+ // 1 byte
420
+ case irsdk_char:
421
+ case irsdk_bool:
422
+ return (double)(((const char*)data)[entry]);
423
+ break;
424
+
425
+ // 4 bytes
426
+ case irsdk_int:
427
+ case irsdk_bitField:
428
+ return (double)(((const int*)data)[entry]);
429
+ break;
430
+
431
+ case irsdk_float:
432
+ return (double)(((const float*)data)[entry]);
433
+ break;
434
+
435
+ // 8 bytes
436
+ case irsdk_double:
437
+ return (double)(((const double*)data)[entry]);
438
+ break;
439
+ }
440
+ }
441
+ else
442
+ {
443
+ // invalid offset
444
+ assert(false);
445
+ }
446
+ }
447
+ else
448
+ {
449
+ //invalid variable index
450
+ assert(false);
451
+ }
452
+ }
453
+
454
+ return 0.0;
455
+ }
456
+
457
+ //path is in the form of "DriverInfo:Drivers:CarIdx:{%d}UserName:"
458
+ int irsdkDiskClient::getSessionStrVal(const char *path, char *val, int valLen)
459
+ {
460
+ if(m_ibtFile && path && val && valLen > 0)
461
+ {
462
+ const char *tVal = NULL;
463
+ int tValLen = 0;
464
+ if(parseYaml(m_sessionInfoString, path, &tVal, &tValLen))
465
+ {
466
+ // dont overflow out buffer
467
+ int len = tValLen;
468
+ if(len > valLen)
469
+ len = valLen;
470
+
471
+ // copy what we can, even if buffer too small
472
+ memcpy(val, tVal, len);
473
+ val[len] = '\0'; // origional string has no null termination...
474
+
475
+ // if buffer was big enough, return success
476
+ if(valLen >= tValLen)
477
+ return 1;
478
+ else // return size of buffer needed
479
+ return -tValLen;
480
+ }
481
+ }
482
+
483
+ return 0;
484
+ }
485
+
486
+ //-----------------
487
+
488
+ irsdkDiskWriter::irsdkDiskWriter()
489
+ {
490
+ initialize();
491
+ }
492
+
493
+ irsdkDiskWriter::irsdkDiskWriter(const char *path)
494
+ {
495
+ initialize();
496
+
497
+ openFile(path);
498
+ }
499
+
500
+ void irsdkDiskWriter::initialize()
501
+ {
502
+ memset(&m_header, 0, sizeof(m_header));
503
+ memset(&m_diskSubHeader, 0, sizeof(m_diskSubHeader));
504
+ memset(&m_sessionInfoString, 0, sizeof(m_sessionInfoString));
505
+ memset(&m_varHeaders, 0, sizeof(m_varHeaders));
506
+ memset(m_varBuf, 0, sizeof(m_varBuf));
507
+
508
+ m_ibtFile = NULL;
509
+ m_diskSubHeaderOffset = 0;
510
+ m_isHeaderFinalized = false;
511
+
512
+ // default initialization of header
513
+ m_header.ver = IRSDK_VER;
514
+ m_header.status = irsdk_stConnected;
515
+ m_header.tickRate = 60;
516
+ m_header.sessionInfoUpdate = 0;
517
+ m_header.numBuf = 1;
518
+ m_header.varBuf[0].tickCount = 0;
519
+
520
+ // fake up yaml string, in case we don't add one later
521
+ sprintf(m_sessionInfoString, "---\n...\n");
522
+ }
523
+
524
+ bool irsdkDiskWriter::openFile(const char *path)
525
+ {
526
+ assert(m_ibtFile == NULL);
527
+
528
+ m_ibtFile = fopen(path, "wb");
529
+ if(m_ibtFile)
530
+ {
531
+ //****FixMe, is this needed?
532
+ m_diskSubHeaderOffset = 0;
533
+ m_isHeaderFinalized = false;
534
+
535
+ return true;
536
+ }
537
+
538
+ return false;
539
+ }
540
+
541
+ void irsdkDiskWriter::closeFile()
542
+ {
543
+ if(m_ibtFile)
544
+ {
545
+ fseek(m_ibtFile, m_diskSubHeaderOffset, SEEK_SET);
546
+ fwrite(&m_diskSubHeader, 1, sizeof(m_diskSubHeader), m_ibtFile);
547
+ fclose(m_ibtFile);
548
+ }
549
+ m_ibtFile = NULL;
550
+ }
551
+
552
+ int getSizeOfVarType(const irsdk_VarType type)
553
+ {
554
+ switch(type)
555
+ {
556
+ // 1 byte
557
+ case irsdk_char:
558
+ case irsdk_bool: return 1;
559
+
560
+ // 4 bytes
561
+ case irsdk_int:
562
+ case irsdk_bitField:
563
+ case irsdk_float: return 4;
564
+
565
+ // 8 bytes
566
+ case irsdk_double: return 8;
567
+
568
+ default:
569
+ assert(false);
570
+ return 1;
571
+ }
572
+ }
573
+
574
+ int irsdkDiskWriter::addNewVariable(const char *name, const char *desc, const char *unit, const irsdk_VarType type, int count)
575
+ {
576
+ assert(m_ibtFile);
577
+ assert(!m_isHeaderFinalized);
578
+ assert(count >= 1);
579
+
580
+ if(m_ibtFile && !m_isHeaderFinalized)
581
+ {
582
+ // room for the variable?
583
+ if(m_header.numVars < MAX_VAR_COUNT &&
584
+ (m_header.bufLen + count) < MAX_VAR_BUF_SIZE)
585
+ {
586
+ int idx = m_header.numVars;
587
+ m_header.numVars++;
588
+
589
+ m_varHeaders[idx].type = type; // irsdk_VarType
590
+ m_varHeaders[idx].offset = m_header.bufLen; // offset fron start of buffer row
591
+ m_varHeaders[idx].count = count; // number of entrys (array)
592
+ m_header.bufLen += count * getSizeOfVarType(type);
593
+
594
+ m_varHeaders[idx].countAsTime = false;
595
+
596
+ strncpy(m_varHeaders[idx].name, name, sizeof(char) * IRSDK_MAX_STRING);
597
+ strncpy(m_varHeaders[idx].desc, desc, sizeof(char) * IRSDK_MAX_DESC);
598
+ strncpy(m_varHeaders[idx].unit, unit, sizeof(char) * IRSDK_MAX_STRING); // something like "kg/m^2"
599
+
600
+ return idx;
601
+ }
602
+ }
603
+
604
+ return -1; // bogus index
605
+ }
606
+
607
+ void irsdkDiskWriter::finalizeHeader()
608
+ {
609
+ assert(m_ibtFile);
610
+ assert(!m_isHeaderFinalized);
611
+
612
+ if(m_ibtFile && !m_isHeaderFinalized)
613
+ {
614
+ int offset = 0;
615
+
616
+ // main header
617
+ offset += sizeof(m_header);
618
+
619
+ // sub header is written out at end of session
620
+ m_diskSubHeaderOffset = offset;
621
+ offset += sizeof(m_diskSubHeader);
622
+
623
+ // pointer to var definitions
624
+ m_header.varHeaderOffset = offset;
625
+ offset += m_header.numVars * sizeof(irsdk_varHeader);
626
+
627
+ // pointer to session info string
628
+ m_header.sessionInfoLen = (int)strlen(m_sessionInfoString);
629
+ m_header.sessionInfoOffset = offset;
630
+ offset += m_header.sessionInfoLen;
631
+
632
+ // pointer to start of buffered data
633
+ m_header.varBuf[0].bufOffset = offset;
634
+
635
+ fwrite(&m_header, 1, sizeof(m_header), m_ibtFile);
636
+ fwrite(&m_diskSubHeader, 1, sizeof(m_diskSubHeader), m_ibtFile);
637
+ fwrite(&m_varHeaders, 1, m_header.numVars * sizeof(irsdk_varHeader), m_ibtFile);
638
+ fwrite(m_sessionInfoString, 1, m_header.sessionInfoLen, m_ibtFile);
639
+
640
+ if(ftell(m_ibtFile) != m_header.varBuf[0].bufOffset)
641
+ printf("ERROR: m_ibtFile pointer mismach: %d != %d\n", ftell(m_ibtFile), m_header.varBuf[0].bufOffset);
642
+
643
+ m_isHeaderFinalized = true;
644
+ }
645
+ }
646
+
647
+ // write next line to file and clear buffers
648
+ void irsdkDiskWriter::writeLine()
649
+ {
650
+ assert(m_ibtFile);
651
+ assert(m_isHeaderFinalized);
652
+
653
+ if(m_ibtFile && m_isHeaderFinalized)
654
+ {
655
+ fwrite(m_varBuf, 1, m_header.bufLen, m_ibtFile);
656
+ m_diskSubHeader.sessionRecordCount++;
657
+
658
+ // zero out data so we are ready for the next line
659
+ memset(m_varBuf, 0, sizeof(m_varBuf));
660
+ }
661
+ }
662
+
663
+ // return how many variables this .ibt file has in the header
664
+ int irsdkDiskWriter::getNumVars()
665
+ {
666
+ assert(m_ibtFile);
667
+
668
+ if(m_ibtFile)
669
+ return m_header.numVars;
670
+
671
+ return -1;
672
+ }
673
+
674
+ int irsdkDiskWriter::getVarIdx(const char *name)
675
+ {
676
+ assert(m_ibtFile);
677
+ assert(name);
678
+
679
+ if(m_ibtFile && name)
680
+ {
681
+ for(int idx=0; idx<m_header.numVars; idx++)
682
+ {
683
+ if(0 == strncmp(name, m_varHeaders[idx].name, IRSDK_MAX_STRING))
684
+ {
685
+ return idx;
686
+ }
687
+ }
688
+ }
689
+
690
+ return -1;
691
+ }
692
+
693
+ irsdk_VarType irsdkDiskWriter::getVarType(int idx)
694
+ {
695
+ assert(m_ibtFile);
696
+
697
+ if(m_ibtFile)
698
+ {
699
+ if(idx >= 0 && idx < m_header.numVars)
700
+ {
701
+ return (irsdk_VarType)m_varHeaders[idx].type;
702
+ }
703
+
704
+ //invalid variable index
705
+ assert(false);
706
+ }
707
+
708
+ return irsdk_char;
709
+ }
710
+
711
+ // get info on the var
712
+ const char* irsdkDiskWriter::getVarName(int idx)
713
+ {
714
+ assert(m_ibtFile);
715
+
716
+ if(m_ibtFile)
717
+ {
718
+ if(idx >= 0 && idx < m_header.numVars)
719
+ {
720
+ return m_varHeaders[idx].name;
721
+ }
722
+
723
+ //invalid variable index
724
+ assert(false);
725
+ }
726
+
727
+ return NULL;
728
+ }
729
+
730
+ const char* irsdkDiskWriter::getVarDesc(int idx)
731
+ {
732
+ assert(m_ibtFile);
733
+
734
+ if(m_ibtFile)
735
+ {
736
+ if(idx >= 0 && idx < m_header.numVars)
737
+ {
738
+ return m_varHeaders[idx].desc;
739
+ }
740
+
741
+ //invalid variable index
742
+ assert(false);
743
+ }
744
+
745
+ return NULL;
746
+ }
747
+
748
+ const char* irsdkDiskWriter::getVarUnit(int idx)
749
+ {
750
+ assert(m_ibtFile);
751
+
752
+ if(m_ibtFile)
753
+ {
754
+ if(idx >= 0 && idx < m_header.numVars)
755
+ {
756
+ return m_varHeaders[idx].unit;
757
+ }
758
+
759
+ //invalid variable index
760
+ assert(false);
761
+ }
762
+
763
+ return NULL;
764
+ }
765
+
766
+ int irsdkDiskWriter::getVarCount(int idx)
767
+ {
768
+ assert(m_ibtFile);
769
+
770
+ if(m_ibtFile)
771
+ {
772
+ if(idx >= 0 && idx < m_header.numVars)
773
+ {
774
+ return m_varHeaders[idx].count;
775
+ }
776
+
777
+ //invalid variable index
778
+ assert(false);
779
+ }
780
+
781
+ return 0;
782
+ }
783
+
784
+ //---
785
+
786
+ bool irsdkDiskWriter::setVar(bool val, int idx, int entry)
787
+ {
788
+ assert(m_ibtFile);
789
+
790
+ if(m_ibtFile)
791
+ {
792
+ if(idx >= 0 && idx < m_header.numVars)
793
+ {
794
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
795
+ {
796
+ char * data = m_varBuf + m_varHeaders[idx].offset;
797
+ switch(m_varHeaders[idx].type)
798
+ {
799
+ // 1 byte
800
+ case irsdk_char:
801
+ case irsdk_bool:
802
+ ((char*)data)[entry] = val;
803
+ break;
804
+
805
+ // 4 bytes
806
+ case irsdk_int:
807
+ case irsdk_bitField:
808
+ ((int*)data)[entry] = (int)val;
809
+ break;
810
+
811
+ // technically there is no right conversion...
812
+ case irsdk_float:
813
+ ((float*)data)[entry] = (float)val;
814
+ break;
815
+
816
+ // 8 bytes
817
+ case irsdk_double:
818
+ ((double*)data)[entry] = (double)val;
819
+ break;
820
+ }
821
+
822
+ return true;
823
+ }
824
+ else
825
+ {
826
+ // invalid offset
827
+ assert(false);
828
+ }
829
+ }
830
+ else
831
+ {
832
+ //invalid variable index
833
+ assert(false);
834
+ }
835
+ }
836
+
837
+ return false;
838
+ }
839
+
840
+ bool irsdkDiskWriter::setVar(int val, int idx, int entry)
841
+ {
842
+ assert(m_ibtFile);
843
+
844
+ if(m_ibtFile)
845
+ {
846
+ if(idx >= 0 && idx < m_header.numVars)
847
+ {
848
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
849
+ {
850
+ char * data = m_varBuf + m_varHeaders[idx].offset;
851
+ switch(m_varHeaders[idx].type)
852
+ {
853
+ // 1 byte
854
+ case irsdk_char:
855
+ case irsdk_bool:
856
+ ((char*)data)[entry] = (bool)val;
857
+ break;
858
+
859
+ // 4 bytes
860
+ case irsdk_int:
861
+ case irsdk_bitField:
862
+ ((int*)data)[entry] = (int)val;
863
+ break;
864
+
865
+ case irsdk_float:
866
+ ((float*)data)[entry] = (float)val;
867
+ break;
868
+
869
+ // 8 bytes
870
+ case irsdk_double:
871
+ ((double*)data)[entry] = (double)val;
872
+ break;
873
+ }
874
+
875
+ return true;
876
+ }
877
+ else
878
+ {
879
+ // invalid offset
880
+ assert(false);
881
+ }
882
+ }
883
+ else
884
+ {
885
+ //invalid variable index
886
+ assert(false);
887
+ }
888
+ }
889
+
890
+ return false;
891
+ }
892
+
893
+ bool irsdkDiskWriter::setVar(float val, int idx, int entry)
894
+ {
895
+ assert(m_ibtFile);
896
+
897
+ if(m_ibtFile)
898
+ {
899
+ if(idx >= 0 && idx < m_header.numVars)
900
+ {
901
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
902
+ {
903
+ char * data = m_varBuf + m_varHeaders[idx].offset;
904
+ switch(m_varHeaders[idx].type)
905
+ {
906
+ // 1 byte
907
+ case irsdk_char:
908
+ case irsdk_bool:
909
+ ((char*)data)[entry] = (bool)val;
910
+ break;
911
+
912
+ // 4 bytes
913
+ case irsdk_int:
914
+ case irsdk_bitField:
915
+ ((int*)data)[entry] = (int)val;
916
+ break;
917
+
918
+ case irsdk_float:
919
+ ((float*)data)[entry] = (float)val;
920
+ break;
921
+
922
+ // 8 bytes
923
+ case irsdk_double:
924
+ ((double*)data)[entry] = (double)val;
925
+ break;
926
+ }
927
+
928
+ return true;
929
+ }
930
+ else
931
+ {
932
+ // invalid offset
933
+ assert(false);
934
+ }
935
+ }
936
+ else
937
+ {
938
+ //invalid variable index
939
+ assert(false);
940
+ }
941
+ }
942
+
943
+ return false;
944
+ }
945
+
946
+ bool irsdkDiskWriter::setVar(double val, int idx, int entry)
947
+ {
948
+ assert(m_ibtFile);
949
+
950
+ if(m_ibtFile)
951
+ {
952
+ if(idx >= 0 && idx < m_header.numVars)
953
+ {
954
+ if(entry >= 0 && entry < m_varHeaders[idx].count)
955
+ {
956
+ char * data = m_varBuf + m_varHeaders[idx].offset;
957
+ switch(m_varHeaders[idx].type)
958
+ {
959
+ // 1 byte
960
+ case irsdk_char:
961
+ case irsdk_bool:
962
+ ((char*)data)[entry] = (bool)val;
963
+ break;
964
+
965
+ // 4 bytes
966
+ case irsdk_int:
967
+ case irsdk_bitField:
968
+ ((int*)data)[entry] = (int)val;
969
+ break;
970
+
971
+ case irsdk_float:
972
+ ((float*)data)[entry] = (float)val;
973
+ break;
974
+
975
+ // 8 bytes
976
+ case irsdk_double:
977
+ ((double*)data)[entry] = (double)val;
978
+ break;
979
+ }
980
+
981
+ return true;
982
+ }
983
+ else
984
+ {
985
+ // invalid offset
986
+ assert(false);
987
+ }
988
+ }
989
+ else
990
+ {
991
+ //invalid variable index
992
+ assert(false);
993
+ }
994
+ }
995
+
996
+ return false;
997
+ }