📄 dbgformat.c
字号:
/*******************************************************************************
* Copyright: Copyright (c) 2007. Hisilicon Technologies, CO., LTD.
* Version: V300R001B04
* Filename: Format.c
* Description: 实现将带参数打印信息转变为字符串的功能
* History:
1.Created by SunShaoJie on 2007/12/25
*******************************************************************************/
#ifdef DEBUG
#include "sys.h"
//#include <stdlib.h>
/*******************************************************************************
Function: Reverse
Description: 将内存中的数据排序反向
Input: pFirst: 内存反向的开始地址
pLast: 内存反向的结束地址
Output: none
Return: none
*******************************************************************************/
static void Reverse(UINT8 *pFirst, UINT8 *pLast)
{
INT32 swaps;
UINT8 ch;
swaps = (pLast - pFirst + 1) >> 1;
while (swaps--)
{
ch = *pFirst;
*pFirst++ = *pLast;
*pLast-- = ch;
}
}
/*******************************************************************************
Function: GetFormatValue
Description: 从字符串信息中得到宽度信息
Input: pszFormat: 字符串指针,相对于printf的第一个参数
ppArgList: 相对于printf后面的参数
Output: pWidth: 得到的格式宽度
Return: none
*******************************************************************************/
static void GetFormatValue(UINT8 **pszFormat, INT32 *pWidth, va_list *ppArgList)
{
INT32 width = 0;
if (**pszFormat == '*')
{
*pWidth = va_arg(*ppArgList, UINT32);
(*pszFormat)++;
}
else
{
while (**pszFormat >= '0' && **pszFormat <= '9')
{
width = width * 10 + (**pszFormat - '0');
(*pszFormat)++;
}
*pWidth = width;
}
}
/*******************************************************************************
Function: DbgSprintf
Description: 将带参数的打印信息转换成全字符串
Input: pszFormat: 字符串指针,相对于printf的第一个参数
ppArgList: 相对于printf后面的参数
maxChars: 解析后最大的字符串长度
Output: szBuffer: 解析后得到的字符串
Return: 解析后字符串长度
*******************************************************************************/
UINT32 DbgSprintf(UINT8 *szBuffer, UINT8 *szFormat, va_list pArgList, UINT32 maxChars)
{
static UINT8 upch[] = "0123456789ABCDEF";
static UINT8 lowch[] = "0123456789abcdef";
enum { typeNone = 0, typeNumber, typeCh, typeString } type;
enum { modeNone = 0, modeH, modeL} mode;
BOOL padLeft, prefix, sign, upper;
INT32 width, precision, radix, chars;
UINT8 ch, fillCh;
UINT8 *szPos, *szW;
UINT8 *szC;
UINT32 value;
// First check input params
if (szBuffer == NULL || szFormat == NULL || maxChars < 1) return 0;
// Set actual possition
szPos = szBuffer;
// While there is format strings
while (*szFormat != '\0' && maxChars > 0)
{
// If it is other than format prefix, print it and move to next one
if (*szFormat != '%')
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = *szFormat++;
continue;
}
// Set flags to default values
padLeft = FALSE;
prefix = FALSE;
sign = FALSE;
upper = FALSE;
fillCh = ' ';
width = 0;
precision = -1;
type = typeNone;
mode = modeNone;
// read pad left and prefix flags
while (*++szFormat != '\0')
{
if (*szFormat == '-')
{
padLeft = TRUE;
}
else if (*szFormat == '#')
{
prefix = TRUE;
}
else
{
break;
}
}
// find fill character
if (*szFormat == '0')
{
fillCh = '0';
szFormat++;
}
// read the width specification
GetFormatValue(&szFormat, &width, &pArgList);
// read the precision
if (*szFormat == '.')
{
szFormat++;
GetFormatValue(&szFormat, &precision, &pArgList);
}
// get the operand size
if (*szFormat == 'l')
{
mode = modeL;
szFormat++;
}
else if (*szFormat == 'w')
{
mode = modeL;
szFormat++;
}
else if(szFormat[0] == 'I' && szFormat[1] == '3' && szFormat[2] == '2')
{
mode = modeL;
szFormat += 3;
}
else if (*szFormat == 'h')
{
mode = modeH;
szFormat++;
}
// break if there is unexpected format string end
if (*szFormat == '\0') break;
switch (*szFormat)
{
case 'i':
case 'd':
sign = TRUE;
case 'u':
radix = 10;
type = typeNumber;
if (mode == modeNone) mode = modeL;
break;
case 'X':
upper = TRUE;
case 'p':
case 'x':
radix = 16;
type = typeNumber;
if (mode == modeNone) mode = modeL;
break;
case 'c':
if (mode == modeNone) mode = modeL;
type = typeCh;
break;
case 'C':
if (mode == modeNone) mode = modeH;
type = typeCh;
break;
case 'a':
mode = modeH;
type = typeString;
break;
case 'S':
if (mode == modeNone) mode = modeH;
type = typeString;
break;
case 's':
if (mode == modeNone) mode = modeL;
type = typeString;
break;
default:
if (--maxChars <= 0) goto cleanUp;
*szPos++ = *szFormat;
}
// Move to next format character
szFormat++;
switch (type)
{
case typeNumber:
// Special cases to act like MSC v5.10
if (padLeft || precision >= 0) fillCh = ' ';
// Fix possible prefix
if (radix != 16) prefix = FALSE;
// Depending on mode obtain value
if (mode == modeH)
{
if (sign)
{
value = (INT32)va_arg(pArgList, INT16);
}
else
{
value = (UINT32)va_arg(pArgList, UINT16);
}
}
else if (mode == modeL)
{
if (sign)
{
value = (INT32)va_arg(pArgList, INT32);
}
else
{
value = (UINT32)va_arg(pArgList, UINT32);
}
}
else
{
goto cleanUp;
}
// Should sign be printed?
if (sign && (value >= 0x80000000))
{
value = -(INT32)value;
}
else
{
sign = FALSE;
}
// Start with reverse string
szW = szPos;
chars = 0;
do
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = upper ? upch[value%radix] : lowch[value%radix];
chars++;
}while ((value /= radix) != 0 && maxChars > 0);
// Fix sizes
width -= chars;
precision -= chars;
if (precision > 0) width -= precision;
// Fill to the field precision
while (precision-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = '0';
}
if (width > 0 && !padLeft)
{
// If we're filling with spaces, put sign first
if (fillCh != '0')
{
if (sign)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = '-';
width--;
sign = FALSE;
}
if (prefix && radix == 16)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = upper ? 'X' : 'x';
if (--maxChars <= 0) goto cleanUp;
*szW++ = '0';
prefix = FALSE;
}
}
// Leave place for sign
if (sign) width--;
// Fill to the field width
while (width-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = fillCh;
}
// Still have sign?
if (sign)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = '-';
sign = FALSE;
}
// Or prefix?
if (prefix)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = upper ? 'X' : 'x';
if (--maxChars <= 0) goto cleanUp;
*szW++ = '0';
prefix = FALSE;
}
// Now reverse the string in place
Reverse(szPos, szW - 1);
szPos = szW;
}
else
{
// Add the sign character
if (sign)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = '-';
sign = FALSE;
}
if (prefix)
{
if (--maxChars <= 0) goto cleanUp;
*szW++ = upper ? 'X' : 'x';
if (--maxChars <= 0) goto cleanUp;
*szW++ = '0';
prefix = FALSE;
}
// Reverse the string in place
Reverse(szPos, szW - 1);
szPos = szW;
// Pad to the right of the string in case left aligned
while (width-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = fillCh;
}
}
break;
case typeCh:
// Depending on size obtain value
if (mode == modeH)
{
ch = (UINT8)va_arg(pArgList, INT8);
}
else if (mode == modeL)
{
ch = va_arg(pArgList, UINT8);
}
else
{
goto cleanUp;
}
if (--maxChars <= 0) goto cleanUp;
*szPos++ = ch;
break;
case typeString:
if (mode == modeH)
{
// It is ascii string
szC = va_arg(pArgList, UINT8 *);
if (szC == NULL) szC = (UINT8 *)"(NULL)";
// Get string size
chars = 0;
while (chars < maxChars && szC[chars] != '\0') chars++;
// Fix string size
if (precision >= 0 && chars > precision) chars = precision;
width -= chars;
if (padLeft)
{
while (chars-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = (UINT8)*szC++;
}
while (width-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = fillCh;
}
}
else
{
while (width-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = fillCh;
}
while (chars-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = (UINT8)*szC++;
}
}
}
else if (mode == modeL)
{
// It is unicode string
szW = va_arg(pArgList, UINT8 *);
if (szW == NULL) szW = (UINT8 *)"(NULL)";
// Get string size
chars = 0;
while (chars < maxChars && szW[chars] != '\0') chars++;
// Fix string size
if (precision >= 0 && chars > precision) chars = precision;
width -= chars;
if (padLeft)
{
while (chars-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = *szW++;
}
while (width-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = fillCh;
}
}
else
{
while (width-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = fillCh;
}
while (chars-- > 0)
{
if (--maxChars <= 0) goto cleanUp;
*szPos++ = *szW++;
}
}
}
else
{
goto cleanUp;
}
break;
case typeNone:
default:
break;
}
}
cleanUp:
*szPos = '\0';
return (szPos - szBuffer);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -