WIP overbuilt solution to exercise 1-17.
This commit is contained in:
parent
2084b2ae84
commit
848898ff11
2 changed files with 101 additions and 0 deletions
76
KNR C/ex1-17/ex1-17-2-modular.c
Normal file
76
KNR C/ex1-17/ex1-17-2-modular.c
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define maxInputSize 32000
|
||||
#define maxOutputSize 32100
|
||||
|
||||
char buffer[maxInputSize] = { 0 };
|
||||
char output[maxOutputSize] = { 0 };
|
||||
|
||||
void store(char to[]); // Complete
|
||||
void enumerate(char data[], char output[]); // Complete, pending testing. Needs replicate() before testing.
|
||||
void enumerateAdvance(int extDataCrsr, char accessedData, char externalBuffer[]);// Complete, pending testing.
|
||||
void replicate(char to[], char from[]);// Not Started
|
||||
|
||||
//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 to be used unil enumerate() has been
|
||||
//validated, at which point the for loop of enumerate can be modified to use
|
||||
//enumerateAdvance() as its completion action rather than calling an increment
|
||||
//and char fetch at the end of each loop independently. This function exists
|
||||
//purely for readability and style.
|
||||
void enumerateAdvance(int extDataCrsr, char accessedData, char externalBuffer[]){
|
||||
++extDataCrsr;
|
||||
accessedData = externalBuffer[extDataCrsr];
|
||||
}
|
||||
|
||||
//Status: Complete, pending testing. Not testable until replicate is complete.
|
||||
void enumerate(char data[], char output[]){
|
||||
|
||||
int dataCrsr = 0; // Stores currently accessed position in data[]
|
||||
int wscursor = 0; //Stores the cursor position in workingStorage
|
||||
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 byte at the end of valid data
|
||||
//for replicate to break on.
|
||||
++wscursor;
|
||||
workingStorage[wscursor] = 0;
|
||||
//Copy workingStorage into output.
|
||||
replicate(output, workingStorage);
|
||||
//Reset wscuror. Existing data in workingStorage will be ovrwritten
|
||||
//by next dataset.
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue