home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 01e / margin.zip / MARGIN.C next >
Text File  |  1986-11-18  |  2KB  |  80 lines

  1. /*    Last revision: November 18, 1986 at 18:56  */
  2.  
  3.  
  4. /**************************************************************************/
  5.  
  6. #include <extend.h> 
  7.  
  8. #define LF        '\n'
  9. #define SOFT_LF  ('\n' | 0x80)
  10. #define MAX_SIZE 0x7FF0    /* 32K */
  11.  
  12. /*************************************************************
  13. * MARGIN()
  14. * Syntax: MARGIN( <expC>, [<expN>] )
  15. * Return: A string containing in the contents of the
  16. *         expC string with all hi bits stripped and a margin
  17. *         width of expN. Range of expN is 0..32, default of 8
  18. * Note..: compile with Lattice >LC -ml -v -n MARGIN
  19. *         
  20. */
  21.  
  22. MARGIN()
  23. {
  24.    unsigned char *_str_all();
  25.    unsigned char *str1, *pos1;     /* string and a pointer into it */
  26.    unsigned char *str2, *pos2;
  27.    unsigned char c;
  28.  
  29.    unsigned size2;
  30.    int spaces, idx, lines;
  31.  
  32.    if (PCOUNT >= 1 && ISCHAR(1)) {
  33.       str1 = _parc(1);
  34.       pos1 = str1;
  35.       spaces = 8;
  36.  
  37.       if (PCOUNT == 2 && ISNUM(2)) {
  38.          spaces = _parni(2);
  39.          if (spaces < 0 || spaces > 32)
  40.             spaces = 8;
  41.       }
  42.  
  43.       lines = 2;
  44.       while (*pos1) {
  45.          if (*pos1 == LF || *pos1 == SOFT_LF )
  46.             lines++; 
  47.          pos1++;
  48.       }
  49.       pos1 = str1;
  50.       size2 = strlen(str1) + (spaces*(lines)) + 2;
  51.       if (size2 < MAX_SIZE) {
  52.          str2 = _str_all(size2); 
  53.          pos2 = str2;
  54.          while ((*pos1) && *pos1 != 0x1A) { /* scan the str1 until it's end */
  55.             *pos2 = ((*pos1) & 0x7F);   /* strip hi bit and move char */
  56.             c = *pos2;                 /* get the char from str1 */
  57.             pos1++;
  58.             pos2++;
  59.             if (c == LF && (spaces)) { /* if we found a new line */
  60.                 if (*pos1 >= ' ')      /* and the new line has text on it */
  61.                    /* insert the proper number of spaces into str2 */
  62.                    for (idx = 1; idx <= spaces; ++idx)
  63.                       *pos2++ = ' ';  
  64.             }
  65.          }
  66.          *pos2 = '\0';               /* terminate string */
  67.          _retc(str2);
  68.          _str_rel(str2, size2);       /* release temp space */
  69.       }
  70.       else
  71.          _retc("ERROR: String too long for MARGIN()!");
  72.    }
  73.    else
  74.      _retc("ERROR: MARGIN() has no string parameter!");
  75. }
  76.  
  77.  
  78.  
  79.  
  80.