You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.0 KiB

  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, const char** src, int base, int* nump) {
  83. const 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, const char* fmt, va_list va) {
  104. // This used to handle max of 12, but binary support jumps this to at least 32
  105. char bf[36];
  106. char ch;
  107. while ((ch = *(fmt++))) {
  108. if (ch != '%')
  109. putf(putp, ch);
  110. else {
  111. char lz = 0;
  112. #ifdef PRINTF_LONG_SUPPORT
  113. char lng = 0;
  114. #endif
  115. int w = 0;
  116. ch = *(fmt++);
  117. if (ch == '0') {
  118. ch = *(fmt++);
  119. lz = 1;
  120. }
  121. if (ch >= '0' && ch <= '9') {
  122. ch = a2i(ch, &fmt, 10, &w);
  123. }
  124. #ifdef PRINTF_LONG_SUPPORT
  125. if (ch == 'l') {
  126. ch = *(fmt++);
  127. lng = 1;
  128. }
  129. #endif
  130. switch (ch) {
  131. case 0:
  132. goto abort;
  133. case 'u': {
  134. #ifdef PRINTF_LONG_SUPPORT
  135. if (lng)
  136. uli2a(va_arg(va, unsigned long int), 10, 0, bf);
  137. else
  138. #endif
  139. ui2a(va_arg(va, unsigned int), 10, 0, bf);
  140. putchw(putp, putf, w, lz, bf);
  141. break;
  142. }
  143. case 'd': {
  144. #ifdef PRINTF_LONG_SUPPORT
  145. if (lng)
  146. li2a(va_arg(va, unsigned long int), bf);
  147. else
  148. #endif
  149. i2a(va_arg(va, int), bf);
  150. putchw(putp, putf, w, lz, bf);
  151. break;
  152. }
  153. case 'x':
  154. case 'X':
  155. #ifdef PRINTF_LONG_SUPPORT
  156. if (lng)
  157. uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf);
  158. else
  159. #endif
  160. ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf);
  161. putchw(putp, putf, w, lz, bf);
  162. break;
  163. case 'c':
  164. putf(putp, (char)(va_arg(va, int)));
  165. break;
  166. case 's':
  167. putchw(putp, putf, w, 0, va_arg(va, char*));
  168. break;
  169. case 'b':
  170. #ifdef PRINTF_LONG_SUPPORT
  171. if (lng)
  172. uli2a(va_arg(va, unsigned long int), 2, 0, bf);
  173. else
  174. #endif
  175. ui2a(va_arg(va, unsigned int), 2, 0, bf);
  176. putchw(putp, putf, w, lz, bf);
  177. break;
  178. case '%':
  179. putf(putp, ch);
  180. default:
  181. break;
  182. }
  183. }
  184. }
  185. abort:;
  186. }
  187. void init_printf(void* putp, void (*putf)(void*, char)) {
  188. stdout_putf = putf;
  189. stdout_putp = putp;
  190. }
  191. int tfp_printf(const char* fmt, ...) {
  192. va_list va;
  193. va_start(va, fmt);
  194. tfp_format(stdout_putp, stdout_putf, fmt, va);
  195. va_end(va);
  196. return 1;
  197. }
  198. static void putcp(void* p, char c) { *(*((char**)p))++ = c; }
  199. int tfp_sprintf(char* s, const char* fmt, ...) {
  200. va_list va;
  201. va_start(va, fmt);
  202. tfp_format(&s, putcp, fmt, va);
  203. putcp(&s, 0);
  204. va_end(va);
  205. return 1;
  206. }