fmt_conv.c

来自「澳洲人写的Cortex,包括uC_IP协议栈」· C语言 代码 · 共 767 行 · 第 1/3 页

C
767
字号
<HTML><HEAD><TITLE>/home/asysweb/public_html/cortex/kernel/src/fmt_conv.c</TITLE></HEAD><BODY><pre><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/*     Copyright (c) 1997-1999 Australian Real Time Embedded Systems     */</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/* PROPRIETARY RIGHTS of Australian Real Time Embedded Systems           */</font><font color="#6920ac">/* are involved in the subject matter of this material. All reproduction,*/</font><font color="#6920ac">/* manufacturing, use, and sales rights pertaining to this subject matter*/</font><font color="#6920ac">/* are governed by the license agreement. The recipient of this software */</font><font color="#6920ac">/* implicitly accepts the terms of the license.                          */</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/************************************************************************** * * FILE NAME * *      fmt_conv.c * * SYSTEM COMPONENT * *      Formatted I/O * * DESCRIPTION * *      This file contains internal conversion function for *      default format specifiers (%c, %c, %d, etc); * * SERVICES * *      frmt_Convert_c  - format %c convertion finction *      frmt_Convert_s  - format %s convertion finction *      frmt_Convert_d  - format %d convertion finction *      frmt_Convert_u  - format %u convertion finction *      frmt_Convert_o  - format %o convertion finction *      frmt_Convert_x  - format %x convertion finction *      frmt_Convert_p  - format %p convertion finction *      frmt_Convert_n  - format %n convertion finction *      frmt_Convert_f  - format %f convertion finction *      frmt_Convert_e  - format %e convertion finction *      frmt_Convert_g  - format %g convertion finction *      frmt_Convert_z  - format %z convertion finction *      frmt_Convert_Z  - format %Z convertion finction *      frmt_Convert_H  - format %H convertion finction *      frmt_Convert_Unknown - unknown (unregistered format) * *      frmt_PutDec - emit decimal number *      frmt_PutStr - emit string *      frmt_PutCharPrintable - emit character in readable representation *      frmt_PutStrPrintable - emit string in readable representation * * SCCS HISTORY * *      @(#)fmt_conv.c  1.5, 03/06/00, 19:21:45 * * AUTHOR * *      Vadim N. Azarovsky * * CREATED * *      23-Mar-1997         by Vadim N. Azarovsky * * REVISION HISTORY * *      NAME                DATE                    REMARKS * **************************************************************************/</font><b><font color='DarkGreen'>#include</font></b> <a href="fmt_defs.h.FIND-INC"><font color="blue">"fmt_defs.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="fmt_priv.h.FIND-INC"><font color="blue">"fmt_priv.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="ass_defs.h.FIND-INC"><font color="blue">"ass_defs.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="limits.h.FIND-INC">&lt;<font color="blue">limits.h</font>&gt;</a><b><font color='DarkGreen'>#include</font></b> <a href="string.h.FIND-INC">&lt;<font color="blue">string.h</font>&gt;</a><font color="#6920ac">/* specifier '%c' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_c">frmt_Convert_c</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Int_t</i> n = 0;    <a href="CRTX_ASSERT.FIND-DEF">CRTX_ASSERT</a>(pFmt_a)    <a href="CRTX_ASSERT.FIND-DEF">CRTX_ASSERT</a>(pFmt_a-&gt;pPut)    <b>if</b> (pFmt_a-&gt;Width == <a href="INT_MIN.FIND-DEF">INT_MIN</a>)        pFmt_a-&gt;Width = 0;    <b>if</b> (pFmt_a-&gt;Width &lt; 0) {        pFmt_a-&gt;Width = - pFmt_a-&gt;Width;        <a href="vbit_Put.FIND-FUNC">vbit_Put</a>(&amp;pFmt_a-&gt;Flags, <font color="DarkGreen">'-'</font>, 1);    }    <b>if</b> (<a href="vbit_Get.FIND-FUNC">vbit_Get</a>(&amp;pFmt_a-&gt;Flags, <font color="DarkGreen">'-'</font>)==0)       <a href="FRMT_PAD.FIND-DEF">FRMT_PAD</a>(pFmt_a, pFmt_a-&gt;Width-1, <font color="DarkGreen">' '</font>, n);    <b>if</b> (pFmt_a-&gt;<a href="pPut.FIND-FUNC">pPut</a>(<a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>int</i>), pFmt_a-&gt;pEnv)&lt;0)       <b>return</b> -1;    n++;    <b>if</b> (<a href="vbit_Get.FIND-FUNC">vbit_Get</a>(&amp;pFmt_a-&gt;Flags, <font color="DarkGreen">'-'</font>)==1)       <a href="FRMT_PAD.FIND-DEF">FRMT_PAD</a>(pFmt_a, pFmt_a-&gt;Width-1, <font color="DarkGreen">' '</font>, n);    <b>return</b> n;}<font color="#6920ac">/* specifier '%z' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_z">frmt_Convert_z</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Int_t</i>  n1 = 0, n2;    <i>crtx_Char_t</i> Char;    <a href="CRTX_ASSERT.FIND-DEF">CRTX_ASSERT</a>(pFmt_a)    <a href="CRTX_ASSERT.FIND-DEF">CRTX_ASSERT</a>(pFmt_a-&gt;pPut)    <b>if</b> (pFmt_a-&gt;Width == <a href="INT_MIN.FIND-DEF">INT_MIN</a>)        pFmt_a-&gt;Width = 0;    <b>if</b> (pFmt_a-&gt;Width &lt; 0) {        pFmt_a-&gt;Width = - pFmt_a-&gt;Width;        <a href="vbit_Put.FIND-FUNC">vbit_Put</a>(&amp;pFmt_a-&gt;Flags, <font color="DarkGreen">'-'</font>, 1);    }    Char = (<i>crtx_Char_t</i>)<a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>int</i>);    n2 = <a href="#frmt_PutCharPrintable">frmt_PutCharPrintable</a>(Char, <a href="CRTX_TRUE.FIND-DEF">CRTX_TRUE</a>, pFmt_a);    <b>if</b> (<a href="vbit_Get.FIND-FUNC">vbit_Get</a>(&amp;pFmt_a-&gt;Flags, <font color="DarkGreen">'-'</font>)==0)       <a href="FRMT_PAD.FIND-DEF">FRMT_PAD</a>(pFmt_a, pFmt_a-&gt;Width-n2, <font color="DarkGreen">' '</font>, n1);    <b>if</b> (<a href="#frmt_PutCharPrintable">frmt_PutCharPrintable</a>(Char, <a href="CRTX_FALSE.FIND-DEF">CRTX_FALSE</a>, pFmt_a) &lt; 0)        <b>return</b> -1;    n1 += n2;    <b>if</b> (<a href="vbit_Get.FIND-FUNC">vbit_Get</a>(&amp;pFmt_a-&gt;Flags, <font color="DarkGreen">'-'</font>)==1)       <a href="FRMT_PAD.FIND-DEF">FRMT_PAD</a>(pFmt_a, pFmt_a-&gt;Width-n2, <font color="DarkGreen">' '</font>, n1);    <b>return</b> n1;}<font color="#6920ac">/* specifier '%s' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_s">frmt_Convert_s</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Char_t</i> *pStr = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>char</i> *);    <b>return</b> <a href="#frmt_PutStr">frmt_PutStr</a>(pStr, <a href="strlen.FIND-FUNC">strlen</a>(pStr), pFmt_a);}<font color="#6920ac">/* specifier '%Z' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_Z">frmt_Convert_Z</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Char_t</i> *pStr = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>char</i> *);    <b>return</b> <a href="#frmt_PutStrPrintable">frmt_PutStrPrintable</a>(pStr, <a href="strlen.FIND-FUNC">strlen</a>(pStr), pFmt_a);}<font color="#6920ac">/* specifier '%H' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_H">frmt_Convert_H</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Char_t</i> *pData = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>char</i> *);    <b>return</b> <a href="#frmt_PutDataHex">frmt_PutDataHex</a>(pData, pFmt_a);}<font color="#6920ac">/* specifier '%d' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_d">frmt_Convert_d</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Signed_t</i>    Val;    <i>crtx_Unsigned_t</i>  m;    <i>crtx_Char_t</i>      Buf[43];    <i>crtx_Char_t</i>     *p = Buf + <i>sizeof</i>(Buf);    <a href="CRTX_ASSERT.FIND-DEF">CRTX_ASSERT</a>(pFmt_a)    <b>if</b> (pFmt_a-&gt;LenMod == <font color="DarkGreen">'h'</font> || pFmt_a-&gt;LenMod == <font color="DarkGreen">'H'</font>)       Val = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>short</i> <i>int</i>);    <b>else</b> <b>if</b> (pFmt_a-&gt;LenMod == <font color="DarkGreen">'l'</font> || pFmt_a-&gt;LenMod == <font color="DarkGreen">'L'</font>)       Val = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>long</i> <i>int</i>);    <b>else</b>       Val = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>int</i>);    <b>if</b> (Val == <a href="INT_MIN.FIND-DEF">INT_MIN</a>)       m = <a href="INT_MAX.FIND-DEF">INT_MAX</a> + 1U;    <b>else</b> <b>if</b> (Val &lt; 0)       m = - Val;    <b>else</b>       m = Val;    <b>do</b> {       *--p = m%10 + <font color="DarkGreen">'0'</font>;    } <b>while</b> ((m /= 10) &gt; 0);    <b>if</b> (Val &lt; 0)       *--p = <font color="DarkGreen">'-'</font>;    pFmt_a-&gt;pPrefix = <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>;    <b>return</b> <a href="#frmt_PutDec">frmt_PutDec</a> (p, (<i>crtx_Int_t</i>)((Buf + <i>sizeof</i>(Buf)) - p), pFmt_a);}<font color="#6920ac">/* specifier '%u' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_u">frmt_Convert_u</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Unsigned_t</i>  m;    <i>crtx_Char_t</i>      Buf[43];    <i>crtx_Char_t</i>     *p = Buf + <i>sizeof</i>(Buf);    <a href="CRTX_ASSERT.FIND-DEF">CRTX_ASSERT</a>(pFmt_a)    <b>if</b> (pFmt_a-&gt;LenMod == <font color="DarkGreen">'h'</font> || pFmt_a-&gt;LenMod == <font color="DarkGreen">'H'</font>)       m = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>unsigned</i> <i>short</i>);    <b>else</b> <b>if</b> (pFmt_a-&gt;LenMod == <font color="DarkGreen">'l'</font> || pFmt_a-&gt;LenMod == <font color="DarkGreen">'L'</font>)       m = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>unsigned</i> <i>long</i>);    <b>else</b>       m = <a href="va_arg.FIND-FUNC">va_arg</a>(*(pFmt_a-&gt;pArg), <i>unsigned</i> <i>int</i>);    <b>do</b> {       *--p = m%10 + <font color="DarkGreen">'0'</font>;    } <b>while</b> ((m /= 10) &gt; 0);    pFmt_a-&gt;pPrefix = <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>;    <b>return</b> <a href="#frmt_PutDec">frmt_PutDec</a> (p, (<i>crtx_Int_t</i>)((Buf + <i>sizeof</i>(Buf)) - p), pFmt_a);}<font color="#6920ac">/* specifier '%o' convertion function */</font><font size="+1"><i>crtx_Int_t</i> <b><font color="azure1"><a name="frmt_Convert_o">frmt_Convert_o</a></font></b>(    <i>frmt_SpecDesc_t</i> *pFmt_a){</font>    <i>crtx_Unsigned_t</i>  m;    <i>crtx_Char_t</i>      Buf[44];    <i>crtx_Char_t</i>     *p = Buf + <i>sizeof</i>(Buf);    <a href="CRTX_ASSERT.FIND-DEF">CRTX_ASSERT</a>(pFmt_a)    <b>if</b> (pFmt_a-&gt;LenMod == <font color="DarkGreen">'h'</font> || pFmt_a-&gt;LenMod == <font color="DarkGreen">'H'</font>)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?