home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2174 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  1.9 KB

  1. From: drh@duke.cs.duke.edu (D. Richard Hipp)
  2. Newsgroups: alt.sources
  3. Subject: xprintf update
  4. Message-ID: <659977812@romeo.cs.duke.edu>
  5. Date: 30 Nov 90 15:10:14 GMT
  6.  
  7. brad@hcx1.ssd.csd.harris.com (Brad Appleton) writes (via mail):
  8. >.... I happened to be thinking that
  9. >in addition to stuff like:
  10. >
  11. >        printf( "%10s", "str" )
  12. >and
  13. >        printf( "%-10s", "str" )
  14. >
  15. >to do right & left justification, that it would be *_really_* nice
  16. >to also have something like:
  17. >
  18. >        printf( "%=10s", "str" )
  19. >
  20. >that would print "str" centered within a ten character field.
  21.  
  22. Good idea!  It turns out that a "=" flag for centering is easy to
  23. implement in the xprintf sources I posted yesterday.  The diffs are
  24. shown below, together with a test program and the test program's output.
  25.  
  26. The output of "diff xprintf.c newversion.c" is:
  27.   293a294
  28.   >   int flag_center;          /* True if "=" flag is present */
  29.   340a342
  30.   >         case '=':   flag_center = 1;          c = 0;   break;
  31.   343a346
  32.   >     if( flag_center ) flag_leftjustify = 0;
  33.   624a628,632
  34.   >         if( flag_center ){
  35.   >           nspace = nspace/2;
  36.   >           width -= nspace;
  37.   >           flag_leftjustify = 1;
  38.   >         }
  39.  
  40. A program to test Brad's idea:
  41.   void main(void){
  42.     int i;
  43.     for(i=1; i<27; i++) printf("[%=20.*s]\n",i,"abcdefghijklmnopqrstuvwxyz");
  44.   }
  45.  
  46. The output of the test program:
  47. [         a          ]
  48. [         ab         ]
  49. [        abc         ]
  50. [        abcd        ]
  51. [       abcde        ]
  52. [       abcdef       ]
  53. [      abcdefg       ]
  54. [      abcdefgh      ]
  55. [     abcdefghi      ]
  56. [     abcdefghij     ]
  57. [    abcdefghijk     ]
  58. [    abcdefghijkl    ]
  59. [   abcdefghijklm    ]
  60. [   abcdefghijklmn   ]
  61. [  abcdefghijklmno   ]
  62. [  abcdefghijklmnop  ]
  63. [ abcdefghijklmnopq  ]
  64. [ abcdefghijklmnopqr ]
  65. [abcdefghijklmnopqrs ]
  66. [abcdefghijklmnopqrst]
  67. [abcdefghijklmnopqrstu]
  68. [abcdefghijklmnopqrstuv]
  69. [abcdefghijklmnopqrstuvw]
  70. [abcdefghijklmnopqrstuvwx]
  71. [abcdefghijklmnopqrstuvwxy]
  72. [abcdefghijklmnopqrstuvwxyz]
  73.