From 8665f996e59f4dd70167fdebc8574edeb5fcf03a Mon Sep 17 00:00:00 2001 From: Reina Harrington-Affine Date: Fri, 19 Sep 2025 00:00:30 +0000 Subject: [PATCH] At long, panful last, a functional version of exercise 1-17. The code has contracted considerably since the intiially architected version, with replicate() and its functionality having been eliminated as unnecessary, saving numerous cycles and ~64kB of memory, advance() has been eliminated due to creating needless complexity, hindering readability, and decreasing efficiency, and recall has been eliminated because it depended on replicate, and was also unnecessary as enumerate() now contains three lines of code that completely eliminate the need for replicate() or enumerate(). --- KNR C/ex1-17/ex1-17-2-modular.c | 49 ++++++++++++++------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/KNR C/ex1-17/ex1-17-2-modular.c b/KNR C/ex1-17/ex1-17-2-modular.c index 74436e8..b1a43e9 100644 --- a/KNR C/ex1-17/ex1-17-2-modular.c +++ b/KNR C/ex1-17/ex1-17-2-modular.c @@ -4,12 +4,15 @@ #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 +void enumerate(char data[]); // Complete, pending testing. + +int main(){ + store(buffer); + enumerate(buffer); + return 0; +} //Status: Complete & Tested void store(char to[]){ @@ -20,21 +23,11 @@ void store(char to[]){ } } -//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[]){ +void enumerate(char data[]){ int dataCrsr = 0; // Stores currently accessed position in data[] - int wscursor = 0; //Stores the cursor position in workingStorage + 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. @@ -45,24 +38,26 @@ void enumerate(char data[], char output[]){ 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. + // Increment wscursor and add a null ek yte 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. + // 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. + // 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. + // Begin case: non-newline character. }else if(current != '\n'){ - //Increment line length, write to workingStorage at wscursor, and - //increment wscursor. + // Increment line length, write to workingStorage at wscursor, and + // increment wscursor. ++lineLen; workingStorage[wscursor] = current; ++wscursor; @@ -72,5 +67,3 @@ void enumerate(char data[], char output[]){ current = data[dataCrsr]; } } - -