⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rprintf.c

📁 sprintf函数的简单功能实现。函数实现的功能不如库函数
💻 C
字号:
#include "stdarg.h"
#include "string.h"

static unsigned char Temp[128]; 

unsigned int str_len( char * str);
unsigned char Isdigit(char c);
int atoi_beta(char *str);


//-------------------------------------------------------------
//to caculate the  length of a string.
//
//------------------------------------------------------------
unsigned int str_len( char * str)
{
  int i=0;
  unsigned int count=0;
  
  while(str[i]!=0)
  {
    i++;
    count++; 
  }
  return count;
}

//--------------------------------------------------------------
// to decide whether a char is in range '0' ~ '9'
//
//--------------------------------------------------------------
unsigned char Isdigit(char c)
{
   if((c >= 0x30) && (c <= 0x39))   return 1;
   else   return 0;
}

//--------------------------------------------------------------
//transform a string  into  an   interger  formed 
//for example   "123456789" ----->   123456789(dec)
//--------------------------------------------------------------
int atoi_beta(char *str)
{
   int num = 0; 
 
   while(Isdigit(*str))
   {
      num *= 10;
      num +=  *str++  - 0x30 ;
   }
   return num;
}
  
//--------------------------------------------------------------
//Usage:     sprintf1(array,"%?%?",v1,v2,...);
//function:  to transform a  various value into a certain format,and store it in a spcified container.
//supported  : 
// %d - decimal
// %u - unsigned decimal
// %o - octal
// %x - hex
// %c - character
// %s - strings
//--------------------------------------------------------------
char * sprintf1(char *where,const char *sfmt, ...)
{
   register unsigned char *f, *bp, *str;
   register long l;
   register unsigned long u;
   register int i;
   register int fmt;
   register unsigned char pad = ' ';
   int     flush_left = 0, f_width = 0, prec = 32766, hash = 0, do_long = 0;
   int     sign = 0;

    va_list ap;
    va_start(ap, sfmt);

    str = (unsigned char*)where;
    f = (unsigned char *) sfmt;

    for (; *f; f++)
    {
       if (*f != '%')     *str++ = *f;                                // if not a format character,then just output the char                                         	 
  
       else 
       {
          f++;                                                        // if we have a "%" then skip it
          if (*f == '-')
          {
             flush_left = 1;                                          // minus: flush left
             f++;
          }
          if (*f == '0' || *f == '.')
          {                                   
             pad = '0';                                               // padding with 0 rather than blank
             f++;
          }
          if (*f == '*')
          {                                                           // field width
             f_width = va_arg(ap, int);
             f++;
          }
          else if (Isdigit(*f))
               {
                  f_width = atoi_beta((char *) f);
                  while (Isdigit(*f))
                  f++;                                                // skip the digits
               }
               if (*f == '.')
               {                                                      // precision
                  f++;
                  if (*f == '*')
                  {
                     prec = va_arg(ap, int);
                     f++;
                  }
  
                  else if (Isdigit(*f))
                  {
                     prec = atoi_beta((char *) f);
                     while (Isdigit(*f))   f++;                       // skip the digits
                
                  }
               }
               if (*f == '#')
               {                                                      // alternate format
                  hash = 1;
                  f++;
               }
               if (*f == 'l')
               {                                                      // long format
                  do_long = 1;
                  f++;
               }

            fmt = *f;
            bp = Temp;
 
            switch (fmt)                                              // do the formatting
            {
             case 'd':                                                // 'd' signed decimal
                        if (do_long)   l = va_arg(ap, long);
                        else   l = (long) (va_arg(ap, int));
 
                        if (l < 0)
                        {
                           sign = 1;
                           l = -l;
                        }
                        do
                        {
                           *bp++ = l % 10 + '0';
                        }while ((l /= 10) > 0);
                    
                        if (sign)   *bp++ = '-';
                        f_width = f_width - (bp - Temp);
 
                        if (!flush_left)
                           while (f_width-- > 0)   *str++ = (pad);
                        for (bp--; bp >= Temp; bp--)   *str++ = (*bp);
                        if (flush_left)
                            while (f_width-- > 0)   *str++ = (' ');
                        break;
               case 'o':                                              // 'o' octal number
               case 'x':                                              // 'x' hex number
               case 'u':                                              // 'u' unsigned decimal
                        if (do_long)   u = va_arg(ap, unsigned long);
                        else   u = (unsigned long) (va_arg(ap, unsigned));
 
                        if (fmt == 'u')
                        {                                             // unsigned decimal
                           do
                           {
                              *bp++ = u % 10 + '0';
                           }while ((u /= 10) > 0);
                        }
                        else if (fmt == 'o')
                        {                                             // octal
                           do
                           {
                              *bp++ = u % 8 + '0';
                           }while ((u /= 8) > 0);
                           if (hash)   *bp++ = '0';
                        }
                        else if (fmt == 'x')
                        {                                              // hex
                           do
                           {
                              i = u % 16;
                              if (i < 10)   *bp++ = i + '0';
                              else   *bp++ = i - 10 + 'a';
                           } while ((u /= 16) > 0);
                           if (hash)
                           {
                              *bp++ = 'x';
                              *bp++ = '0';
                           }
                        }
                        i = f_width - (bp - Temp);
                        if (!flush_left)
                           while (i-- > 0)   *str++ = (pad);
                        for (bp--; bp >= Temp; bp--)   *str++ = ((int) (*bp));
                        if (flush_left)
                           while (i-- > 0)   *str++ = (' ');
                        break;
               case 'c':                                              // 'c' character
                        i = va_arg(ap, int);
                        *str++ = ((int) (i));
                        break;
               case 's':                                              // 's' string
                        bp = va_arg(ap, unsigned char *);
                        if (!bp)   bp = (unsigned char *) "(nil)";
           
                        f_width = f_width - strlen((char *)bp);
                        if (!flush_left)
                           while (f_width-- > 0)   *str++ = (pad);
                        for (i = 0; *bp && i < prec; i++)
                        {
                           *str++ = (*bp);
                           bp++;
                        }
                        if (flush_left)
                           while (f_width-- > 0)   *str++ = (' ');
                        break;
               case '%':                                              // '%' character
                        *str++ = ('%');
                        break;
            }
            flush_left = 0, f_width = 0, prec =32766, hash = 0, do_long = 0;
            sign = 0;
            pad = ' ';
        }   
    }   

      va_end(ap);
                                                                      // terminate string with null
      *str++ = '\0';

      return where;
}

⌨️ 快捷键说明

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