Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

476 rader
15 KiB

  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <stdarg.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "hashmap.hpp"
  9. #include "hooks.hpp"
  10. FILE* ftb_stdout = stdout;
  11. #define str_eq(s1, s2) (strcmp(s1, s2) == 0)
  12. #define console_normal "\x1B[0m"
  13. #define console_red "\x1B[31m"
  14. #define console_green "\x1B[32m"
  15. #define console_cyan "\x1B[36m"
  16. typedef const char* static_string;
  17. typedef int (*printer_function_32b)(FILE*, u32);
  18. typedef int (*printer_function_64b)(FILE*, u64);
  19. typedef int (*printer_function_flt)(FILE*, double);
  20. typedef int (*printer_function_ptr)(FILE*, void*);
  21. typedef int (*printer_function_void)(FILE*);
  22. enum struct Printer_Function_Type {
  23. unknown,
  24. _32b,
  25. _64b,
  26. _flt,
  27. _ptr,
  28. _void
  29. };
  30. Array_List<char*> color_stack = {0};
  31. Hash_Map<char*, printer_function_ptr> printer_map = {0};
  32. Hash_Map<char*, int> type_map = {0};
  33. #define register_printer(spec, fun, type) \
  34. register_printer_ptr(spec, (printer_function_ptr)fun, type)
  35. void register_printer_ptr(const char* spec, printer_function_ptr fun, Printer_Function_Type type) {
  36. printer_map.set_object((char*)spec, fun);
  37. type_map.set_object((char*)spec, (int)type);
  38. }
  39. int maybe_special_print(FILE* file, static_string format, int* pos, va_list* arg_list) {
  40. if(format[*pos] != '{')
  41. return 0;
  42. int end_pos = (*pos)+1;
  43. while (format[end_pos] != '}' &&
  44. format[end_pos] != ',' &&
  45. format[end_pos] != '[' &&
  46. format[end_pos] != 0)
  47. ++end_pos;
  48. if (format[end_pos] == 0)
  49. return 0;
  50. char* spec = (char*)malloc(end_pos - (*pos));
  51. strncpy(spec, format+(*pos)+1, end_pos - (*pos));
  52. spec[end_pos - (*pos)-1] = '\0';
  53. u64 spec_hash = hm_hash(spec);
  54. Printer_Function_Type type = (Printer_Function_Type)type_map.get_object(spec, spec_hash);
  55. union {
  56. printer_function_32b printer_32b;
  57. printer_function_64b printer_64b;
  58. printer_function_ptr printer_ptr;
  59. printer_function_flt printer_flt;
  60. printer_function_void printer_void;
  61. } printer;
  62. // just grab it, it will have the correct type
  63. printer.printer_ptr = printer_map.get_object(spec, spec_hash);
  64. if (type == Printer_Function_Type::unknown) {
  65. printf("ERROR: %s printer not found\n", spec);
  66. free(spec);
  67. return 0;
  68. }
  69. free(spec);
  70. if (format[end_pos] == ',') {
  71. int element_count;
  72. ++end_pos;
  73. sscanf(format+end_pos, "%d", &element_count);
  74. while (format[end_pos] != '}' &&
  75. format[end_pos] != 0)
  76. ++end_pos;
  77. if (format[end_pos] == 0)
  78. return 0;
  79. // both brackets already included:
  80. int written_length = 2;
  81. fputs("[", file);
  82. for (int i = 0; i < element_count - 1; ++i) {
  83. if (type == Printer_Function_Type::_32b) written_length += printer.printer_32b(file, va_arg(*arg_list, u32));
  84. else if (type == Printer_Function_Type::_64b) written_length += printer.printer_64b(file, va_arg(*arg_list, u64));
  85. else if (type == Printer_Function_Type::_flt) written_length += printer.printer_flt(file, va_arg(*arg_list, double));
  86. else if (type == Printer_Function_Type::_ptr) written_length += printer.printer_ptr(file, va_arg(*arg_list, void*));
  87. else written_length += printer.printer_void(file);
  88. written_length += 2;
  89. fputs(", ", file);
  90. }
  91. if (element_count > 0) {
  92. if (type == Printer_Function_Type::_32b) written_length += printer.printer_32b(file, va_arg(*arg_list, u32));
  93. else if (type == Printer_Function_Type::_64b) written_length += printer.printer_64b(file, va_arg(*arg_list, u64));
  94. else if (type == Printer_Function_Type::_flt) written_length += printer.printer_flt(file, va_arg(*arg_list, double));
  95. else if (type == Printer_Function_Type::_ptr) written_length += printer.printer_ptr(file, va_arg(*arg_list, void*));
  96. else written_length += printer.printer_void(file);
  97. }
  98. fputs("]", file);
  99. *pos = end_pos;
  100. return written_length;
  101. } else if (format[end_pos] == '[') {
  102. end_pos++;
  103. u32 element_count;
  104. union {
  105. u32* arr_32b;
  106. u64* arr_64b;
  107. f32* arr_flt;
  108. void** arr_ptr;
  109. } arr;
  110. if (type == Printer_Function_Type::_32b) arr.arr_32b = va_arg(*arg_list, u32*);
  111. else if (type == Printer_Function_Type::_64b) arr.arr_64b = va_arg(*arg_list, u64*);
  112. else if (type == Printer_Function_Type::_flt) arr.arr_flt = va_arg(*arg_list, f32*);
  113. else arr.arr_ptr = va_arg(*arg_list, void**);
  114. if (format[end_pos] == '*') {
  115. element_count = va_arg(*arg_list, u32);
  116. } else {
  117. sscanf(format+end_pos, "%d", &element_count);
  118. }
  119. // skip to next ']'
  120. while (format[end_pos] != ']' &&
  121. format[end_pos] != 0)
  122. ++end_pos;
  123. if (format[end_pos] == 0)
  124. return 0;
  125. // skip to next '}'
  126. while (format[end_pos] != '}' &&
  127. format[end_pos] != 0)
  128. ++end_pos;
  129. if (format[end_pos] == 0)
  130. return 0;
  131. // both brackets already included:
  132. int written_length = 2;
  133. fputs("[", file);
  134. for (u32 i = 0; i < element_count - 1; ++i) {
  135. if (type == Printer_Function_Type::_32b) written_length += printer.printer_32b(file, arr.arr_32b[i]);
  136. else if (type == Printer_Function_Type::_64b) written_length += printer.printer_64b(file, arr.arr_64b[i]);
  137. else if (type == Printer_Function_Type::_flt) written_length += printer.printer_flt(file, arr.arr_flt[i]);
  138. else if (type == Printer_Function_Type::_ptr) written_length += printer.printer_ptr(file, arr.arr_ptr[i]);
  139. else written_length += printer.printer_void(file);
  140. written_length += 2;
  141. fputs(", ", file);
  142. }
  143. if (element_count > 0) {
  144. if (type == Printer_Function_Type::_32b) written_length += printer.printer_32b(file, arr.arr_32b[element_count - 1]);
  145. else if (type == Printer_Function_Type::_64b) written_length += printer.printer_64b(file, arr.arr_64b[element_count - 1]);
  146. else if (type == Printer_Function_Type::_flt) written_length += printer.printer_flt(file, arr.arr_flt[element_count - 1]);
  147. else if (type == Printer_Function_Type::_ptr) written_length += printer.printer_ptr(file, arr.arr_ptr[element_count - 1]);
  148. else written_length += printer.printer_void(file);
  149. }
  150. fputs("]", file);
  151. *pos = end_pos;
  152. return written_length;
  153. } else {
  154. *pos = end_pos;
  155. if (type == Printer_Function_Type::_32b) return printer.printer_32b(file, va_arg(*arg_list, u32));
  156. else if (type == Printer_Function_Type::_64b) return printer.printer_64b(file, va_arg(*arg_list, u64));
  157. else if (type == Printer_Function_Type::_flt) return printer.printer_flt(file, va_arg(*arg_list, double));
  158. else if (type == Printer_Function_Type::_ptr) return printer.printer_ptr(file, va_arg(*arg_list, void*));
  159. else return printer.printer_void(file);
  160. }
  161. return 0;
  162. }
  163. int maybe_fprintf(FILE* file, static_string format, int* pos, va_list* arg_list) {
  164. // %[flags][width][.precision][length]specifier
  165. // flags ::= [+- #0]
  166. // width ::= [<number>+ \*]
  167. // precision ::= \.[<number>+ \*]
  168. // length ::= [h l L]
  169. // specifier ::= [c d i e E f g G o s u x X p n %]
  170. int end_pos = *pos;
  171. int writen_len = 0;
  172. int used_arg_values = 1;
  173. // overstep flags:
  174. while(format[end_pos] == '+' ||
  175. format[end_pos] == '-' ||
  176. format[end_pos] == ' ' ||
  177. format[end_pos] == '#' ||
  178. format[end_pos] == '0')
  179. ++end_pos;
  180. // overstep width
  181. if (format[end_pos] == '*') {
  182. ++used_arg_values;
  183. ++end_pos;
  184. }
  185. else {
  186. while(format[end_pos] >= '0' && format[end_pos] <= '9')
  187. ++end_pos;
  188. }
  189. // overstep precision
  190. if (format[end_pos] == '.') {
  191. ++end_pos;
  192. if (format[end_pos] == '*') {
  193. ++used_arg_values;
  194. ++end_pos;
  195. }
  196. else {
  197. while(format[end_pos] >= '0' && format[end_pos] <= '9')
  198. ++end_pos;
  199. }
  200. }
  201. // overstep length:
  202. while(format[end_pos] == 'h' ||
  203. format[end_pos] == 'l' ||
  204. format[end_pos] == 'L')
  205. ++end_pos;
  206. // check for actual built_in specifier
  207. if(format[end_pos] == 'c' ||
  208. format[end_pos] == 'd' ||
  209. format[end_pos] == 'i' ||
  210. format[end_pos] == 'e' ||
  211. format[end_pos] == 'E' ||
  212. format[end_pos] == 'f' ||
  213. format[end_pos] == 'g' ||
  214. format[end_pos] == 'G' ||
  215. format[end_pos] == 'o' ||
  216. format[end_pos] == 's' ||
  217. format[end_pos] == 'u' ||
  218. format[end_pos] == 'x' ||
  219. format[end_pos] == 'X' ||
  220. format[end_pos] == 'p' ||
  221. format[end_pos] == 'n' ||
  222. format[end_pos] == '%')
  223. {
  224. writen_len = end_pos - *pos + 2;
  225. char* temp = (char*)malloc((writen_len+1)* sizeof(char));
  226. temp[0] = '%';
  227. temp[1] = 0;
  228. strncpy(temp+1, format+*pos, writen_len);
  229. temp[writen_len] = 0;
  230. // printf("\ntest:: len(%s) = %d\n", temp, writen_len+1);
  231. /// NOTE(Felix): Somehow we have to pass a copy of the list to vfprintf
  232. // because otherwise it destroys it on some platforms :(
  233. va_list arg_list_copy;
  234. va_copy(arg_list_copy, *arg_list);
  235. writen_len = vfprintf(file, temp, arg_list_copy);
  236. va_end(arg_list_copy);
  237. // NOTE(Felix): maually overstep the args that vfprintf will have used
  238. for (int i = 0; i < used_arg_values; ++i) {
  239. va_arg(*arg_list, void*);
  240. }
  241. free(temp);
  242. *pos = end_pos;
  243. }
  244. return writen_len;
  245. }
  246. int print_va_args_to_file(FILE* file, static_string format, va_list* arg_list) {
  247. int printed_chars = 0;
  248. char c;
  249. int pos = -1;
  250. while ((c = format[++pos])) {
  251. if (c != '%') {
  252. putc(c, file);
  253. ++printed_chars;
  254. } else {
  255. c = format[++pos];
  256. int move = maybe_special_print(file, format, &pos, arg_list);
  257. if (move == 0) {
  258. move = maybe_fprintf(file, format, &pos, arg_list);
  259. if (move == -1) {
  260. fputc('%', file);
  261. fputc(c, file);
  262. move = 1;
  263. }
  264. }
  265. printed_chars += move;
  266. }
  267. }
  268. return printed_chars;
  269. }
  270. int print_va_args_to_string(char** out, static_string format, va_list* arg_list) {
  271. FILE* t_file = tmpfile();
  272. if (!t_file) {
  273. return 0;
  274. }
  275. int num_printed_chars = print_va_args_to_file(t_file, format, arg_list);
  276. *out = (char*)malloc(sizeof(char) * (num_printed_chars+1));
  277. rewind(t_file);
  278. fread(*out, sizeof(char), num_printed_chars, t_file);
  279. (*out)[num_printed_chars] = '\0';
  280. fclose(t_file);
  281. return num_printed_chars;
  282. }
  283. int print_va_args(static_string format, va_list* arg_list) {
  284. return print_va_args_to_file(stdout, format, arg_list);
  285. }
  286. int print_to_string(char** out, static_string format, ...) {
  287. va_list arg_list;
  288. va_start(arg_list, format);
  289. FILE* t_file = tmpfile();
  290. if (!t_file) {
  291. return 0;
  292. }
  293. int num_printed_chars = print_va_args_to_file(t_file, format, &arg_list);
  294. va_end(arg_list);
  295. *out = (char*)malloc(sizeof(char) * (num_printed_chars+1));
  296. rewind(t_file);
  297. fread(*out, sizeof(char), num_printed_chars, t_file);
  298. (*out)[num_printed_chars] = '\0';
  299. fclose(t_file);
  300. return num_printed_chars;
  301. }
  302. int print_to_file(FILE* file, static_string format, ...) {
  303. va_list arg_list;
  304. va_start(arg_list, format);
  305. int num_printed_chars = print_va_args_to_file(file, format, &arg_list);
  306. va_end(arg_list);
  307. return num_printed_chars;
  308. }
  309. int print(static_string format, ...) {
  310. va_list arg_list;
  311. va_start(arg_list, format);
  312. int num_printed_chars = print_va_args_to_file(ftb_stdout, format, &arg_list);
  313. va_end(arg_list);
  314. return num_printed_chars;
  315. }
  316. int print_bool(FILE* f, u32 val) {
  317. return print_to_file(f, val ? "true" : "false");
  318. }
  319. int print_u32(FILE* f, u32 num) {
  320. return print_to_file(f, "%u", num);
  321. }
  322. int print_u64(FILE* f, u64 num) {
  323. return print_to_file(f, "%llu", num);
  324. }
  325. int print_s32(FILE* f, s32 num) {
  326. return print_to_file(f, "%d", num);
  327. }
  328. int print_s64(FILE* f, s64 num) {
  329. return print_to_file(f, "%lld", num);
  330. }
  331. int print_flt(FILE* f, double arg) {
  332. return print_to_file(f, "%f", arg);
  333. }
  334. int print_str(FILE* f, char* str) {
  335. return print_to_file(f, "%s", str);
  336. }
  337. int print_color_start(FILE* f, char* str) {
  338. color_stack.append(str);
  339. return print_to_file(f, "%s", str);
  340. }
  341. int print_color_end(FILE* f) {
  342. --color_stack.count;
  343. if (color_stack.count == 0) {
  344. return print_to_file(f, "%s", console_normal);
  345. } else {
  346. return print_to_file(f, "%s", color_stack[color_stack.count-1]);
  347. }
  348. }
  349. int print_ptr(FILE* f, void* ptr) {
  350. if (ptr)
  351. return print_to_file(f, "%#0*X", sizeof(void*)*2+2, ptr);
  352. return print_to_file(f, "nullptr");
  353. }
  354. auto print_Str(FILE* f, String* str) -> s32 {
  355. return print_to_file(f, "%.*s", str->length, str->data);
  356. }
  357. auto print_str_line(FILE* f, char* str) -> s32 {
  358. u32 length = 0;
  359. char* str_cpy = str;
  360. while (str[length] != '\0') {
  361. if (str[length] == '\n')
  362. break;
  363. length++;
  364. }
  365. return print_to_file(f, "%.*s", length, str);
  366. }
  367. void init_printer() {
  368. color_stack.alloc();
  369. printer_map.alloc();
  370. type_map.alloc();
  371. system_shutdown_hook << [](){
  372. color_stack.dealloc();
  373. printer_map.dealloc();
  374. type_map.dealloc();
  375. };
  376. register_printer("u32", print_u32, Printer_Function_Type::_32b);
  377. register_printer("u64", print_u64, Printer_Function_Type::_64b);
  378. register_printer("bool", print_bool, Printer_Function_Type::_32b);
  379. register_printer("s64", print_s64, Printer_Function_Type::_64b);
  380. register_printer("s32", print_s32, Printer_Function_Type::_32b);
  381. register_printer("f32", print_flt, Printer_Function_Type::_flt);
  382. register_printer("f64", print_flt, Printer_Function_Type::_flt);
  383. register_printer("->char", print_str, Printer_Function_Type::_ptr);
  384. register_printer("->", print_ptr, Printer_Function_Type::_ptr);
  385. register_printer("color<", print_color_start, Printer_Function_Type::_ptr);
  386. register_printer(">color", print_color_end, Printer_Function_Type::_void);
  387. register_printer("->Str", print_Str, Printer_Function_Type::_ptr);
  388. register_printer("->char_line", print_str_line, Printer_Function_Type::_ptr);
  389. }