1212#define PRINTDBG (fmt , ...)
1313#endif
1414
15- unsigned int NextFile (FILE * * currentFile , char * outputFileNames [], unsigned int numOutputFiles ) {
16- static int current = -1 ;
17-
18- if (!currentFile ) {
19- PRINTDBG ("%s: Invalid file pointer, aborting\n" , __func__ );
20- exit (1 );
21- }
22-
23- if (++ current >= numOutputFiles ) {
24- return numOutputFiles ;
25- }
26-
27- // the order of the ifs here means that NextFile will close all files but the last one,
28- // which the caller will be responsible for.
29- if (* currentFile ) {
30- if (current >= 0 ) {
31- PRINTDBG ("%s: Will close %s output file\n" , __func__ , outputFileNames [current - 1 ]);
32- }
33- fclose (* currentFile );
34- * currentFile = NULL ;
35- }
36-
37- PRINTDBG ("%s: Will open %s output file\n" , __func__ , outputFileNames [current ]);
38- FILE * nextFile = fopen (outputFileNames [current ], "w" );
39- if (!nextFile ) {
40- perror (LOC );
41- exit (1 );
42- }
43-
44- PRINTDBG ("%s: Successfully opened %s output file\n" , __func__ , outputFileNames [current ]);
45- * currentFile = nextFile ;
46-
47- return current ;
48- }
49-
5015void SplitInput (char * input , int batchSize , char * outputFileNames [], unsigned int numOutputFiles ) {
5116 PRINTDBG ("%s: will split input\n" , __func__ );
52- FILE * outputFile = NULL ;
53- NextFile (& outputFile , outputFileNames , numOutputFiles );
54- if (!outputFile ) {
55- PRINTDBG ("%s: No output file in list, quitting\n" , __func__ );
56- return ;
57- }
5817
59- FILE * inputFile = fopen (input , "r" );
60- if (!inputFile ) {
18+ PRINTDBG ("%s: Openning output files\n" , __func__ );
19+ int current = 0 ;
20+ FILE * * outputFiles = malloc (sizeof (FILE * ) * numOutputFiles );
21+ for (int i = 0 ; i < numOutputFiles ; i ++ )
22+ {
23+ outputFiles [i ] = fopen (outputFileNames [i ], "w" );
24+ if (!outputFiles [i ])
25+ {
26+ perror (LOC );
27+ exit (1 );
28+ }
29+ }
30+ FILE * outputFile = outputFiles [current ];
31+
32+ FILE * inputFile = fopen (input , "r" );
33+ if (!inputFile )
34+ {
6135 perror (LOC );
6236 exit (1 );
6337 }
@@ -67,18 +41,16 @@ void SplitInput(char* input, int batchSize, char* outputFileNames[], unsigned in
6741 unsigned int readLines = 0 ;
6842
6943 size_t len = 0 ;
70- while (getline (& inputBuffer , & len , inputFile ) > 0 ) {
71- if (++ readLines == batchSize ) {
44+ while (getline (& inputBuffer , & len , inputFile ) > 0 && ! ferror ( outputFile ) ) {
45+ if (++ readLines == batchSize && current < numOutputFiles - 1 ) {
7246 readLines = 0 ;
73- NextFile (& outputFile , outputFileNames , numOutputFiles );
47+ fclose (outputFile );
48+ current += 1 ;
49+ outputFile = outputFiles [current ];
7450 }
7551 fputs (inputBuffer , outputFile );
7652 }
7753
78- // need to exhaust our output files list
79- while (NextFile (& outputFile , outputFileNames , numOutputFiles ) < numOutputFiles ) {
80- }
81-
8254 PRINTDBG ("%s: Done splitting input %s, will clean up\n" , __func__ , input );
8355 fclose (inputFile );
8456 // need to close the last output file
0 commit comments