-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdna.c
1764 lines (1659 loc) · 49 KB
/
dna.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <userint.h>
#include "face.h"
#include "dna.h"
//
//This is the DNA of Word Generator 2
//
//This contains implimentations of data structures needed to make
//the program work.
//
//These are used in the BRAIN of Word Generator 2 to instantiate global
//variables.
//
//The FACE of WG2 (user interface source code) uses the BRAIN and hence this DNA
//
// reminder from DNA.h
// typedef enum {dnaNoRange,dnaUnipolar,dnaBipolar,
// dnaAmplitude,dnaFrequency} dnaRanges;
// RangeMax[dnaAmplitude] for the SRS is 5.0 (volts) but for the amplifier
// after the SRS we need to limit this to 1.0 volt. This could be done
// as a user selection, but to simplify things, we are doing it here
// Obviously, these are all GOTCHA values. The digitizing function used
// for the analog channels was prodded and tested to come up with them.
const double RangeMax[dnaNumRanges]=
{brainMaxDouble, 9.998779296874, 9.99755859374, 10.0, 30.0};
const double RangeMin[dnaNumRanges]=
{brainMinDouble,-0.001220703124,-10.00244140624, 0.0, 0.0};
// FROM BRAIN.c
int GetYMin(dnaARFGraph *graph, double *value);
int GetYMax(dnaARFGraph *graph, double *value);
extern brainInterpData InterpData[dnaNumInterps];
int CVIFUNC dna_InitClusterArray (dnaClusterArray *clusterArray,
int numberOfClusters, int ports)
{ // Assumes that Clusters is not already allocated
int i,err=0;
int outputs;
dnaCluster *cp;
clusterArray->NumberOfClusters = numberOfClusters;
clusterArray->NumberOfPorts = ports;
outputs=ports*8; // GOTCHA : hand coded 8, ha ha
for (i=0; i<outputs; i++) {
clusterArray->OutputLabelSize[i]=0;
clusterArray->OutputLabels[i]=0;
}
// ALLOCATION
clusterArray->MemoryUsed = sizeof(dnaCluster)*numberOfClusters;
clusterArray->Clusters = calloc (numberOfClusters, sizeof(dnaCluster));
nullChk(clusterArray->Clusters);
for (i=0,cp=clusterArray->Clusters; i<numberOfClusters; i++,cp++) {
errChk(dna_InitCluster (cp, 8*ports)); // GOTCHA...
};
return 0;
Error:
return -1;
}
// option = 0 to clear, 1 to copy old cluster data
int CVIFUNC dna_ReInitClusterArray (dnaClusterArray *clusterArray,
int numberOfClusters, int ports, int option)
{
int i,err=0;
int outputs;
int N,M;
dnaCluster *cp;
nullChk(clusterArray);
if ((0==option) || (0==numberOfClusters)) {
outputs=clusterArray->NumberOfPorts*8;
// Need to release all the old memory
for (i=0; i<outputs; i++) {
if (clusterArray->OutputLabels[i])
free(clusterArray->OutputLabels[i]);
clusterArray->OutputLabelSize[i]=0;
clusterArray->OutputLabels[i]=0;
}
for (i=0; i<clusterArray->NumberOfClusters;i++) {
// frees all memory used by the cluster
errChk(dna_ReInitCluster (&clusterArray->Clusters[i], 0, 0));
}
free(clusterArray->Clusters);
clusterArray->Clusters=0;
clusterArray->MemoryUsed=0;
// ALLOCATION call
errChk(dna_InitClusterArray(clusterArray,numberOfClusters,ports));
}
else {
nullChk(clusterArray->Clusters);
N=clusterArray->MemoryUsed/sizeof(dnaCluster); // In memory
M=clusterArray->NumberOfClusters; // In use
if (0==N)
require(0==clusterArray->Clusters);
else
require(0!=clusterArray->Clusters);
outputs=8*clusterArray->NumberOfPorts;
for (i=8*ports; i<outputs; i++) { // free unneeded labels
if (clusterArray->OutputLabels[i])
free(clusterArray->OutputLabels[i]);
clusterArray->OutputLabelSize[i]=0;
clusterArray->OutputLabels[i]=0;
}
if (N<numberOfClusters) { // Need more memory
// ALLOCATION realloc
clusterArray->Clusters=realloc(clusterArray->Clusters,
numberOfClusters*sizeof(dnaCluster));
nullChk(clusterArray->Clusters);
clusterArray->MemoryUsed=numberOfClusters*sizeof(dnaCluster);
for (i=N; i<numberOfClusters; i++) // Clear new memory area
errChk(dna_InitCluster (&clusterArray->Clusters[i], 8*ports));
N=numberOfClusters;
}
else if (0==N) {
// ALLOCATION
clusterArray->Clusters=calloc(numberOfClusters,sizeof(dnaCluster));
nullChk(clusterArray->Clusters);
clusterArray->MemoryUsed=numberOfClusters*sizeof(dnaCluster);
for (i=N; i<numberOfClusters; i++) // Clear new memory area
errChk(dna_InitCluster (&clusterArray->Clusters[i], 0));
N=numberOfClusters;
}
// Now we have at least as much memory as needed
for (i=numberOfClusters; i<M; i++) // free memory used by old clusters
errChk(dna_ReInitCluster (&clusterArray->Clusters[i], 0, 0));
clusterArray->NumberOfClusters=numberOfClusters;
// calls on old data may be redundant
for (i=0; i<numberOfClusters; i++) // adjust ports on new clusters
errChk(dna_ReInitCluster (&clusterArray->Clusters[i], 8*ports, 1));
clusterArray->NumberOfPorts=ports;
}
return 0;
Error:
return -1;
}
int CVIFUNC dna_SetOutputLabel (dnaClusterArray *clusterArray, int index,
char *outputName)
{
int err=0;
if (clusterArray->OutputLabels[index]){
free(clusterArray->OutputLabels[index]);
clusterArray->OutputLabels[index]=0;
clusterArray->OutputLabelSize[index]=0;
}
if (outputName) {
// ALLOCATION call
clusterArray->OutputLabels[index]=StrDup(outputName);
nullChk(clusterArray->OutputLabels[index]);
clusterArray->OutputLabelSize[index]=strlen(outputName);
}
return 0;
Error:
return -1;
}
int CVIFUNC dna_InsertCluster (dnaClusterArray *clusterArray, int index)
{
int err=0;
int i,N,M; // Number allocated
dnaCluster Temp;
nullChk(clusterArray);
// make sure graph is in consistant state
N=clusterArray->MemoryUsed/sizeof(dnaCluster);
require(N>=0);
if (0==N)
require(0==clusterArray->Clusters);
else
require(0!=clusterArray->Clusters);
// make index conform to size of array
if (index<0)
index=0;
M=clusterArray->NumberOfClusters;
require(M<=N);
if (index>M)
index=M;
// Now do trivial case
if (0==clusterArray->Clusters) { // easy case
// ALLOCATION call
// Allocate space for 4 clusters in clear mode
errChk(dna_ReInitClusterArray (clusterArray, 4,
clusterArray->NumberOfPorts,0));
// Reducing to one cluster in copy mode keeps memory allocated
errChk(dna_ReInitClusterArray (clusterArray, 1,
clusterArray->NumberOfPorts,1));
N=4;
M=1;
return 0;
}
if (N==M) { // Need more memory
// ALLOCATION realloc
// Allocate space for 4 clusters in copy mode
errChk(dna_ReInitClusterArray (clusterArray, N+4,
clusterArray->NumberOfPorts,1));
// Reducing clusters in copy mode keeps memory allocated
errChk(dna_ReInitClusterArray (clusterArray, M+1,
clusterArray->NumberOfPorts,1));
N+=4;
M+=1;
// new piece of memory need not be cleared, probably
}
else { // Initialize extra memory
errChk(dna_ReInitClusterArray (clusterArray, M+1,
clusterArray->NumberOfPorts,1));
M+=1;
}
Temp=clusterArray->Clusters[M-1]; // End one is blank ?
// move over
for (i=M-2; i>=index; i--) // move data over
clusterArray->Clusters[i+1]=clusterArray->Clusters[i];
// clear the fields of the "inserted" cluster
// Clusters[index] no longer "owns" the memory it points to so we
// can cheat and call InitCluster
clusterArray->Clusters[index]=Temp;
Error:
return (err?-1:0);
}
int CVIFUNC dna_DeleteCluster (dnaClusterArray *clusterArray, int index)
{
int err=0;
int i,N,M; // Number allocated
nullChk(clusterArray);
N=clusterArray->MemoryUsed/sizeof(dnaCluster);
M=clusterArray->NumberOfClusters;
// Block of paranoia code
require(N>0);
require(M<=N);
require(index>=0);
require(index<M);
require(0!=clusterArray->Clusters);
// Free memory
errChk(dna_ReInitCluster (&clusterArray->Clusters[index], 0, 0));
// Move over data
for (i=index; i<M-1; i++) // move data over
clusterArray->Clusters[i]=clusterArray->Clusters[i+1];
// Last one now does not own its pointers, so cheat and call init
errChk(dna_InitCluster (&clusterArray->Clusters[M-1], 0));
// Now we can truncate the last cluster
errChk(dna_ReInitClusterArray (clusterArray, M-1,
clusterArray->NumberOfPorts, 1));
Error:
return (err?-1:0);
}
int CVIFUNC dna_InitCluster (dnaCluster *clusterPointer,
int numberofDigitalValues)
{
int err=0;
// Assumes that Digital is not already allocated
// Some nice default values:
clusterPointer->LabelSize = 0;
clusterPointer->Label = 0;
clusterPointer->Ticks = 1;
clusterPointer->TimeUnit = dnaSEC;
clusterPointer->EnabledQ = dnaFalse;
clusterPointer->AnalogSelector = dnaARFContinue;
clusterPointer->AnalogGroup = -1;
clusterPointer->RFSelector = dnaARFContinue;
clusterPointer->RFGroup = -1;
clusterPointer->NumberOfValues = numberofDigitalValues;
// ALLOCATION
clusterPointer->MemoryUsed = numberofDigitalValues;
if (numberofDigitalValues) {
clusterPointer->Digital = calloc(numberofDigitalValues,sizeof(dnaByte));
nullChk(clusterPointer->Digital);
}
else {
clusterPointer->Digital=0;
}
return 0;
Error:
return -1;
}
int CVIFUNC dna_ReInitCluster (dnaCluster *clusterPointer,
int numberofDigitalValues, int option)
{
int err=0;
int i;
if (0==option) {
if (clusterPointer->Label) {
free(clusterPointer->Label);
clusterPointer->Label=0;
clusterPointer->LabelSize=0;
}
else {
clusterPointer->LabelSize=0;
}
clusterPointer->Ticks=1;
clusterPointer->TimeUnit = dnaSEC;
clusterPointer->EnabledQ = dnaFalse;
clusterPointer->AnalogSelector = dnaARFContinue;
clusterPointer->AnalogGroup = -1;
clusterPointer->RFSelector = dnaARFContinue;
clusterPointer->RFGroup = -1;
clusterPointer->NumberOfValues = numberofDigitalValues;
if (clusterPointer->Digital) {
free(clusterPointer->Digital);
clusterPointer->Digital=0;
clusterPointer->MemoryUsed=0;
}
// ALLOCATION
clusterPointer->MemoryUsed = numberofDigitalValues;
if (numberofDigitalValues) {
clusterPointer->Digital = calloc(numberofDigitalValues,sizeof(dnaByte));
nullChk(clusterPointer->Digital);
}
}
else {
// Need to save the old data
if (clusterPointer->Digital) {
if (numberofDigitalValues>clusterPointer->MemoryUsed) {
// ALLOCATION realloc
realloc(clusterPointer->Digital,
numberofDigitalValues*sizeof(dnaByte));
// need to initialize the new memory
for (i=clusterPointer->MemoryUsed; i<numberofDigitalValues; i++)
clusterPointer->Digital[i]=0;
clusterPointer->NumberOfValues = numberofDigitalValues;
clusterPointer->MemoryUsed = numberofDigitalValues;
nullChk(clusterPointer->Digital);
}
else {
clusterPointer->NumberOfValues = numberofDigitalValues;
}
}
else {
if (numberofDigitalValues) {
// ALLOCATION
clusterPointer->NumberOfValues = numberofDigitalValues;
clusterPointer->MemoryUsed = numberofDigitalValues;
clusterPointer->Digital = calloc(numberofDigitalValues,
sizeof(dnaByte));
nullChk(clusterPointer->Digital);
}
else {
clusterPointer->NumberOfValues = numberofDigitalValues;
clusterPointer->MemoryUsed = numberofDigitalValues;
clusterPointer->Digital=0;
}
}
}
return 0;
Error:
return -1;
}
int CVIFUNC dna_SetClusterLabel (dnaCluster *clusterPointer, char *clusterName)
{
int err=0;
// This copies the string of clusterName to fresh memory space
if (0==clusterPointer) {
err-1;
goto Error;
}
if (clusterPointer->Label) {
// ALLOCATION
free (clusterPointer->Label);
clusterPointer->LabelSize=0;
};
if (clusterName) {
clusterPointer->LabelSize = strlen (clusterName);
// ALLOCATION
clusterPointer->Label = calloc (clusterPointer->LabelSize+1, 1);
nullChk(clusterPointer->Label);
strcpy (clusterPointer->Label, clusterName);
};
return 0;
Error:
return -1;
}
int CVIFUNC dna_SetClusterLength (dnaCluster *clusterPointer,
int numberofDigitalValues, int option)
{
return 0;
}
int CVIFUNC dna_CopyClusterData (dnaCluster *target, dnaCluster *source)
{
int i,err=0;
errChk(dna_ReInitCluster (target, source->NumberOfValues, 0));
if (source->Label) {
target->LabelSize=source->LabelSize;
target->Label=StrDup(source->Label);
}
target->Ticks=source->Ticks;
target->TimeUnit=source->TimeUnit;
target->EnabledQ=source->EnabledQ;
target->AnalogSelector=source->AnalogSelector;
target->AnalogGroup=source->AnalogGroup;
target->RFSelector=source->RFSelector;
target->RFGroup=source->RFGroup;
target->NumberOfValues=source->NumberOfValues;
target->MemoryUsed=source->MemoryUsed;
for(i=0;i<source->NumberOfValues;i++)
target->Digital[i]=source->Digital[i];
Error:
return (err?-1:0);
}
int CVIFUNC dna_InitARFGroupArray (dnaARFGroupArray *groupArray, int groups,
dnaByte graphsinaGroup)
{
int err=0;
int i; // looping;
groupArray->NumberOfGroups=groups;
groupArray->NumberOfGraphs=graphsinaGroup;
// ALLOCATION
if(graphsinaGroup) {
groupArray->EnabledQ=calloc(graphsinaGroup,sizeof(dnaByte));
nullChk(groupArray->EnabledQ);
}
else {
groupArray->EnabledQ=0;
}
// GOTCHA : dnaAnalogChannels from DNA.h define
for (i=0; i<dnaAnalogChannels; i++) {
groupArray->LabelSize[i]=0;
groupArray->Labels[i]=0;
}
// ALLOCATION
if (groups) {
groupArray->MemoryUsed = groups*sizeof(dnaARFGroup);
groupArray->ARFGroups = calloc(groups,sizeof(dnaARFGroup));
nullChk(groupArray->ARFGroups);
}
else {
groupArray->MemoryUsed = 0;
groupArray->ARFGroups = 0;
}
for (i=0; i<groups; i++) {
errChk(dna_InitGroup (&groupArray->ARFGroups[i], 0, graphsinaGroup));
}
return 0;
Error:
return -1;
}
// option = 0 to clear old data, option = 1 to save old data
int CVIFUNC dna_ReInitARFGroupArray (dnaARFGroupArray *groupArray, int groups,
dnaByte graphsinaGroup, int values,
int option)
{
int err=0;
int i,j;
dnaByte *B;
// As a GOTCHA, if graphsinagroup==2 we are RF, otherwise Analog
if (0==groups)
graphsinaGroup=0;
if (0==graphsinaGroup)
values=0;
if (0==option) {
// need to free old allocation
if (groupArray->EnabledQ) {
free(groupArray->EnabledQ);
groupArray->EnabledQ=0;
}
for (i=0; i<dnaAnalogChannels; i++) {
if (groupArray->Labels[i])
free(groupArray->Labels[i]);
groupArray->Labels[i]=0;
groupArray->LabelSize[i]=0;
}
if (groupArray->ARFGroups) {
// Need to erase old group/graph data
for (i=0; i<groupArray->NumberOfGroups; i++) {
dna_ReInitGroup (&groupArray->ARFGroups[i], 0, 0, 0, 0);
}
// Now free the group memory
free(groupArray->ARFGroups);
groupArray->MemoryUsed = 0;
groupArray->ARFGroups = 0;
}
if (groups) {
if(graphsinaGroup) {
groupArray->EnabledQ=calloc(graphsinaGroup,sizeof(dnaByte));
nullChk(groupArray->EnabledQ);
}
groupArray->MemoryUsed = groups*sizeof(dnaARFGroup);
// ALLOCATION
groupArray->ARFGroups = calloc(groups,sizeof(dnaARFGroup));
nullChk(groupArray->ARFGroups);
}
for (i=0; i<groups; i++) {
// ALLOCATION calls
errChk(dna_InitGroup (&groupArray->ARFGroups[i],
0, graphsinaGroup));
if (values) // Add allocation for values
dna_ReInitGroup (&groupArray->ARFGroups[i], 0,
graphsinaGroup, values, 0);
}
}
else { // SAVE WHAT WE CAN
if (0==groups) { // Kill them all!
if (groupArray->EnabledQ) {
free(groupArray->EnabledQ);
groupArray->EnabledQ=0;
}
for (i=0; i<dnaAnalogChannels; i++) {
if (groupArray->Labels[i])
free(groupArray->Labels[i]);
groupArray->Labels[i]=0;
groupArray->LabelSize[i]=0;
}
if (groupArray->ARFGroups) {
// Need to erase old group/graph data
for (i=0; i<groupArray->NumberOfGroups; i++) {
dna_ReInitGroup (&groupArray->ARFGroups[i], 0, 0, 0, 0);
}
// Now free the group memory
free(groupArray->ARFGroups);
groupArray->MemoryUsed = 0;
groupArray->ARFGroups = 0;
}
groupArray->NumberOfGroups=0;
groupArray->NumberOfGraphs=0;
}
else { // Save what we can
if (groupArray->ARFGroups) { // We have existing memory
if (groups*sizeof(dnaARFGroup)>groupArray->MemoryUsed) {
// we need more memory for groups
// ALLOCATION realloc
groupArray->ARFGroups=realloc(groupArray->ARFGroups,
groups*sizeof(dnaARFGroup));
nullChk(groupArray->ARFGroups);
groupArray->MemoryUsed=groups*sizeof(dnaARFGroup);
// Need to initialize the newly allocated bit of memory
// Note : we assume anything we overwrite does not have
// any allocated pointers in it
for (i=groupArray->NumberOfGroups; i<groups; i++) {
// ALLOCATION call
errChk(dna_InitGroup (&groupArray->ARFGroups[i],
0, graphsinaGroup));
}
}
else {
if (groups<groupArray->NumberOfGraphs) {
// we need less memory for groups
// very important we free the excess groups
for (i=groups; i<groupArray->NumberOfGroups; i++) {
// ALLOCATION call
errChk(dna_ReInitGroup (&groupArray->ARFGroups[i],
0, 0, 0, 0));
}
}
}
}
else { // We need fresh memory
// ALLOCATION
groupArray->ARFGroups = calloc(groups,sizeof(dnaARFGroup));
nullChk(groupArray->ARFGroups);
groupArray->MemoryUsed = groups*sizeof(dnaARFGroup);
for (i=0; i<groups; i++) {
// ALLOCATION call // Need to initialize the new memory
errChk(dna_InitGroup (&groupArray->ARFGroups[i],
0, graphsinaGroup));
}
}
for (i=0; i<groups; i++) {
errChk(dna_ReInitGroup (&groupArray->ARFGroups[i], 0,
graphsinaGroup, values, option));
}
}
// We now have the right amount of groups ready
// Each with the right number of graphs
// And with at least as many values are requested
if (0==graphsinaGroup) {
if (groupArray->EnabledQ) {
free(groupArray->EnabledQ);
groupArray->EnabledQ=0;
}
}
else {
if (groupArray->EnabledQ) {
// ALLOCATION realloc
groupArray->EnabledQ=realloc(groupArray->EnabledQ,
graphsinaGroup*sizeof(dnaByte));
nullChk(groupArray->EnabledQ);
// May need to initilize extra memory, if allocated
for (i=groupArray->NumberOfGraphs; i<graphsinaGroup; i++)
groupArray->EnabledQ[i]=0;
}
else {
// ALLOCATION groupArray->EnabledQ=calloc(graphsinaGroup,sizeof(dnaByte));
nullChk(groupArray->EnabledQ);
}
}
for (i=graphsinaGroup; i<groupArray->NumberOfGroups; i++) {
if (groupArray->Labels[i])
free(groupArray->Labels[i]);
groupArray->Labels[i]=0;
groupArray->LabelSize[i]=0;
}
}
// The last thing done in this function :
groupArray->NumberOfGroups=groups;
groupArray->NumberOfGraphs=graphsinaGroup;
return 0;
Error:
return -1;
}
int CVIFUNC dna_DeleteGroup (dnaARFGroupArray *groupArray, int index)
{
int err=0;
int i;
require(index<groupArray->NumberOfGroups);
// Kill the target
errChk(dna_ReInitGroup (&groupArray->ARFGroups[index], 0, 0, 0, 0));
for (i=index; i<groupArray->NumberOfGroups-1;i++)
groupArray->ARFGroups[i]=groupArray->ARFGroups[i+1];
// Kill the duplicate
errChk(dna_ReInitGroup (&groupArray->ARFGroups[groupArray->NumberOfGroups-1],
0, 0, 0, 0));
--groupArray->NumberOfGroups;
Error:
return (err?-1:0);
}
int CVIFUNC dna_SetGraphLabel (dnaARFGroupArray *groupArray, int graphIndex,
char *graphName)
{
int err=0;
char * buf;
if (graphName) {
buf = StrDup(graphName);
nullChk(buf);
groupArray->LabelSize[graphIndex]=strlen(buf);
groupArray->Labels[graphIndex]=buf;
}
else {
groupArray->LabelSize[graphIndex]=0;
groupArray->Labels[graphIndex]=0;
}
return 0;
Error:
return -1;
}
int CVIFUNC dna_InitGroup (dnaARFGroup *groupPointer, char *groupName,
dnaByte graphs)
{
int err=0;
int i;
if (groupName==0) {
groupPointer->LabelSize=0;
groupPointer->Label=0;
}
else {
// ALLOCATION
groupPointer->LabelSize=strlen(groupName);
groupPointer->Label=calloc(groupPointer->LabelSize+1,sizeof(char));
}
groupPointer->Ticks=1;
groupPointer->TimeUnit=dnaMSEC;
groupPointer->TicksD=1;
groupPointer->TimeUnitD=dnaSEC;
groupPointer->NumberOfGraphs=graphs;
// ALLOCATION
groupPointer->MemoryUsed = graphs * sizeof(dnaARFGraph);
groupPointer->ARFGraphs = calloc(graphs,sizeof(dnaARFGraph));
nullChk(groupPointer->ARFGraphs);
for (i=0; i<graphs; i++) {
errChk(dna_InitARFGraph(&groupPointer->ARFGraphs[i]));
groupPointer->ARFGraphs[i].RangeIndex=i;
if (2==groupPointer->NumberOfGraphs) // Must be RF
groupPointer->ARFGraphs[i].RangeIndex+=dnaAnalogChannels;
}
return 0;
Error:
return -1;
}
// option =0 to clear old data, option =1 to save old data
// graphs=0 frees memory
// values is number of XY pairs to allocate in the graphs
// values must be !=0 to reallocate graphs
int CVIFUNC dna_ReInitGroup (dnaARFGroup *groupPointer, char *groupName,
dnaByte graphs, int values, int option)
{
int err=0;
int i;
if (0==graphs)
values=0;
if (0==option) {
if (groupPointer->Label) {
free(groupPointer->Label);
groupPointer->Label=0;
groupPointer->LabelSize=0;
}
if (groupName) {
groupPointer->LabelSize=strlen(groupName);
// ALLOCATION call
groupPointer->Label=StrDup(groupName);
}
groupPointer->Ticks=1;
groupPointer->TimeUnit=dnaMSEC;
groupPointer->TicksD=1;
groupPointer->TimeUnitD=dnaSEC;
if (groupPointer->ARFGraphs) {
// free the memory that the graphs allocated
for (i=0; i<groupPointer->NumberOfGraphs; i++) {
errChk(dna_ReInitARFGraph (&groupPointer->ARFGraphs[i],
0, option)); // 0==values IMPLIES Free Memory
}
// then lose the memory of the graphs themselves
free(groupPointer->ARFGraphs);
groupPointer->ARFGraphs=0;
groupPointer->MemoryUsed=0;
}
if (graphs) {
groupPointer->MemoryUsed = graphs * sizeof(dnaARFGraph);
// ALLOCATION
groupPointer->ARFGraphs = calloc(graphs,sizeof(dnaARFGraph));
nullChk(groupPointer->ARFGraphs);
}
groupPointer->NumberOfGraphs=graphs;
for (i=0; i<graphs; i++) {
errChk(dna_InitARFGraph(&groupPointer->ARFGraphs[i]));
groupPointer->ARFGraphs[i].RangeIndex=i;
if (2==graphs) // Must be RF
groupPointer->ARFGraphs[i].RangeIndex+=dnaAnalogChannels;
}
}
else {
if (groupName) {
if (groupPointer->Label) {
free(groupPointer->Label);
groupPointer->Label=0;
groupPointer->LabelSize=0;
}
groupPointer->LabelSize=strlen(groupName);
// ALLOCATION call
groupPointer->Label=StrDup(groupName);
}
if (0==graphs) { // blow away existing data
if (groupPointer->ARFGraphs) {
// Need to free existing graphs
for (i=0; i<groupPointer->NumberOfGraphs; i++) {
errChk(dna_ReInitARFGraph(&groupPointer->ARFGraphs[i],
0,0));
}
free(groupPointer->ARFGraphs);
groupPointer->ARFGraphs=0;
}
groupPointer->MemoryUsed=0;
groupPointer->NumberOfGraphs=0;
}
else {
if (groupPointer->ARFGraphs) {
if (graphs*sizeof(dnaARFGraph)>groupPointer->MemoryUsed) {
// Need more memory
// ALLOCATION realloc
groupPointer->ARFGraphs=realloc(groupPointer->ARFGraphs,
graphs*sizeof(dnaARFGraph));
nullChk(groupPointer->ARFGraphs);
groupPointer->MemoryUsed=graphs*sizeof(dnaARFGraph);
// Must initialize the newly allocated piece of memory
// Note : everything beyond NumberOfGraphs but that was
// part of old memory block is assumed to have been
// NOT holding only any allocated pointers
for (i=groupPointer->NumberOfGraphs; i<graphs; i++) {
errChk(dna_InitARFGraph(&groupPointer->ARFGraphs[i]));
groupPointer->ARFGraphs[i].RangeIndex=i;
if (2==graphs) // Must be RF
groupPointer->ARFGraphs[i].RangeIndex+=dnaAnalogChannels;
}
}
else {
if (graphs<groupPointer->NumberOfGraphs) {
// Need fewer graphs, destroy extra ones
for (i=graphs; i<groupPointer->NumberOfGraphs; i++) {
errChk(dna_ReInitARFGraph(
&groupPointer->ARFGraphs[i],0,0));
groupPointer->ARFGraphs[i].RangeIndex=i;
if (2==graphs) // Must be RF
groupPointer->ARFGraphs[i].RangeIndex+=dnaAnalogChannels;
}
}
}
}
else { // We get fresh memory
groupPointer->MemoryUsed = graphs * sizeof(dnaARFGraph);
// ALLOCATION
groupPointer->ARFGraphs = calloc(graphs,sizeof(dnaARFGraph));
nullChk(groupPointer->ARFGraphs);
}
// Now we can manipulate the graphs
for (i=0; i<graphs; i++) {
errChk(dna_ReInitARFGraph(&groupPointer->ARFGraphs[i],
values,option));
groupPointer->ARFGraphs[i].RangeIndex=i;
if (2==graphs) // Must be RF
groupPointer->ARFGraphs[i].RangeIndex+=dnaAnalogChannels;
}
}
// Lastly we can set the NumberOfGraphs
groupPointer->NumberOfGraphs=graphs;
}
return 0;
Error:
return -1;
}
// HOW THE HELL CAN I FIX THIS WITH RESPECT TO RANGEINDEX?
// ANSWER : LIKE RANGE BEFORE, DO NOT COPY RANGEINDEX
int CVIFUNC dna_CopyARFGroup (dnaARFGroup *destination, dnaARFGroup *source)
{
int err=0;
int i,max; // max is total graphs allocated
nullChk(destination);
nullChk(source);
errChk(dna_ReInitGroup (destination, 0, 0, 0, 0)); // erase destination
// ALLOCATION call
errChk(dna_ReInitGroup (destination, 0, source->NumberOfGraphs, 0, 0));
// ALLOCATION call
errChk(dna_SetGroupLabel (destination, source->Label));
destination->Ticks=source->Ticks;
destination->TimeUnit=source->TimeUnit;
destination->TicksD=source->TicksD;
destination->TimeUnitD=source->TimeUnitD;
destination->MemoryUsed=source->MemoryUsed;
// copy the graphs
max=(destination->MemoryUsed)/sizeof(dnaARFGraph);
if (0<max)
nullChk(destination->ARFGraphs);
for (i=0;i<max;i++) {
// ALLOCATION call
errChk(dna_CopyARFGraph(&destination->ARFGraphs[i],
&source->ARFGraphs[i]));
}
Error:
return (err?-1:0);
}
int CVIFUNC dna_SetGroupLabel (dnaARFGroup *groupPointer, char *groupName)
{
int err=0;
nullChk(groupPointer);
if (groupPointer->Label)
// ALLOCATION free
free(groupPointer->Label);
groupPointer->LabelSize=0;
if (groupName) {
groupPointer->Label = StrDup (groupName);
nullChk(groupPointer->Label);
groupPointer->LabelSize=strlen(groupName);
}
else {
groupPointer->Label=0;
groupPointer->LabelSize=0;
}
return 0;
Error :
return -1;
}
// NOT IMPLIMENTED
int CVIFUNC dna_SetGroupGraphs (dnaARFGroup *groupPointer, dnaByte graphs)
{
int err=0;
require(0); // die if you try....
Error:
return -1;
}
int CVIFUNC dna_DuplicateGraphValue (dnaARFGraph *graphPointer, int index)
{
int err=0,i;
int max;
nullChk(graphPointer);
max = graphPointer->ValueMemoryUsed/sizeof(double);
// Paranoia state verification
require(0<max);
nullChk(graphPointer->XValues);
nullChk(graphPointer->YValues);
require(0<=graphPointer->NumberOfValues);
require(graphPointer->NumberOfValues<=max);
require(0<=index);
require(index<=graphPointer->NumberOfValues);
// Now move data
for (i=max-1; i>index; i--) {
graphPointer->XValues[i]=graphPointer->XValues[i-1];
graphPointer->YValues[i]=graphPointer->YValues[i-1];
}
if (graphPointer->NumberOfValues<max)
++graphPointer->NumberOfValues;
Error:
return (err?-1:0);
}
int CVIFUNC dna_DeleteGraphValue (dnaARFGraph *graphPointer, int index)
{
int err=0,i;
int max;
nullChk(graphPointer);
max = graphPointer->ValueMemoryUsed/sizeof(double);
// Paranoia state verification
require(0<max);
nullChk(graphPointer->XValues);
nullChk(graphPointer->YValues);
require(0<=graphPointer->NumberOfValues);
require(graphPointer->NumberOfValues<=max);
require(0<=index);
require(index<=graphPointer->NumberOfValues);
// Now move data
for (i=index+1; i<max; i++) {
graphPointer->XValues[i-1]=graphPointer->XValues[i];
graphPointer->YValues[i-1]=graphPointer->YValues[i];
}
if (0<graphPointer->NumberOfValues)
--graphPointer->NumberOfValues;
Error:
return (err?-1:0);
}
// i had to make this a more paranoid function
int CVIFUNC dna_CalcXYValuesMinMax (dnaARFGraph *graphPointer, int which)
{
int i,err=0;
double min,max;
nullChk(graphPointer);
if (!InterpData[graphPointer->InterpType].ValuesEnabledQ) {
graphPointer->XMin = 0.0;
graphPointer->XMax = 0.0;
graphPointer->YMin = 0.0;
graphPointer->YMax = 0.0;
}
else {
if (graphPointer->NumberOfValues) {
if (which && 1) {
min = graphPointer->XValues[0];
max = graphPointer->XValues[0];
for (i=1; i<graphPointer->NumberOfValues; i++) {
if (graphPointer->XValues[i]<min)
min = graphPointer->XValues[i];
if (graphPointer->XValues[i]>max)
max = graphPointer->XValues[i];
}
graphPointer->XMin = min;
graphPointer->XMax = max;
}
if (which && 2) {
min = graphPointer->YValues[0];
max = graphPointer->YValues[0];
for (i=1; i<graphPointer->NumberOfValues; i++) {
if (graphPointer->YValues[i]<min)
min = graphPointer->YValues[i];
if (graphPointer->YValues[i]>max)
max = graphPointer->YValues[i];
}
graphPointer->YMin = min;
graphPointer->YMax = max;
}
}
else {
// Note that in this case which is ignored and both are cleared
graphPointer->XMin = 0.0;
graphPointer->XMax = 0.0;
graphPointer->YMin = 0.0;
graphPointer->YMax = 0.0;
}
}
Error:
return (err?-1:0);
}