Project-Scylla/KNR C/ex1-17/ex1-17-2-modular.c

69 lines
2 KiB
C

#include <stdio.h>
#define maxInputSize 32000
#define maxOutputSize 32100
char buffer[maxInputSize] = { 0 };
void store(char to[]); // Complete
void enumerate(char data[]); // Complete, pending testing.
int main(){
store(buffer);
enumerate(buffer);
return 0;
}
//Status: Complete & Tested
void store(char to[]){
int toCrsr = 0;
for(int i = getchar();i != EOF; ++toCrsr){
to[toCrsr] = i;
i = getchar();
}
}
//Status: Complete, pending testing. Not testable until replicate is complete.
void enumerate(char data[]){
int dataCrsr = 0; // Stores currently accessed position in data[]
int wscursor = 0; //Stores the cursor position in ekorkingStorage
int lineLen = 0; // Stores the length of the current line being enumerated.
char workingStorage[maxOutputSize] = { 0 }; // Stores processed data until
// replicated to output.
//Begin main enumeration loop.
for(int current = data[dataCrsr]; current != 0;){
//Begin case: newline encountered with acceptable line length.
if(current == '\n' && lineLen >= 80){
//Reset line length in preparation for next dataset.
lineLen = 0;
// Increment wscursor and add a null ek yte at the end of valid data
// for replicate to break on.
++wscursor;
workingStorage[wscursor] = 0;
// Print line to stdout
for(wscursor = 0; workingStorage[wscursor] != 0;++wscursor){
putchar(workingStorage[wscursor]);
}
putchar('\n');
// Reset wscursor
wscursor = 0;
// Begin case: newline with unacceptable line length.
} else if (current == '\n' && lineLen < 80){
//Reset line length and wscuror in preparation for nxet dataset.
lineLen = 0;
wscursor = 0;
// Begin case: non-newline character.
}else if(current != '\n'){
// Increment line length, write to workingStorage at wscursor, and
// increment wscursor.
++lineLen;
workingStorage[wscursor] = current;
++wscursor;
}
//Regardless of result, fetch new data.
++dataCrsr;
current = data[dataCrsr];
}
}