Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2976323
  • 博文数量: 401
  • 博客积分: 12926
  • 博客等级: 上将
  • 技术积分: 4588
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-22 14:51
文章分类

全部博文(401)

文章存档

2015年(16)

2014年(4)

2013年(12)

2012年(82)

2011年(98)

2010年(112)

2009年(77)

分类: C/C++

2009-03-16 19:16:15

static char log_buf[1024];
static char store[20];
int pos = 0;
char *itoa(int num, char *str, int radix) ;



char *itoa(int num, char *str, int radix)
{
    char string[] = "0123456789abcdefghijklmnopqrstuvwxyz";

    char* ptr = str;
    int i = 0;
    int j = 0;
    int k = 0;
    
    if(num == 0)
    {
        str[0]='0';
        str[1]=0;
        return str;
    }

    while (num)
    {
        k = num -(num/radix)*radix;
        *ptr++ = string[k];
        num /= radix;

        if (num < radix)
        {
            *ptr++ = string[num];
            *ptr = '\0';
            break;
        }
    }

    j = ptr - str - 1;

    //exchange number sequence
    for (i = 0; i < (ptr - str) / 2; i++)
    {
        int temp = str[i];
        str[i] = str[j];
        str[j--] = temp;
    }

    return str;
}



unsigned long printf(
    void *pbUserData,                    /* User data passed to all porting routines */
    const char *aFormat,...)            /* aFormat string to printf */
{
  
    char* pArg=NULL;
    char c;
   
    pos = 0;
   
    pArg = (char*) &aFormat;;
    pArg += sizeof(aFormat);

///    va_list arg;
    int done = 0;
    char* str = NULL;
    int i = 0; 
    int align_len = 0;
    int data_len = 0;
  
     
//    va_start (arg, aFormat);
   

    while( *aFormat != '\0')
    {
        if( *aFormat == '%')
        {
            if( *(aFormat+1) == '0' || *(aFormat+1) == 'f')
            {
                aFormat ++;
                c = *(aFormat+1);  //only support one digital length control
                if(*(aFormat +2) != 'x')
                      return -1;
                     
           
               switch (c) {
               case '1':
                     align_len = 1;
                     break;
               case '2':
                     align_len = 2;
                     break;
               case '3':
                     align_len = 3;
                     break;
               case '4':
                     align_len = 4;
                     break;
               case '5':
                     align_len = 5;
                     break;
               case '6':
                     align_len = 6;
                     break;
               case '7':
                     align_len = 7;
                     break;
               case '8':
                     align_len = 8;
                     break;
               case '9':
                     align_len = 9;
                     break;
               default:
                     align_len = 0;
                     break;
               }
                            
               //i = va_arg(arg, int);
               i = *((int*)pArg);
               str = store;
               itoa(i, store, 16);
              
               data_len = 0;
               while(*(str)!= 0)
               {
                    str ++;
                    data_len ++;
               }
               //patch zero
               if(align_len > data_len && *aFormat=='0')
               {
                  for(i=0; i<(align_len - data_len); i++)
                  {
                      log_buf[pos++]='0';
                  }
               }
               //patch space
               if(align_len > data_len && *aFormat=='f')
               {
                  for(i=0; i<(align_len - data_len); i++)
                  {
                      log_buf[pos++]=' ';
                  }
               }
               str = store;
               while(*str != 0)
               {
                  log_buf[pos++]= *str;
                  str ++;
               }
               aFormat +=3;
               //point to next param
               pArg += sizeof(int);
               continue;
            }
           
            if( *(aFormat+1) == 'c' )
            {
                //char c = (char)va_arg(arg, int);
                char c = *((char*)pArg);
                log_buf[pos++] = c;
                aFormat += 2;
                //point to next param
                pArg += sizeof(int);
                continue;
            }
            else if( *(aFormat+1) == 'd')
            {
                //i = va_arg(arg, int);
                i = *((int*)pArg);
                str = store;
                itoa(i, store, 10);
                while( *str != '\0'){
                      log_buf[pos++] = *str;
                      str ++;
                }
                aFormat += 2;
                //point to next param
                pArg += sizeof(int);
                continue;
            }
            else if(*(aFormat+1) == 'u')
            {
                //i = va_arg(arg, int);
                i = *((int*)pArg);
                str = store;
                itoa(i, store, 10);
                while( *str != '\0'){
                      log_buf[pos++] = *str;
                      str ++;
                }
                aFormat += 2;
                //point to next param
                pArg += sizeof(int);
                continue;
            }   
            else if( *(aFormat+1) == 'o')
            {
               
                //i = va_arg(arg, int);
                i = *((int*)pArg);
                str = store;
                itoa(i, store, 8);
                while( *str != '\0'){
                      log_buf[pos++] = *str;
                      str ++;
                }
                aFormat +=2;
                //point to next param
                pArg += sizeof(int);
                continue;
            }
            else if( *(aFormat+1) == 'x')
            {
                //i = va_arg(arg, int);
                i = *((int*)pArg);
                str = store;
                itoa(i, store, 16);
                while( *str != '\0'){
                      log_buf[pos++] = *str;
                      str ++;
                }
                aFormat += 2;
                //point to next param
                pArg += sizeof(int);
                continue;
            }
            else if( *(aFormat+1) == 's' )
            {
                //str = va_arg(arg, char*);
                str = *((int*)pArg);
                while( *str != '\0'){
                      log_buf[pos++] = *str;
                      str ++;
                }
                aFormat += 2;
                //point to next param
                pArg += sizeof(int);
                continue;
            }
            else if( *(aFormat+1) == '%' )
            {
                log_buf[pos++] = '%';
                aFormat += 2;
                //point to next param
                pArg += sizeof(int);
                continue;
            }


           
        } else {
             log_buf[pos++] = *(aFormat ++);
        }
    }
    log_buf[pos] = 0;
    UartPrint(log_buf);  //change your uart send string data function.

    return 0;
}


Usage:
printf(NULL, "test %d.\r\n", 10);
printf(NULL, "test %x.\r\n", 0x55);
printf(NULL, "test %s.\r\n", "%s");
阅读(1505) | 评论(0) | 转发(1) |
0

上一篇:const 详解

下一篇:trick of sudo

给主人留下些什么吧!~~