-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNIRecodeMTFiles.c
More file actions
783 lines (728 loc) · 23.4 KB
/
Copy pathKNIRecodeMTFiles.c
File metadata and controls
783 lines (728 loc) · 23.4 KB
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
// Copyright (c) 2023-2026 Orange. All rights reserved.
// This software is distributed under the BSD 3-Clause-clear License, the text of which is available
// at https://spdx.org/licenses/BSD-3-Clause-Clear.html or see the "LICENSE" file for more details.
#ifdef _MSC_VER
// To disable fopen warnings (Visual C++ deprecated method)
#define _CRT_SECURE_NO_WARNINGS
#endif // _MSC_VER
#include "KNIRecodeMTFiles.h"
#include "KhiopsNativeInterface.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
/*
* Initialize recoding operands
*/
static void KNIInitializeRecodingOperands(KNIMTRecodingOperands* recodingOperands)
{
strcpy(recodingOperands->DictionaryFileName, "");
strcpy(recodingOperands->DictionaryName, "");
recodingOperands->FieldSeparator = '\t';
strcpy(recodingOperands->InputFile.DataPath, "");
strcpy(recodingOperands->InputFile.FileName, "");
recodingOperands->InputFile.KeyFieldNumber = 0;
recodingOperands->SecondaryFileNumber = 0;
recodingOperands->ExternalFileNumber = 0;
strcpy(recodingOperands->OutputFileName, "");
strcpy(recodingOperands->LogFileName, "");
recodingOperands->MaxMemory = 0;
}
/*
* Set recoding operands from command line parameters
* Return 1 if OK, 0 otherwise
*/
static int KNIInitializeRecodingOperandsFromCommandLine(KNIMTRecodingOperands* recodingOperands, int argc, char** argv)
{
int i;
char* option;
int nFileIndex;
int nKeyIndex;
int nIntegerValue;
// Initialize recoding operands
KNIInitializeRecodingOperands(recodingOperands);
// Analyse command line parameters
i = 1;
while (i < argc)
{
option = argv[i];
// Analyze dictionary operand
if (strcmp(option, "-d") == 0)
{
if (argc - i <= 2)
{
printf("Error : Two operands are required for -d option\n");
return EXIT_FAILURE;
}
else
{
strcpy(recodingOperands->DictionaryFileName, argv[i + 1]);
strcpy(recodingOperands->DictionaryName, argv[i + 2]);
i += 3;
}
}
// Analyze field separator operand
else if (strcmp(option, "-f") == 0)
{
if (argc - i <= 1)
{
printf("Error : One operand is required for -f option\n");
return EXIT_FAILURE;
}
else
{
recodingOperands->FieldSeparator = argv[i + 1][0];
i += 2;
}
}
// Analyze input file operand
else if (strcmp(option, "-i") == 0)
{
if (argc - i <= 2)
{
printf("Error : At least one operands are required for -i option\n");
return EXIT_FAILURE;
}
else
{
strcpy(recodingOperands->InputFile.DataPath, "");
strcpy(recodingOperands->InputFile.FileName, argv[i + 1]);
i += 2;
// Analyse key field indexes
nKeyIndex = 0;
while (i < argc && argv[i][0] != '-')
{
if (nKeyIndex == MAXKEYFIELDNUMBER - 1)
{
printf("Error : Too many key fields (max %d) for input file %s\n",
MAXKEYFIELDNUMBER, recodingOperands->InputFile.FileName);
return EXIT_FAILURE;
}
nIntegerValue = atoi(argv[i]);
if (nIntegerValue <= 0)
{
printf("Error : Key field index (%s) invalid for input file %s\n",
argv[i], recodingOperands->InputFile.FileName);
return EXIT_FAILURE;
}
recodingOperands->InputFile.KeyFieldIndexes[nKeyIndex] = nIntegerValue;
nKeyIndex++;
i++;
}
recodingOperands->InputFile.KeyFieldNumber = nKeyIndex;
// Check that key fields are specified
if (recodingOperands->InputFile.KeyFieldNumber == 0)
{
printf("Error : key fields indexes are not specified for input file %s\n",
recodingOperands->InputFile.FileName);
return EXIT_FAILURE;
}
}
}
// Analyze secondary file operand
else if (strcmp(option, "-s") == 0)
{
if (argc - i <= 2)
{
printf("Error : At least two operands are required for -s option\n");
return EXIT_FAILURE;
}
else
{
nFileIndex = recodingOperands->SecondaryFileNumber;
if (nFileIndex == MAXFILENUMBER - 1)
{
printf("Error : Too many secondary files (max %d) with -s option\n",
MAXFILENUMBER);
return EXIT_FAILURE;
}
strcpy(recodingOperands->SecondaryFiles[nFileIndex].DataPath, argv[i + 1]);
strcpy(recodingOperands->SecondaryFiles[nFileIndex].FileName, argv[i + 2]);
recodingOperands->SecondaryFileNumber = nFileIndex + 1;
i += 3;
// Analyse key field indexes
nKeyIndex = 0;
while (i < argc && argv[i][0] != '-')
{
if (nKeyIndex == MAXKEYFIELDNUMBER - 1)
{
printf("Error : Too many key fields (max %d) for secondary file %s\n",
MAXKEYFIELDNUMBER,
recodingOperands->SecondaryFiles[nFileIndex].FileName);
return EXIT_FAILURE;
}
nIntegerValue = atoi(argv[i]);
if (nIntegerValue <= 0)
{
printf("Error : Key field index (%s) invalid for secondary file %s\n",
argv[i], recodingOperands->SecondaryFiles[nFileIndex].FileName);
return EXIT_FAILURE;
}
recodingOperands->SecondaryFiles[nFileIndex].KeyFieldIndexes[nKeyIndex] =
nIntegerValue;
nKeyIndex++;
i++;
}
recodingOperands->SecondaryFiles[nFileIndex].KeyFieldNumber = nKeyIndex;
// Check that key fields are specified
if (recodingOperands->SecondaryFiles[nFileIndex].KeyFieldNumber == 0)
{
printf("Error : key fields indexes are not specified for secondary file %s\n",
recodingOperands->SecondaryFiles[nFileIndex].FileName);
return EXIT_FAILURE;
}
}
}
// Analyze external file operand
else if (strcmp(option, "-x") == 0)
{
if (argc - i <= 3)
{
printf("Error : Three operands are required for -x option\n");
return EXIT_FAILURE;
}
else
{
nFileIndex = recodingOperands->ExternalFileNumber;
if (nFileIndex == MAXFILENUMBER - 1)
{
printf("Error : Too many input files (max %d) with -x option\n", MAXFILENUMBER);
return EXIT_FAILURE;
}
strcpy(recodingOperands->ExternalFiles[nFileIndex].DataRoot, argv[i + 1]);
strcpy(recodingOperands->ExternalFiles[nFileIndex].DataPath, argv[i + 2]);
strcpy(recodingOperands->ExternalFiles[nFileIndex].FileName, argv[i + 3]);
recodingOperands->ExternalFileNumber = nFileIndex + 1;
i += 4;
}
}
// Analyze output file operand
else if (strcmp(option, "-o") == 0)
{
if (argc - i <= 1)
{
printf("Error : One operand is required for -o option\n");
return EXIT_FAILURE;
}
else
{
strcpy(recodingOperands->OutputFileName, argv[i + 1]);
i += 2;
}
}
// Analyze log file operand
else if (strcmp(option, "-e") == 0)
{
if (argc - i <= 1)
{
printf("Error : One operand is required for -e option\n");
return EXIT_FAILURE;
}
else
{
strcpy(recodingOperands->LogFileName, argv[i + 1]);
i += 2;
}
}
// Analyze memory operand
else if (strcmp(option, "-m") == 0)
{
if (argc - i <= 1)
{
printf("Error : One operand is required for -e option\n");
return EXIT_FAILURE;
}
else
{
nIntegerValue = atoi(argv[i + 1]);
if (nIntegerValue <= 0)
{
printf("Error : Max memory (%s) invalid\n", argv[i + 1]);
return EXIT_FAILURE;
}
recodingOperands->MaxMemory = nIntegerValue;
i += 2;
}
}
// Unknown operand
else
{
printf("Error : Wrong operand %d (%s)\n", i, argv[i]);
return EXIT_FAILURE;
}
}
// Check that at least the input file is initialized
if (strcmp(recodingOperands->InputFile.FileName, "") == 0)
{
printf("Error : input file is not specified\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* Compare two records of input files according to their key fields
* (up to the number of fields in the main input file)
*/
static int KNICompareInputRecords(char cFieldSeparator, KNIInputFile* mainFile, char* sMainRecord,
KNIInputFile* secondaryFile, char* sSecondaryRecord)
{
int nCompare;
int i;
int nKeyIndex1;
int nKeyIndex2;
char* sStartKey1;
char* sStartKey2;
char* sKey;
int nKey1Length;
int nKey2Length;
int nMinKeyLength;
int nIndex;
// Loop on key fields
nCompare = 0;
for (i = 0; i < mainFile->KeyFieldNumber; i++)
{
nKeyIndex1 = mainFile->KeyFieldIndexes[i];
nKeyIndex2 = secondaryFile->KeyFieldIndexes[i];
// Search start of key field in main record
nIndex = 1;
sStartKey1 = sMainRecord;
while (*sStartKey1 != '\0')
{
if (nIndex == nKeyIndex1)
break;
if (*sStartKey1 == cFieldSeparator)
nIndex++;
sStartKey1++;
}
// Search start of key field in secondary record
nIndex = 1;
sStartKey2 = sSecondaryRecord;
while (*sStartKey2 != '\0')
{
if (nIndex == nKeyIndex2)
break;
if (*sStartKey2 == cFieldSeparator)
nIndex++;
sStartKey2++;
}
// Calculate length of key field i, main record
sKey = sStartKey1;
nKey1Length = 0;
while (*sKey != '\0' && *sKey != cFieldSeparator && *sKey != '\r' && *sKey != '\n')
{
nKey1Length++;
sKey++;
}
// Calculate length of key field in secondary record
sKey = sStartKey2;
nKey2Length = 0;
while (*sKey != '\0' && *sKey != cFieldSeparator && *sKey != '\r' && *sKey != '\n')
{
nKey2Length++;
sKey++;
}
// Compare keys
nMinKeyLength = (nKey1Length < nKey2Length) ? nKey1Length : nKey2Length;
nCompare = strncmp(sStartKey1, sStartKey2, nMinKeyLength);
if (nCompare == 0)
nCompare = nKey1Length - nKey2Length;
// If keys differ, no need to compare further
if (nCompare != 0)
break;
}
return nCompare;
}
/*
* Clean a record by supressing the potential last end of line char.
* This is useful for display purpose, and has no impact on recoding records
* (input records may be indifferently terminated by '\0' or '\n' '\r\n' then '\0').
*/
static char* CleanRecord(char* sRecord)
{
int nLength;
nLength = (int)strlen(sRecord);
while (nLength > 0)
{
nLength--;
if (sRecord[nLength] == '\n' || sRecord[nLength] == '\r')
sRecord[nLength] = '\0';
}
return sRecord;
}
/*
* Read a record from a file.
* Return NULL if error or line too long.
*/
static char* ReadRecord(char* sRecord, int nMaxLineLength, FILE* file)
{
if (!fgets(sRecord, nMaxLineLength, file))
return NULL;
// Discard rest of line if line too long
if (strchr(sRecord, '\n') == NULL && !feof(file))
{
int ch;
while ((ch = fgetc(file)) != '\n' && ch != EOF)
;
return NULL;
}
return sRecord;
}
/*
* Recode a set of input files and external files to an output file, using a Khiops dictionary from a dictionary file
* The recoding parameters stores all required parameters and must be correctly initialized
* Returns recoded record number
*/
int KNIRecodeMTFiles(KNIMTRecodingOperands* recodingOperands)
{
int nVerbose = 0; // 0: minimal message; 1: display input parameters; 2: display all file input and output lines
int nFileIndex;
int nKeyIndex;
int nRetCode;
int hStream;
char* sInputRecord;
char* sSecondaryRecords[MAXFILENUMBER];
char* sOutputRecord;
FILE* fInputFile;
FILE* fSecondaryFiles[MAXFILENUMBER];
FILE* fSecondaryFile;
FILE* fOutputFile;
char* sRetCode;
int nErrorNumber;
int nRecordNumber;
int nCompare;
assert(recodingOperands != NULL);
assert(recodingOperands->SecondaryFileNumber <= MAXFILENUMBER);
// Allocate buffers
sInputRecord = (char*)malloc(MAXRECORDLENGTH * sizeof(char));
sOutputRecord = (char*)malloc(MAXRECORDLENGTH * sizeof(char));
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
sSecondaryRecords[nFileIndex] = (char*)malloc(MAXRECORDLENGTH * sizeof(char));
// Show recoding parameters
if (nVerbose >= 1)
{
printf("Multi-table recoding\n");
printf(" Dictionary file name: %s\n", recodingOperands->DictionaryFileName);
printf(" Dictionary name: %s\n", recodingOperands->DictionaryName);
printf(" Field separator: %c\n", recodingOperands->FieldSeparator);
printf(" Input file name: %s\n", recodingOperands->InputFile.FileName);
if (recodingOperands->InputFile.KeyFieldNumber)
{
printf(" Key field indexes: ");
for (nKeyIndex = 0; nKeyIndex < recodingOperands->InputFile.KeyFieldNumber; nKeyIndex++)
{
if (nKeyIndex > 0)
printf(", ");
printf("%d", recodingOperands->InputFile.KeyFieldIndexes[nKeyIndex]);
}
printf("\n");
}
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
{
printf(" Secondary file %d\n", nFileIndex + 1);
printf(" Data path: %s\n", recodingOperands->SecondaryFiles[nFileIndex].DataPath);
printf(" File name: %s\n", recodingOperands->SecondaryFiles[nFileIndex].FileName);
if (recodingOperands->SecondaryFiles[nFileIndex].KeyFieldNumber)
{
printf(" Key field indexes: ");
for (nKeyIndex = 0;
nKeyIndex < recodingOperands->SecondaryFiles[nFileIndex].KeyFieldNumber;
nKeyIndex++)
{
if (nKeyIndex > 0)
printf(", ");
printf("%d",
recodingOperands->SecondaryFiles[nFileIndex].KeyFieldIndexes[nKeyIndex]);
}
printf("\n");
}
}
for (nFileIndex = 0; nFileIndex < recodingOperands->ExternalFileNumber; nFileIndex++)
{
printf(" External file %d\n", nFileIndex + 1);
printf(" Data root: %s\n", recodingOperands->ExternalFiles[nFileIndex].DataRoot);
printf(" Data path: %s\n", recodingOperands->ExternalFiles[nFileIndex].DataPath);
printf(" File name: %s\n", recodingOperands->ExternalFiles[nFileIndex].FileName);
}
printf(" Output file name: %s\n", recodingOperands->OutputFileName);
printf(" Log file name: %s\n", recodingOperands->LogFileName);
printf(" Max memory (MB): %d\n", recodingOperands->MaxMemory);
}
// Set log file
if (strcmp(recodingOperands->LogFileName, "") != 0)
KNISetLogFileName(recodingOperands->LogFileName);
// Set max memory
if (recodingOperands->MaxMemory >= 0)
{
KNISetStreamMaxMemory(recodingOperands->MaxMemory);
if (nVerbose >= 1)
printf("Actual max memory (MB): %d\n", KNIGetStreamMaxMemory());
}
// Open input and output files
nErrorNumber = 0;
fInputFile = fopen(recodingOperands->InputFile.FileName, "r");
if (fInputFile == NULL)
{
printf("Error : Unable to open input file %s\n", recodingOperands->InputFile.FileName);
nErrorNumber++;
}
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
{
fSecondaryFile = fopen(recodingOperands->SecondaryFiles[nFileIndex].FileName, "r");
fSecondaryFiles[nFileIndex] = fSecondaryFile;
if (fSecondaryFile == NULL)
{
printf("Error : Unable to open secondary file %s\n",
recodingOperands->SecondaryFiles[nFileIndex].FileName);
nErrorNumber++;
}
// Check that number of key fields is greater than 0 in case of multi-tables schema
if (recodingOperands->SecondaryFileNumber >= 2 &&
recodingOperands->SecondaryFiles[nFileIndex].KeyFieldNumber == 0)
{
printf("Error : Secondary file %s should be specified with key field indexes (multi-tables "
"schema)\n",
recodingOperands->SecondaryFiles[nFileIndex].FileName);
nErrorNumber++;
}
// Check that number of key fields in secondary input file is greater or equal than that of input file
if (recodingOperands->SecondaryFiles[nFileIndex].KeyFieldNumber <
recodingOperands->InputFile.KeyFieldNumber)
{
printf("Error : Number of key fields (%d) in secondary file %s should greater or equal than "
"that of input file (%d)\n",
recodingOperands->SecondaryFiles[nFileIndex].KeyFieldNumber,
recodingOperands->SecondaryFiles[nFileIndex].FileName,
recodingOperands->InputFile.KeyFieldNumber);
nErrorNumber++;
}
}
fOutputFile = fopen(recodingOperands->OutputFileName, "w");
if (fOutputFile == NULL)
{
printf("Error : Unable to open output file %s\n", recodingOperands->OutputFileName);
nErrorNumber++;
}
// Open KNI stream
hStream = -1;
nRecordNumber = 0;
if (nErrorNumber == 0)
{
// Read header line of input file
sRetCode = ReadRecord(sInputRecord, MAXRECORDLENGTH, fInputFile);
// Open stream
if (sRetCode != NULL)
{
hStream = KNIOpenStream(recodingOperands->DictionaryFileName, recodingOperands->DictionaryName,
sInputRecord, recodingOperands->FieldSeparator);
if (hStream < 0)
{
printf("Error : Open stream error: %d\n", hStream);
nErrorNumber++;
}
}
}
// Set specific multi-table parameters
if (hStream > 0 && nErrorNumber == 0 &&
(recodingOperands->SecondaryFileNumber > 0 || recodingOperands->ExternalFileNumber > 0))
{
// Read header line of secondary files
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
{
fSecondaryFile = fSecondaryFiles[nFileIndex];
sRetCode = ReadRecord(sInputRecord, MAXRECORDLENGTH, fSecondaryFile);
if (sRetCode != NULL)
{
nRetCode = KNISetSecondaryHeaderLine(
hStream, recodingOperands->SecondaryFiles[nFileIndex].DataPath, sInputRecord);
if (nRetCode != KNI_OK)
nErrorNumber++;
}
}
// Set external tables
for (nFileIndex = 0; nFileIndex < recodingOperands->ExternalFileNumber; nFileIndex++)
{
nRetCode = KNISetExternalTable(hStream, recodingOperands->ExternalFiles[nFileIndex].DataRoot,
recodingOperands->ExternalFiles[nFileIndex].DataPath,
recodingOperands->ExternalFiles[nFileIndex].FileName);
if (nRetCode != KNI_OK)
nErrorNumber++;
}
// Finish opening the stream
nRetCode = KNIFinishOpeningStream(hStream);
if (nRetCode < 0)
{
printf("Error : Finish opening stream error: %d\n", nRetCode);
nErrorNumber++;
hStream = -1;
}
}
// Recode all records of the input file
if (hStream > 0 && nErrorNumber == 0)
{
// Initialize empty records for all secondary files
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
sSecondaryRecords[nFileIndex][0] = '\0';
// Loop on input records
sInputRecord[0] = '\0';
while (!feof(fInputFile))
{
// Read input record
sRetCode = ReadRecord(sInputRecord, MAXRECORDLENGTH, fInputFile);
// Recode record
if (sRetCode != NULL)
{
if (nVerbose >= 2)
printf("Main: %.20s\n", CleanRecord(sInputRecord));
// Read lines of secondary input files with same key fields
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
{
fSecondaryFile = fSecondaryFiles[nFileIndex];
// Exploit last stored record (empty record the first time)
nCompare = KNICompareInputRecords(recodingOperands->FieldSeparator,
&recodingOperands->InputFile, sInputRecord,
&recodingOperands->SecondaryFiles[nFileIndex],
sSecondaryRecords[nFileIndex]);
if (nCompare == 0)
KNISetSecondaryInputRecord(
hStream, recodingOperands->SecondaryFiles[nFileIndex].DataPath,
sSecondaryRecords[nFileIndex]);
// Read new records if main key greater than secondary key
if (nCompare >= 0)
{
while (!feof(fSecondaryFile))
{
sRetCode = ReadRecord(sSecondaryRecords[nFileIndex],
MAXRECORDLENGTH, fSecondaryFile);
if (sRetCode == NULL)
break;
// Compare key with main record
nCompare = KNICompareInputRecords(
recodingOperands->FieldSeparator,
&recodingOperands->InputFile, sInputRecord,
&recodingOperands->SecondaryFiles[nFileIndex],
sSecondaryRecords[nFileIndex]);
if (nVerbose >= 2)
{
const char* sSigns = "<=>";
printf(" %c %.20s: %.20s\n", sSigns[nCompare + 1],
recodingOperands->SecondaryFiles[nFileIndex]
.DataPath,
CleanRecord(sSecondaryRecords[nFileIndex]));
}
// Secondary record is related to the main record (same key)
if (nCompare == 0)
{
KNISetSecondaryInputRecord(
hStream,
recodingOperands->SecondaryFiles[nFileIndex]
.DataPath,
sSecondaryRecords[nFileIndex]);
}
// Stop reading if secondary record greater than main record
// (record stored for next main record)
else if (nCompare < 0)
break;
// Otherwise, continue reading if secondary record smaller than
// main record (orphan secondary record)
}
}
}
// Recode main record, once all related secondary records have been set
nRetCode = KNIRecodeStreamRecord(hStream, sInputRecord, sOutputRecord, MAXRECORDLENGTH);
if (nRetCode != KNI_OK)
{
printf("Error : Recode failure (%d) in record \"%.20s...\"\n", nRetCode,
CleanRecord(sInputRecord));
nErrorNumber++;
}
// Write output record
if (nRetCode == KNI_OK)
{
fprintf(fOutputFile, "%s\n", sOutputRecord);
nRecordNumber++;
if (nVerbose >= 2)
printf(" -> %.20s\n", sOutputRecord);
}
}
}
}
// Close files
if (fInputFile != NULL)
fclose(fInputFile);
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
{
fSecondaryFile = fSecondaryFiles[nFileIndex];
if (fSecondaryFile != NULL)
{
if (nVerbose >= 2 && !feof(fSecondaryFile))
printf("Error : Remaining records in file %s\n",
recodingOperands->SecondaryFiles[nFileIndex].FileName);
fclose(fSecondaryFile);
}
}
if (fOutputFile != NULL)
fclose(fOutputFile);
// Close stream
if (hStream > 0)
nRetCode = KNICloseStream(hStream);
// Free buffers
free(sInputRecord);
free(sOutputRecord);
for (nFileIndex = 0; nFileIndex < recodingOperands->SecondaryFileNumber; nFileIndex++)
free(sSecondaryRecords[nFileIndex]);
// End message
printf("Recoded record number: %d\n", nRecordNumber);
return nRecordNumber;
}
/*
* Recode a set of multi-tables input files to an output file.
* -d: <input dictionary file> <input dictionary>
* [-f: <field separator>]
* -i: <input file name> [<key index>...]
* -s: <secondary data path> <secondary file name> <key index>...
* -x: <external data root> <external data path> <external file name>
* -o: <output file name>
* [-e: <error file name>]
* [-m: <max memory (MB)>]
* Data files must have a header line.
* The field separator is tabulation by default.
* One file must be specified per input or external file (see Khiops documentation),
* with the first input file corresponding to the input dictionary.
* In case of multi-tables schema, each input file must be specified and the index of
* their key fields (starting at 1) must be specified.
* These key fields index do not belong to the KNI API, but they are necessary in this
* sample code to read all the input files synchronously.
*/
void mainKNIRecodeMTFiles(int argc, char** argv)
{
KNIMTRecodingOperands recodingOperands;
int retCode;
// Analyse command line and stores the parameters into a KNIMTRecodingOperands structure.
retCode = KNIInitializeRecodingOperandsFromCommandLine(&recodingOperands, argc, argv);
// Print help in case of error
if (retCode == 1)
{
printf("\nRecode a set of multi-tables input files to an output file.\n");
printf(" -d: <input dictionary file> <input dictionary>\n");
printf(" [-f: <field separator>]\n");
printf(" -i: <input file name> [<key index>...]\n");
printf(" -s: <secondary data path> <secondary file name> <key index>...\n");
printf(" -x: <external data root> <external data path> <external file name>\n");
printf(" -o: <output file name>\n");
printf(" [-e: <error file name>]\n");
printf(" [-m: <max memory (MB)>]\n");
printf("Data file must have a header line. \n");
printf("The field separator is tabulation by default.\n");
printf("The main input file must be the first one.\n");
printf("For input files in multi-tables schema, the index of the key fields (starting at 1) must be "
"specified.\n");
}
// Recode files
else
KNIRecodeMTFiles(&recodingOperands);
}
int main(int argv, char** argc)
{
mainKNIRecodeMTFiles(argv, argc);
return 0;
}