-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTauArgus.cpp
6137 lines (5370 loc) · 170 KB
/
TauArgus.cpp
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
/*
* Argus Open Source
* Software to apply Statistical Disclosure Control techniques
*
* Copyright 2014 Statistics Netherlands
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the European Union Public Licence
* (EUPL) version 1.1, as published by the European Commission.
*
* You can find the text of the EUPL v1.1 on
* https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
*
* This software is distributed on an "AS IS" basis without
* warranties or conditions of any kind, either express or implied.
*/
#include "TauArgus.h"
#include <map>
#include <cstdio>
#include <cstring>
#include <cfloat>
#include <cmath>
#include <cstdarg>
#include <algorithm>
#include <vector>
#include <sstream>
#include "General.h"
#include "Globals.h"
#include "AMPL.h"
#include "Properties.h"
#include "PTable.h"
#include "PTableCont.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
double mysign(double x){
return (x >= 0.0) ? 1.0 : -1.0; // 1 in case x >= 0, -1 otherwise
}
static Properties TauArgusErrors("TauArgus.properties");
//extern int CurrentHoldingNr;
/////////////////////////////////////////////////////////////////////////////
// TauArgus
#ifdef _DEBUG
static int DEBUGprintf(const char *fmt, ...)
{
int retval = -1;
// FILE *f = fopen("C:\\Users\\Gebruiker\\Documents\\debug.txt","a");
// if (f) {
va_list arg; /* argument pointer */
va_start(arg, fmt); /* make arg point to first unnamed argument */
retval = vfprintf(stderr, fmt, arg);
// retval = vfprintf(f, fmt, arg);
// fclose(f);
// }
return retval;
}
#else
inline static int DEBUGprintf(const char *fmt, ...) { return 0; }
#endif
void TauArgus::SetProgressListener(IProgressListener* ProgressListener)
{
m_ProgressListener = ProgressListener;
}
void TauArgus::FireUpdateProgress(int Perc)
{
if (m_ProgressListener != NULL){
m_ProgressListener->UpdateProgress(Perc);
}
}
// cells that are set as secondary unsafe to be undone
bool TauArgus::UndoSecondarySuppress(long TableIndex, long SortSuppress)
{
if (TableIndex < 0 || TableIndex >= m_ntab){
return false;
}
if ((SortSuppress < 1) || (SortSuppress > 3)){
return false;
}
CTable *tab = GetTable(TableIndex);
tab->UndoSecondarySuppress(SortSuppress);
return true;
}
// Set number of Variables
bool TauArgus::SetNumberVar(long nVar)
{
if (nVar < 1){
return false;
}
CleanUp(); // in case of a second call very usefull
m_nvar = nVar;
m_var = new CVariable[m_nvar+1]; // Last variable for the empty variables
// if the table is a freq table etc
if (m_var == 0){
return false;
}
m_ntab = 0;
m_var[m_nvar].nDec =0; //Anders gaat het schrijven van JJ-files later mis!! Anco 1-6-2004
return true;
}
// set number of tables
bool TauArgus::SetNumberTab(long nTab)
{
// Not the right moment, first call SetNumberVar
if (m_nvar == 0 || nTab < 1){
return false;
}
// ev. delete previous tables
CleanTables();
m_ntab = nTab;
m_tab = new CTable[nTab + nTab]; // second part for recoded tables
if (m_tab == 0){
return false;
}
return true;
}
// Compute the Tabels. In this function all the tables are filled.
bool TauArgus::ComputeTables(long *ErrorCode, long *TableIndex)
{
// long MemSizeAll = 0, MemSizeTable;
// initialize errorcodes
*ErrorCode = -1; // na
*TableIndex = -1; // na
// Not the right moment, first call SetNumberVar
if (m_nvar == 0){
*ErrorCode = NOVARIABLES;
return false;
}
// Not the right moment, first call SetNumberTab
if (m_ntab == 0){
*ErrorCode = NOTABLES;
return false;
}
// First do Explore File
if (!m_CompletedCodeList){
*ErrorCode = NODATAFILE;
return false;
}
// All tables set?
int i;
for (i = 0; i < m_ntab; i++){
if (m_tab[i].nDim == 0){
*ErrorCode = TABLENOTSET;
return false;
}
}
// Prepare each table
// Allocate memory for cells in tables
for (i = 0; i < m_ntab; i++){
if (!m_tab[i].PrepareTable() ){
*ErrorCode = NOTABLEMEMORY;
*TableIndex = i;
return false;
}
}
// do datafile
FILE *fd = fopen(m_fname, "r");
if (fd == 0){
*ErrorCode = FILENOTFOUND;
return false;
}
int recnr = 0;
while (!feof(fd) ){
char str[MAXRECORDLENGTH];
int res = ReadMicroRecord(fd, str);
if (++recnr % FIREPROGRESS == 0){
FireUpdateProgress((int)(ftell(fd) * 100.0 / m_fSize)); // for progressbar in container
}
switch (res){
case -1: // error
goto error;
break;
case 0: // eof
goto oke;
break;
case 1:
// here is where tables are filled
FillTables(str);
break;
}
}
oke:
fclose(fd);
FireUpdateProgress(100); // for progressbar in container
// Once more add freq
// For holding tables the last holding has to be
// corrected
MergeLastTempShadow();
// compute Status Cells for all base tables
for (i = 0; i < m_ntab; i++){
ComputeCellStatuses(m_tab[i]);
}
// compute protection levels
for (i=0; i<m_ntab; i++){
SetProtectionLevels(m_tab[i]);
}
// compute cellkey from added recordkeys
// only if record key variable is present
// [30-06-2020] taking fractional part is already done when contructing tablecell, so no longer needed
/*for (i=0; i<m_ntab;i++){
if (m_tab[i].CellKeyVarnr>=0){ ComputeCellKeys(m_tab[i]); }
}*/
#ifdef _DEBUGG
{
for (int i = 0; i < m_ntab; i++) {
string fname;
fname.Format("tab%02d.txt", i);
ShowTable((LPCTSTR) fname, m_tab[i]);
}
}
#endif // _DEBUG
for (i = 0; i < m_nvar; i++) {
if (m_var[i].IsHierarchical && m_var[i].nDigitSplit == 0){
// empty no longer needed arrays
m_var[i].hLevel.clear();
m_var[i].hLevelBasic.clear();
}
}
return true;
error:
// You could change the errors with throws.
fclose(fd);
return false;
}
bool TauArgus::DoRecode(long VarIndex, const char* RecodeString, long nMissing, const char* eMissing1, const char* eMissing2,
long *ErrorType, long *ErrorLine, long *ErrorPos, const char** WarningString)
{
// RecodeStrings are changed to string. Check this out
int i, v = VarIndex, oke, maxwidth = 0;
*ErrorType = *ErrorLine = *ErrorPos = -1;
string Missing1 = eMissing1;
string Missing2 = eMissing2;
// for warnings
m_nOverlap = 0;
m_nUntouched = 0;
m_nNoSense = 0;
m_WarningRecode.resize(0);
// too early?
/*
if (m_nvar == 0 || m_ntab == 0 || m_fname[0] == 0) {
*ErrorType = E_NOVARTABDATA;
return false;
}
*/
if (m_nvar == 0 || m_ntab == 0 || !m_CompletedCodeList){
*ErrorType = E_NOVARTABDATA;
return false;
}
// wrong VarIndex
if (v < 0 || v >= m_nvar || !m_var[v].IsCategorical){
*ErrorType = E_VARINDEXWRONG;
return false;
}
// remove existing one, if needed
// have problems here
/*
if (m_var[v].HasRecode) {
UndoRecode(VarIndex);
}*/
// only check syntax, phase = CHECK
oke = ParseRecodeString(v, RecodeString, ErrorType, ErrorLine, ErrorPos, CHECK);
if (!oke){
return false;
}
// recode oke
// remove old recode
if (m_var[v].Recode.DestCode != 0){
free(m_var[v].Recode.DestCode);
m_var[v].Recode.DestCode = 0;
}
// initialize new recode
m_var[v].Recode.DestCode = (int *) malloc(m_var[v].nCode * sizeof(int) );
if (m_var[v].Recode.DestCode == 0){
*ErrorType = NOTENOUGHMEMORY;
return false;
}
for (i = 0; i < m_var[v].nCode; i++){
if (m_var[v].IsCodeBasic(i) ) m_var[v].Recode.DestCode[i] = -1; // no (sub)total
else m_var[v].Recode.DestCode[i] = -2; // (sub)total
}
// Missing2 specified but not Missing1? Swap!
if (Missing1.empty() && !Missing2.empty()){
Missing1 = Missing2;
}
// no missings for recode specified? Take the missing(s) of the source
if (Missing1.empty() && Missing2.empty()){
Missing1 = m_var[v].Missing1;
if (m_var[v].nMissing == 2){
Missing2 = m_var[v].Missing2;
}
else{
Missing2 = Missing1;
}
}
m_var[v].Recode.sCode.clear();
m_var[v].Recode.nCode = 0;
m_var[v].Recode.CodeWidth = 0;
m_var[v].Recode.nMissing = 0;
m_var[v].Recode.sCode.push_back(""); // for Total
// another time, now compute list of dest codes
ParseRecodeString(v, RecodeString, ErrorType, ErrorLine, ErrorPos, DESTCODE);
if (m_var[v].Recode.sCode.size() < 2){
*ErrorType = E_EMPTYSPEC;
*ErrorLine = 1;
*ErrorPos = 1;
return false;
}
// sort list of dest codes, still without missing values (coming soon)
QuickSortStringArray(m_var[v].Recode.sCode);
// now the number of codes is known, but not the not mentioned ones
// m_var[v].Recode.nCode = m_var[v].Recode.sCode.GetSize();
// again, now compute dest codes and link between dest and src
oke = ParseRecodeString(v, RecodeString, ErrorType, ErrorLine, ErrorPos, SRCCODE);
if (!oke){
return false; // missing to valid codes, a terrible shame
}
// compute untouched codes, add them to the recode codelist
m_nUntouched = 0;
for (i = 0; i < m_var[v].nCode - m_var[v].nMissing; i++){
if (m_var[v].Recode.DestCode[i] == -1){ // not touched
m_nUntouched++;
AddRecode(v, m_var[v].sCode[i].c_str());
}
}
// make all recode codes same width
maxwidth = MakeRecodelistEqualWidth(v, Missing1.c_str(), (LPCTSTR) Missing2.c_str());
// remove the missing codes,
if (!((nMissing == 0) && (m_var[v].nMissing ==0))){
int n;
bool IsMissing;
string mis = Missing1;
AddSpacesBefore(mis, maxwidth);
if (n = BinSearchStringArray(m_var[v].Recode.sCode, mis, 0, IsMissing), n >= 0){
vector<string>::iterator p = m_var[v].Recode.sCode.begin() + n;
m_var[v].Recode.sCode.erase(p);
}
mis = Missing2;
AddSpacesBefore(mis, maxwidth);
if (n = BinSearchStringArray(m_var[v].Recode.sCode, mis, 0, IsMissing), n >= 0){
vector<string>::iterator p = m_var[v].Recode.sCode.begin() + n;
m_var[v].Recode.sCode.erase(p);
}
}
// sort list of dest codes, still without missing values (coming soon)
QuickSortStringArray(m_var[v].Recode.sCode);
// ADD MISSING1 AND -2
// both empty impossible, see start of function
// swap missings if missing1 empty
if (!((nMissing == 0) && (m_var[v].nMissing ==0))){
if (Missing1.empty() && Missing2.empty()){ // no missing specified?
m_var[v].Recode.Missing1 = m_var[v].Missing1; // take the missing of source variable
m_var[v].Recode.Missing2 = m_var[v].Missing2;
}
else{
if (Missing1.empty()){ // at least one missing specified
m_var[v].Recode.Missing1 = Missing2;
m_var[v].Recode.Missing2 = Missing1;
}
else{
m_var[v].Recode.Missing1 = Missing1;
m_var[v].Recode.Missing2 = Missing2;
}
}
// second empty?
if (m_var[v].Recode.Missing2.empty()) {
m_var[v].Recode.Missing2 = m_var[v].Recode.Missing1;
m_var[v].Recode.nMissing = 1;
}
else {
// equal?
if (m_var[v].Recode.Missing1 == m_var[v].Recode.Missing2){
m_var[v].Recode.nMissing = 1;
}
else{
m_var[v].Recode.nMissing = 2;
}
}
// put in list, last one or two
AddSpacesBefore(m_var[v].Recode.Missing1, maxwidth);
AddRecode(v, m_var[v].Recode.Missing1.c_str());
if (m_var[v].Recode.nMissing == 2) {
AddSpacesBefore(m_var[v].Recode.Missing2, maxwidth);
AddRecode(v, m_var[v].Recode.Missing2.c_str());
}
}
// last time, now compute dest codes and link between dest and src
// for warnings
m_nOverlap = 0;
m_nNoSense = 0;
for (i = 0; i < m_var[v].nCode; i++) {
if (m_var[v].IsCodeBasic(i) ) m_var[v].Recode.DestCode[i] = -1; // no (sub)total
else m_var[v].Recode.DestCode[i] = -2; // (sub)total
}
oke = ParseRecodeString(v, RecodeString, ErrorType, ErrorLine, ErrorPos, SRCCODE);
if (!oke) {
return false; // missing to valid codes, a terrible shame
}
// yep, the number of codes is known and the codes are sorted (except one or two MISSINGs at the end of te list)
m_var[v].Recode.nCode = m_var[v].Recode.sCode.size();
// do the same for MISSINGS, more complicated
{
RECODE *c = &(m_var[v].Recode); // for easier reference
i = m_var[v].nCode - m_var[v].nMissing; // first missing
if (c->DestCode[i] == -1) { // missing 1 not specified
c->DestCode[i] = c->nCode - c->nMissing; // make missing1 equal to missing1 source
}
if (m_var[v].nMissing == 2) { // two missings in source?
i++;
if (c->nMissing == 2) { // two missings in dest?
if (c->DestCode[i] == -1) { // missing 2 not specified
c->DestCode[i] = c->nCode - c->nMissing + 1; // make missing2 equal to missing2 source
}
}
else {
if (c->DestCode[i] == -1) { // missing 2 not specified
c->DestCode[i] = c->nCode - c->nMissing; // make missing2 equal to missing1 source
}
}
}
}
// set untouched codes on right index
{
RECODE *c = &(m_var[v].Recode); // for easier reference
bool IsMissing;
int index, ncode = m_var[v].nCode - m_var[v].nMissing;
for (i = 0; i < ncode; i++) {
if (c->DestCode[i] == -1) {
string str = m_var[v].sCode[i];
AddSpacesBefore(str, c->CodeWidth);
index = BinSearchStringArray(c->sCode, str, c->nMissing, IsMissing);
ASSERT(index >= 0 && index < c->nCode);
c->DestCode[i] = index;
}
}
}
// WARNINGS in recode:
// show untouched codes:
ostringstream ss;
if (m_nUntouched > 0) {
ss << "Number of untouched codes: " << m_nUntouched << "\r\n";
}
// show warnings
if (m_nOverlap > 0) {
ss << "Number of overlapping codes: " << m_nOverlap << "\r\n";
}
if (m_nNoSense > 0) {
ss << "Number of \"no sense\" codes: " << m_nNoSense << "\r\n";
}
m_WarningRecode = ss.str();
if (m_WarningRecode.empty() ) {
m_WarningRecode = "Recode OK";
}
*WarningString = m_WarningRecode.c_str();
m_var[v].HasRecode = true;
m_var[v].Recode.nCode = m_var[v].Recode.sCode.size();
m_var[v].Recode.sCode[0] = "";
return true;
}
// Apply Recoding. You need this for recoding tables
void TauArgus::ApplyRecode()
{
ComputeRecodeTables();
}
// Clean all allocated memory. Destructor does this
void TauArgus::CleanAll()
{
CleanUp();
}
// Used for setting Hierarchical Variables with digit Split
bool TauArgus::SetHierarchicalDigits(long VarIndex, long nDigitPairs, long *nDigits)
{
if (VarIndex < 0 || VarIndex >= m_nvar || !m_var[VarIndex].IsHierarchical){
return false;
}
if (m_var[VarIndex].hLevel.size() != 0){
return false;
}
// return m_var[VarIndex].SetHierarchicalDigits(nDigitPairs, nDigits);
if (m_var[VarIndex].SetHierarchicalDigits(nDigitPairs, nDigits) == 0){
return false;
}
else{
return true;
}
}
// For every row get all cells with corresponding information
// Not used in GUI nor in this dll????
/*bool TauArgus::GetTableRow(long TableIndex, long *DimIndex, double *Cell,
long *Status, long CountType)
{
int coldim = -1, nCodes;
long DimNr[MAXDIM];
long DoAudit, CountTypeLocal;
// check parameters
if (TableIndex < 0 || TableIndex >= m_ntab) {
return false;
}
DoAudit = 0;
CountTypeLocal = CountType;
if (CountTypeLocal < 0 ) {
DoAudit = 1;
CountTypeLocal = -CountTypeLocal;
}
CTable *Table = GetTable(TableIndex);
// check DimIndices
for (int i = 0; i < Table->nDim; i++) {
CVariable *var = &(m_var[Table->ExplVarnr[i]]);
nCodes = var->GetnCode();
DimNr[i] = DimIndex[i];
switch (DimIndex[i]) {
case -1:
if (coldim == -1) {
coldim = i;
}
else {
return false; // more than one coldim specified
}
break;
default:
if (DimIndex[i] < 0 || DimIndex[i] >= nCodes) {
return false;
}
break;
}
}
if (coldim == -1) { // no coldim specified
return false;
}
// parameters oke
CDataCell *dc;
nCodes = m_var[Table->ExplVarnr[coldim]].GetnCode();
for (int c = 0; c < nCodes; c++) {
// fill table row cells
DimNr[coldim] = c;
dc = Table->GetCell(DimNr); // get pointer to cell from table
switch (CountTypeLocal) {
case CT_RESPONS:
Cell[c] = dc->GetResp();
break;
case CT_SHADOW:
Cell[c] = dc->GetShadow();
break;
case CT_COST:
Cell[c] = dc->GetCost(Table->Lambda);
break;
case CT_ROUNDEDRESP:
Cell[c] = dc->GetRoundedResponse();
break;
case CT_CTA:
Cell[c] = dc->GetCTAValue();
break;
case CT_CKM:
Cell[c] = dc->GetCKMValue();
default:
return false;
}
// set Status
Status[c] = dc->GetStatus();
if (DoAudit==1) {
double XRL, XRU, XPL, XPU, XC;
if ((Status[c] >= CS_UNSAFE_RULE) && (Status[c] <= CS_UNSAFE_MANUAL )) {
XRL = dc->GetRealizedLowerValue();
XRU = dc->GetRealizedUpperValue();
XPL = dc->GetLowerProtectionLevel();
XPU = dc->GetUpperProtectionLevel();
XC = dc->GetResp();
if ( (XRU < (XC + XPU)) || (XRL > (XC-XPL)) ){
Status[c] = -Status[c];
}
}//prim
if ((Status[c] == CS_SECONDARY_UNSAFE) || (Status[c] == CS_SECONDARY_UNSAFE_MANUAL )) {
XRL = dc->GetRealizedLowerValue();
XRU = dc->GetRealizedUpperValue();
XC = dc->GetResp();
if ((XRU==XC) && (XRL==XC)) {
Status[c]=-Status[c];
}
} // sec
} //if audit
} //for
return true;
}*/
// return information about Unsafe Variable
bool TauArgus::UnsafeVariable(long VarIndex, long *Count, long *UCArray)
{
int t;
if (VarIndex < 0 || VarIndex >= m_nvar) {
return false;
}
// compute count
*Count = 0;
for (t = 0; t < m_ntab; t++) {
CTable *tab = GetTable(t);
if (tab->nDim > *Count) {
*Count = tab->nDim;
}
}
memset(UCArray, 0, *Count * sizeof(long) );
// compute nUnsafe for variable VarIndex, add to UCArray
long nUnsafe[MAXDIM];
for (t = 0; t < m_ntab; t++) {
CTable *tab = GetTable(t);
int var;
for (var = 0; var < tab->nDim; var++) {
if (tab->ExplVarnr[var] == VarIndex) break; // hebbes
}
if (var < tab->nDim) {
tab->GetUnsafeCells(VarIndex, nUnsafe);
for (int i = 0; i < tab->nDim; i++) {
UCArray[i] += nUnsafe[i];
}
}
}
TRACE("Var %d unsafe %d %d %d\n", VarIndex, UCArray[0], UCArray[1], UCArray[2]);
return true;
}
// In this function the input file is read and the code list is built
bool TauArgus::ExploreFile(const char* FileName, long *ErrorCode, long *LineNumber, long *ErrorVarIndex)
{
char str[MAXRECORDLENGTH];
int i, length, recnr = 0, Result;
*ErrorCode = *LineNumber = 0;
*ErrorVarIndex = -1;
if (m_nvar == 0) {
*ErrorCode = NOVARIABLES;
return false;
}
for (i = 0; i < m_nvar; i++) {
if (InFileIsFixedFormat) {
if (m_var[i].bPos < 0) {
*ErrorCode = VARIABLENOTSET;
return false;
}
}
else {
if (!m_var[i].PositionSet) {
*ErrorCode = VARIABLENOTSET;
return false;
}
}
}
FILE *fd = fopen(FileName, "r");
if (fd == 0) {
*ErrorCode = FILENOTFOUND;
return false;
}
fseek(fd, 0, SEEK_END);
m_fSize = ftell(fd);
rewind(fd);
// read first record to determine the fixed recordlength
str[0] = 0;
fgets((char *)str, MAXRECORDLENGTH, fd);
if (str[0] == 0) {
*ErrorCode = EMPTYFILE;
goto error;
}
length = strlen((char *)str) - 1;
while (length > 0 && str[length] < ' ') length--;
m_fixedlength = length + 1;
if (length == 0) {
*ErrorCode = EMPTYFILE; // first record empty
goto error;
}
if (!InFileIsFixedFormat) {
m_maxBPos = -1;
for (i = 0; i < m_nvar; i++) {
if (m_var[i].bPos > m_maxBPos) {
m_maxBPos = m_var[i].bPos;
}
}
}
// record length oke? hierachie oke?
for (i = 0; i < m_nvar; i++) {
if (InFileIsFixedFormat) {
if (m_var[i].bPos + m_var[i].nPos > m_fixedlength) {
*ErrorCode = RECORDTOOSHORT;
goto error;
}
}
if (m_var[i].IsHierarchical) {
if (m_var[i].nDigitSplit == 0 && m_var[i].hLevel.size() == 0) {
*ErrorCode = WRONGHIERARCHY;
goto error;
}
}
// initialize Min and Max Value for Numerics
if (m_var[i].IsNumeric) {
m_var[i].MaxValue = -DBL_MAX;
m_var[i].MinValue = DBL_MAX;
}
}
// read micro data to make codelists for categorical variables
rewind(fd);
while (!feof(fd) ) {
int res, varindex;
res = ReadMicroRecord(fd, str);
switch (res) {
case -1: // error
recnr++;
*ErrorCode = WRONGLENGTH;
*LineNumber = recnr;
goto error;
case 0: // eof
goto oke;
case 1: // oke
recnr++;
if (recnr % FIREPROGRESS == 0) {
FireUpdateProgress((int)(ftell(fd) * 100.0 / m_fSize)); // for progressbar in container
}
// Here is where the code lists are built
if (Result = DoMicroRecord(str, &varindex), Result >= 1000) { // error?
*ErrorCode = Result;
*LineNumber = recnr;
*ErrorVarIndex = varindex;
goto error;
}
break;
}
}
oke:
fclose(fd);
m_nRecFile = recnr;
FireUpdateProgress(100); // for progressbar in container
// add hierarchy, Missing1 and -2
for (i = 0; i < m_nvar; i++) {
// add hierarchy for split levels
if (m_var[i].nDigitSplit > 0) {
if (!m_var[i].ComputeHierarchicalCodes() ) {
*ErrorCode = WRONGHIERARCHY;
*LineNumber = -1;
return false;
}
}
// add total
if (!m_var[i].IsHierarchical || m_var[i].nDigitSplit > 0) {
m_var[i].AddCode("", false);
}
// add Missing1 and -2
if (m_var[i].IsCategorical) {
if (m_var[i].nMissing != 0) {
m_var[i].AddCode(m_var[i].Missing1.c_str(), true);
if (m_var[i].IsHierarchical && m_var[i].nDigitSplit == 0) {
m_var[i].hLevel.push_back(1);
m_var[i].hLevelBasic.push_back(true);
}
if (m_var[i].nMissing == 2) {
m_var[i].AddCode(m_var[i].Missing2.c_str(), true);
if (m_var[i].IsHierarchical && m_var[i].nDigitSplit == 0) {
m_var[i].hLevel.push_back(1);
m_var[i].hLevelBasic.push_back(true);
}
}
}
}
m_var[i].nCode = m_var[i].sCode.size();
// allocate and initialize memory
if (m_var[i].IsHierarchical) {
if (!m_var[i].SetHierarch() ) {
*ErrorCode = WRONGHIERARCHY;
*LineNumber = -1;
return false;
}
}
}
strcpy(m_fname, FileName); // Save name for use in ComputeTables
m_CompletedCodeList = true;
// ShowCodeLists(); // in output pane
return true;
error:
fclose(fd);
return false;
}
// ??? Never used in this dll nor in the GUI ???
// get maximum unsafe Combination
/*long TauArgus::GetMaxnUc()
{
if (m_nvar == 0 || m_ntab == 0 || !m_CompletedCodeList) {
return -1;
}
int max = 0;
for (int t = 0; t < m_ntab; t++) {
CTable *tab;
tab = GetTable(t);
for (int c = 0; c < tab->nCell; c++) {
long tempstatus = tab->GetCell(c)->GetStatus();
if ((tempstatus == CS_UNSAFE_FREQ) || (tempstatus == CS_UNSAFE_PEEP) ||
(tempstatus == CS_UNSAFE_RULE) || (tempstatus == CS_UNSAFE_SINGLETON) ||
(tempstatus == CS_UNSAFE_ZERO)) max++;
}
}
return max;
}*/
// Undo recode. Undo recodes for a variable. This is used when a table is
// created to be recoded
bool TauArgus::UndoRecode(long VarIndex)
{
/*if (m_nvar == 0 || m_ntab == 0 || m_fname[0] == 0) {
return false;
}*/
if (m_nvar == 0 || m_ntab == 0 || !m_CompletedCodeList) {
return false;
}
// wrong VarIndex
if (VarIndex < 0 || VarIndex >= m_nvar || !m_var[VarIndex].IsCategorical) {
return false;
}
m_var[VarIndex].UndoRecode();
// recomputes for all tables the flag HasRecode
SetTableHasRecode();
return true;
}
//Sets the status of a cell to a given status
bool TauArgus::SetTableCellStatus(long TableIndex, long *DimIndex, long CelStatus)
{
// redo protection levels
int iOriginalStatus;
CDataCell *dc;
// check parameters
if (TableIndex < 0 || TableIndex >= m_ntab) {
return false;
}
//if (m_fname[0] == 0) return false;
if (!m_CompletedCodeList) {
return false;
}
// check CellStatus; status EMPTY cannot be set
// Note empty can be set. empty means freq is nul
if (CelStatus < CS_SAFE || CelStatus >= CS_EMPTY) {
return false;
}
CTable *table = GetTable(TableIndex);
//dc = table ->GetCell(DimIndex);
//iTemporaryStatus = table->ComputeCellSafeCode(*dc);
// check DimIndices
for (int i = 0; i < table->nDim; i++) {
int nCodes = m_var[table->ExplVarnr[i]].GetnCode();
ASSERT(DimIndex[i] >= 0 && DimIndex[i] < nCodes);
if (DimIndex[i] < 0 || DimIndex[i] >= nCodes) {
return false;
}
}
// set CellStatus
// if manual unsafe or manual safe then check rules.
// So that you set the status to the original status.
// Manual unsafe is set as unsafe if the cel was already unsafe
// Manual safe is set as safe if the cel was already safe
dc = table ->GetCell(DimIndex);
iOriginalStatus = table->ComputeCellSafeCode(*dc);