Keyboard firmwares for Atmel AVR and Cortex-M
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.

printf.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. {
  29. int n=0;
  30. unsigned int d=1;
  31. while (num/d >= base)
  32. d*=base;
  33. while (d!=0) {
  34. int dgt = num / d;
  35. num%=d;
  36. d/=base;
  37. if (n || dgt>0|| d==0) {
  38. *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
  39. ++n;
  40. }
  41. }
  42. *bf=0;
  43. }
  44. static void li2a (long num, char * bf)
  45. {
  46. if (num<0) {
  47. num=-num;
  48. *bf++ = '-';
  49. }
  50. uli2a(num,10,0,bf);
  51. }
  52. #endif
  53. static void ui2a(unsigned int num, unsigned int base, int uc,char * bf)
  54. {
  55. int n=0;
  56. unsigned int d=1;
  57. while (num/d >= base)
  58. d*=base;
  59. while (d!=0) {
  60. int dgt = num / d;
  61. num%= d;
  62. d/=base;
  63. if (n || dgt>0 || d==0) {
  64. *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
  65. ++n;
  66. }
  67. }
  68. *bf=0;
  69. }
  70. static void i2a (int num, char * bf)
  71. {
  72. if (num<0) {
  73. num=-num;
  74. *bf++ = '-';
  75. }
  76. ui2a(num,10,0,bf);
  77. }
  78. static int a2d(char ch)
  79. {
  80. if (ch>='0' && ch<='9')
  81. return ch-'0';
  82. else if (ch>='a' && ch<='f')
  83. return ch-'a'+10;
  84. else if (ch>='A' && ch<='F')
  85. return ch-'A'+10;
  86. else return -1;
  87. }
  88. static char a2i(char ch, char** src,int base,int* nump)
  89. {
  90. char* p= *src;
  91. int num=0;
  92. int digit;
  93. while ((digit=a2d(ch))>=0) {
  94. if (digit>base) break;
  95. num=num*base+digit;
  96. ch=*p++;
  97. }
  98. *src=p;
  99. *nump=num;
  100. return ch;
  101. }
  102. static void putchw(void* putp,putcf putf,int n, char z, char* bf)
  103. {
  104. char fc=z? '0' : ' ';
  105. char ch;
  106. char* p=bf;
  107. while (*p++ && n > 0)
  108. n--;
  109. while (n-- > 0)
  110. putf(putp,fc);
  111. while ((ch= *bf++))
  112. putf(putp,ch);
  113. }
  114. void tfp_format(void* putp,putcf putf,char *fmt, va_list va)
  115. {
  116. char bf[12];
  117. char ch;
  118. while ((ch=*(fmt++))) {
  119. if (ch!='%')
  120. putf(putp,ch);
  121. else {
  122. char lz=0;
  123. #ifdef PRINTF_LONG_SUPPORT
  124. char lng=0;
  125. #endif
  126. int w=0;
  127. ch=*(fmt++);
  128. if (ch=='0') {
  129. ch=*(fmt++);
  130. lz=1;
  131. }
  132. if (ch>='0' && ch<='9') {
  133. ch=a2i(ch,&fmt,10,&w);
  134. }
  135. #ifdef PRINTF_LONG_SUPPORT
  136. if (ch=='l') {
  137. ch=*(fmt++);
  138. lng=1;
  139. }
  140. #endif
  141. switch (ch) {
  142. case 0:
  143. goto abort;
  144. case 'u' : {
  145. #ifdef PRINTF_LONG_SUPPORT
  146. if (lng)
  147. uli2a(va_arg(va, unsigned long int),10,0,bf);
  148. else
  149. #endif
  150. ui2a(va_arg(va, unsigned int),10,0,bf);
  151. putchw(putp,putf,w,lz,bf);
  152. break;
  153. }
  154. case 'd' : {
  155. #ifdef PRINTF_LONG_SUPPORT
  156. if (lng)
  157. li2a(va_arg(va, unsigned long int),bf);
  158. else
  159. #endif
  160. i2a(va_arg(va, int),bf);
  161. putchw(putp,putf,w,lz,bf);
  162. break;
  163. }
  164. case 'x': case 'X' :
  165. #ifdef PRINTF_LONG_SUPPORT
  166. if (lng)
  167. uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf);
  168. else
  169. #endif
  170. ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf);
  171. putchw(putp,putf,w,lz,bf);
  172. break;
  173. case 'c' :
  174. putf(putp,(char)(va_arg(va, int)));
  175. break;
  176. case 's' :
  177. putchw(putp,putf,w,0,va_arg(va, char*));
  178. break;
  179. case '%' :
  180. putf(putp,ch);
  181. default:
  182. break;
  183. }
  184. }
  185. }
  186. abort:;
  187. }
  188. void init_printf(void* putp,void (*putf) (void*,char))
  189. {
  190. stdout_putf=putf;
  191. stdout_putp=putp;
  192. }
  193. void tfp_printf(char *fmt, ...)
  194. {
  195. va_list va;
  196. va_start(va,fmt);
  197. tfp_format(stdout_putp,stdout_putf,fmt,va);
  198. va_end(va);
  199. }
  200. static void putcp(void* p,char c)
  201. {
  202. *(*((char**)p))++ = c;
  203. }
  204. void tfp_sprintf(char* s,char *fmt, ...)
  205. {
  206. va_list va;
  207. va_start(va,fmt);
  208. tfp_format(&s,putcp,fmt,va);
  209. putcp(&s,0);
  210. va_end(va);
  211. }