当前位置:编程学习 > C/C++ >>

可移植printf源码

/* tab space = 4 */   
/********************************************************************* 
* DISCLAIMER:                                                        * 
* The software supplied by Renesas Technology America Inc. is        * 
* intended and supplied for use on Renesas Technology products.      * 
* This software is owned by Renesas Technology America, Inc. or      * 
* Renesas Technology Corporation and is protected under applicable   * 
* copyright laws. All rights are reserved.                           * 
*                                                                    * 
* THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, * 
* IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO IMPLIED      * 
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * 
* APPLY TO THIS SOFTWARE. RENESAS TECHNOLOGY AMERICA, INC. AND       * 
* AND RENESAS TECHNOLOGY CORPORATION RESERVE THE RIGHT, WITHOUT      * 
* NOTICE, TO MAKE CHANGES TO THIS SOFTWARE. NEITHER RENESAS          * 
* TECHNOLOGY AMERICA, INC. NOR RENESAS TECHNOLOGY CORPORATION SHALL, * 
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR        * 
* CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER ARISING OUT OF THE * 
* USE OR APPLICATION OF THIS SOFTWARE.                               * 
*********************************************************************/  
  
/*----------------------------------------------------------------------------- 
  FILE NAME: 易做图_printf.c 
----------- 
DESCRIPTION: Simplified printf and sprintf. (float and signed values no supported) 
----------- 
    DETAILS:  
 
------------------ 
 Revision History 
------------------ 
   1.6 April 7, 2007 
       Changed "..." access over to using standard "va_" macros for compatibility with other compilers. 
   1.5 July 16, 2006 
       Initialized local "s" in _print_out() in order to suppress compiler warning. 
   1.4 April 28, 2006 
       ROM/RAM reductions. Also NC30WA V.5.40 compatable. 
   1.3 Feb 21, 2006 
       Combined printf and sprintf functions 
   1.2 Nov 15, 2005 
       Fixed bug when using "%lx" or %lX 
   1.1 Sept 8, 2005 
       Fixed bug when using "%Ld" 
       Fixed bug for sprintf (did not add NULL at end) 
   1.0 ??, 2005 
       Initial Version 
 
-----------------------------------------------------------------------------*/  
  
#include <stdio.h>  
#include <stdarg.h>  
  
#if defined NC308  
#define FAR _far  
#elif defined NC30  
#define FAR _far  
#else  
#define FAR  
#endif  
  
enum { UI, UH, UL, SI, SH, SL };  
enum { LEFT_SPACE, PREFIX, ZERO_PAD, VALUE_TXT, RIGHT_SPACE, OUT_DONE};  
  
static const struct  
{  
  char c[2];  
  char size;  
} prefix[]=  
{  
  {{0,0},0},      /* 0: default, no prefix */  
  {{'-',0},1},    /* 1: minus sign */  
  {{'+',0},1},    /* 2: plus sign */  
  {{' ',0},1},    /* 3: space */  
  {{'0','x'},2},  /* 4: hex 0x */  
  {{'0','X'},2},  /* 5: hex 0X */  
};  
  
/* Function Prototypes */  
int _print_out(char FAR *s, const char FAR *format, va_list sp);  
  
int sprintf(char FAR *s, const char FAR *format, ...)  
{  
  int return_count;  
  va_list sp;  
  
  va_start(sp, format);  
  
  return_count = _print_out(s, format, sp);  
  va_end(sp);  
  return (return_count);  
}  
  
int printf(const char FAR *format, ...)  
{  
  int return_count;  
  va_list sp;  
  
  va_start(sp, format);  
  return_count = _print_out(NULL, format, sp);  
  va_end(sp);  
  return (return_count);  
}  
  
int _print_out(char FAR *s, const char FAR *format, va_list sp)  
{  
  int total = 0;  
  
  char tmp_buf[12];                         /* maximum tmp_buf usage is 12 in %d */  
  
  /* Loop through format string */  
  while ( *format )                         /* loop till hit NULL */  
  {  
    unsigned char tmp_char, index, out_state;  
    unsigned char left_justify = 0;         /* set default to right justify */  
    unsigned char prefix_select = 0;        /* set default prefix to none */  
    int  min_width = 0;            /* set default field min_width to 0 */  
    int  precision = 0;            /* set default precision to 0 */  
    unsigned char size_type = UI;           /* set default type to unsigned int */  
    char const FAR * str_ptr = tmp_buf;    /* set default string source to buffer */  
  
    union  
    {  
      unsigned long UL;  
      signed long SL;  
  &n
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,