printf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * found at: http://www.sparetimelabs.com/tinyprintf/tinyprintf.php
  3. * and: http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
  4. */
  5. /*
  6. File: printf.c
  7. Copyright (C) 2004 Kustaa Nyholm
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "printf.h"
  21. typedef void (*putcf)(void*, char);
  22. static putcf stdout_putf;
  23. static void* stdout_putp;
  24. // this adds cca 400 bytes
  25. #define PRINTF_LONG_SUPPORT
  26. #ifdef PRINTF_LONG_SUPPORT
  27. static void uli2a(unsigned long int num, unsigned int base, int uc, char* bf) {
  28. int n = 0;
  29. unsigned int d = 1;
  30. while (num / d >= base) d *= base;
  31. while (d != 0) {
  32. int dgt = num / d;
  33. num %= d;
  34. d /= base;
  35. if (n || dgt > 0 || d == 0) {
  36. *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10);
  37. ++n;
  38. }
  39. }
  40. *bf = 0;
  41. }
  42. static void li2a(long num, char* bf) {
  43. if (num < 0) {
  44. num = -num;
  45. *bf++ = '-';
  46. }
  47. uli2a(num, 10, 0, bf);
  48. }
  49. #endif
  50. static void ui2a(unsigned int num, unsigned int base, int uc, char* bf) {
  51. int n = 0;
  52. unsigned int d = 1;
  53. while (num / d >= base) d *= base;
  54. while (d != 0) {
  55. int dgt = num / d;
  56. num %= d;
  57. d /= base;
  58. if (n || dgt > 0 || d == 0) {
  59. *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10);
  60. ++n;
  61. }
  62. }
  63. *bf = 0;
  64. }
  65. static void i2a(int num, char* bf) {
  66. if (num < 0) {
  67. num = -num;
  68. *bf++ = '-';
  69. }
  70. ui2a(num, 10, 0, bf);
  71. }
  72. static int a2d(char ch) {
  73. if (ch >= '0' && ch <= '9')
  74. return ch - '0';
  75. else if (ch >= 'a' && ch <= 'f')
  76. return ch - 'a' + 10;
  77. else if (ch >= 'A' && ch <= 'F')
  78. return ch - 'A' + 10;
  79. else
  80. return -1;
  81. }
  82. static char a2i(char ch, char** src, int base, int* nump) {
  83. char* p = *src;
  84. int num = 0;
  85. int digit;
  86. while ((digit = a2d(ch)) >= 0) {
  87. if (digit > base) break;
  88. num = num * base + digit;
  89. ch = *p++;
  90. }
  91. *src = p;
  92. *nump = num;
  93. return ch;
  94. }
  95. static void putchw(void* putp, putcf putf, int n, char z, char* bf) {
  96. char fc = z ? '0' : ' ';
  97. char ch;
  98. char* p = bf;
  99. while (*p++ && n > 0) n--;
  100. while (n-- > 0) putf(putp, fc);
  101. while ((ch = *bf++)) putf(putp, ch);
  102. }
  103. void tfp_format(void* putp, putcf putf, char* fmt, va_list va) {
  104. char bf[12];
  105. char ch;
  106. while ((ch = *(fmt++))) {
  107. if (ch != '%')
  108. putf(putp, ch);
  109. else {
  110. char lz = 0;
  111. #ifdef PRINTF_LONG_SUPPORT
  112. char lng = 0;
  113. #endif
  114. int w = 0;
  115. ch = *(fmt++);
  116. if (ch == '0') {
  117. ch = *(fmt++);
  118. lz = 1;
  119. }
  120. if (ch >= '0' && ch <= '9') {
  121. ch = a2i(ch, &fmt, 10, &w);
  122. }
  123. #ifdef PRINTF_LONG_SUPPORT
  124. if (ch == 'l') {
  125. ch = *(fmt++);
  126. lng = 1;
  127. }
  128. #endif
  129. switch (ch) {
  130. case 0:
  131. goto abort;
  132. case 'u': {
  133. #ifdef PRINTF_LONG_SUPPORT
  134. if (lng)
  135. uli2a(va_arg(va, unsigned long int), 10, 0, bf);
  136. else
  137. #endif
  138. ui2a(va_arg(va, unsigned int), 10, 0, bf);
  139. putchw(putp, putf, w, lz, bf);
  140. break;
  141. }
  142. case 'd': {
  143. #ifdef PRINTF_LONG_SUPPORT
  144. if (lng)
  145. li2a(va_arg(va, unsigned long int), bf);
  146. else
  147. #endif
  148. i2a(va_arg(va, int), bf);
  149. putchw(putp, putf, w, lz, bf);
  150. break;
  151. }
  152. case 'x':
  153. case 'X':
  154. #ifdef PRINTF_LONG_SUPPORT
  155. if (lng)
  156. uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf);
  157. else
  158. #endif
  159. ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf);
  160. putchw(putp, putf, w, lz, bf);
  161. break;
  162. case 'c':
  163. putf(putp, (char)(va_arg(va, int)));
  164. break;
  165. case 's':
  166. putchw(putp, putf, w, 0, va_arg(va, char*));
  167. break;
  168. case 'b':
  169. #ifdef PRINTF_LONG_SUPPORT
  170. if (lng)
  171. uli2a(va_arg(va, unsigned long int), 2, 0, bf);
  172. else
  173. #endif
  174. ui2a(va_arg(va, unsigned int), 2, 0, bf);
  175. putchw(putp, putf, w, lz, bf);
  176. break;
  177. case '%':
  178. putf(putp, ch);
  179. default:
  180. break;
  181. }
  182. }
  183. }
  184. abort:;
  185. }
  186. void init_printf(void* putp, void (*putf)(void*, char)) {
  187. stdout_putf = putf;
  188. stdout_putp = putp;
  189. }
  190. void tfp_printf(char* fmt, ...) {
  191. va_list va;
  192. va_start(va, fmt);
  193. tfp_format(stdout_putp, stdout_putf, fmt, va);
  194. va_end(va);
  195. }
  196. static void putcp(void* p, char c) { *(*((char**)p))++ = c; }
  197. void tfp_sprintf(char* s, char* fmt, ...) {
  198. va_list va;
  199. va_start(va, fmt);
  200. tfp_format(&s, putcp, fmt, va);
  201. putcp(&s, 0);
  202. va_end(va);
  203. }