📄 wsprintf.cpp
字号:
out(*lpT++);
while (width-- > 0)
out(fillch);
} else {
while (width-- > 0)
out(fillch);
while (cch--)
out(*lpT++);
}
}
break;
default:
normalch:
out(*lpFmt);
break;
} /* END OF SWITCH(*lpFmt) */
} /* END OF IF(%) */ else
goto normalch; /* character not a '%', just do it */
/*
* advance to next format string character
*/
lpFmt++;
} /* END OF OUTER WHILE LOOP */
errorout:
*lpOut = 0;
return WSPRINTF_LIMIT - cchLimit;
}
/***************************************************************************\
* StringPrintfA (API)
*
* Windows version of sprintf
*
* History:
* 11-12-90 MikeHar Ported from windows 3
* 02-05-90 DarrinM Cleaned up with STDARG.h vararg stuff.
\***************************************************************************/
int WINAPIV wsprintfA(
LPSTR lpOut,
LPCSTR lpFmt,
...)
{
va_list arglist;
int ret;
va_start(arglist, lpFmt);
ret = xwvsprintf(lpOut, lpFmt, arglist);
va_end(arglist);
return ret;
}
#if defined(NEVER)
int WINAPIV _wsprintf(
LPWSTR pwszOut,
LPSTR lpFmt,
...)
{
va_list arglist;
int ret;
LPSTR psz;
va_start(arglist, lpFmt);
// temporarily use pwszOut to store the ANSI string.
ret = xwvsprintf((LPSTR)pwszOut, lpFmt, arglist);
va_end(arglist);
// Now we know the length, we can duplicate the ANSI string.
psz = (LPSTR)LocalAlloc(LMEM_FIXED, ret + 1);
if (!psz) {
return 0;
}
strcpy(psz, (LPSTR)pwszOut);
ret = MBToWCSEx(0, psz, -1, &pwszOut, (ret +1) * sizeof(WCHAR), FALSE);
LocalFree(psz);
return ret;
}
#endif
/***************************************************************************\
* SP_PutNumberW
*
* Takes an unsigned long integer and places it into a buffer, respecting
* a buffer limit, a radix, and a case select (upper or lower, for hex).
*
*
* History:
* 11-12-90 MikeHar Ported from windows 3 asm --> C
* 12-11-90 GregoryW need to increment lpstr after assignment of mod
* 02-11-92 GregoryW temporary version until we have C runtime support
\***************************************************************************/
int SP_PutNumberW(
LPWSTR lpstr,
DWORD n,
int limit,
DWORD radix,
int uppercase)
{
DWORD mod;
int count = 0;
if(uppercase)
uppercase = 'A'-'0'-10;
else
uppercase = 'a'-'0'-10;
if (count <= limit) {
do {
mod = n % radix;
n /= radix;
mod += '0';
if (mod > '9')
mod += uppercase;
*lpstr++ = (WCHAR)mod;
count++;
} while((count <= limit) && n);
}
return count;
}
/***************************************************************************\
* SP_ReverseW
*
* reverses a string in place
*
* History:
* 11-12-90 MikeHar Ported from windows 3 asm --> C
* 12-11-90 GregoryW fixed boundary conditions; removed count
* 02-11-92 GregoryW temporary version until we have C runtime support
\***************************************************************************/
void SP_ReverseW(
LPWSTR lpFirst,
LPWSTR lpLast)
{
WCHAR ch;
while(lpLast > lpFirst){
ch = *lpFirst;
*lpFirst++ = *lpLast;
*lpLast-- = ch;
}
}
/***************************************************************************\
* wvsprintfW (API)
*
* wsprintfW() calls this function.
*
* History:
* 11-Feb-1992 GregoryW copied xwvsprintf
* Temporary hack until we have C runtime support
\***************************************************************************/
#if defined(NEVER)
int wvsprintfW(
LPWSTR lpOut,
LPCWSTR lpFmt,
va_list arglist)
{
WCHAR prefix, fillch;
int left, width, prec, size, sign, radix, upper, hprefix;
int cchLimit = WSPRINTF_LIMIT, cch;
LPWSTR lpT;
LPBYTE psz;
va_list varglist = arglist;
union {
long l;
unsigned long ul;
WCHAR wsz[sizeof(long)/sizeof(WCHAR)];
} val;
while (*lpFmt != 0) {
if (*lpFmt == L'%') {
/*
* read the flags. These can be in any order
*/
left = 0;
prefix = 0;
while (*++lpFmt) {
if (*lpFmt == L'-')
left++;
else if (*lpFmt == L'#')
prefix++;
else
break;
}
/*
* find fill character
*/
if (*lpFmt == L'0') {
fillch = L'0';
lpFmt++;
} else
fillch = L' ';
/*
* read the width specification
*/
lpFmt = SP_GetFmtValueW(lpFmt, &cch);
width = cch;
/*
* read the precision
*/
if (*lpFmt == L'.') {
lpFmt = SP_GetFmtValueW(++lpFmt, &cch);
prec = cch;
} else
prec = -1;
/*
* get the operand size
* default size: size == 0
* long number: size == 1
* wide chars: size == 2
* It may be a good idea to check the value of size when it
* is tested for non-zero below (IanJa)
*/
hprefix = 0;
if ((*lpFmt == L'w') || (*lpFmt == L't')) {
size = 2;
lpFmt++;
} else if (*lpFmt == L'l') {
size = 1;
lpFmt++;
} else {
size = 0;
if (*lpFmt == L'h') {
lpFmt++;
hprefix = 1;
}
}
upper = 0;
sign = 0;
radix = 10;
switch (*lpFmt) {
case 0:
goto errorout;
case L'i':
case L'd':
size=1;
sign++;
/*** FALL THROUGH to case 'u' ***/
case L'u':
/* turn off prefix if decimal */
prefix = 0;
donumeric:
/* special cases to act like MSC v5.10 */
if (left || prec >= 0)
fillch = L' ';
/*
* if size == 1, "%lu" was specified (good);
* if size == 2, "%wu" was specified (bad)
*/
if (size) {
val.l = va_arg(varglist, LONG);
} else if (sign) {
val.l = va_arg(varglist, SHORT);
} else {
val.ul = va_arg(varglist, unsigned);
}
if (sign && val.l < 0L)
val.l = -val.l;
else
sign = 0;
lpT = lpOut;
/*
* blast the number backwards into the user buffer
*/
cch = SP_PutNumberW(lpOut, val.l, cchLimit, radix, upper);
if (!(cchLimit -= cch))
goto errorout;
lpOut += cch;
width -= cch;
prec -= cch;
if (prec > 0)
width -= prec;
/*
* fill to the field precision
*/
while (prec-- > 0)
out(L'0');
if (width > 0 && !left) {
/*
* if we're filling with spaces, put sign first
*/
if (fillch != L'0') {
if (sign) {
sign = 0;
out(L'-');
width--;
}
if (prefix) {
out(prefix);
out(L'0');
prefix = 0;
}
}
if (sign)
width--;
/*
* fill to the field width
*/
while (width-- > 0)
out(fillch);
/*
* still have a sign?
*/
if (sign)
out(L'-');
if (prefix) {
out(prefix);
out(L'0');
}
/*
* now reverse the string in place
*/
SP_ReverseW(lpT, lpOut - 1);
} else {
/*
* add the sign character
*/
if (sign) {
out(L'-');
width--;
}
if (prefix) {
out(prefix);
out(L'0');
}
/*
* reverse the string in place
*/
SP_ReverseW(lpT, lpOut - 1);
/*
* pad to the right of the string in case left aligned
*/
while (width-- > 0)
out(fillch);
}
break;
case L'X':
upper++;
/*** FALL THROUGH to case 'x' ***/
case L'x':
radix = 16;
if (prefix)
if (upper)
prefix = L'X';
else
prefix = L'x';
goto donumeric;
case L'c':
if (!size && !hprefix) {
size = 1; // force WCHAR
}
/*** FALL THROUGH to case 'C' ***/
case L'C':
/*
* if size == 0, "%C" or "%hc" was specified (CHAR);
* if size == 1, "%c" or "%lc" was specified (WCHAR);
* if size == 2, "%wc" or "%tc" was specified (WCHAR)
*/
if (size) {
val.wsz[0] = va_arg(varglist, WCHAR);
val.wsz[1] = 0;
lpT = val.wsz;
goto putwstring;
} else {
val.wsz[0] = (WCHAR)va_arg(varglist, CHAR);
val.wsz[1] = 0;
lpT = val.wsz;
goto putwstring;
}
case L's':
if (!size && !hprefix) {
size = 1; // force LPWSTR
}
/*** FALL THROUGH to case 'S' ***/
case L'S':
/*
* if size == 0, "%S" or "%hs" was specified (LPSTR)
* if size == 1, "%s" or "%ls" was specified (LPWSTR);
* if size == 2, "%ws" or "%ts" was specified (LPWSTR)
*/
if (size) {
lpT = va_arg(varglist, LPWSTR);
putwstring:
cch = wcslen(lpT);
if (prec >= 0 && cch > prec)
cch = prec;
width -= cch;
if (left) {
while (cch--)
out(*lpT++);
while (width-- > 0)
out(fillch);
} else {
while (width-- > 0)
out(fillch);
while (cch--)
out(*lpT++);
}
} else {
psz = va_arg(varglist, LPBYTE);
cch = strlen(psz);
if (prec >= 0 && cch > prec)
cch = prec;
width -= cch;
if (left) {
while (cch--)
out((WCHAR)*psz++);
while (width-- > 0)
out(fillch);
} else {
while (width-- > 0)
out(fillch);
while (cch--)
out((WCHAR)*psz++);
}
}
break;
default:
normalch:
out((WCHAR)*lpFmt);
break;
} /* END OF SWITCH(*lpFmt) */
} /* END OF IF(%) */ else
goto normalch; /* character not a '%', just do it */
/*
* advance to next format string character
*/
lpFmt++;
} /* END OF OUTER WHILE LOOP */
errorout:
*lpOut = 0;
return WSPRINTF_LIMIT - cchLimit;
}
int WINAPIV wsprintfW(
LPWSTR lpOut,
LPCWSTR lpFmt,
...)
{
va_list arglist;
int ret;
va_start(arglist, lpFmt);
ret = wvsprintfW(lpOut, lpFmt, arglist);
va_end(arglist);
return ret;
}
#endif
#endif // UNDER_CE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -