fprintf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fprintf.c
  15. * Date: 2021-08-23 16:24:02
  16. * LastEditTime: 2022-02-17 18:01:19
  17. * Description:  This file is for creating custom print interface for standlone sdk.
  18. *
  19. * Modify History:
  20. * Ver Who Date Changes
  21. * ----- ------ -------- --------------------------------------
  22. * 1.0 huanghe 2022/7/23 first release
  23. */
  24. #include <stdarg.h>
  25. #include "fearly_uart.h"
  26. #define putchar(c) OutByte(c)
  27. #define PAD_RIGHT 1
  28. #define PAD_ZERO 2
  29. static void printchar(char **str, int c)
  30. {
  31. if (str)
  32. {
  33. **str = c;
  34. ++(*str);
  35. }
  36. else
  37. {
  38. (void)putchar((const char)c);
  39. }
  40. }
  41. static int prints(char **out, const char *string, int width, int pad)
  42. {
  43. register int pc = 0, padchar = ' ';
  44. if (width > 0)
  45. {
  46. register int len = 0;
  47. register const char *ptr;
  48. for (ptr = string; *ptr; ++ptr)
  49. {
  50. ++len;
  51. }
  52. if (len >= width)
  53. {
  54. width = 0;
  55. }
  56. else
  57. {
  58. width -= len;
  59. }
  60. if (pad & PAD_ZERO)
  61. {
  62. padchar = '0';
  63. }
  64. }
  65. if (!(pad & PAD_RIGHT))
  66. {
  67. for (; width > 0; --width)
  68. {
  69. printchar(out, padchar);
  70. ++pc;
  71. }
  72. }
  73. for (; *string; ++string)
  74. {
  75. printchar(out, *string);
  76. ++pc;
  77. }
  78. for (; width > 0; --width)
  79. {
  80. printchar(out, padchar);
  81. ++pc;
  82. }
  83. return pc;
  84. }
  85. /* the following should be enough for 32 bit int */
  86. #define PRINT_BUF_LEN 12
  87. static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
  88. {
  89. char print_buf[PRINT_BUF_LEN];
  90. register char *s;
  91. register int t, neg = 0, pc = 0;
  92. register unsigned int u = i;
  93. if (i == 0)
  94. {
  95. print_buf[0] = '0';
  96. print_buf[1] = '\0';
  97. return prints(out, print_buf, width, pad);
  98. }
  99. if (sg && b == 10 && i < 0)
  100. {
  101. neg = 1;
  102. u = -i;
  103. }
  104. s = print_buf + PRINT_BUF_LEN - 1;
  105. *s = '\0';
  106. while (u)
  107. {
  108. t = u % b;
  109. if (t >= 10)
  110. {
  111. t += letbase - '0' - 10;
  112. }
  113. *--s = t + '0';
  114. u /= b;
  115. }
  116. if (neg)
  117. {
  118. if (width && (pad & PAD_ZERO))
  119. {
  120. printchar(out, '-');
  121. ++pc;
  122. --width;
  123. }
  124. else
  125. {
  126. *--s = '-';
  127. }
  128. }
  129. return pc + prints(out, s, width, pad);
  130. }
  131. static int print(char **out, const char *format, va_list args)
  132. {
  133. register int width, pad;
  134. register int pc = 0;
  135. char scr[2];
  136. for (; *format != 0; ++format)
  137. {
  138. if (*format == '%')
  139. {
  140. ++format;
  141. width = pad = 0;
  142. if (*format == '\0')
  143. {
  144. break;
  145. }
  146. if (*format == '%')
  147. {
  148. goto out;
  149. }
  150. if (*format == '-')
  151. {
  152. ++format;
  153. pad = PAD_RIGHT;
  154. }
  155. while (*format == '0')
  156. {
  157. ++format;
  158. pad |= PAD_ZERO;
  159. }
  160. for (; *format >= '0' && *format <= '9'; ++format)
  161. {
  162. width *= 10;
  163. width += *format - '0';
  164. }
  165. if (*format == 's')
  166. {
  167. //register char *s = (char *)va_arg( args, int );
  168. register char *s = (char *)va_arg(args, char *);
  169. pc += prints(out, s ? s : "(null)", width, pad);
  170. continue;
  171. }
  172. if (*format == 'd')
  173. {
  174. pc += printi(out, va_arg(args, int), 10, 1, width, pad, 'a');
  175. continue;
  176. }
  177. if (*format == 'x')
  178. {
  179. pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'a');
  180. continue;
  181. }
  182. if (*format == 'X')
  183. {
  184. pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'A');
  185. continue;
  186. }
  187. if (*format == 'u')
  188. {
  189. pc += printi(out, va_arg(args, int), 10, 0, width, pad, 'a');
  190. continue;
  191. }
  192. if (*format == 'c')
  193. {
  194. /* char are converted to int then pushed on the stack */
  195. scr[0] = (char)va_arg(args, int);
  196. scr[1] = '\0';
  197. pc += prints(out, scr, width, pad);
  198. continue;
  199. }
  200. }
  201. else
  202. {
  203. out:
  204. printchar(out, *format);
  205. ++pc;
  206. }
  207. }
  208. if (out)
  209. {
  210. **out = '\0';
  211. }
  212. va_end(args);
  213. return pc;
  214. }
  215. int f_printf(const char *format, ...)
  216. {
  217. va_list args;
  218. va_start(args, format);
  219. return print(0, format, args);
  220. }
  221. #ifdef TEST_PRINTF
  222. int test_printf(void)
  223. {
  224. char *ptr = "Hello world!";
  225. char *np = 0;
  226. int i = 5;
  227. unsigned int bs = sizeof(int) * 8;
  228. int mi;
  229. char buf[80];
  230. mi = (1 << (bs - 1)) + 1;
  231. f_printf("%s\n", ptr);
  232. f_printf("printf test\n");
  233. f_printf("%s is null pointer\n", np);
  234. f_printf("%d = 5\n", i);
  235. f_printf("%d = - max int\n", mi);
  236. f_printf("char %c = 'a'\n", 'a');
  237. f_printf("hex %x = ff\n", 0xff);
  238. f_printf("hex %02x = 00\n", 0);
  239. f_printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
  240. f_printf("%d %s(s)%", 0, "message");
  241. f_printf("\n");
  242. f_printf("%d %s(s) with %%\n", 0, "message");
  243. sprintf(buf, "justif: \"%-10s\"\n", "left");
  244. f_printf("%s", buf);
  245. sprintf(buf, "justif: \"%10s\"\n", "right");
  246. f_printf("%s", buf);
  247. sprintf(buf, " 3: %04d zero padded\n", 3);
  248. f_printf("%s", buf);
  249. sprintf(buf, " 3: %-4d left justif.\n", 3);
  250. f_printf("%s", buf);
  251. sprintf(buf, " 3: %4d right justif.\n", 3);
  252. f_printf("%s", buf);
  253. sprintf(buf, "-3: %04d zero padded\n", -3);
  254. f_printf("%s", buf);
  255. sprintf(buf, "-3: %-4d left justif.\n", -3);
  256. f_printf("%s", buf);
  257. sprintf(buf, "-3: %4d right justif.\n", -3);
  258. f_printf("%s", buf);
  259. return 0;
  260. }
  261. #endif