home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / snip1091.arj / BASINP.C < prev    next >
C/C++ Source or Header  |  1991-07-19  |  5KB  |  142 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*  This Program Written By Paul Edwards.                            */
  4. /*                                                                   */
  5. /*********************************************************************/
  6. /*********************************************************************/
  7. /*                                                                   */
  8. /*  This program takes the following parameters:                     */
  9. /*  1) A file pointer to a stream with basic format input.           */
  10. /*  2) A character indicating the type of input required.            */
  11. /*  3) A pointer to a variable you want filled in.                   */
  12. /*                                                                   */
  13. /*  If the character is a NUL, then this signifies end of input.     */
  14. /*                                                                   */
  15. /*  It returns the number of fields successfully read.  It will      */
  16. /*  stop on a bad field.  e.g.                                       */
  17. /*                                                                   */
  18. /*  rc = basinp(fp,'d',&num,'s',string,'\0');                        */
  19. /*                                                                   */
  20. /*  if the pointer is NULL, the variable is read but not stored,     */
  21. /*  and the count is not increased.                                  */
  22. /*                                                                   */
  23. /*********************************************************************/
  24. #include <stdarg.h>
  25. #include <ctype.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <basic.h>
  29.  
  30. static int finrdb(BAS *bp);
  31.  
  32. #define TESTEOB                                     \
  33.         if (!c)                                     \
  34.         {                                           \
  35.           if (finrdb(bp))                           \
  36.           {                                         \
  37.             va_end(va);                             \
  38.             return(cnt);                            \
  39.           }                                         \
  40.           else c = *(myup = bp->strtptr);           \
  41.         }
  42.  
  43. int basinp(BAS *bp,int ftype,...)
  44. {
  45.       register int c;
  46.       register char *myup;
  47.       register char *sptr;
  48.       register int x;
  49.       va_list va;
  50.       int assign;
  51.       int *dptr;
  52.       int type;
  53.       int cnt=0;
  54.  
  55.       myup = bp->upto;
  56.       va_start(va,ftype);
  57.       type = ftype;
  58.       for (;(type != '\0');type=va_arg(va,int))
  59.       {
  60.             c = *myup;
  61.             if (type == 's')
  62.             {
  63.                   sptr = va_arg(va,char *);
  64.                   if (sptr == NULL)
  65.                         assign = 0;
  66.                   else  assign = 1;
  67.                   while (c != '\"')
  68.                   {
  69.                         while ((c != '\"') && (c))
  70.                               c = *++myup;
  71.                         TESTEOB;
  72.                   }
  73.                   c = *++myup;
  74.                   if (assign)
  75.                   {
  76.                         while (c != '\"')
  77.                         {
  78.                               while ((c != '\"') && (c))
  79.                               {
  80.                                     *sptr++ = c;
  81.                                     c = *++myup;
  82.                               }
  83.                               TESTEOB;
  84.                         }
  85.                         *sptr = '\0';
  86.                         cnt++;
  87.                   }
  88.                   else while (c != '\"')
  89.                   {
  90.                         while ((c != '\"') && (c))
  91.                               c = *++myup;
  92.                         TESTEOB;
  93.                   }
  94.                   myup++;
  95.             }
  96.             else      /* if type == 'd' assumed for speed */
  97.             {
  98.                   dptr = va_arg(va,int *);
  99.                   if (dptr == NULL)
  100.                         assign = 0;
  101.                   else  assign = 1;
  102.                   while (!isdigit(c))
  103.                   {
  104.                         while (!isdigit(c) && c)
  105.                               c = *++myup;
  106.                         TESTEOB;
  107.                   }
  108.                   x = c - '0';
  109.                   c = *++myup;
  110.                   TESTEOB;
  111.                   while (isdigit(c))
  112.                   {
  113.                         while (isdigit(c))
  114.                         {
  115.                               x = x*10+c-'0';
  116.                               c = *++myup;
  117.                         }
  118.                         TESTEOB;
  119.                   }
  120.                   if (assign)
  121.                   {
  122.                         *dptr = x;
  123.                         if (assign) cnt++;
  124.                   }
  125.             }
  126.       }
  127.       va_end(va);
  128.       bp->upto = myup;
  129.       return (cnt);
  130. }
  131.  
  132. static int finrdb(BAS *bp)
  133. {
  134.       register int bytes;
  135.  
  136.       bytes = fread(bp->strtptr,1,bp->bufamt,bp->fptr);
  137.       bp->strtptr[bytes] = '\0';
  138.       if (bytes == 0)
  139.             return (1);
  140.       else  return (0);
  141. }
  142.