
- 加为好友
- 发送私信
- 在线聊天
|
| 发表于:2008-06-24 16:12:521楼 得分:0 |
int _tk_sprintf(char* buf,char flag,int width,int prec,char size,char type,va_list* parg) { wchar_t wch; char tmp[NUM_LEN + 1] = {0}; int len,pos; char sign; short s; int i; unsigned short us; unsigned int ui,xi; double dbl; char* sz; wchar_t* wsz; switch(type) { case 'c': if(buf) { buf[0] = va_arg(*parg,char); buf[1] = '\0'; } return 1; case 'C': wch = va_arg(*parg,wchar_t); len = WideCharToMultiByte(CP_ACP,0,&wch,1,NULL,0,NULL,NULL); if(buf) { WideCharToMultiByte(CP_ACP,0,&wch,1,buf,len,NULL,NULL); buf[len] = '\0'; } return len; case 'd': len = 0; sign = 0; pos = 0; if(size == 'h') { s = va_arg(*parg,short); if(s < 0) { sign = '-'; s = 0 - s; } while(s) { tmp[len ++] = s % 10 + 48; s /= 10; } }else { i = va_arg(*parg,int); if(i < 0) { sign = '-'; i = 0 - i; } while(i) { tmp[len ++] = i % 10 + 48; i /= 10; } } if(sign) { if(buf) buf[pos] = sign; pos ++; width --; } width -= len; while(width > 0) { if(buf) buf[pos] = '0'; pos ++; width --; } while(len--) { if(buf) buf[pos] = tmp[len]; pos ++; } if(buf) buf[pos] = '\0'; return pos; case 'u': len = 0; sign = 0; pos = 0; if(size == 'h') { us = va_arg(*parg,unsigned short); while(us) { tmp[len ++] = us % 10 + 48; us /= 10; } }else { ui = va_arg(*parg,unsigned int); while(ui) { tmp[len ++] = ui % 10 + 48; ui /= 10; } } width -= len; while(width > 0) { if(buf) buf[pos] = '0'; pos ++; width --; } while(len--) { if(buf) buf[pos] = tmp[len]; pos ++; } if(buf) buf[pos] = '\0'; return pos; case 'x': case 'X': len = 0; sign = 0; pos = 0; if(buf) { buf[0] = '0'; buf[1] = type; } pos += 2; ui = va_arg(*parg,unsigned int); while(ui) { us = ui % 16; if(type == 'x') tmp[len ++] = (us < 9)? (us + 48) : (us + 87); else tmp[len ++] = (us < 9)? (us + 48) : (us + 55); ui /= 16; } width -= len; while(width > 0) { if(buf) buf[pos] = '0'; pos ++; width --; } while(len--) { if(buf) buf[pos] = tmp[len]; pos ++; } if(buf) buf[pos] = '\0'; return pos; case 'f': len = 0; sign = 0; pos = 0; dbl = va_arg(*parg,double); if(dbl < 0) { sign = '-'; dbl = 0 - dbl; } xi = (int)dbl; dbl -= (double)xi; if(prec) s = (short)prec; else s = MAX_PREC; while(s--) dbl *= 10.0; ui = (int)floor(dbl + 0.5); if(!prec) { while(!(ui % 10)) ui /= 10; } while(ui) { tmp[len ++] = ui % 10 + 48; ui /= 10; } if(len) tmp[len ++] = '.'; while(xi) { tmp[len ++] = xi % 10 + 48; xi /= 10; width --; } if(sign) { if(buf) buf[pos] = sign; pos ++; width --; } while(width > 0) { if(buf) buf[pos] = '0'; pos ++; width --; } while(len--) { if(buf) buf[pos] = tmp[len]; pos ++; } if(buf) buf[pos] = '\0'; return pos; case 's': len = 0; sign = 0; pos = 0; sz = va_arg(*parg,char*); if(!sz) { if(buf) buf[pos] = '\0'; return pos; } len = strlen(sz); width -= len; while(width > 0) { if(buf) buf[pos] = flag; pos ++; width --; } if(buf) memcpy((void*)(buf + pos),(void*)sz,len * sizeof(char)); pos += len; if(buf) buf[pos] = '\0'; return pos; case 'S': len = 0; sign = 0; pos = 0; wsz = va_arg(*parg,wchar_t*); if(!wsz) { if(buf) buf[pos] = '\0'; return pos; } ui = wcslen(wsz); len = WideCharToMultiByte(CP_ACP,0,wsz,ui,NULL,0,NULL,NULL); width -= len; while(width > 0) { if(buf) buf[pos] = flag; pos ++; width --; } if(buf) WideCharToMultiByte(CP_ACP,0,wsz,ui,buf + pos,len,NULL,NULL); pos += len; if(buf) buf[pos] = '\0'; return pos; } return 0; } 这样: int len = xspritf(NULL,"这次要分配%d个字节,18)可以返回结果串的精确长 xsprintf(buf,"a=%s",NULL)不会格式成a=(null) xsprintf(buf,"a=%f",8.0/9.0)不会返回长长的尾巴了 xsprintf(buf,"%svs%S","俄罗斯",L"荷兰")可以处理MBCS和UNICODE 少了些麻烦 不过没有支持[e,E,f,g,]的格式符,抱歉了 UICODE版的xwprintf道理也是一样的,就不贴出来了 | | |
修改
删除
举报
引用
回复
| |