Duane Ellis <openocd@duaneellis.com>: This simple patch adds precision support to JIM's format command.

git-svn-id: svn://svn.berlios.de/openocd/trunk@767 b42882b7-edfa-0310-969c-e2dbd0fdcd60
This commit is contained in:
oharboe 2008-07-07 18:57:27 +00:00
parent 994396da59
commit cfba2f4a79
1 changed files with 45 additions and 7 deletions

View File

@ -2180,6 +2180,10 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
int spad; int spad;
int altfm; int altfm;
int forceplus; int forceplus;
int prec;
int inprec;
int haveprec;
int accum;
while (*fmt != '%' && fmtLen) { while (*fmt != '%' && fmtLen) {
fmt++; fmtLen--; fmt++; fmtLen--;
@ -2194,6 +2198,9 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
ljust = 0; ljust = 0;
altfm = 0; altfm = 0;
forceplus = 0; forceplus = 0;
inprec = 0;
haveprec = 0;
prec = -1; /* not found yet */
next_fmt: next_fmt:
if( fmtLen <= 0 ){ if( fmtLen <= 0 ){
break; break;
@ -2238,6 +2245,11 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
*fmt++; fmtLen--; *fmt++; fmtLen--;
goto next_fmt; goto next_fmt;
case '.':
inprec = 1;
*fmt++; fmtLen--;
goto next_fmt;
break;
case '1': case '1':
case '2': case '2':
case '3': case '3':
@ -2247,11 +2259,17 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
case '7': case '7':
case '8': case '8':
case '9': case '9':
width = 0; accum = 0;
while( isdigit(*fmt) && (fmtLen > 0) ){ while( isdigit(*fmt) && (fmtLen > 0) ){
width = (width * 10) + (*fmt - '0'); accum = (accum * 10) + (*fmt - '0');
fmt++; fmtLen--; fmt++; fmtLen--;
} }
if( inprec ){
haveprec = 1;
prec = accum;
} else {
width = accum;
}
goto next_fmt; goto next_fmt;
case '*': case '*':
/* suck up the next item as an integer */ /* suck up the next item as an integer */
@ -2264,10 +2282,20 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
Jim_FreeNewObj(interp, resObjPtr ); Jim_FreeNewObj(interp, resObjPtr );
return NULL; return NULL;
} }
width = wideValue; if( inprec ){
if( width < 0 ){ haveprec = 1;
ljust = 1; prec = wideValue;
width = -width; if( prec < 0 ){
/* man 3 printf says */
/* if prec is negative, it is zero */
prec = 0;
}
} else {
width = wideValue;
if( width < 0 ){
ljust = 1;
width = -width;
}
} }
objv++; objv++;
goto next_fmt; goto next_fmt;
@ -2313,6 +2341,16 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
/* skip ahead */ /* skip ahead */
cp = strchr(cp,0); cp = strchr(cp,0);
} }
/* did we find a period? */
if( inprec ){
/* then add it */
*cp++ = '.';
/* did something occur after the period? */
if( haveprec ){
sprintf( cp, "%d", prec );
}
cp = strchr(cp,0);
}
*cp = 0; *cp = 0;
/* here we do the work */ /* here we do the work */