| 1 |
#include <stdio.h> |
| 2 |
#include <ctype.h> |
| 3 |
#include "xtdio.h" |
| 4 |
|
| 5 |
/* ------------------------------------------------------------------ |
| 6 |
* Definitions Section: |
| 7 |
*/ |
| 8 |
#define MAXCNT 65535 |
| 9 |
|
| 10 |
/* ------------------------------------------------------------------ |
| 11 |
* Source Code: |
| 12 |
*/ |
| 13 |
|
| 14 |
/* stpnxt: returns a pointer to the next argument in the given string, |
| 15 |
* assuming a %d, %s, %f, etc. code was used to interpret the |
| 16 |
* present argument. |
| 17 |
*/ |
| 18 |
#ifdef __PROTOTYPE__ |
| 19 |
char *stpnxt( |
| 20 |
char *s, /* source string */ |
| 21 |
char *fmt) /* format control string (ie. format control code string) */ |
| 22 |
#else |
| 23 |
char *stpnxt(s,fmt) |
| 24 |
char *s; /* source string */ |
| 25 |
char *fmt; /* format control string (ie. format control code string) */ |
| 26 |
#endif |
| 27 |
{ |
| 28 |
char *ss; |
| 29 |
int cmax; |
| 30 |
|
| 31 |
/* if s is NULL return NULL */ |
| 32 |
if(s == NULL) { |
| 33 |
return (char *) NULL; |
| 34 |
} |
| 35 |
|
| 36 |
/* process control string */ |
| 37 |
for(ss= s; *s && *fmt ; ++fmt, ss= s) { |
| 38 |
/* scan code processing */ |
| 39 |
if(*fmt == '%') { |
| 40 |
++fmt; |
| 41 |
|
| 42 |
/* %[-]###.: handle predecessor stuff */ |
| 43 |
if(*fmt == '-') ++fmt; |
| 44 |
if(*fmt && isdigit(*fmt)) { |
| 45 |
sscanf(fmt,"%d",&cmax); |
| 46 |
while(*fmt && (isdigit(*fmt) || *fmt == '.')) ++fmt; |
| 47 |
} |
| 48 |
else cmax= MAXCNT; |
| 49 |
|
| 50 |
/* %...l.: remove the 'l' from consideration |
| 51 |
* %...h.: remove the 'h' from consideration |
| 52 |
*/ |
| 53 |
if(*fmt == 'l' || *fmt == 'h') ++fmt; |
| 54 |
|
| 55 |
/* handle the format code character */ |
| 56 |
switch(*fmt) { |
| 57 |
|
| 58 |
/* %s: string processing */ |
| 59 |
case 's': |
| 60 |
while(*s && isspace(*s)) ++s; |
| 61 |
while(*s && !isspace(*s)) ++s; |
| 62 |
break; |
| 63 |
|
| 64 |
/* %c: character processing */ |
| 65 |
case 'c': |
| 66 |
if(*s) ++s; |
| 67 |
break; |
| 68 |
|
| 69 |
/* %x: hexadecimal processing */ |
| 70 |
case 'x': |
| 71 |
s= stpblk(s); |
| 72 |
ss= s; |
| 73 |
if(*s == '+' || *s == '-') ++s; |
| 74 |
if(s[0] == '0' && s[1] == 'x') s+= 2; |
| 75 |
while(*s && (isdigit(*s) || (*s >= 'a' && *s <= 'f') || (*s >= 'A' && *s <= 'F'))) ++s; |
| 76 |
break; |
| 77 |
|
| 78 |
/* %o: octal processing */ |
| 79 |
case 'o': |
| 80 |
while(*s && isspace(*s)) ++s; |
| 81 |
ss= s; |
| 82 |
if(*s == '+' || *s == '-') ++s; |
| 83 |
while(*s && *s >= '0' && *s <= '7') ++s; |
| 84 |
break; |
| 85 |
|
| 86 |
/* %d, %e, %f, %g, %u: integer and floating point number processing */ |
| 87 |
case 'd': |
| 88 |
case 'e': |
| 89 |
case 'f': |
| 90 |
case 'g': |
| 91 |
case 'u': |
| 92 |
while(*s && isspace(*s)) ++s; |
| 93 |
ss= s; |
| 94 |
if(*s == '-' || *s == '+') ++s; |
| 95 |
while(*s && isdigit(*s)) ++s; |
| 96 |
if(*fmt == 'd') break; |
| 97 |
if(*s == '.') ++s; |
| 98 |
while(*s && isdigit(*s)) ++s; |
| 99 |
if(*s == 'e') { /* exponent handling */ |
| 100 |
++s; |
| 101 |
if(*s == '-' || *s == '+') ++s; |
| 102 |
while(isdigit(*s) && *s) ++s; |
| 103 |
} |
| 104 |
break; |
| 105 |
|
| 106 |
/* %%: single character processing */ |
| 107 |
case '%': |
| 108 |
while(*s && isspace(*s)) ++s; |
| 109 |
if(*s != *fmt) { |
| 110 |
return s; |
| 111 |
} |
| 112 |
break; |
| 113 |
|
| 114 |
/* %b: slight extension, skips over blanks */ |
| 115 |
case 'b': |
| 116 |
while(*s && isspace(*s)) ++s; |
| 117 |
break; |
| 118 |
|
| 119 |
/* unknown */ |
| 120 |
default: |
| 121 |
error(XTDIO_WARNING,"unknown stpnxt code <%s%c%s>\n", |
| 122 |
RD_MAGENTA, |
| 123 |
*fmt, |
| 124 |
RD_GREEN); |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* white space in control string skipping */ |
| 130 |
else if(*fmt && isspace(*fmt)) continue; |
| 131 |
|
| 132 |
/* non-white space: must match string */ |
| 133 |
else { |
| 134 |
while(*s && isspace(*s)) ++s; |
| 135 |
if(*s != *fmt) { |
| 136 |
return s; |
| 137 |
} |
| 138 |
else { /* s and c match, so continue forth */ |
| 139 |
++s; |
| 140 |
continue; |
| 141 |
} |
| 142 |
} |
| 143 |
if(s <= ss) { |
| 144 |
return ss; |
| 145 |
} |
| 146 |
} /* c: loop */ |
| 147 |
|
| 148 |
return s; |
| 149 |
} |
| 150 |
|
| 151 |
/* --------------------------------------------------------------------- */ |