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

  1. /**
  2. *
  3. * Name        STPTABFY -- Replace blanks with tab characters.
  4. *
  5. * Synopsis    presult = stptabfy(psource,incr);
  6. *
  7. *        char *presult    Pointer to converted psource.
  8. *        char *psource    Pointer to the source string.
  9. *        int  incr    Interval separating tab stops.
  10. *
  11. * Description    This function replaces blank spaces (' ') with tab
  12. *        characters ('\t') so as to possibly shorten the length
  13. *        of a string in memory.    Use of tab characters is
  14. *        minimal:  that is, whenever a single blank space will
  15. *        serve instead of a tab, the blank is used.
  16. *
  17. *        Tab stops are defined as follows:  If incr is positive
  18. *        then tab stops are set every incr characters starting
  19. *        from position 0 (i.e., the first character of the
  20. *        line).    If incr is 0 or negative, then tab stops are
  21. *        set at each position, so that all blanks and tabs in
  22. *        psource are copied intact.
  23. *
  24. *        Existing tab characters are left intact.  They are
  25. *        regarded as advancing to the next tab stop.
  26. *
  27. *        STPTABFY regards all characters in psource (except
  28. *        TABs and NULs) as graphic characters occupying one
  29. *        space in the result.
  30. *
  31. * Returns    presult     Pointer to the altered source string.
  32. *        *psource    The altered source string.
  33. *
  34. * Version    6.00 (C)Copyright Blaise Computing Inc.  1987, 1989
  35. *
  36. **/
  37.  
  38.  
  39. #include <bstrings.h>
  40.  
  41. #define  BLANK     ' '
  42. #define  NEWLINE '\n'
  43. #define  TAB     '\t'
  44.  
  45.  
  46. char *stptabfy(psource,incr)
  47. char *psource;
  48. int  incr;
  49. {
  50.     char c;
  51.     register int col = 0;    /* Column counter (modulo incr).      */
  52.     register int numblanks = 0; /* Number of blanks we've saved up.   */
  53.  
  54.     char      *pfrom = psource;
  55.     register char *pto     = psource;
  56.  
  57.     do
  58.     {
  59.     switch (c = *pfrom++)
  60.     {
  61.         case BLANK:
  62.         numblanks++;
  63.         col++;
  64.         if ((incr <= 0) ||
  65.             (col % incr == 0))
  66.         {
  67.             *pto++    = (char) ((numblanks > 1) ? TAB : BLANK);
  68.             numblanks = 0;
  69.         }
  70.         break;
  71.  
  72.         case TAB:
  73.         col      =
  74.         numblanks = 0;        /* Discard the saved blanks   */
  75.         *pto++      = TAB;
  76.         break;
  77.  
  78.         default:
  79.         col++;
  80.         for (; numblanks; numblanks--)
  81.             *pto++ = BLANK;        /* Spill any saved blanks */
  82.         *pto++ = c;
  83.         break;
  84.     }
  85.     } while (c);
  86.  
  87.     return (psource);
  88. }
  89.