00001
00007
00008
00009 #include <vararg.h>
00010
00011 #define MAXSTR 80
00012 #define local static
00013
00014 local void _prtl10(long num, char *str);
00015 local void _prtl8(long num, char *str);
00016 local void _prtX16(long num, char *str);
00017 local void _prtl16(long num, char *str);
00018 local void _prtl2(long num, char *str);
00019
00028 void _doprnt(char *fmt, va_list ap, int (*func)(), int farg)
00029 {
00030 int c;
00031 int i;
00032 int f;
00033 char *str;
00034 char string[20];
00035
00036 int length;
00037 char fill;
00038 int leftjust;
00039 int fmax,fmin;
00040 int leading;
00041 char sign;
00042 char digit1;
00043 long larg;
00044
00045 for(;;)
00046 {
00047
00048 while( (c = *fmt++) != '%' )
00049 {
00050 if( c == '\0' )
00051 { return; }
00052 (*func)(farg,c);
00053 }
00054
00055 if( *fmt == '%' )
00056 {
00057 (*func)(farg,*fmt++);
00058 continue;
00059 }
00060
00061 if ((leftjust = ((*fmt=='-')) ? 1 : 0) )
00062 { fmt++; }
00063
00064 fill = (*fmt=='0') ? *fmt++ : ' ';
00065
00066
00067 fmin = 0;
00068 if( *fmt == '*' )
00069 {
00070 fmin = va_arg (ap, int);
00071 ++fmt;
00072 }
00073 else
00074 {
00075 while( '0' <= *fmt && *fmt <= '9' )
00076 { fmin = fmin * 10 + *fmt++ - '0';}
00077 }
00078
00079 fmax = 0;
00080 if( *fmt == '.' )
00081 {
00082 if( *(++fmt) == '*' )
00083 {
00084 fmax = va_arg (ap, int);
00085 ++fmt;
00086 }
00087 else
00088 {
00089 while( '0' <= *fmt && *fmt <= '9' )
00090 { fmax = fmax * 10 + *fmt++ - '0'; }
00091 }
00092 }
00093
00094 str = string;
00095 if( (f= *fmt++) == '\0' )
00096 {
00097 (*func)(farg,'%');
00098 return;
00099 }
00100 sign = '\0';
00101
00102 switch( f )
00103 {
00104 case 'c' :
00105 string[0] = va_arg(ap, int);
00106 string[1] = '\0';
00107 fmax = 0;
00108 fill = ' ';
00109 break;
00110
00111 case 's' :
00112 str = va_arg(ap, char *);
00113 fill = ' ';
00114 break;
00115
00116 case 'd' :
00117 larg = va_arg(ap, long);
00118 if (larg < 0)
00119 {
00120 sign = '-';
00121 larg = -larg;
00122 }
00123 _prtl10(larg, str);
00124 break;
00125
00126 case 'u':
00127 digit1 = '\0';
00128
00129
00130
00131
00132 larg = va_arg(ap, long);
00133 while(larg < 0)
00134 {
00135 larg -= 1000000000L;
00136 ++digit1;
00137 }
00138 _prtl10(larg, str);
00139 str[0] += digit1;
00140 fmax = 0;
00141 break;
00142
00143 case 'o' :
00144 larg = va_arg(ap, long);
00145 _prtl8(larg, str);
00146 fmax = 0;
00147 break;
00148
00149 case 'X' :
00150 larg = va_arg(ap, long);
00151 _prtX16(larg, str);
00152 fmax = 0;
00153 break;
00154
00155 case 'x' :
00156 larg = va_arg(ap, long);
00157 _prtl16(larg, str);
00158 fmax = 0;
00159 break;
00160
00161 case 'b' :
00162 larg = va_arg(ap, long);
00163 _prtl2(larg, str);
00164 fmax = 0;
00165 break;
00166
00167 default :
00168 (*func)(farg,f);
00169 break;
00170 }
00171 for(length = 0; str[length] != '\0'; length++)
00172 { ; }
00173 if ( fmin > MAXSTR || fmin < 0 )
00174 { fmin = 0; }
00175 if ( fmax > MAXSTR || fmax < 0 )
00176 { fmax = 0; }
00177 leading = 0;
00178 if ( fmax != 0 || fmin != 0 )
00179 {
00180 if ( fmax != 0 )
00181 {
00182 if ( length > fmax )
00183 { length = fmax; }
00184 }
00185 if ( fmin != 0 )
00186 { leading = fmin - length; }
00187 if ( sign == '-' )
00188 { --leading; }
00189 }
00190 if( sign == '-' && fill == '0' )
00191 { (*func)(farg,sign); }
00192 if( leftjust == 0 )
00193 {
00194 for( i = 0; i < leading; i++ )
00195 { (*func)(farg,fill); }
00196 }
00197 if( sign == '-' && fill == ' ' )
00198 { (*func)(farg,sign); }
00199 for( i = 0 ; i < length ; i++ )
00200 { (*func)(farg,str[i]); }
00201 if ( leftjust != 0 )
00202 {
00203 for( i = 0; i < leading; i++ )
00204 (*func)(farg,fill);
00205 }
00206 }
00207
00208 }
00209
00215 local void _prtl10(long num, char *str)
00216 {
00217 int i;
00218 char temp[11];
00219
00220 temp[0] = '\0';
00221 for(i = 1; i <= 10; i++) {
00222 temp[i] = num % 10 + '0';
00223 num /= 10;
00224 }
00225 for(i = 10; temp[i] == '0'; i--);
00226 if( i == 0 )
00227 i++;
00228 while( i >= 0 )
00229 *str++ = temp[i--];
00230 }
00231
00237 local void _prtl8(long num, char *str)
00238 {
00239 int i;
00240 char temp[12];
00241
00242 temp[0] = '\0';
00243 for(i = 1; i <= 11; i++) {
00244 temp[i] = (num & 07) + '0';
00245 num = num >> 3;
00246 }
00247 temp[11] &= '3';
00248 for(i = 11; temp[i] == '0'; i--);
00249 if( i == 0 )
00250 i++;
00251 while( i >= 0 )
00252 *str++ = temp[i--];
00253 }
00254
00260 local void _prtl16(long num, char *str)
00261 {
00262 int i;
00263 char temp[9];
00264
00265 temp[0] = '\0';
00266 for(i = 1; i <= 8; i++) {
00267 temp[i] = "0123456789abcdef"[num & 0x0F];
00268 num = num >> 4;
00269 }
00270 for(i = 8; temp[i] == '0'; i--);
00271 if( i == 0 )
00272 i++;
00273 while( i >= 0 )
00274 *str++ = temp[i--];
00275 }
00276
00282 local void _prtX16(long num, char *str)
00283 {
00284 int i;
00285 char temp[9];
00286
00287 temp[0] = '\0';
00288 for(i = 1; i <= 8; i++) {
00289 temp[i] = "0123456789ABCDEF"[num & 0x0F];
00290 num = num >> 4;
00291 }
00292 for(i = 8; temp[i] == '0'; i--);
00293 if( i == 0 )
00294 i++;
00295 while( i >= 0 )
00296 *str++ = temp[i--];
00297 }
00298
00304 local void _prtl2(long num, char *str)
00305 {
00306 int i;
00307 char temp[35];
00308
00309 temp[0] = '\0';
00310 for(i = 1; i <= 32; i++) {
00311 temp[i] = ((num%2) == 0) ? '0' : '1';
00312 num = num >> 1;
00313 }
00314 for(i = 32; temp[i] == '0'; i--);
00315 if( i == 0 )
00316 i++;
00317 while( i >= 0 )
00318 *str++ = temp[i--];
00319 }
00320
00321