printft.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. File: printf.c
  3. Copyright (C) 2004,2008 Kustaa Nyholm
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "printft.h"
  17. void putchar(char c);
  18. static char* bf;
  19. static char buf[12];
  20. static unsigned int num;
  21. static char uc;
  22. static char zs;
  23. static void out(char c) {
  24. *bf++ = c;
  25. }
  26. static void outDgt(char dgt) {
  27. out(dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10));
  28. zs=1;
  29. }
  30. static void divOut(unsigned int div) {
  31. unsigned char dgt=0;
  32. num &= 0xffff; // just for testing the code with 32 bit ints
  33. while (num>=div) {
  34. num -= div;
  35. dgt++;
  36. }
  37. if (zs || dgt>0)
  38. outDgt(dgt);
  39. }
  40. void printft(char *fmt, ...)
  41. {
  42. va_list va;
  43. char ch;
  44. char* p;
  45. va_start(va,fmt);
  46. while ((ch=*(fmt++))) {
  47. if (ch!='%') {
  48. putchar(ch);
  49. }
  50. else {
  51. char lz=0;
  52. char w=0;
  53. ch=*(fmt++);
  54. if (ch=='0') {
  55. ch=*(fmt++);
  56. lz=1;
  57. }
  58. if (ch>='0' && ch<='9') {
  59. w=0;
  60. while (ch>='0' && ch<='9') {
  61. w=(((w<<2)+w)<<1)+ch-'0';
  62. ch=*fmt++;
  63. }
  64. }
  65. bf=buf;
  66. p=bf;
  67. zs=0;
  68. switch (ch) {
  69. case 0:
  70. goto abort;
  71. case 'u':
  72. case 'd' :
  73. num=va_arg(va, unsigned int);
  74. if (ch=='d' && (int)num<0) {
  75. num = -(int)num;
  76. out('-');
  77. }
  78. divOut(10000);
  79. divOut(1000);
  80. divOut(100);
  81. divOut(10);
  82. outDgt(num);
  83. break;
  84. case 'x':
  85. case 'X' :
  86. uc= ch=='X';
  87. num=va_arg(va, unsigned int);
  88. divOut(0x1000);
  89. divOut(0x100);
  90. divOut(0x10);
  91. outDgt(num);
  92. break;
  93. case 'c' :
  94. out((char)(va_arg(va, int)));
  95. break;
  96. case 's' :
  97. p=va_arg(va, char*);
  98. break;
  99. case '%' :
  100. out('%');
  101. default:
  102. break;
  103. }
  104. *bf=0;
  105. bf=p;
  106. while (*bf++ && w > 0)
  107. w--;
  108. while (w-- > 0)
  109. putchar(lz ? '0' : ' ');
  110. while ((ch= *p++))
  111. putchar(ch);
  112. }
  113. }
  114. abort:;
  115. va_end(va);
  116. }