home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_11 / 1011119b < prev    next >
Text File  |  1992-09-14  |  745b  |  34 lines

  1. /* align.c: Align a string within a background */
  2.  
  3. #include <string.h>
  4.  
  5. #define LEFT (-1)
  6. #define CENTER 0
  7. #define RIGHT 1
  8.  
  9. #define min(x,y) ((x) <= (y) ? (x) : (y))
  10.  
  11. char *align(char *buf,int width,char fill,int justify,char *data)
  12. {
  13.     char *p;
  14.     
  15.     /* Truncate, if necessary */
  16.     int dlen = min(width,strlen(data));
  17.  
  18.     /* Populate with fill character */
  19.     memset(buf,fill,width);
  20.     buf[width] = '\0';
  21.     
  22.     /* Calculate starting point */
  23.     if (justify == LEFT)
  24.         p = buf;                                
  25.     else if (justify == CENTER)
  26.         p = buf + (width-dlen)/2;
  27.     else
  28.         p = buf + width-dlen;
  29.  
  30.     /* Insert the data there */
  31.     memcpy(p,data,dlen);
  32.     return buf;
  33. }
  34.