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