home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-2 / SCOTT.C < prev    next >
Text File  |  2000-06-30  |  4KB  |  163 lines

  1. /* SCOTT.C    Scott Layson's personal miscellaneous-function
  2.             library
  3.  
  4.     This code is in the public domain.
  5.     Created from functions in STDLIB?.C 81.10.29 Gyro
  6.  
  7. */
  8.  
  9.  
  10. #include "bdscio.h"
  11.  
  12.  
  13.     /* This deletes elements in the array <ary>, which consists of
  14.         <len> elements of <eltsize> bytes; elements from <dello>
  15.         through <delhi> - 1 are deleted. */
  16. arydel (ary, len, eltsize, dello, delhi)
  17.     char *ary;
  18.     int len, eltsize, dello, delhi;
  19. {
  20.     movmem (ary + delhi * eltsize,
  21.            ary + dello * eltsize,
  22.            (len - delhi) * eltsize);
  23.     }
  24.  
  25.  
  26. match (s1, s2)                /* case-independent null-term compare */
  27.     char *s1, *s2;
  28. {
  29.     do if (toupper (*s1) != toupper (*s2)) return (FALSE);
  30.         while ((s1++, *s2++));
  31.     return (TRUE);
  32.     }
  33.  
  34.  
  35. upcase (str)            /* convert a string to upper case */
  36.     char *str;
  37. {
  38.     for (;*str; ++str) *str = toupper (*str);
  39.     }
  40.  
  41.  
  42. /* The version of this in STDLIB2.C is bugous!!! */
  43. int
  44. fscanf(iobuf,format)
  45. char *format;
  46. struct _buf *iobuf;
  47. {
  48.     char text[MAXLINE];
  49.     if (!fgets(text,iobuf)) return /* 0 NO!!! */ -1;
  50.     return _scn(text,&format);
  51. }
  52.  
  53.  
  54. /* This is the standard one, except: if the character after "%s" in the format
  55. string is a '%', the %s breaks on any whitespace.  */
  56. int
  57. _scn(line,fmt)
  58. char *line, **fmt;
  59. {
  60.     char sf, c, base, n, *sptr, *format, matchchar;
  61.     int sign, val, **args;
  62.  
  63.     format = *fmt++;    /* fmt first points to the format string */
  64.     args = fmt;        /* now it points to the arg list */
  65.  
  66.     n = 0;
  67.     while (c = *format++) {
  68.        _igs (&line);
  69.        if (!*line) return n;    /* if end of input string, return */
  70.        if (isspace(c)) continue;    /* skip white space in format string */
  71.        if (c != '%') {        /* if not %, must match text */
  72.         if (c != _igs(&line)) return n;
  73.         else line++;
  74.         }
  75.        else {            /* process conversion */
  76.         sign = 1;
  77.         base = 10;
  78.         sf = 0;
  79.         if ((c = *format++) == '*') {
  80.             sf++;        /* if "*" given, supress assignment */
  81.             c = *format++;
  82.          }
  83.         switch (toupper(c)) {
  84.            case 'X': base = 16;
  85.                  goto doval;
  86.  
  87.            case 'O': base = 8;
  88.                  goto doval;
  89.  
  90.            case 'D': if (_igs(&line) == '-') {
  91.                 sign = -1;
  92.                 line++;
  93.                   }
  94.  
  95.        doval:  case 'U': val = 0;
  96.                  if (_bc(_igs(&line),base) == ERROR)
  97.                 return n;
  98.                  while ((c = _bc(*line++,base)) != 255)
  99.                 val = val * base + c;
  100.                  line--;
  101.                  break;
  102.  
  103.            case 'S': _igs(&line);
  104.                  sptr = *args;
  105.                 if (*format != '%') matchchar = *format++;
  106.                 else matchchar = '\0';
  107.                  while (c = *line++)   {
  108.                     if (matchchar ? c == matchchar : isspace (c)) break;
  109.                     if (!sf) *sptr++ = c;
  110.                   }                
  111.                  if (!sf) {
  112.                 n++;
  113.                 *sptr = '\0';
  114.                 args++;
  115.                   }
  116.                  continue;
  117.  
  118.            case 'C': if (!sf) {
  119.                 poke(*args++, *line);
  120.                 n++;
  121.                  }
  122.                  line++;
  123.                  continue;
  124.  
  125.            default:  return n;
  126.          }
  127.         if (!sf) {
  128.             **args++ = val * sign;
  129.             n++;
  130.          }
  131.     }}
  132.     return n;
  133. }
  134.  
  135.  
  136. /* This one doesn't leave a trailing newline on the end.  It also doesn't
  137.    care about CRs at all. */
  138. char *
  139. fgets(s,iobuf)
  140.     char *s;
  141.     struct _buf *iobuf;
  142. {
  143.     int count, c;
  144.     char *cptr;
  145.     
  146.     count = (MAXLINE - 1);
  147.     cptr = s;
  148.     if ((c = getc(iobuf)) == CPMEOF  ||  c == EOF) return NULL;
  149.     do {
  150.         if (c == '\r') continue;
  151.         if (c == '\n') break;
  152.         *cptr++ = c;
  153.         }
  154.         while (--count  &&  (c = getc(iobuf)) != EOF  &&  c != CPMEOF);
  155.     if (c == CPMEOF) ungetc(c,iobuf);    /* push back control-Z */
  156.     *cptr = '\0';
  157.     return s;
  158. }
  159.  
  160.  
  161.  
  162. /* End of SCOTT.C */
  163.