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