Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. /*
  2. File: printf.c
  3.  
  4. Copyright (C) 2004 Kustaa Nyholm
  5.  
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10.  
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15.  
  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. */
  21.  
  22. #include "printf.h"
  23.  
  24. typedef void (*putcf) (void*,char);
  25. static putcf stdout_putf;
  26. static void* stdout_putp;
  27.  
  28.  
  29. #ifdef PRINTF_LONG_SUPPORT
  30.  
  31. static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf)
  32. {
  33. int n=0;
  34. unsigned int d=1;
  35. while (num/d >= base)
  36. d*=base;
  37. while (d!=0) {
  38. int dgt = num / d;
  39. num%=d;
  40. d/=base;
  41. if (n || dgt>0|| d==0) {
  42. *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
  43. ++n;
  44. }
  45. }
  46. *bf=0;
  47. }
  48.  
  49. static void li2a (long num, char * bf)
  50. {
  51. if (num<0) {
  52. num=-num;
  53. *bf++ = '-';
  54. }
  55. uli2a(num,10,0,bf);
  56. }
  57.  
  58. #endif
  59.  
  60. static void ui2a(unsigned int num, unsigned int base, int uc,char * bf)
  61. {
  62. int n=0;
  63. unsigned int d=1;
  64. while (num/d >= base)
  65. d*=base;
  66. while (d!=0) {
  67. int dgt = num / d;
  68. num%= d;
  69. d/=base;
  70. if (n || dgt>0 || d==0) {
  71. *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
  72. ++n;
  73. }
  74. }
  75. *bf=0;
  76. }
  77.  
  78. static void i2a (int num, char * bf)
  79. {
  80. if (num<0) {
  81. num=-num;
  82. *bf++ = '-';
  83. }
  84. ui2a(num,10,0,bf);
  85. }
  86.  
  87. static int a2d(char ch)
  88. {
  89. if (ch>='0' && ch<='9')
  90. return ch-'0';
  91. else if (ch>='a' && ch<='f')
  92. return ch-'a'+10;
  93. else if (ch>='A' && ch<='F')
  94. return ch-'A'+10;
  95. else return -1;
  96. }
  97.  
  98. static char a2i(char ch, char** src,int base,int* nump)
  99. {
  100. char* p= *src;
  101. int num=0;
  102. int digit;
  103. while ((digit=a2d(ch))>=0) {
  104. if (digit>base) break;
  105. num=num*base+digit;
  106. ch=*p++;
  107. }
  108. *src=p;
  109. *nump=num;
  110. return ch;
  111. }
  112.  
  113. static void putchw(void* putp,putcf putf,int n, char z, char* bf)
  114. {
  115. char fc=z? '0' : ' ';
  116. char ch;
  117. char* p=bf;
  118. while (*p++ && n > 0)
  119. n--;
  120. while (n-- > 0)
  121. putf(putp,fc);
  122. while ((ch= *bf++))
  123. putf(putp,ch);
  124. }
  125.  
  126. void tfp_format(void* putp,putcf putf,char *fmt, va_list va)
  127. {
  128. char bf[12];
  129.  
  130. char ch;
  131.  
  132.  
  133. while ((ch=*(fmt++))) {
  134. if (ch!='%')
  135. putf(putp,ch);
  136. else {
  137. char lz=0;
  138. #ifdef PRINTF_LONG_SUPPORT
  139. char lng=0;
  140. #endif
  141. int w=0;
  142. ch=*(fmt++);
  143. if (ch=='0') {
  144. ch=*(fmt++);
  145. lz=1;
  146. }
  147. if (ch>='0' && ch<='9') {
  148. ch=a2i(ch,&fmt,10,&w);
  149. }
  150. #ifdef PRINTF_LONG_SUPPORT
  151. if (ch=='l') {
  152. ch=*(fmt++);
  153. lng=1;
  154. }
  155. #endif
  156. switch (ch) {
  157. case 0:
  158. goto abort;
  159. case 'u' : {
  160. #ifdef PRINTF_LONG_SUPPORT
  161. if (lng)
  162. uli2a(va_arg(va, unsigned long int),10,0,bf);
  163. else
  164. #endif
  165. ui2a(va_arg(va, unsigned int),10,0,bf);
  166. putchw(putp,putf,w,lz,bf);
  167. break;
  168. }
  169. case 'd' : {
  170. #ifdef PRINTF_LONG_SUPPORT
  171. if (lng)
  172. li2a(va_arg(va, unsigned long int),bf);
  173. else
  174. #endif
  175. i2a(va_arg(va, int),bf);
  176. putchw(putp,putf,w,lz,bf);
  177. break;
  178. }
  179. case 'x': case 'X' :
  180. #ifdef PRINTF_LONG_SUPPORT
  181. if (lng)
  182. uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf);
  183. else
  184. #endif
  185. ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf);
  186. putchw(putp,putf,w,lz,bf);
  187. break;
  188. case 'c' :
  189. putf(putp,(char)(va_arg(va, int)));
  190. break;
  191. case 's' :
  192. putchw(putp,putf,w,0,va_arg(va, char*));
  193. break;
  194. case '%' :
  195. putf(putp,ch);
  196. default:
  197. break;
  198. }
  199. }
  200. }
  201. abort:;
  202. }
  203.  
  204.  
  205. void init_printf(void* putp,void (*putf) (void*,char))
  206. {
  207. stdout_putf=putf;
  208. stdout_putp=putp;
  209. }
  210.  
  211. void tfp_printf(char *fmt, ...)
  212. {
  213. va_list va;
  214. va_start(va,fmt);
  215. tfp_format(stdout_putp,stdout_putf,fmt,va);
  216. va_end(va);
  217. }
  218.  
  219. static void putcp(void* p,char c)
  220. {
  221. *(*((char**)p))++ = c;
  222. }
  223.  
  224.  
  225.  
  226. void tfp_sprintf(char* s,char *fmt, ...)
  227. {
  228. va_list va;
  229. va_start(va,fmt);
  230. tfp_format(&s,putcp,fmt,va);
  231. putcp(&s,0);
  232. va_end(va);
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement