home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************/
- /* */
- /* This Program Written By Paul Edwards. */
- /* */
- /*********************************************************************/
- /*********************************************************************/
- /* */
- /* This program takes the following parameters: */
- /* 1) A file pointer to a stream with basic format input. */
- /* 2) A character indicating the type of input required. */
- /* 3) A pointer to a variable you want filled in. */
- /* */
- /* If the character is a NUL, then this signifies end of input. */
- /* */
- /* It returns the number of fields successfully read. It will */
- /* stop on a bad field. e.g. */
- /* */
- /* rc = basinp(fp,'d',&num,'s',string,'\0'); */
- /* */
- /* if the pointer is NULL, the variable is read but not stored, */
- /* and the count is not increased. */
- /* */
- /*********************************************************************/
- #include <stdarg.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <basic.h>
-
- static int finrdb(BAS *bp);
-
- #define TESTEOB \
- if (!c) \
- { \
- if (finrdb(bp)) \
- { \
- va_end(va); \
- return(cnt); \
- } \
- else c = *(myup = bp->strtptr); \
- }
-
- int basinp(BAS *bp,int ftype,...)
- {
- register int c;
- register char *myup;
- register char *sptr;
- register int x;
- va_list va;
- int assign;
- int *dptr;
- int type;
- int cnt=0;
-
- myup = bp->upto;
- va_start(va,ftype);
- type = ftype;
- for (;(type != '\0');type=va_arg(va,int))
- {
- c = *myup;
- if (type == 's')
- {
- sptr = va_arg(va,char *);
- if (sptr == NULL)
- assign = 0;
- else assign = 1;
- while (c != '\"')
- {
- while ((c != '\"') && (c))
- c = *++myup;
- TESTEOB;
- }
- c = *++myup;
- if (assign)
- {
- while (c != '\"')
- {
- while ((c != '\"') && (c))
- {
- *sptr++ = c;
- c = *++myup;
- }
- TESTEOB;
- }
- *sptr = '\0';
- cnt++;
- }
- else while (c != '\"')
- {
- while ((c != '\"') && (c))
- c = *++myup;
- TESTEOB;
- }
- myup++;
- }
- else /* if type == 'd' assumed for speed */
- {
- dptr = va_arg(va,int *);
- if (dptr == NULL)
- assign = 0;
- else assign = 1;
- while (!isdigit(c))
- {
- while (!isdigit(c) && c)
- c = *++myup;
- TESTEOB;
- }
- x = c - '0';
- c = *++myup;
- TESTEOB;
- while (isdigit(c))
- {
- while (isdigit(c))
- {
- x = x*10+c-'0';
- c = *++myup;
- }
- TESTEOB;
- }
- if (assign)
- {
- *dptr = x;
- if (assign) cnt++;
- }
- }
- }
- va_end(va);
- bp->upto = myup;
- return (cnt);
- }
-
- static int finrdb(BAS *bp)
- {
- register int bytes;
-
- bytes = fread(bp->strtptr,1,bp->bufamt,bp->fptr);
- bp->strtptr[bytes] = '\0';
- if (bytes == 0)
- return (1);
- else return (0);
- }
-