Skip to content

Instantly share code, notes, and snippets.

@nyuichi
Created January 27, 2015 14:27
Show Gist options
  • Save nyuichi/14bb2d07072740671f58 to your computer and use it in GitHub Desktop.
Save nyuichi/14bb2d07072740671f58 to your computer and use it in GitHub Desktop.
PIC_INLINE int
xvfprintf(xFILE *stream, const char *fmt, va_list ap)
{
int flags, width, precision, length;
enum {
ALT = 1,
ZERO = 2,
LEFT = 4,
SIGN = 8,
SPACE = 16
};
enum {
LEN_H = 1,
LEN_L = 2
};
const char *p;
union {
char *sval;
int ival;
short hval;
long lval;
double dval;
} u;
char buf[256], bufp;
for (p = fmt; *p; p++) {
if (*p != '%') {
xputc(*p, stream);
continue;
} else {
p++;
goto FLAGS;
}
FLAGS:
flags = 0;
while (1) {
switch (*p) {
case '#':
flags |= ALT;
break;
case '0':
flags |= ZERO;
break;
case '+':
flags |= SIGN;
break;
case '-':
flags |= LEFT;
break;
case ' ':
flags |= SPACE;
break;
default:
goto WIDTH;
}
p++;
}
WIDTH:
width = 0;
while (1) {
switch (*p) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
width = width * 10 + *p - '0';
break;
case '*':
width = va_arg(ap, int);
default:
goto PRECISION;
}
p++;
}
PRECISION:
precision = -1;
if (*p == '.') {
precision = 0;
p++;
while (1) {
switch (*p) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
precision = precision * 10 + *p - '0';
break;
case '*':
precision = va_arg(ap, int);
default:
goto LENGTH;
}
p++;
}
}
LENGTH:
length = 0;
switch (*p) {
case 'h':
p++;
length = LEN_H;
goto SPECIFIER;
case 'l':
p++;
length = LEN_L;
goto SPECIFIER;
}
SPECIFIER: /* TODO */
switch (*p) {
case 'd': case 'i':
case 'u': case 'o': case 'x': case 'X':
case 'f': case 'F': case 'e': case 'E':
case 'g': case 'G': case 'a': case 'A':
case 'c':
case 's':
case 'p':
case '%':
default:
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment