/* ECP: FILEname=fig8_29.c */ /* 1*/ /* If Word Is FLUSH, Then Output The Current Line. */ /* 2*/ /* Otherwise, Add Word To End Of Line; if It Doesn't Fit */ /* 3*/ /* Then Output The Current Line ( Padded ) */ /* 4*/ void /* 5*/ Justify( const char Word[ ] ) /* 6*/ { /* 7*/ static char CurrentLine[ LineLen + 1 ] = ""; /* 8*/ static int CharsHeld = 0; /* 9*/ static int Indent = IndentLen; /* Current Amount To Indent */ /*10*/ static int LineLimit = LineLen - IndentLen; /*11*/ int WordLen = strlen( Word ); /*12*/ if( strcmp( Word, FLUSH ) == 0 ) /*13*/ { /*14*/ /* Print The Pending Line. */ /*15*/ PrintSpaces( Indent ); /*16*/ puts( CurrentLine ); /* No Padding */ /*17*/ Indent = IndentLen; /* For Next Line */ /*18*/ } /*19*/ else /*20*/ if( CharsHeld + WordLen < LineLimit ) /*21*/ { /*22*/ /* Word Fits On The Line */ /*23*/ if( CharsHeld ) /* Toss in Blank if Not First Word */ /*24*/ { /*25*/ strcat( CurrentLine, " " ); /*26*/ CharsHeld++; /*27*/ } /*28*/ strcat( CurrentLine, Word ); /*29*/ CharsHeld += WordLen; /*30*/ return; /*31*/ } /*32*/ else /*33*/ { /* Print The Pending Line With Padding*/ /*34*/ PrintSpaces( Indent ); /*35*/ PutOut( CurrentLine, LineLimit ); /*36*/ Indent = 0; /* Next Line Is Not Indented */ /*37*/ } /*38*/ /* Start Up The Next Line */ /*39*/ strcpy( CurrentLine, Word ); /*40*/ CharsHeld = WordLen; /*41*/ LineLimit = LineLen - Indent; /*42*/ }