home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / STPEXPAN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  2.4 KB  |  94 lines

  1. /**
  2. *
  3. * Name        STPEXPAN -- Replace tab characters with blanks.
  4. *
  5. * Synopsis    presult = stpexpan(ptarget,psource,incr,tarsize);
  6. *
  7. *        char *presult    NIL if conversion was completed; else
  8. *                pointer to next character in psource.
  9. *        char *ptarget    Pointer to the target string.
  10. *        char *psource    Pointer to the source string.
  11. *        int  incr    Interval separating tab stops.
  12. *        int tarsize    The maximum size of the target string.
  13. *
  14. * Description    This function replaces tab characters ('\t') with enough
  15. *        blank spaces (' ') to advance to the following tab
  16. *        position.
  17. *
  18. *        Definition of tab stops:  If incr is positive then tab
  19. *        stops are set every incr characters starting from
  20. *        position 0 (i.e., the first character of the line).  If
  21. *        incr is 0 or negative, then no tab stops are set, and
  22. *        any tab characters in psource are copied intact to
  23. *        ptarget.
  24. *
  25. *        STPEXPAN regards all characters in psource (except tabs
  26. *        and NULs) as graphic characters occupying one space in
  27. *        ptarget.
  28. *
  29. *        Warning:  ptarget and psource must not overlap.
  30. *
  31. * Returns    presult     Pointer to the remainder of psource if
  32. *                there wasn't enough room in ptarget;
  33. *                otherwise NIL.
  34. *        ptarget     Pointer to the altered target string.
  35. *
  36. * Version    6.00 (C)Copyright Blaise Computing Inc.  1987,1989
  37. *
  38. **/
  39.  
  40.  
  41. #include <bstrings.h>
  42. #include <butil.h>
  43.  
  44. #define  BLANK     ' '
  45. #define  NEWLINE '\n'
  46. #define  TAB     '\t'
  47. #define  NUL     '\0'
  48.  
  49.  
  50. char *stpexpan(ptarget,psource,incr,tarsize)
  51. register char *ptarget;
  52. char          *psource;
  53. int          incr,tarsize;
  54. {
  55.     char c;
  56.     register int len;        /* Running total of characters put  */
  57.                 /* into ptarget, also index of next */
  58.                 /* char in ptarget.            */
  59.     int numspaces;
  60.  
  61.     len = 0;
  62.     tarsize--;
  63.     while (((c = *psource) != NUL) && (len < tarsize))
  64.     {
  65.     switch (c)
  66.     {
  67.         case TAB:
  68.         if ((incr > 0) &&
  69.             ((numspaces = (incr - (len % incr))) != 0))
  70.         {
  71.             if ((len += numspaces) < tarsize)
  72.             {        /* There's enough room.             */
  73.             while (numspaces--)
  74.                 *ptarget++ = BLANK;
  75.             break;
  76.             }
  77.             else        /* There isn't enough room, */
  78.             continue;    /* so quit.            */
  79.         }
  80.  
  81.         /* Else TAB expansion is not in effect:            */
  82.         /* just fall through and copy the TAB to ptarget.        */
  83.  
  84.         default:
  85.         *ptarget++ = c;
  86.         len++;
  87.         break;
  88.     }
  89.     psource++;
  90.     }
  91.     *ptarget = '\0';
  92.     return (c ? psource : NIL);
  93. }
  94.