-
Notifications
You must be signed in to change notification settings - Fork 0
/
savtocsvlib.c
1548 lines (1117 loc) · 37.3 KB
/
savtocsvlib.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 <math.h>
#include "common.h"
/** @var bool bigEndian flag to see if file was oringinally stored on Big Endian OS... */
bool bigEndian = false;
/** @var FILE* savPtr File pointer for sav file */
FILE* savPtr;
/** @var int cursor Internal library cursor */
int cursor = 0;
/** @var char wordBuffer[4] Buffer for storing 4byte strings */
char wordBuffer[4];
/** @var int8_t intByteBuffer Buffer for storing 8 bit / 1 byte ints */
uint8_t intByteBuffer;
/** @var int8_t intByteBuffer Buffer for storing 8 bit / 1 byte ints */
//int8_t intByteBufferS;
/** @var int int32Buffer Buffer for storing 32 bit / 4 byte ints */
int int32Buffer;
/** @var int64_t int64Buffer Buffer for storing 64 bit / 8 byte ints */
int64_t int64Buffer;
/** @var double flt64Buffer Buffer for storing 64 bit / 8 byte floating point numbers */
double flt64Buffer;
/** @var int compressionSwitch Compression on or not */
int compressionSwitch;
/** @var double compressionBias Used to decode data */
double compressionBias;
/** @var int numberOfCases Num cases in sav file */
int numberOfCases = 0;
/** @var int numberOfVariables Num vars in sav file */
int numberOfVariables = 0;
/** @var variable_t* variablesList Linked list of Variable structures */
struct Variable* variablesList = NULL;
/**
* Close file
* @return void
*/
void closeFile(){
fclose(savPtr);
}
/**
* Print error, close file and exit
* @return void
*/
void exitAndCloseFile(char *str, char *bound){
printOutErr(str, bound);
closeFile();
exit(EXIT_FAILURE);
}
/**
* Add a variable to the Variables linked list
* @param variable_t * head Pointer to head of list
* @param int type Variable typecode to add one creation
* @return void
*/
void addVariable(struct Variable * head, int type, int writeFormatCode) {
struct Variable * current = head;
//find last element
while (current->next != NULL) {
current = current->next;
}
//add new var
current->next = (struct Variable *) malloc(sizeof(struct Variable));
current->next->type = type;
current->next->measure = 0;
current->next->cols = 0;
current->next->alignment = 0;
current->next->writeFormatCode = writeFormatCode;
current->next->next = NULL;
}
/**
* Main run through method
* @return void
*/
void convertToCSV(char *filename){
//try to open for read in binary mode
savPtr = fopen(filename, "rb");
//file open?
if (savPtr == NULL) {
exitAndCloseFile("Unable to open file (permission denied, try sudo): %s", filename);
}
//file passed isn't sav file
if(strcmp(getFileExt(filename),"sav") != 0){
exitAndCloseFile("File not a .sav file: %s", filename);
}
//can't open file passed
else if(savPtr == NULL){
exitAndCloseFile("Unable to open file: %s", filename);
}
//log
printOut("Opened .sav file: \n\t%s", filename, "cyan");
//initialise linked list
variablesList = (struct Variable*)malloc(sizeof(struct Variable));
if (variablesList == NULL) {
exitAndCloseFile("Failed to allocate memory for variables.", "");
}
variablesList->type = 0;
variablesList->measure = 0;
variablesList->cols = 0;
variablesList->alignment = 0;
variablesList->next = NULL;
//header
readHeader();
//meta
readMeta();
//data
if(longCsv){
dataToCsvLong();
} else {
dataToCsvFlat();
}
closeFile();
}
/**
* Read from the sav file until the start of the data blocks
* @return void
*/
void readHeader(){
if(!silent){
printOut("Reading file header:", "", "cyan");
}
//reset file pointer location to start
fseek(savPtr, 0, SEEK_SET);
//get file type
readWord("File Identifier:");
if (strcmp(wordBuffer, "$FL2") != 0){
exitAndCloseFile("File must begin with chars $FL2 for a valid SPSS .sav file.", "");
}
//@4
//read SPSS Version text
readOver((size_t)60, "Header:");
//@64
//layout code should be 2 or 3
int layout = readInt32("Layout Code:");
if(layout != 2 && layout != 3){
bigEndian = true;
printOut("File stored as Big Endian found in layout code.", "", "yellow");
}
//@68
// OBS
readInt32("OBS:");
//@72
// compression
compressionSwitch = readInt32("Compression:");
//@76
// weight
readInt32("Weight:");
//@80
// cases
numberOfCases = readInt32("Number of Cases:");
//@84
// compression bias
compressionBias = readDouble("Compression Bias:");
//@92
// creation date
readOver((size_t)9, "Creation Date:");
readOver((size_t)8, "Creation Time:");
//@109
// file label
readOver((size_t)64, "File Label:");
//@173
// padding
readOver((size_t)3, "Padding:");
//@176
if(!silent){
printOut("\t%s Cases found", intToStr32(numberOfCases), "cyan");
}
}
/**
* Read from the sav file until the start of the data blocks
* @return void
*/
void readMeta(){
if(!silent){
printOut("Reading meta data:", "", "cyan");
}
bool stop = false;
while (!stop) {
if(debug){
printOut("-------------------------", "", "blue");
printOut("-------------------------", "", "blue");
}
int recordType = readInt32("Record type:");
switch (recordType) {
// Variable Record (2)
case RECORD_TYPE_VARIABLE:
readVariable();
break;
// Value and labels (3)
case RECORD_TYPE_VALUE_LABELS:
readValueLabels();
break;
// Read and parse document records (6)
case RECORD_TYPE_DOCUMENTS:
{
// number of variables
int numberOfLines = readInt32("Number of Docs Vars:");
// read the lines
int i;
for (i = 0; i < numberOfLines; i++) {
readOver((size_t)80, "Doc Content:");
}
}
break;
// Read and parse additional records (7)
case RECORD_TYPE_ADDITIONAL:
{
int subtype = readInt32("SubType:");
//@4
int size = readInt32("Size:");
//@8
int count = readInt32("Count:");
//@12
int datalen = size * count;
switch (subtype) {
// SPSS Record Type 7 Subtype 3 - Source system characteristics
case 3:
readOver((size_t)32, "Source system characteristics:");
break;
// SPSS Record Type 7 Subtype 4 - Source system floating pt constants
case 4:
readOver((size_t)24, "Source system floating pt constants:");
break;
// SPSS Record Type 7 Subtype 5 - Variable sets
case 5:
readOver((size_t)datalen, "Variable Sets:");
break;
// SPSS Record Type 7 Subtype 6 - Trends date information
case 6:
readOver((size_t)datalen, "Trends Date Info:");
break;
// SPSS Record Type 7 Subtype 7 - Multi response groups
case 7:
readOver((size_t)datalen, "Multi Response Groups:");
break;
// SPSS Record Type 7 Subtype 11 - Variable meta SPSS bits...
case 11:
if (size != 4) {
exitAndCloseFile("Error reading record type 7 subtype 11: bad data element length [%s]. Expecting 4.", intToStr32(size));
}
if ((count % 3) != 0) {
exitAndCloseFile("Error reading record type 7 subtype 11: number of data elements [%s] is not a multiple of 3.", intToStr32(size));
}
//go through vars and set meta
struct Variable * current = variablesList;
current = current->next;
int i;
for(i = 0; i < count/3; ++i){
if(debug){
printOut("~~~Var Meta~~~", "", "magenta");
printOut("\n~~~Var Type: %s \n", intToStr32(current->type), "yellow");
}
current->measure = readInt32("~~~Var Measure:");
current->cols = readInt32("~~~Var Cols:");
current->alignment = readInt32("~~~Var Alignment:");
current = current->next;
}
break;
// SPSS Record Type 7 Subtype 13 - Extended names
case 13:
readOver((size_t)datalen, "Extended Names:");
break;
// SPSS Record Type 7 Subtype 14 - Extended strings
case 14:
readOver((size_t)datalen, "Extended Strings:");
break;
// SPSS Record Type 7 Subtype 16 - Number Of Cases
case 16:
readInt32("Byte Order:");
readOver(4, "Skip:");
readInt32("Count:");
readOver(4, "Skip:");
break;
// SPSS Record Type 7 Subtype 17 - Dataset Attributes
case 17:
readOver((size_t)datalen, "Dataset Attributes:");
break;
// SPSS Record Type 7 Subtype 18 - Variable Attributes
case 18:
readOver((size_t)datalen, "Variable Attributes:");
break;
// SPSS Record Type 7 Subtype 19 - Extended multiple response groups
case 19:
readOver((size_t)datalen, "Extended multiple response groups:");
break;
// SPSS Record Type 7 Subtype 20 - Encoding, aka code page
case 20:
readOver((size_t)datalen, "Encoding, aka code page:");
break;
// SPSS Record Type 7 Subtype 21 - Extended value labels
case 21:
readOver((size_t)datalen, "Extended value labels:");
break;
// SPSS Record Type 7 Subtype 22 - Missing values for long strings
case 22:
readOver((size_t)datalen, "Missing values for long strings:");
break;
// SPSS Record Type 7 Subtype 23 - Sort Index information
case 23:
readOver((size_t)datalen, "Sort Index information:");
break;
// SPSS Record Type 7 Subtype 24 - XML info
case 24:
readOver((size_t)datalen, "XML info:");
break;
// Other info
default:
readOver((size_t)datalen, "Misc info:");
break;
}
}
break;
// Finish
case RECORD_TYPE_FINAL:
stop = true;
int test = readInt32("Test for final rec type:");
if (test != 0) {
exitAndCloseFile("Error reading record type 999: Non-zero value found.", "");
}
break;
default:
exitAndCloseFile("Read error: invalid record type [%s]", intToStr32(recordType));
break;
}
}
//struct Variable * current = variablesList;
//current = current->next;
//int variableId = 1;
//int j;
//for(j = 0; j < numberOfVariables; j++){
//
//}
if(!silent){
printOut("\t%s Variables found", intToStr32(numberOfVariables), "cyan");
}
}
/**
* SPSS Record Type 2 - Variable information
* @throws \Exception
* @return void
*/
void readVariable()
{
int typeCode = readInt32("---Var Type Code:");
//if numeric, type code here = 0
//if string, type code is length of string.
//@4
//if TYPECODE is -1, record is a continuation of a string var
if(typeCode == -1) {
//read and ignore the next 24 bytes
readOver(24, "---String Continuation Var Skip 24:");
//otherwise normal var
} else {
numberOfVariables++;
// read label flag
int hasLabel = readInt32("---Var Has Label:");
//could throw exception here as missing label?
//@8
// read missing value format code
int missingValueFormatCode = readInt32("---Missing Format Code:");
if (abs(missingValueFormatCode) > 3) {
exitAndCloseFile("Error reading variable Record: invalid missing value format code [%s]. Range is -3 to 3.", intToStr32(missingValueFormatCode));
}
//@12
// read print format code
readInt32("---Print Format Code:");
//@16
// read write format code
int writeFormatCode = readInt32("---Write Format Code:");
//@20
// read varname
readOver((size_t)8, "---Var Short Name:");
//@28
// read label length and label only if a label exists
if (hasLabel == 1) {
int labelLength = readInt32("---Label Length:");
//@32
//need to ensure we read word-divisable amount of bytes
int rem = 4-(labelLength % 4);
if(rem == 4){
rem = 0;
}
readOver((size_t)labelLength, "---Label:");
readOver((size_t)rem, "---label Skip:");
}
// missing values
if (missingValueFormatCode != 0) {
int i;
for (i = 0; i < abs(missingValueFormatCode); ++i) {
readInt64("---Missing Values:");
}
}
addVariable(variablesList, typeCode, writeFormatCode);
}
}
/**
* SPSS Record Type 3 - Value labels
* @return void
*/
void readValueLabels()
{
// number of labels
int numberOfLabels = readInt32("+++Number of Labels:");
//@4
// labels
int i;
for (i = 0; i < numberOfLabels; i++) {
// read the label value
//double labelValue = readDouble("+++Value:");
readDouble("+++Value:");
//@8
// read the length of a value label
// the following byte in an unsigned integer (max value is 60)
uint8_t labelLength = readIntByte("+++Label Length:");
//if (labelLength > 255) {
// exitAndCloseFile("The length of a value label(%s) must be less than 60.", doubleToStr(labelValue));
//}
//need to ensure we read word-divisable amount of bytes
int rem = 8-((labelLength+1) % 8);
if(rem == 8){
rem = 0;
}
readOver((size_t)labelLength, "+++Label:");
readOver((size_t)rem, "+++Label Skip:");
}
// read type 4 record (that must follow type 3!)
// record type
int recordTypeCode = readInt32("+++Record Type Code (Should be 4):");
if (recordTypeCode != 4) {
exitAndCloseFile("Error reading Variable Index record: bad record type [%s]. Expecting Record Type 4.", intToStr32(recordTypeCode));
}
// number of variables to add to?
int numVars = readInt32("+++Number of Variables:");
// variableRecord indexes
int j;
for (j = 0; j < numVars; j++) {
readInt32("+++Var Index:");
}
}
/**
* Convert data to long format csv's
* @return void
*/
void dataToCsvLong(){
//initialise counters for the loops
int fileNumber = 1;
int caseid = 1;
int rowCount = 1;
//cluster for compression reads
uint8_t cluster[8] = {0,0,0,0,0,0,0,0};
int clusterIndex = 8;
//how many rows overall?
int totalRows = numberOfVariables * numberOfCases;
//progress tracking
int progressDivision;
int progressCount = 0;
if(totalRows < lineLimit){
progressDivision = totalRows / 20;
} else {
progressDivision = lineLimit / 20;
}
//work out files and file pointers
int filesAmount;
if(totalRows > lineLimit){
filesAmount = (totalRows / lineLimit) + 1;
} else {
filesAmount = 1;
}
FILE * csvs[filesAmount];
//first filename
char filename[100] = "";
strcat(filename, csv);
strcat(filename, intToStr32(fileNumber));
strcat(filename, ".csv");
//first file
csvs[0] = fopen(filename, "w");
//can we open and edit?
if (csvs[0] == NULL) {
exitAndCloseFile("Unable to open file (permission denied, try sudo): %s", filename);
}
if(!silent){
printOut("Building Long CSV:", "", "cyan");
printOut("\t%s", filename, "cyan");
}
double numData;
int i;
for(i = 1; i <= numberOfCases; i++){
//loop through vars, skipping head of list
struct Variable * current = variablesList;
current = current->next;
int variableId = 1;
int j;
for(j = 0; j < numberOfVariables; j++){
if(current->type != 0){
int charactersToRead = (current->writeFormatCode >> 8) & 0xFF; //byte 2
double blocksToRead = floorf( (((float)charactersToRead - 1) / 8) + 1 );
if(!silent && debug){
printOut("\tString var ", "", "");
printf("\twriteformatcode: %#X\n", current->writeFormatCode);
printf("\twriteformatcode: %d\n", charactersToRead);
printOut("\twriteformatcode: %s", intToStr32(current->writeFormatCode), "");
printOut("\tcharstoread: %s", intToStr32(charactersToRead), "");
printOut("\tblocksToRead: %s", intToStr64((int64_t)blocksToRead), "");
}
while (blocksToRead > 0) {
if (compressionSwitch > 0) {
if(clusterIndex > 7){
cluster[0] = readIntByteNoOutput();
cluster[1] = readIntByteNoOutput();
cluster[2] = readIntByteNoOutput();
cluster[3] = readIntByteNoOutput();
cluster[4] = readIntByteNoOutput();
cluster[5] = readIntByteNoOutput();
cluster[6] = readIntByteNoOutput();
cluster[7] = readIntByteNoOutput();
clusterIndex = 0;
}
// convert byte to an unsigned byte in an int
int byteValue = (0x000000FF & (int)cluster[clusterIndex]);
clusterIndex++;
switch (byteValue) {
// skip this code
case COMPRESS_SKIP_CODE:
// all blanks
case COMPRESS_ALL_BLANKS:
break;
// end of file, no more data to follow. This should not happen.
case COMPRESS_END_OF_FILE:
exitAndCloseFile("Error reading data: unexpected end of compressed data file (cluster code 252)", "");
break;
// data cannot be compressed, the value follows the cluster
case COMPRESS_NOT_COMPRESSED:
{
// read a maximum of 8 characters but could be less if this is the last block
size_t blockStringLength = (size_t)MIN(8, (float)charactersToRead);
// append to existing value
readOver((size_t)blockStringLength, "");
// if this is the last block, skip the remaining dummy byte(s) (in the block of 8 bytes)
if (charactersToRead < 8) {
readOver((size_t)(8 - charactersToRead), "");
}
// update the characters counter
charactersToRead -= (int)blockStringLength;
}
break;
// system missing value
case COMPRESS_MISSING_VALUE:
exitAndCloseFile("Error reading data: unexpected SYSMISS for string variable", "");
break;
// 1-251 value is code minus the compression BIAS (normally always equal to 100)
default:
exitAndCloseFile("Error reading data: unexpected compression code for string variable", "");
break;
}
//UNCOMPRESSED DATA
} else {
// read a maximum of 8 characters but could be less if this is the last block
size_t blockStringLength = (size_t)MIN(8, (float)charactersToRead);
// append to existing value
readOver(blockStringLength, "");
// update counter
charactersToRead -= (int)blockStringLength;
}
blocksToRead--;
}
current = current->next;
continue;
}
bool insertNull = false;
numData = 0;
if(compressionSwitch > 0){
if(clusterIndex > 7){
cluster[0] = readIntByteNoOutput();
cluster[1] = readIntByteNoOutput();
cluster[2] = readIntByteNoOutput();
cluster[3] = readIntByteNoOutput();
cluster[4] = readIntByteNoOutput();
cluster[5] = readIntByteNoOutput();
cluster[6] = readIntByteNoOutput();
cluster[7] = readIntByteNoOutput();
clusterIndex = 0;
}
// convert byte to an unsigned byte in an int
int byteValue = (0x000000FF & (int)cluster[clusterIndex]);
clusterIndex++;
switch (byteValue) {
// skip this code
case COMPRESS_SKIP_CODE:
break;
// end of file, no more data to follow. This should not happen.
case COMPRESS_END_OF_FILE:
exitAndCloseFile("Error reading data: unexpected end of compressed data file (cluster code 252)", "");
break;
// data cannot be compressed, the value follows the cluster
case COMPRESS_NOT_COMPRESSED:
numData = readDoubleNoOuput();
break;
// all blanks
case COMPRESS_ALL_BLANKS:
numData = 0;
break;
// system missing value
case COMPRESS_MISSING_VALUE:
//used to be 'NULL' but LOAD DATA INFILE requires \N instead, otherwise a '0' get's inserted instead
insertNull = true;
break;
// 1-251 value is code minus the compression BIAS (normally always equal to 100)
default:
numData = byteValue - compressionBias;
break;
}
} else {
numData = readDoubleNoOuput();
}
//write to file
if(includeRowIndex){
fprintf(csvs[fileNumber-1],"%d,",rowCount);
}
if(insertNull){
fprintf(csvs[fileNumber-1],"%d,%d,\\N\n", caseid, variableId);
} else if (dubIsInt(numData)) {
fprintf(csvs[fileNumber-1],"%d,%d,%d\n", caseid, variableId, (int)numData);
} else {
fprintf(csvs[fileNumber-1],"%d,%d,%f\n", caseid, variableId, numData);
}
//switch to new file
if(rowCount % lineLimit == 0){
//close current file
fclose(csvs[fileNumber-1]);
//finish progressCount output
if(progressCount < 20 && !silent){
int x;
for(x = 0; x <= (20 - progressCount); x++){
printf("#");
}
fflush(stdout);
}
progressCount = 0;
//make and open new file
fileNumber++;
char filenameHere[100] = "";
strcat(filenameHere, csv);
strcat(filenameHere, intToStr32(fileNumber));
strcat(filenameHere, ".csv");
csvs[fileNumber-1] = fopen(filenameHere,"w");
if (csvs[fileNumber-1] == NULL) {
exitAndCloseFile("Unable to open file (permission denied, try sudo): %s", filenameHere);
}
if(!silent){
printOut("\nBuilding Long CSV:", "", "cyan");
printOut("\t%s", filenameHere, "cyan");
}
} else if (rowCount % progressDivision == 0){
if(!silent){
printf("#");
fflush(stdout);
}
progressCount++;
}
current = current->next;
variableId++;
rowCount++;
}
caseid++;
}
//finish progressCount output
if(progressCount < 20 && !silent){
int x;
for(x = 0; x <= (20 - progressCount); x++){
printf("#");
}
fflush(stdout);
}
if(!silent){
printOut("\nWrote %s rows.", intToStr32(totalRows), "green");
printOut("Wrote %s files.", intToStr32(filesAmount), "green");
}
//close current file
fclose(csvs[fileNumber-1]);
}
/**
* Convert data to flat format csv's
* @return void
*/
void dataToCsvFlat(){
int fileNumber = 1;
//cluster for compression reads
uint8_t cluster[8] = {0,0,0,0,0,0,0,0};
int clusterIndex = 8;
//progress tracking
int progressDivision;
int progressCount = 0;
if(numberOfCases < lineLimit){
progressDivision = numberOfCases / 20;
} else {
progressDivision = lineLimit / 20;
}
//work out files and file pointers
int filesAmount;
if(numberOfCases > lineLimit){
filesAmount = (numberOfCases / lineLimit) + 1;
} else {
filesAmount = 1;
}
FILE * csvs[filesAmount];
//first filename
char filename[100] = "";
strcat(filename, csv);
strcat(filename, intToStr32(fileNumber));
strcat(filename, ".csv");
//first file
csvs[0] = fopen(filename, "w");
//try to open
if (csvs[0] == NULL) {
exitAndCloseFile("Unable to open file (permission denied, try sudo): %s", filename);
}
if(!silent){
printOut("Building Flat CSV:", "", "cyan");
printOut("\t%s", filename, "cyan");
}
double numData;