stdio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <kernel/stdio.h>
  2. #include <types/stdint.h>
  3. // where n is in the range of [0, 9]
  4. static inline char d_to_c(int32_t n)
  5. {
  6. return '0' + n;
  7. }
  8. // where n is between 0 and 15
  9. // base is either 'a' of 'A',
  10. // depending on you want capitalized
  11. // or not
  12. static inline char hex_to_c(int32_t n, char base)
  13. {
  14. if (n < 10) {
  15. // n belongs to [0, 9]
  16. return d_to_c(n);
  17. } else {
  18. // n belongs to [10, 15]
  19. return base + (n - 10);
  20. }
  21. }
  22. static inline char x_to_c(int32_t n)
  23. {
  24. return hex_to_c(n, 'a');
  25. }
  26. static inline char X_to_c(int32_t n)
  27. {
  28. return hex_to_c(n, 'A');
  29. }
  30. // this will check if there is still free space
  31. // in the buffer. if so, push the char into it,
  32. // change the value of buf_size and move pointer
  33. // forward
  34. //
  35. // x: char* buf
  36. // y: size_t buf_size
  37. // z: char c
  38. #define do_write_if_free(x, y, z) \
  39. if ((y) > 1) { \
  40. *((x)++) = (z); \
  41. --(y); \
  42. }
  43. size_t
  44. snprint_decimal(
  45. char* buf,
  46. size_t buf_size,
  47. int32_t num)
  48. {
  49. size_t n_write = 0;
  50. if (num < 0) {
  51. do_write_if_free(buf, buf_size, '-');
  52. ++n_write;
  53. num *= (-1);
  54. }
  55. char* orig_buf = buf;
  56. do {
  57. do_write_if_free(buf, buf_size, d_to_c(num % 10));
  58. num /= 10;
  59. ++n_write;
  60. } while (num != 0);
  61. // prepend trailing '\0'
  62. if (buf_size > 0)
  63. *buf = 0x00;
  64. // move buf pointer to the last digit of number
  65. --buf;
  66. // reverse output
  67. while (orig_buf < buf) {
  68. char c = *buf;
  69. *buf = *orig_buf;
  70. *orig_buf = c;
  71. --buf;
  72. ++orig_buf;
  73. }
  74. return n_write;
  75. }
  76. size_t
  77. snprint_hex(
  78. char* buf,
  79. size_t buf_size,
  80. uint32_t num,
  81. int32_t capitalized)
  82. {
  83. size_t n_write = 0;
  84. char* orig_buf = buf;
  85. do {
  86. if (capitalized) {
  87. do_write_if_free(buf, buf_size, X_to_c(num % 16));
  88. } else {
  89. do_write_if_free(buf, buf_size, x_to_c(num % 16));
  90. }
  91. num /= 16;
  92. ++n_write;
  93. } while (num != 0);
  94. // prepend trailing '\0'
  95. if (buf_size > 0)
  96. *buf = 0x00;
  97. // move buf pointer to the last digit of number
  98. --buf;
  99. // reverse output
  100. while (orig_buf < buf) {
  101. char c = *buf;
  102. *buf = *orig_buf;
  103. *orig_buf = c;
  104. --buf;
  105. ++orig_buf;
  106. }
  107. return n_write;
  108. }
  109. static inline size_t
  110. snprint_char(
  111. char* buf,
  112. size_t buf_size,
  113. char c)
  114. {
  115. if (buf_size > 1)
  116. *buf = c;
  117. return sizeof(c);
  118. }
  119. size_t
  120. snprintf(
  121. char* buf,
  122. size_t buf_size,
  123. const char* fmt,
  124. ...)
  125. {
  126. size_t n_write = 0;
  127. void* arg_ptr = ((void*)&buf) + sizeof(char*) + sizeof(size_t) + sizeof(const char*);
  128. for (char c; (c = *fmt) != 0x00; ++fmt) {
  129. if (c == '%') {
  130. size_t n_tmp_write = 0;
  131. switch (*(++fmt)) {
  132. // int32 decimal
  133. case 'd':
  134. n_tmp_write = snprint_decimal(buf, buf_size, *(int32_t*)arg_ptr);
  135. arg_ptr += sizeof(int32_t);
  136. break;
  137. case 'x':
  138. n_tmp_write = snprint_hex(buf, buf_size, *(uint32_t*)arg_ptr, 0);
  139. arg_ptr += sizeof(uint32_t);
  140. break;
  141. case 'X':
  142. n_tmp_write = snprint_hex(buf, buf_size, *(uint32_t*)arg_ptr, 1);
  143. arg_ptr += sizeof(uint32_t);
  144. break;
  145. // c string
  146. case 's':
  147. n_tmp_write = snprintf(buf, buf_size, *(const char**)arg_ptr);
  148. arg_ptr += sizeof(const char*);
  149. break;
  150. // int8 char
  151. case 'c':
  152. n_tmp_write = snprint_char(buf, buf_size, *(char*)arg_ptr);
  153. arg_ptr += sizeof(char);
  154. break;
  155. default:
  156. n_tmp_write = snprint_char(buf, buf_size, *(fmt - 1));
  157. break;
  158. }
  159. n_write += n_tmp_write;
  160. if (buf_size > 1) {
  161. if (buf_size > n_tmp_write) {
  162. buf += n_tmp_write;
  163. buf_size -= n_tmp_write;
  164. } else {
  165. // no enough space
  166. // shrink buf_size to one
  167. buf += (buf_size - 1);
  168. buf_size = 1;
  169. }
  170. }
  171. } else {
  172. ++n_write;
  173. do_write_if_free(buf, buf_size, c);
  174. }
  175. }
  176. if (buf_size > 0)
  177. *buf = 0x00;
  178. return n_write;
  179. }