📄 lcformat.c
字号:
*szOut-- = *lpszNeg--;
break;
}
}
szOut++;
iRet = strlenW(szOut) + 1;
if (cchOut)
{
if (iRet <= cchOut)
memcpy(lpNumberStr, szOut, iRet * sizeof(WCHAR));
else
{
memcpy(lpNumberStr, szOut, cchOut * sizeof(WCHAR));
lpNumberStr[cchOut - 1] = '\0';
SetLastError(ERROR_INSUFFICIENT_BUFFER);
iRet = 0;
}
}
return iRet;
}
/**************************************************************************
* GetCurrencyFormatA (KERNEL32.@)
*
* Format a currency string for a given locale.
*
* PARAMS
* lcid [I] Locale to format for
* dwFlags [I] LOCALE_ flags from "winnls.h"
* lpszValue [I] String to format
* lpFormat [I] Formatting overrides
* lpCurrencyStr [O] Destination for formatted string
* cchOut [I] Size of lpCurrencyStr, or 0 to calculate the resulting size
*
* NOTES
* - lpszValue can contain only '0' - '9', '-' and '.'.
* - If lpFormat is non-NULL, dwFlags must be 0. In this case lpszValue will
* be formatted according to the format details returned by GetLocaleInfoA().
* - This function rounds the currency if the number of decimals exceeds the
* locales number of currency decimal places.
* - If cchOut is 0, this function does not write to lpCurrencyStr.
* - The Ascii version of this function fails if lcid is Unicode only.
*
* RETURNS
* Success: The number of character written to lpNumberStr, or that would
* have been written, if cchOut is 0.
* Failure: 0. Use GetLastError() to determine the cause.
*/
INT WINAPI GetCurrencyFormatA(LCID lcid, DWORD dwFlags,
LPCSTR lpszValue, const CURRENCYFMTA *lpFormat,
LPSTR lpCurrencyStr, int cchOut)
{
DWORD cp = CP_ACP;
WCHAR szDec[8], szGrp[8], szCy[8], szIn[128], szOut[128];
CURRENCYFMTW fmt;
const CURRENCYFMTW *pfmt = NULL;
INT iRet;
TRACE("(0x%04lx,0x%08lx,%s,%p,%p,%d)\n", lcid, dwFlags, lpszValue,
lpFormat, lpCurrencyStr, cchOut);
if (NLS_IsUnicodeOnlyLcid(lcid))
{
GetCurrencyFormatA_InvalidParameter:
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (!(dwFlags & LOCALE_USE_CP_ACP))
{
const NLS_FORMAT_NODE *node = NLS_GetFormats(lcid, dwFlags);
if (!node)
goto GetCurrencyFormatA_InvalidParameter;
cp = node->dwCodePage;
}
if (lpFormat)
{
memcpy(&fmt, lpFormat, sizeof(fmt));
pfmt = &fmt;
if (lpFormat->lpDecimalSep)
{
MultiByteToWideChar(cp, 0, lpFormat->lpDecimalSep, -1, szDec, sizeof(szDec)/sizeof(WCHAR));
fmt.lpDecimalSep = szDec;
}
if (lpFormat->lpThousandSep)
{
MultiByteToWideChar(cp, 0, lpFormat->lpThousandSep, -1, szGrp, sizeof(szGrp)/sizeof(WCHAR));
fmt.lpThousandSep = szGrp;
}
if (lpFormat->lpCurrencySymbol)
{
MultiByteToWideChar(cp, 0, lpFormat->lpCurrencySymbol, -1, szCy, sizeof(szCy)/sizeof(WCHAR));
fmt.lpCurrencySymbol = szCy;
}
}
if (lpszValue)
MultiByteToWideChar(cp, 0, lpszValue, -1, szIn, sizeof(szIn)/sizeof(WCHAR));
if (cchOut > (int)(sizeof(szOut)/sizeof(WCHAR)))
cchOut = sizeof(szOut)/sizeof(WCHAR);
szOut[0] = '\0';
iRet = GetCurrencyFormatW(lcid, dwFlags, lpszValue ? szIn : NULL, pfmt,
lpCurrencyStr ? szOut : NULL, cchOut);
if (szOut[0] && lpCurrencyStr)
WideCharToMultiByte(cp, 0, szOut, -1, lpCurrencyStr, cchOut, 0, 0);
return iRet;
}
/* Formatting states for Currencies. We use flags to avoid code duplication. */
#define CF_PARENS 0x1 /* Parentheses */
#define CF_MINUS_LEFT 0x2 /* '-' to the left */
#define CF_MINUS_RIGHT 0x4 /* '-' to the right */
#define CF_MINUS_BEFORE 0x8 /* '-' before '$' */
#define CF_CY_LEFT 0x10 /* '$' to the left */
#define CF_CY_RIGHT 0x20 /* '$' to the right */
#define CF_CY_SPACE 0x40 /* ' ' by '$' */
/**************************************************************************
* GetCurrencyFormatW (KERNEL32.@)
*
* See GetCurrencyFormatA.
*/
INT WINAPI GetCurrencyFormatW(LCID lcid, DWORD dwFlags,
LPCWSTR lpszValue, const CURRENCYFMTW *lpFormat,
LPWSTR lpCurrencyStr, int cchOut)
{
static const BYTE NLS_NegCyFormats[16] =
{
CF_PARENS|CF_CY_LEFT, /* ($1.1) */
CF_MINUS_LEFT|CF_MINUS_BEFORE|CF_CY_LEFT, /* -$1.1 */
CF_MINUS_LEFT|CF_CY_LEFT, /* $-1.1 */
CF_MINUS_RIGHT|CF_CY_LEFT, /* $1.1- */
CF_PARENS|CF_CY_RIGHT, /* (1.1$) */
CF_MINUS_LEFT|CF_CY_RIGHT, /* -1.1$ */
CF_MINUS_RIGHT|CF_MINUS_BEFORE|CF_CY_RIGHT, /* 1.1-$ */
CF_MINUS_RIGHT|CF_CY_RIGHT, /* 1.1$- */
CF_MINUS_LEFT|CF_CY_RIGHT|CF_CY_SPACE, /* -1.1 $ */
CF_MINUS_LEFT|CF_MINUS_BEFORE|CF_CY_LEFT|CF_CY_SPACE, /* -$ 1.1 */
CF_MINUS_RIGHT|CF_CY_RIGHT|CF_CY_SPACE, /* 1.1 $- */
CF_MINUS_RIGHT|CF_CY_LEFT|CF_CY_SPACE, /* $ 1.1- */
CF_MINUS_LEFT|CF_CY_LEFT|CF_CY_SPACE, /* $ -1.1 */
CF_MINUS_RIGHT|CF_MINUS_BEFORE|CF_CY_RIGHT|CF_CY_SPACE, /* 1.1- $ */
CF_PARENS|CF_CY_LEFT|CF_CY_SPACE, /* ($ 1.1) */
CF_PARENS|CF_CY_RIGHT|CF_CY_SPACE, /* (1.1 $) */
};
static const BYTE NLS_PosCyFormats[4] =
{
CF_CY_LEFT, /* $1.1 */
CF_CY_RIGHT, /* 1.1$ */
CF_CY_LEFT|CF_CY_SPACE, /* $ 1.1 */
CF_CY_RIGHT|CF_CY_SPACE, /* 1.1 $ */
};
WCHAR szBuff[128], *szOut = szBuff + sizeof(szBuff) / sizeof(WCHAR) - 1;
WCHAR szNegBuff[8];
const WCHAR *lpszNeg = NULL, *lpszNegStart, *szSrc, *lpszCy, *lpszCyStart;
DWORD dwState = 0, dwDecimals = 0, dwGroupCount = 0, dwCurrentGroupCount = 0, dwFmt;
INT iRet;
TRACE("(0x%04lx,0x%08lx,%S,%p,%p,%d)\n", lcid, dwFlags, lpszValue,
lpFormat, lpCurrencyStr, cchOut);
if (!lpszValue || cchOut < 0 || (cchOut > 0 && !lpCurrencyStr) ||
!IsValidLocale(lcid, 0) ||
(lpFormat && (dwFlags || !lpFormat->lpDecimalSep || !lpFormat->lpThousandSep ||
!lpFormat->lpCurrencySymbol || lpFormat->NegativeOrder > 15 ||
lpFormat->PositiveOrder > 3)))
{
GetCurrencyFormatW_Error:
SetLastError(lpFormat && dwFlags ? ERROR_INVALID_FLAGS : ERROR_INVALID_PARAMETER);
return 0;
}
if (!lpFormat)
{
const NLS_FORMAT_NODE *node = NLS_GetFormats(lcid, dwFlags);
if (!node)
goto GetCurrencyFormatW_Error;
lpFormat = &node->cyfmt;
lpszNegStart = lpszNeg = GetNegative(node);
}
else
{
GetLocaleInfoW(lcid, LOCALE_SNEGATIVESIGN|(dwFlags & LOCALE_NOUSEROVERRIDE),
szNegBuff, sizeof(szNegBuff)/sizeof(WCHAR));
lpszNegStart = lpszNeg = szNegBuff;
}
dwFlags &= (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
lpszNeg = lpszNeg + strlenW(lpszNeg) - 1;
lpszCyStart = lpFormat->lpCurrencySymbol;
lpszCy = lpszCyStart + strlenW(lpszCyStart) - 1;
/* Format the currency backwards into a temporary buffer */
szSrc = lpszValue;
*szOut-- = '\0';
/* Check the number for validity */
while (*szSrc)
{
if (*szSrc >= '0' && *szSrc <= '9')
{
dwState |= NF_DIGITS;
if (dwState & NF_ISREAL)
dwDecimals++;
}
else if (*szSrc == '-')
{
if (dwState)
goto GetCurrencyFormatW_Error; /* '-' not first character */
dwState |= NF_ISNEGATIVE;
}
else if (*szSrc == '.')
{
if (dwState & NF_ISREAL)
goto GetCurrencyFormatW_Error; /* More than one '.' */
dwState |= NF_ISREAL;
}
else
goto GetCurrencyFormatW_Error; /* Invalid char */
szSrc++;
}
szSrc--; /* Point to last character */
if (!(dwState & NF_DIGITS))
goto GetCurrencyFormatW_Error; /* No digits */
if (dwState & NF_ISNEGATIVE)
dwFmt = NLS_NegCyFormats[lpFormat->NegativeOrder];
else
dwFmt = NLS_PosCyFormats[lpFormat->PositiveOrder];
/* Add any trailing negative or currency signs */
if (dwFmt & CF_PARENS)
*szOut-- = ')';
while (dwFmt & (CF_MINUS_RIGHT|CF_CY_RIGHT))
{
switch (dwFmt & (CF_MINUS_RIGHT|CF_MINUS_BEFORE|CF_CY_RIGHT))
{
case CF_MINUS_RIGHT:
case CF_MINUS_RIGHT|CF_CY_RIGHT:
while (lpszNeg >= lpszNegStart)
*szOut-- = *lpszNeg--;
dwFmt &= ~CF_MINUS_RIGHT;
break;
case CF_CY_RIGHT:
case CF_MINUS_BEFORE|CF_CY_RIGHT:
case CF_MINUS_RIGHT|CF_MINUS_BEFORE|CF_CY_RIGHT:
while (lpszCy >= lpszCyStart)
*szOut-- = *lpszCy--;
if (dwFmt & CF_CY_SPACE)
*szOut-- = ' ';
dwFmt &= ~(CF_CY_RIGHT|CF_MINUS_BEFORE);
break;
}
}
/* Copy all digits up to the decimal point */
if (!lpFormat->NumDigits)
{
if (dwState & NF_ISREAL)
{
while (*szSrc != '.') /* Don't write any decimals or a separator */
{
if (*szSrc >= '5' || (*szSrc == '4' && (dwState & NF_ROUND)))
dwState |= NF_ROUND;
else
dwState &= ~NF_ROUND;
szSrc--;
}
szSrc--;
}
}
else
{
LPWSTR lpszDec = lpFormat->lpDecimalSep + strlenW(lpFormat->lpDecimalSep) - 1;
if (dwDecimals <= lpFormat->NumDigits)
{
dwDecimals = lpFormat->NumDigits - dwDecimals;
while (dwDecimals--)
*szOut-- = '0'; /* Pad to correct number of dp */
}
else
{
dwDecimals -= lpFormat->NumDigits;
/* Skip excess decimals, and determine if we have to round the number */
while (dwDecimals--)
{
if (*szSrc >= '5' || (*szSrc == '4' && (dwState & NF_ROUND)))
dwState |= NF_ROUND;
else
dwState &= ~NF_ROUND;
szSrc--;
}
}
if (dwState & NF_ISREAL)
{
while (*szSrc != '.')
{
if (dwState & NF_ROUND)
{
if (*szSrc == '9')
*szOut-- = '0'; /* continue rounding */
else
{
dwState &= ~NF_ROUND;
*szOut-- = (*szSrc)+1;
}
szSrc--;
}
else
*szOut-- = *szSrc--; /* Write existing decimals */
}
szSrc--; /* Skip '.' */
}
while (lpszDec >= lpFormat->lpDecimalSep)
*szOut-- = *lpszDec--; /* Write decimal separator */
}
dwGroupCount = lpFormat->Grouping;
/* Write the remaining whole number digits, including grouping chars */
while (szSrc >= lpszValue && *szSrc >= '0' && *szSrc <= '9')
{
if (dwState & NF_ROUND)
{
if (*szSrc == '9')
*szOut-- = '0'; /* continue rounding */
else
{
dwState &= ~NF_ROUND;
*szOut-- = (*szSrc)+1;
}
szSrc--;
}
else
*szOut-- = *szSrc--;
dwState |= NF_DIGITS_OUT;
dwCurrentGroupCount++;
if (szSrc >= lpszValue && dwCurrentGroupCount == dwGroupCount)
{
LPWSTR lpszGrp = lpFormat->lpThousandSep + strlenW(lpFormat->lpThousandSep) - 1;
while (lpszGrp >= lpFormat->lpThousandSep)
*szOut-- = *lpszGrp--; /* Write grouping char */
dwCurrentGroupCount = 0;
}
}
if (dwState & NF_ROUND)
*szOut-- = '1'; /* e.g. .6 > 1.0 */
else if (!(dwState & NF_DIGITS_OUT) && lpFormat->LeadingZero)
*szOut-- = '0'; /* Add leading 0 if we have no digits before the decimal point */
/* Add any leading negative or currency sign */
while (dwFmt & (CF_MINUS_LEFT|CF_CY_LEFT))
{
switch (dwFmt & (CF_MINUS_LEFT|CF_MINUS_BEFORE|CF_CY_LEFT))
{
case CF_MINUS_LEFT:
case CF_MINUS_LEFT|CF_CY_LEFT:
while (lpszNeg >= lpszNegStart)
*szOut-- = *lpszNeg--;
dwFmt &= ~CF_MINUS_LEFT;
break;
case CF_CY_LEFT:
case CF_CY_LEFT|CF_MINUS_BEFORE:
case CF_MINUS_LEFT|CF_MINUS_BEFORE|CF_CY_LEFT:
if (dwFmt & CF_CY_SPACE)
*szOut-- = ' ';
while (lpszCy >= lpszCyStart)
*szOut-- = *lpszCy--;
dwFmt &= ~(CF_CY_LEFT|CF_MINUS_BEFORE);
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -