Back to index

radiance  4R0+20100331
words.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: words.c,v 2.9 2004/09/28 17:54:18 greg Exp $";
00003 #endif
00004 /*
00005  * Routines for recognizing and moving about words in strings.
00006  *
00007  * External symbols declared in standard.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include  <ctype.h>
00013 #include  <string.h>
00014 
00015 #include  "rtio.h"
00016 
00017 char *
00018 atos(rs, nb, s)                    /* get word from string, returning rs */
00019 char  *rs;
00020 register int  nb;
00021 register char  *s;
00022 {
00023        register char  *cp = rs;
00024 
00025        while (isspace(*s))
00026               s++;
00027        while (--nb > 0 && *s && !isspace(*s))
00028               *cp++ = *s++;
00029        *cp = '\0';
00030        return(rs);
00031 }
00032 
00033 
00034 char *
00035 nextword(cp, nb, s)         /* get (quoted) word, returning new s */
00036 register char  *cp;
00037 register int  nb;
00038 register char  *s;
00039 {
00040        int    quote = 0;
00041 
00042        if (s == NULL) return(NULL);
00043        while (isspace(*s))
00044               s++;
00045        switch (*s) {
00046        case '\0':
00047               return(NULL);
00048        case '"':
00049        case '\'':
00050               quote = *s++;
00051        }
00052        while (--nb > 0 && *s && (quote ? *s!=quote : !isspace(*s)))
00053               *cp++ = *s++;
00054        *cp = '\0';
00055        if (quote && *s==quote)
00056               s++;
00057        return(s);
00058 }
00059 
00060 
00061 char *
00062 sskip(s)                    /* skip word in string, leaving on space */
00063 register char  *s;
00064 {
00065        while (isspace(*s))
00066               s++;
00067        while (*s && !isspace(*s))
00068               s++;
00069        return(s);
00070 }
00071 
00072 
00073 char *
00074 sskip2(s, n)                /* skip word(s) in string, leaving on word */
00075 register char  *s;
00076 register int  n;
00077 {
00078        while (isspace(*s))
00079               s++;
00080        while (n-- > 0) {
00081               while (*s && !isspace(*s))
00082                      s++;
00083               while (isspace(*s))
00084                      s++;
00085        }
00086        return(s);
00087 }
00088 
00089 
00090 char *
00091 iskip(s)                    /* skip integer in string */
00092 register char  *s;
00093 {
00094        while (isspace(*s))
00095               s++;
00096        if (*s == '-' || *s == '+')
00097               s++;
00098        if (!isdigit(*s))
00099               return(NULL);
00100        do
00101               s++;
00102        while (isdigit(*s));
00103        return(s);
00104 }
00105 
00106 
00107 char *
00108 fskip(s)                    /* skip float in string */
00109 register char  *s;
00110 {
00111        register char  *cp;
00112 
00113        while (isspace(*s))
00114               s++;
00115        if (*s == '-' || *s == '+')
00116               s++;
00117        cp = s;
00118        while (isdigit(*cp))
00119               cp++;
00120        if (*cp == '.') {
00121               cp++; s++;
00122               while (isdigit(*cp))
00123                      cp++;
00124        }
00125        if (cp == s)
00126               return(NULL);
00127        if (*cp == 'e' || *cp == 'E')
00128               return(iskip(cp+1));
00129        return(cp);
00130 }
00131 
00132 
00133 int
00134 isint(s)                    /* check integer format */
00135 char  *s;
00136 {
00137        register char  *cp;
00138 
00139        cp = iskip(s);
00140        return(cp != NULL && *cp == '\0');
00141 }
00142 
00143 
00144 int
00145 isintd(s, ds)               /* check integer format with delimiter set */
00146 char  *s, *ds;
00147 {
00148        register char  *cp;
00149 
00150        cp = iskip(s);
00151        return(cp != NULL && strchr(ds, *cp) != NULL);
00152 }
00153 
00154 
00155 int
00156 isflt(s)                    /* check float format */
00157 char  *s;
00158 {
00159        register char  *cp;
00160 
00161        cp = fskip(s);
00162        return(cp != NULL && *cp == '\0');
00163 }
00164 
00165 
00166 int
00167 isfltd(s, ds)               /* check integer format with delimiter set */
00168 char  *s, *ds;
00169 {
00170        register char  *cp;
00171 
00172        cp = fskip(s);
00173        return(cp != NULL && strchr(ds, *cp) != NULL);
00174 }