1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| int scfvsnprintf(char *buf, int size, const char* fmt,va_list* ap) { int n = 0; while(*fmt){ if("%" != *fmt){ buf[n++]=*fmt++; continue; } fmt++; if("%"==*fmt){ buf[n++]=*fmt++; continue; } int prefix = 0; if('#'==*fmt){ prefix++; fmt++; } int l = 0; if('l' == *fmt){ l++; fmt++; } if('c'==*fmt){ buf[n++]=va_arg(ap,int32_t); } else if('u'==*fmt){ if(l) scf_ulong2a(buf,&n,size,va_arg(ap,uint64_t)); else scf_ulong2a(buf,&n,size,va_arg(ap,uint32_t)); } else if('d'==*fmt){ if(l) scf_long2a(buf,&n,size,va_arg(ap,int64_t)); else scf_long2a(buf,&n,size,va_arg(ap,int32_t)) } else if('x' == *fmt){ if(prefix){ if(l) scf_hex2a_prefix(buf,&n,size,va_arg(ap,uint64_t)); else scf_hex2a(buf,&n,size,va_arg(ap,uint32_t)); } } else if('p'==*fmt) scf_p2a(buf,&n,size,va_arg(ap,uint64_t)); else if('s' == *fmt) scf_str2a(buf,&n,size,va_arg(ap,char*)); else if('f' == *fmt){ if(l) scf_double2a(buf,&n,size,va_arg(ap,double)); else scf_double2a(buf,&n,size,va_arg(ap,float)); }
else return -1; fmt++; } buf[n] = '\0'; return n; }
|