00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdarg.h>
00026
00027 #include "rprintf.h"
00028
00029 #ifndef TRUE
00030 #define TRUE -1
00031 #define FALSE 0
00032 #endif
00033
00034 #define INF 32766 // maximum field size to print
00035 #define READMEMBYTE(a,char_ptr) ((a)?(*char_ptr):(*char_ptr))
00036
00037 #ifdef RPRINTF_COMPLEX
00038 static unsigned char buf[128];
00039 #endif
00040
00041
00042 static const char HexChars[] = "0123456789ABCDEF";
00043 #define hexchar(x) HexChars[(x)&0x0F]
00044
00045
00046
00047 static void (*rputchar)(unsigned char c);
00048
00049
00050
00051
00052 void rprintfInit(void (*putchar_func)(unsigned char c))
00053 {
00054 rputchar = putchar_func;
00055 }
00056
00057
00058
00059 void rprintfChar(unsigned char c)
00060 {
00061
00062 if(c == '\n')
00063 rputchar('\r');
00064
00065 rputchar(c);
00066 }
00067
00068
00069
00070 void rprintfStr(char str[])
00071 {
00072
00073
00074 if (!str) return;
00075
00076
00077 while (*str)
00078 rprintfChar(*str++);
00079 }
00080
00081
00082
00083
00084
00085 void rprintfStrLen(char str[], unsigned char start, unsigned char len)
00086 {
00087 register char i=0;
00088
00089
00090 if (!str) return;
00091
00092
00093 while((i++<start) && (*str++));
00094
00095
00096 for(i=0; i<len; i++)
00097 {
00098
00099
00100 if(*str)
00101 rprintfChar(*str++);
00102 else
00103 rprintfChar(' ');
00104 }
00105
00106 }
00107
00108
00109
00110 void rprintfCRLF(void)
00111 {
00112
00113 rprintfChar('\r');
00114 rprintfChar('\n');
00115 }
00116
00117
00118
00119 void rprintfu04(unsigned char data)
00120 {
00121
00122 rprintfChar( hexchar(data) );
00123 }
00124
00125
00126
00127 void rprintfu08(unsigned char data)
00128 {
00129
00130 rprintfu04(data>>4);
00131 rprintfu04(data);
00132 }
00133
00134
00135
00136 void rprintfu16(unsigned short data)
00137 {
00138
00139 rprintfu08(data>>8);
00140 rprintfu08(data);
00141 }
00142
00143
00144
00145 void rprintfu32(unsigned long data)
00146 {
00147
00148 rprintfu16(data>>16);
00149 rprintfu16(data);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 void rprintfNum(char base, char numDigits, char isSigned, char padchar, long n)
00165 {
00166
00167
00168 char *p, buf[32];
00169 unsigned long x;
00170 unsigned char count;
00171
00172
00173 if( isSigned && (n < 0) )
00174 {
00175 x = -n;
00176 }
00177 else
00178 {
00179 x = n;
00180 }
00181
00182
00183 count = (numDigits-1)-(isSigned?1:0);
00184 p = buf + sizeof (buf);
00185 *--p = '\0';
00186
00187
00188
00189 *--p = hexchar(x%base); x /= base;
00190
00191 while(count--)
00192 {
00193 if(x != 0)
00194 {
00195
00196 *--p = hexchar(x%base); x /= base;
00197 }
00198 else
00199 {
00200
00201 *--p = padchar;
00202 }
00203 }
00204
00205
00206 if( isSigned )
00207 {
00208 if(n < 0)
00209 {
00210 *--p = '-';
00211 }
00212 else if(n > 0)
00213 {
00214 *--p = '+';
00215 }
00216 else
00217 {
00218 *--p = ' ';
00219 }
00220 }
00221
00222
00223 count = numDigits;
00224 while(count--)
00225 {
00226 rprintfChar(*p++);
00227 }
00228 }
00229
00230 #ifdef RPRINTF_FLOAT
00231
00232
00233 void rprintfFloat(char numDigits, double x)
00234 {
00235 unsigned char firstplace = FALSE;
00236 unsigned char negative;
00237 unsigned char i, digit;
00238 double place = 1.0;
00239
00240
00241 negative = (x<0);
00242
00243 x = (x>0)?(x):(-x);
00244
00245
00246 for(i=0; i<15; i++)
00247 {
00248 if((x/place) < 10.0)
00249 break;
00250 else
00251 place *= 10.0;
00252 }
00253
00254 if(negative)
00255 rprintfChar('-');
00256 else
00257 rprintfChar('+');
00258
00259
00260 for(i=0; i<numDigits; i++)
00261 {
00262 digit = (x/place);
00263
00264 if(digit | firstplace | (place == 1.0))
00265 {
00266 firstplace = TRUE;
00267 rprintfChar(digit+0x30);
00268 }
00269 else
00270 rprintfChar(' ');
00271
00272 if(place == 1.0)
00273 {
00274 rprintfChar('.');
00275 }
00276
00277 x -= (digit*place);
00278 place /= 10.0;
00279 }
00280 }
00281 #endif
00282
00283 #ifdef RPRINTF_SIMPLE
00284
00285
00286
00287
00288
00289
00290 int rprintf1RamRom(unsigned char stringInRom, const char *format, ...)
00291 {
00292
00293
00294
00295 char format_flag;
00296 unsigned int u_val, div_val, base;
00297 va_list ap;
00298
00299 va_start(ap, format);
00300 for (;;)
00301 {
00302 while ((format_flag = READMEMBYTE(stringInRom,format++) ) != '%')
00303 {
00304 if (!format_flag)
00305 {
00306 va_end(ap);
00307 return(0);
00308 }
00309 rprintfChar(format_flag);
00310 }
00311
00312 switch (format_flag = READMEMBYTE(stringInRom,format++) )
00313 {
00314 case 'c': format_flag = va_arg(ap,int);
00315 default: rprintfChar(format_flag); continue;
00316 case 'd': base = 10; div_val = 10000; goto CONVERSION_LOOP;
00317 case 'x': base = 16; div_val = 0x1000;
00318
00319 CONVERSION_LOOP:
00320 u_val = va_arg(ap,int);
00321 if (format_flag == 'd')
00322 {
00323 if (((int)u_val) < 0)
00324 {
00325 u_val = - u_val;
00326 rprintfChar('-');
00327 }
00328 while (div_val > 1 && div_val > u_val) div_val /= 10;
00329 }
00330 do
00331 {
00332
00333 rprintfu04(u_val/div_val);
00334 u_val %= div_val;
00335 div_val /= base;
00336 } while (div_val);
00337 }
00338 }
00339 va_end(ap);
00340 }
00341 #endif
00342
00343
00344 #ifdef RPRINTF_COMPLEX
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 int rprintf2RamRom(unsigned char stringInRom, const char *sfmt, ...)
00357 {
00358 register unsigned char *f, *bp;
00359 register long l;
00360 register unsigned long u;
00361 register int i;
00362 register int fmt;
00363 register unsigned char pad = ' ';
00364 int flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00365 int sign = 0;
00366
00367 va_list ap;
00368 va_start(ap, sfmt);
00369
00370 f = (unsigned char *) sfmt;
00371
00372 for (; READMEMBYTE(stringInRom,f); f++)
00373 {
00374 if (READMEMBYTE(stringInRom,f) != '%')
00375 {
00376
00377 rprintfChar(READMEMBYTE(stringInRom,f));
00378 }
00379 else
00380 {
00381 f++;
00382 if (READMEMBYTE(stringInRom,f) == '-')
00383 {
00384 flush_left = 1;
00385 f++;
00386 }
00387 if (READMEMBYTE(stringInRom,f) == '0'
00388 || READMEMBYTE(stringInRom,f) == '.')
00389 {
00390
00391 pad = '0';
00392 f++;
00393 }
00394 if (READMEMBYTE(stringInRom,f) == '*')
00395 {
00396 f_width = va_arg(ap, int);
00397 f++;
00398 }
00399 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00400 {
00401 f_width = atoiRamRom(stringInRom, (char *) f);
00402 while (Isdigit(READMEMBYTE(stringInRom,f)))
00403 f++;
00404 }
00405 if (READMEMBYTE(stringInRom,f) == '.')
00406 {
00407 f++;
00408 if (READMEMBYTE(stringInRom,f) == '*')
00409 {
00410 prec = va_arg(ap, int);
00411 f++;
00412 }
00413 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00414 {
00415 prec = atoiRamRom(stringInRom, (char *) f);
00416 while (Isdigit(READMEMBYTE(stringInRom,f)))
00417 f++;
00418 }
00419 }
00420 if (READMEMBYTE(stringInRom,f) == '#')
00421 {
00422 hash = 1;
00423 f++;
00424 }
00425 if (READMEMBYTE(stringInRom,f) == 'l')
00426 {
00427 do_long = 1;
00428 f++;
00429 }
00430
00431 fmt = READMEMBYTE(stringInRom,f);
00432 bp = buf;
00433 switch (fmt) {
00434 case 'd':
00435 if (do_long)
00436 l = va_arg(ap, long);
00437 else
00438 l = (long) (va_arg(ap, int));
00439 if (l < 0)
00440 {
00441 sign = 1;
00442 l = -l;
00443 }
00444 do {
00445 *bp++ = l % 10 + '0';
00446 } while ((l /= 10) > 0);
00447 if (sign)
00448 *bp++ = '-';
00449 f_width = f_width - (bp - buf);
00450 if (!flush_left)
00451 while (f_width-- > 0)
00452 rprintfChar(pad);
00453 for (bp--; bp >= buf; bp--)
00454 rprintfChar(*bp);
00455 if (flush_left)
00456 while (f_width-- > 0)
00457 rprintfChar(' ');
00458 break;
00459 case 'o':
00460 case 'x':
00461 case 'u':
00462 if (do_long)
00463 u = va_arg(ap, unsigned long);
00464 else
00465 u = (unsigned long) (va_arg(ap, unsigned));
00466 if (fmt == 'u')
00467 {
00468 do {
00469 *bp++ = u % 10 + '0';
00470 } while ((u /= 10) > 0);
00471 }
00472 else if (fmt == 'o')
00473 {
00474 do {
00475 *bp++ = u % 8 + '0';
00476 } while ((u /= 8) > 0);
00477 if (hash)
00478 *bp++ = '0';
00479 }
00480 else if (fmt == 'x')
00481 {
00482 do {
00483 i = u % 16;
00484 if (i < 10)
00485 *bp++ = i + '0';
00486 else
00487 *bp++ = i - 10 + 'a';
00488 } while ((u /= 16) > 0);
00489 if (hash)
00490 {
00491 *bp++ = 'x';
00492 *bp++ = '0';
00493 }
00494 }
00495 i = f_width - (bp - buf);
00496 if (!flush_left)
00497 while (i-- > 0)
00498 rprintfChar(pad);
00499 for (bp--; bp >= buf; bp--)
00500 rprintfChar((int) (*bp));
00501 if (flush_left)
00502 while (i-- > 0)
00503 rprintfChar(' ');
00504 break;
00505 case 'c':
00506 i = va_arg(ap, int);
00507 rprintfChar((int) (i));
00508 break;
00509 case 's':
00510 bp = va_arg(ap, unsigned char *);
00511 if (!bp)
00512 bp = (unsigned char *) "(nil)";
00513 f_width = f_width - strlen((char *) bp);
00514 if (!flush_left)
00515 while (f_width-- > 0)
00516 rprintfChar(pad);
00517 for (i = 0; *bp && i < prec; i++)
00518 {
00519 rprintfChar(*bp);
00520 bp++;
00521 }
00522 if (flush_left)
00523 while (f_width-- > 0)
00524 rprintfChar(' ');
00525 break;
00526 case '%':
00527 rprintfChar('%');
00528 break;
00529 }
00530 flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00531 sign = 0;
00532 pad = ' ';
00533 }
00534 }
00535
00536 va_end(ap);
00537 return 0;
00538 }
00539
00540 unsigned char Isdigit(char c)
00541 {
00542 if((c >= 0x30) && (c <= 0x39))
00543 return TRUE;
00544 else
00545 return FALSE;
00546 }
00547
00548 int atoiRamRom(unsigned char stringInRom, char *str)
00549 {
00550 int num = 0;;
00551
00552 while(Isdigit(READMEMBYTE(stringInRom,str)))
00553 {
00554 num *= 10;
00555 num += ((READMEMBYTE(stringInRom,str++)) - 0x30);
00556 }
00557 return num;
00558 }
00559
00560 #endif
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751