home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / STPJUST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  3.0 KB  |  102 lines

  1. /**
  2. *
  3. * Name        stpjust -- Justify a string within a field
  4. *
  5. * Synopsis    presult = stpjust(ptarget,psource,fill,fldsize,code);
  6. *
  7. *        char *presult    Pointer to the resulting target string
  8. *        char *ptarget    Pointer to the target string, which must
  9. *                be at least (fldsize + 1) bytes long.
  10. *        char *psource    Pointer to the source string
  11. *        char fill    Character to be used for padding
  12. *        int  fldsize    Size of field to be filled
  13. *        int  code    Type of justification:
  14. *                JUST_LEFT, JUST_CENTER, or JUST_RIGHT.
  15. *
  16. * Description    This function justifies or centers a string within a
  17. *        field of a specified size, padding with a given
  18. *        character if necessary.  The resulting string is exactly
  19. *        fldsize characters long.
  20. *
  21. *        If the source string has more than fldsize characters,
  22. *        it is truncated to fit into the target.  Characters from
  23. *        the left, center, or right portion of the source are
  24. *        used depending on whether left, center, or right
  25. *        justification is specified, respectively.
  26. *
  27. *        If the source string is fewer than fldsize bytes long
  28. *        (not counting the trailing NUL ('\0')), the remaining
  29. *        space is filled with the fill character.  If left
  30. *        justification is specified, the filling takes place on
  31. *        the right; if right justification, on the left; if
  32. *        centering, on both sides.
  33. *
  34. *        If code has an unknown value, left justification is
  35. *        performed.
  36. *
  37. * Returns    presult     Pointer to the altered target string.
  38. *        *ptarget    The altered target string.
  39. *
  40. * Version    3.0 (C)Copyright Blaise Computing Inc.    1986
  41. *
  42. **/
  43.  
  44. #include <string.h>
  45.  
  46. #include <bstring.h>
  47.  
  48. char *stpjust(ptarget,psource,fill,fldsize,code)
  49. register char *ptarget,*psource;
  50. char          fill;
  51. int          fldsize,code;
  52. {
  53.     register int diff,i;
  54.     int      numleft;
  55.     char     *savetarget = ptarget;
  56.  
  57.     if (fldsize < 0)
  58.     fldsize = 0;
  59.     if ((diff = ((int) strlen(psource)) - fldsize) >= 0)
  60.     {                  /* Use only a portion of source          */
  61.     switch (code)
  62.     {
  63.         case JUST_RIGHT:          /* Skip leftmost characters     */
  64.         psource += diff;
  65.         break;
  66.         case JUST_CENTER:          /* Use center characters          */
  67.         psource += diff / 2;
  68.         break;
  69.         case JUST_LEFT:          /* Use leftmost characters      */
  70.         default:
  71.         break;
  72.     }
  73.     while (fldsize--)
  74.         *ptarget++ = *psource++;
  75.     }
  76.     else
  77.     {                  /* There's extra space to fill          */
  78.     diff = -diff;          /* diff is number of spaces to fill     */
  79.     switch (code)
  80.     {              /* numleft = number of spaces on left   */
  81.         case JUST_RIGHT:
  82.         numleft = diff;
  83.         break;
  84.         case JUST_CENTER:
  85.         numleft = diff / 2;
  86.         break;
  87.         case JUST_LEFT:
  88.         default:
  89.         numleft = 0;
  90.         break;
  91.     }
  92.     for (i = numleft; i; i--)
  93.         *ptarget++ = fill;       /* Add the fill chars on the left  */
  94.     while (*psource)
  95.         *ptarget++ = *psource++;   /* Copy the string itself      */
  96.     for (i = diff - numleft; i; i--)
  97.         *ptarget++ = fill;       /* Add the fill chars on the right */
  98.     }
  99.     *ptarget = '\0';
  100.     return savetarget;
  101. }
  102.