[alt.sources] xprintf update

drh@duke.cs.duke.edu (D. Richard Hipp) (11/30/90)

brad@hcx1.ssd.csd.harris.com (Brad Appleton) writes (via mail):
>.... I happened to be thinking that
>in addition to stuff like:
>
>        printf( "%10s", "str" )
>and
>        printf( "%-10s", "str" )
>
>to do right & left justification, that it would be *_really_* nice
>to also have something like:
>
>        printf( "%=10s", "str" )
>
>that would print "str" centered within a ten character field.

Good idea!  It turns out that a "=" flag for centering is easy to
implement in the xprintf sources I posted yesterday.  The diffs are
shown below, together with a test program and the test program's output.

The output of "diff xprintf.c newversion.c" is:
  293a294
  >   int flag_center;          /* True if "=" flag is present */
  340a342
  >         case '=':   flag_center = 1;          c = 0;   break;
  343a346
  >     if( flag_center ) flag_leftjustify = 0;
  624a628,632
  >         if( flag_center ){
  >           nspace = nspace/2;
  >           width -= nspace;
  >           flag_leftjustify = 1;
  >         }

A program to test Brad's idea:
  void main(void){
    int i;
    for(i=1; i<27; i++) printf("[%=20.*s]\n",i,"abcdefghijklmnopqrstuvwxyz");
  }

The output of the test program:
[         a          ]
[         ab         ]
[        abc         ]
[        abcd        ]
[       abcde        ]
[       abcdef       ]
[      abcdefg       ]
[      abcdefgh      ]
[     abcdefghi      ]
[     abcdefghij     ]
[    abcdefghijk     ]
[    abcdefghijkl    ]
[   abcdefghijklm    ]
[   abcdefghijklmn   ]
[  abcdefghijklmno   ]
[  abcdefghijklmnop  ]
[ abcdefghijklmnopq  ]
[ abcdefghijklmnopqr ]
[abcdefghijklmnopqrs ]
[abcdefghijklmnopqrst]
[abcdefghijklmnopqrstu]
[abcdefghijklmnopqrstuv]
[abcdefghijklmnopqrstuvw]
[abcdefghijklmnopqrstuvwx]
[abcdefghijklmnopqrstuvwxy]
[abcdefghijklmnopqrstuvwxyz]