| 1 | greg | 1.1 | /* Copyright (c) 1991 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Read white space separated words from stream | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include  <stdio.h> | 
| 12 |  |  |  | 
| 13 |  |  | #include  <ctype.h> | 
| 14 |  |  |  | 
| 15 |  |  |  | 
| 16 |  |  | char * | 
| 17 |  |  | fgetword(s, n, fp)              /* get a word, maximum n-1 characters */ | 
| 18 |  |  | char  *s; | 
| 19 |  |  | int  n; | 
| 20 |  |  | register FILE  *fp; | 
| 21 |  |  | { | 
| 22 |  |  | register char  *cp; | 
| 23 |  |  | register int  c; | 
| 24 |  |  | /* skip initial white space */ | 
| 25 |  |  | do | 
| 26 |  |  | c = getc(fp); | 
| 27 |  |  | while (isspace(c)); | 
| 28 |  |  | /* check for end of file */ | 
| 29 |  |  | if (c == EOF) | 
| 30 |  |  | return(NULL); | 
| 31 |  |  | /* get actual word */ | 
| 32 |  |  | cp = s; | 
| 33 |  |  | do { | 
| 34 |  |  | if (--n <= 0)                   /* check length limit */ | 
| 35 |  |  | break; | 
| 36 |  |  | *cp++ = c; | 
| 37 |  |  | c = getc(fp); | 
| 38 |  |  | } while (c != EOF && !isspace(c)); | 
| 39 |  |  | *cp = '\0'; | 
| 40 |  |  | if (c != EOF)                   /* replace delimiter */ | 
| 41 |  |  | ungetc(c, fp); | 
| 42 |  |  | return(s); | 
| 43 |  |  | } | 
| 44 |  |  |  | 
| 45 |  |  |  | 
| 46 |  |  | isint(s)                        /* check integer format */ | 
| 47 |  |  | char  *s; | 
| 48 |  |  | { | 
| 49 |  |  | register char  *cp = s; | 
| 50 |  |  |  | 
| 51 |  |  | while (isspace(*cp)) | 
| 52 |  |  | cp++; | 
| 53 |  |  | if (*cp == '-' || *cp == '+') | 
| 54 |  |  | cp++; | 
| 55 |  |  | do | 
| 56 |  |  | if (!isdigit(*cp)) | 
| 57 |  |  | return(0); | 
| 58 |  |  | while (*++cp); | 
| 59 |  |  | return(1); | 
| 60 |  |  | } | 
| 61 |  |  |  | 
| 62 |  |  |  | 
| 63 |  |  | isflt(s)                        /* check float format */ | 
| 64 |  |  | char  *s; | 
| 65 |  |  | { | 
| 66 |  |  | register char  *cp = s; | 
| 67 |  |  |  | 
| 68 |  |  | while (isspace(*cp)) | 
| 69 |  |  | cp++; | 
| 70 |  |  | if (*cp == '-' || *cp == '+') | 
| 71 |  |  | cp++; | 
| 72 |  |  | s = cp; | 
| 73 |  |  | while (isdigit(*cp)) | 
| 74 |  |  | cp++; | 
| 75 |  |  | if (*cp == '.') { | 
| 76 |  |  | cp++; s++; | 
| 77 |  |  | while (isdigit(*cp)) | 
| 78 |  |  | cp++; | 
| 79 |  |  | } | 
| 80 |  |  | if (cp == s) | 
| 81 |  |  | return(0); | 
| 82 |  |  | if (*cp == 'e' || *cp == 'E') | 
| 83 |  |  | return(isint(cp+1)); | 
| 84 |  |  | return(1); | 
| 85 |  |  | } |