📄 zprintf.c
字号:
pbRead1 = pbReadFmt;
}
bParseStep = 1, pbRead = pbRead1, pbData1 = *ppbData;
/*----------------------------------------------------------------------*/
/* 解析打印格式串 */
/* 获得格式控制 ...(*a)(-,+,#)(0 )(ddd)(. (ddd))(h,l,L)(f )... */
/* 格式序列: s0 s1 s2 s3 s4 s5 s6 s7 s8 s9 */
/* 格式串最多有8个部分,在相应的状态如果取得的格式串与期望不符就退出 */
/*----------------------------------------------------------------------*/
while (pbRead[0] != '\0')
{
/*------------------------------------------------------------------*/
/* 对齐符,符号显示,以及进制显示控制符不分先后,可有可无 */
/*------------------------------------------------------------------*/
if (pbRead[0] == '-' || pbRead[0] == '+' ||
pbRead[0] == ' ' || pbRead[0] == '#')
{
if (bParseStep > 2)
{
break;
}
if (pbRead[0] == '-' && ptFmt->bRight)
{
ptFmt->bRight = 0;
}
else if (pbRead[0] == '-' && bParseStep == 2 && !ptFmt->bRight)
{
break;
}
if (pbRead[0] == '+' && !ptFmt->bPlus)
{
ptFmt->bPlus = 1;
}
else if (pbRead[0] == ' ' && !ptFmt->bPlus)
{
ptFmt->bPlus = 2;
}
else if (bParseStep == 2 && ptFmt->bPlus &&
(pbRead[0] == '+' || pbRead[0] == ' '))
{
break;
}
if (pbRead[0] == '#' && !ptFmt->bAlter)
{
ptFmt->bAlter = 1;
}
else if (pbRead[0] == '#' && bParseStep == 2 && ptFmt->bAlter)
{
break;
}
bParseStep = 2; pbRead++; continue;
}
if (pbRead[0] == '0')
{
if (bParseStep <= 2)
{
ptFmt->bPreChar = pbRead[0];
}
else if (bParseStep != 5)
{
break;
}
bParseStep = 3; pbRead++; continue;
}
if (pbRead[0] == '.')
{
if (bParseStep >= 5)
{
break;
}
bParseStep = 5; pbRead++; continue;
}
/*------------------------------------------------------------------*/
/* 取数据宽度,对于格式'*',从参数列表中取宽度指示(只认一个字节) */
/*------------------------------------------------------------------*/
if (pbRead[0] == '*' || isDigit(pbRead[0]))
{
/*--------------------------------------------------------------*/
/* 取得数据长度,从格式串中取或者从参数列表中取 */
/*--------------------------------------------------------------*/
if (pbRead[0] == '*' && ptFmt->bBigEndian)
{
lFmtVal = (LONG)(*(char *)(*ppbData)++);
lFmtVal = (lFmtVal << 8) + (LONG)(*(char *)(*ppbData)++);
lFmtVal = (lFmtVal << 8) + (LONG)(*(char *)(*ppbData)++);
lFmtVal = (lFmtVal << 8) + (LONG)(*(char *)(*ppbData)++);
pbRead++;
}
else if (pbRead[0] == '*'/* && ptFmt->bBigEndian */)
{
lFmtVal = (LONG)(*(char *)(*ppbData)++);
lFmtVal += (LONG)((*(char *)(*ppbData)++) << 8);
lFmtVal += (LONG)((*(char *)(*ppbData)++) << 16);
lFmtVal += (LONG)((*(char *)(*ppbData)++) << 24);
pbRead++;
}
else for (lFmtVal = 0; isDigit(pbRead[0]); pbRead++)
{
lFmtVal = lFmtVal * 10 + (pbRead[0] - '0');
}
/*--------------------------------------------------------------*/
/* 将长度赋给宽度或者精度 */
/*--------------------------------------------------------------*/
if (bParseStep < 4 && lFmtVal < 0)
{
ptFmt->bRight = 0;
lFmtVal = -lFmtVal;
}
if (bParseStep < 4)
{
ptFmt->lWidth = lFmtVal, bParseStep = 4;
}
else if (bParseStep == 5)
{
ptFmt->lPrecision = lFmtVal, bParseStep = 6;
}
else
{
break;
}
continue;
} /* if (pbRead[0] == '*' || isDigit(pbRead[0])) */
/*------------------------------------------------------------------*/
/* 检验长度模式 */
/*------------------------------------------------------------------*/
if ((bParseStep < 7) &&
(pbRead[0] == 'h' || pbRead[0] == 'l' || pbRead[0] == 'L'))
{
ptFmt->bMod = pbRead[0];
bParseStep = 7; pbRead++; continue;
}
if (isFormatEndChar(pbRead[0]))
{
ptFmt->bType = *pbRead++, bParseStep = 8;
ptFmt->bSubType = pbRead[0];
if (ptFmt->bType == 'z' && pbRead[0] != '\0')
{
pbRead++;
}
break;
}
else if (0 /* isPermitSpace(pbRead[0]) */)
{
pbRead++; continue;
}
break;
} /* while (pbRead[0] != '\0') */
/*----------------------------------------------------------------------*/
/* 未找到数据声明格式,则取消所有的格式指定,将'%'后的字符原样输出 */
/*----------------------------------------------------------------------*/
if (bParseStep < 7)
{
if (!ptFmt->bTakeDataDecl)
{
ptFmt->bType = '\0', *ppbData = pbData, pbRead = pbReadFmt;
}
else
{
if (ptFmt->bPlus || ptFmt->bAlter ||
ptFmt->bMod != 'l' || !ptFmt->bRight)
{
*ppbData = pbData1, pbRead = pbRead1;
}
ptFmt->bPlus = 0;
ptFmt->bAlter = 0;
ptFmt->bMod = '\0';
ptFmt->bRight = 1;
}
}
return pbRead;
}
/*--------------------------------------------------------------------------*/
/* Function name : PutChar() */
/* Description : 向特定内存输出一个字符 */
/* Input : pbOutLoc - 打印字符输出地址 */
/* : wBufLen - 打印缓存长度 */
/* : bChar - 待输出字符(可以是宽字符格式) */
/* : wNum - 重复输出字符个数 */
/* Output : none */
/* Return : 打印的字符个数 */
/* Global Var : 无 */
/* Author/Date : R.Z.Tian/2003-5-21 16:16 */
/* Note : */
/*--------------------------------------------------------------------------*/
static ULONG PutChar(BYTE *pbOutLoc, WORD wBufLen, BYTE bChar, WORD wNum)
{
BYTE *pbPut = pbOutLoc;
ULONG dwPrnCount = 0;
while (dwPrnCount < wNum && dwPrnCount < wBufLen)
{
*pbPut++ = bChar, dwPrnCount++;
}
pbPut[0] = '\0';
dwPrnCount = pbPut - pbOutLoc;
return dwPrnCount;
}
/*--------------------------------------------------------------------------*/
/* Function name : PutString() */
/* Description : 向特定内存输出一个字符串 */
/* Input : pbOutLoc - 打印字符输出地址 */
/* : wBufLen - 打印缓存长度 */
/* : pbString - 待输出字符串 */
/* : wStrLen - 字符串长度 */
/* Output : none */
/* Return : 打印的字符个数 */
/* Global Var : 无 */
/* Author/Date : R.Z.Tian/2003-5-21 16:16 */
/* Note : */
/*--------------------------------------------------------------------------*/
static ULONG PutString(BYTE *pbOutLoc, WORD wBufLen, BYTE *pbString, WORD wStrLen)
{
BYTE *pbPut = pbOutLoc, *pbRead = pbString;
ULONG dwPrnCount = 0;
while (pbRead[0] != '\0' && dwPrnCount < wBufLen && dwPrnCount < wStrLen)
{
*pbPut++ = *pbRead++, dwPrnCount++;
}
pbPut[0] = '\0';
return dwPrnCount;
}
/*--------------------------------------------------------------------------*/
/* Function name : OutPutWidthCheck() */
/* Description : 进行宽度校验并输出本体字符串 */
/* Input : pbOutLoc - 输出字符起始地址 */
/* : wBufLen - 打印缓存长度 */
/* : ppbS - 本体字符输出起始地址 */
/* : ppbE - 本体字符输出终止地址 */
/* : pbFmt - 打印格式控制结构 */
/* : pbPromtStr - 提示符地址 */
/* : wPromtLen - 提示符长度 */
/* Output : ppbBegin - 输出起始地址 */
/* : ppbEnd - 输出终止地址 */
/* Return : 未补足的字符个数 */
/* Global Var : 无 */
/* Author/Date : R.Z.Tian/2003-5-21 9:24 */
/* Note : 左对齐,左边数字或字符有效,右对齐,右边数字或字符有效 */
/*--------------------------------------------------------------------------*/
ULONG OutPutWidthCheck(BYTE *pbOutLoc, WORD wBufLen, BYTE *pbOutBeg,
BYTE *pbOutEnd, PrnFormatGlb_T *ptFmt, BYTE *pbPromtStr, WORD wPromtLen)
{
ULONG dwPrnCount = 0; /* 打印字符个数 */
WORD wWidthPad = 0; /* 数据宽度补充长度 */
WORD wPrecPad = 0; /* 数据精度补充长度 */
if (!ptFmt->bPlus)
{
pbPromtStr[0] = '\0', wPromtLen = 0;
}
/*----------------------------------------------------------------------*/
/* 按照格式符调整取得的数据 */
/*----------------------------------------------------------------------*/
if (ptFmt->lPrecision > 0 && ptFmt->lWidth < ptFmt->lPrecision + wPromtLen)
{
ptFmt->lWidth = ptFmt->lPrecision + wPromtLen;
}
/*----------------------------------------------------------------------*/
/* 无输出宽度,直接返回 */
/*----------------------------------------------------------------------*/
if (ptFmt->lWidth == 0)
{
return dwPrnCount;
}
/*----------------------------------------------------------------------*/
/* 无输出宽度限制,将提示字符及本体字符串直接输出 */
/*----------------------------------------------------------------------*/
else if (ptFmt->lWidth < 0)
{
goto PrnWidthCheckOutPut_ToDo;
}
/*----------------------------------------------------------------------*/
/* 要输出提示字符串但限定宽度却比提示字符串宽度短,打印部分提示符 */
/*----------------------------------------------------------------------*/
if (ptFmt->lWidth < wPromtLen)
{
dwPrnCount = PutString(
pbOutLoc, (WORD)min(wPromtLen, wBufLen), pbPromtStr, wPromtLen);
return dwPrnCount;
}
/*----------------------------------------------------------------------*/
/* 实际输出长度比限定长度超长,根据对齐方式在前面或后面选择打上'*'标记 */
/*----------------------------------------------------------------------*/
if (ptFmt->lWidth < wPromtLen + (pbOutEnd - pbOutBeg))
{
if (ptFmt->bAlter)
{
goto PrnWidthCheckOutPut_ToDo;
}
if (ptFmt->bRight)
{
pbOutBeg = pbOutEnd + wPromtLen - ptFmt->lWidth;
pbOutBeg[0] = '*';
}
else
{
pbOutEnd = pbOutBeg - wPromtLen + ptFmt->lWidth;
*(pbOutEnd - 1) = '*';
}
goto PrnWidthCheckOutPut_ToDo;
}
/*----------------------------------------------------------------------*/
/* 计算数据填充长度 */
/*----------------------------------------------------------------------*/
if ((ptFmt->lPrecision > 0) && (ptFmt->lPrecision > pbOutEnd - pbOutBeg))
{
wPrecPad = (WORD)((pbOutEnd - pbOutBeg) - ptFmt->lPrecision);
}
wWidthPad = (WORD)(ptFmt->lWidth - (wPromtLen + (pbOutEnd - pbOutBeg)));
/*----------------------------------------------------------------------*/
/* 含宽度校准的输出, 含6步, 其中1, 3不并选, 2, 6不并选: */
/* (1, Prompt)(2, WidthPad)(3, Promt)(4, PreciPad)(5, Body)(6, WidthPad)*/
/*----------------------------------------------------------------------*/
PrnWidthCheckOutPut_ToDo:
if (!ptFmt->bRight || ptFmt->bAlter) /* 1*/
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -