vc++中printf的一个实现

int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
    TCHAR   szBuffer [1024] ;
    va_list pArgList ;

    // The va_start macro (defined in STDARG.H) is usually equivalent to:
    // pArgList = (char *) &szFormat + sizeof (szFormat) ;

    va_start (pArgList, szFormat) ;

    // The last argument to wvsprintf points to the arguments

    _vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
        szFormat, pArgList) ;

    // The va_end macro just zeroes out pArgList for no good reason

    va_end (pArgList) ;

    return MessageBox (NULL, szBuffer, szCaption, 0) ;
}

 

int WINAPI WinMain (
                    __in HINSTANCE hInstance,
                    __in_opt HINSTANCE hPrevInstance,
                    __in_opt LPSTR lpCmdLine,
                    __in int nShowCmd
                    ){                        
                        int cxScreen = 800, cyScreen = 600;
                          MessageBoxPrintf (TEXT ("ScrnSize"),
                            TEXT ("The screen is %i pixels wide by %i pixels high."),
                            cxScreen, cyScreen) ;
}

 

int __cdecl _vsprintf_l (
        char *string,
        const char *format,
        _locale_t plocinfo,
        va_list ap
        )

{
        FILE str;
        REG1 FILE *outfile = &str;
        REG2 int retval;

        _VALIDATE_RETURN( (format != NULL), EINVAL, -1);

#ifdef _COUNT_
        _VALIDATE_RETURN( (count == 0) || (string != NULL), EINVAL, -1 );
#else  /* _COUNT_ */
        _VALIDATE_RETURN( (string != NULL), EINVAL, -1 );
#endif  /* _COUNT_ */

#ifndef _COUNT_
        outfile->_cnt = MAXSTR;
#else  /* _COUNT_ */
        if(count>INT_MAX)
        {
            /* old-style functions allow any large value to mean unbounded */
            outfile->_cnt = INT_MAX;
        }
        else
        {
            outfile->_cnt = (int)count;
        }
#endif  /* _COUNT_ */
        outfile->_flag = _IOWRT|_IOSTRG;
        outfile->_ptr = outfile->_base = string;

#ifndef _SWPRINTFS_ERROR_RETURN_FIX
        retval = _output_l(outfile, format, plocinfo, ap );
#else  /* _SWPRINTFS_ERROR_RETURN_FIX */
        retval = outfn(outfile, format, plocinfo, ap );
#endif  /* _SWPRINTFS_ERROR_RETURN_FIX */
        if ( string==NULL)
            return(retval);

#ifndef _SWPRINTFS_ERROR_RETURN_FIX
        _putc_nolock('/0',outfile);
        return(retval);
#else  /* _SWPRINTFS_ERROR_RETURN_FIX */
        if((retval >= 0) && (_putc_nolock('/0',outfile) != EOF))
            return(retval);

        string[count - 1] = 0;

        if (outfile->_cnt < 0)
        {
            /* the buffer was too small; we return -2 to indicate truncation */
            return -2;
        }
        return -1;
#endif  /* _SWPRINTFS_ERROR_RETURN_FIX */
}

 

int __cdecl _output_l (
    FILE *stream,

    const _TCHAR *format,
    _locale_t plocinfo,
    va_list argptr
    )

{
.....

......
    /* main loop -- loop while format character exist and no I/O errors */
    while ((ch = *format++) != _T('/0') && charsout >= 0) {

 chclass = FIND_CHAR_CLASS(__lookuptable, ch);  /* find character class */
        state = FIND_NEXT_STATE(__lookuptable, chclass, state); /* find next state */

  _VALIDATE_RETURN((state != ST_INVALID), EINVAL, -1);

  switch (state) {
        case ST_NORMAL:

 if (_isleadbyte_l((unsigned char)ch, _loc_update.GetLocaleT())) {
                WRITE_CHAR(ch, &charsout);
                ch = *format++;
                /* don't fall off format string */
                _VALIDATE_RETURN( (ch != _T('/0')), EINVAL, -1);
            }

            WRITE_CHAR(ch, &charsout);
            break;

            ......

            ......

}

......

......

}

 

 

#define WRITE_CHAR(ch, pnw)         write_char(ch, stream, pnw)

 

LOCAL(void) write_char (
    _TCHAR ch,
    FILE *f,
    int *pnumwritten
    )
{
    if ( (f->_flag & _IOSTRG) && f->_base == NULL)
    {
        ++(*pnumwritten);
        return;
    }
#ifdef _UNICODE
    if (_putwc_nolock(ch, f) == WEOF)
#else  /* _UNICODE */
    if (_putc_nolock(ch, f) == EOF)
#endif  /* _UNICODE */
        *pnumwritten = -1;
    else
        ++(*pnumwritten);
}

 

#define _putc_nolock(_c, _stream)   _fputc_nolock(_c, _stream)

#define _fputc_nolock(_c,_stream)    (--(_stream)._cnt >= 0 ? 0xff & (*(_stream)._ptr++ = (char)(_c)) :  _flsbuf((_c),(_stream)))

 

//deal with arguments

 switch (state) {
        case ST_NORMAL:
             switch (ch) {

 case _T('i'):

 l = get_int_arg(&argptr); /* sign extend */

__inline int __cdecl get_int_arg (
    va_list *pargptr
    )
{
    return va_arg(*pargptr, int);
}

}

#define va_arg _crt_va_arg

#define _crt_va_arg(ap,t)    ( *(t *)( (ap += _INTSIZEOF(t)) - _INTSIZEOF(t) )

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值