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.
 
 
 
 

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