25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

471 satır
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;
  31. Hash_Map<char*, printer_function_ptr> printer_map;
  32. Hash_Map<char*, int> type_map;
  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. // printf("%p \n", printer.printer_ptr);
  155. *pos = end_pos;
  156. if (type == Printer_Function_Type::_32b) return printer.printer_32b(file, va_arg(*arg_list, u32));
  157. else if (type == Printer_Function_Type::_64b) return printer.printer_64b(file, va_arg(*arg_list, u64));
  158. else if (type == Printer_Function_Type::_flt) return printer.printer_flt(file, va_arg(*arg_list, double));
  159. else if (type == Printer_Function_Type::_ptr) return printer.printer_ptr(file, va_arg(*arg_list, void*));
  160. else return printer.printer_void(file);
  161. }
  162. return 0;
  163. }
  164. int maybe_fprintf(FILE* file, static_string format, int* pos, va_list* arg_list) {
  165. // %[flags][width][.precision][length]specifier
  166. // flags ::= [+- #0]
  167. // width ::= [<number>+ \*]
  168. // precision ::= \.[<number>+ \*]
  169. // length ::= [h l L]
  170. // specifier ::= [c d i e E f g G o s u x X p n %]
  171. int end_pos = *pos;
  172. int writen_len = 0;
  173. int used_arg_values = 1;
  174. // overstep flags:
  175. while(format[end_pos] == '+' ||
  176. format[end_pos] == '-' ||
  177. format[end_pos] == ' ' ||
  178. format[end_pos] == '#' ||
  179. format[end_pos] == '0')
  180. ++end_pos;
  181. // overstep width
  182. if (format[end_pos] == '*') {
  183. ++used_arg_values;
  184. ++end_pos;
  185. }
  186. else {
  187. while(format[end_pos] >= '0' && format[end_pos] <= '9')
  188. ++end_pos;
  189. }
  190. // overstep precision
  191. if (format[end_pos] == '.') {
  192. ++end_pos;
  193. if (format[end_pos] == '*') {
  194. ++used_arg_values;
  195. ++end_pos;
  196. }
  197. else {
  198. while(format[end_pos] >= '0' && format[end_pos] <= '9')
  199. ++end_pos;
  200. }
  201. }
  202. // overstep length:
  203. while(format[end_pos] == 'h' ||
  204. format[end_pos] == 'l' ||
  205. format[end_pos] == 'L')
  206. ++end_pos;
  207. // check for actual built_in specifier
  208. if(format[end_pos] == 'c' ||
  209. format[end_pos] == 'd' ||
  210. format[end_pos] == 'i' ||
  211. format[end_pos] == 'e' ||
  212. format[end_pos] == 'E' ||
  213. format[end_pos] == 'f' ||
  214. format[end_pos] == 'g' ||
  215. format[end_pos] == 'G' ||
  216. format[end_pos] == 'o' ||
  217. format[end_pos] == 's' ||
  218. format[end_pos] == 'u' ||
  219. format[end_pos] == 'x' ||
  220. format[end_pos] == 'X' ||
  221. format[end_pos] == 'p' ||
  222. format[end_pos] == 'n' ||
  223. format[end_pos] == '%')
  224. {
  225. writen_len = end_pos - *pos + 2;
  226. char* temp = (char*)malloc((writen_len+1)* sizeof(char));
  227. temp[0] = '%';
  228. temp[1] = 0;
  229. strncpy(temp+1, format+*pos, writen_len);
  230. temp[writen_len] = 0;
  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. for (int i = 0; i < used_arg_values; ++i) {
  238. va_arg(*arg_list, void*);
  239. }
  240. free(temp);
  241. *pos = end_pos;
  242. }
  243. return writen_len;
  244. }
  245. int print_va_args_to_file(FILE* file, static_string format, va_list* arg_list) {
  246. int printed_chars = 0;
  247. char c;
  248. int pos = -1;
  249. while ((c = format[++pos])) {
  250. if (c != '%') {
  251. putc(c, file);
  252. ++printed_chars;
  253. } else {
  254. c = format[++pos];
  255. int move = maybe_special_print(file, format, &pos, arg_list);
  256. if (move == 0) {
  257. move = maybe_fprintf(file, format, &pos, arg_list);
  258. if (move == 0) {
  259. putchar('%');
  260. putchar(c);
  261. move = 1;
  262. }
  263. }
  264. printed_chars += move;
  265. }
  266. }
  267. return printed_chars;
  268. }
  269. int print_va_args_to_string(char** out, static_string format, va_list* arg_list) {
  270. FILE* t_file = tmpfile();
  271. if (!t_file) {
  272. return 0;
  273. }
  274. int num_printed_chars = print_va_args_to_file(t_file, format, arg_list);
  275. *out = (char*)malloc(sizeof(char) * (num_printed_chars+1));
  276. rewind(t_file);
  277. fread(*out, sizeof(char), num_printed_chars, t_file);
  278. (*out)[num_printed_chars] = '\0';
  279. fclose(t_file);
  280. return num_printed_chars;
  281. }
  282. int print_va_args(static_string format, va_list* arg_list) {
  283. return print_va_args_to_file(stdout, format, arg_list);
  284. }
  285. int print_to_string(char** out, static_string format, ...) {
  286. va_list arg_list;
  287. va_start(arg_list, format);
  288. FILE* t_file = tmpfile();
  289. if (!t_file) {
  290. return 0;
  291. }
  292. int num_printed_chars = print_va_args_to_file(t_file, format, &arg_list);
  293. va_end(arg_list);
  294. *out = (char*)malloc(sizeof(char) * (num_printed_chars+1));
  295. rewind(t_file);
  296. fread(*out, sizeof(char), num_printed_chars, t_file);
  297. (*out)[num_printed_chars] = '\0';
  298. fclose(t_file);
  299. return num_printed_chars;
  300. }
  301. int print_to_file(FILE* file, static_string format, ...) {
  302. va_list arg_list;
  303. va_start(arg_list, format);
  304. int num_printed_chars = print_va_args_to_file(file, format, &arg_list);
  305. va_end(arg_list);
  306. return num_printed_chars;
  307. }
  308. int print(static_string format, ...) {
  309. va_list arg_list;
  310. va_start(arg_list, format);
  311. int num_printed_chars = print_va_args_to_file(ftb_stdout, format, &arg_list);
  312. va_end(arg_list);
  313. return num_printed_chars;
  314. }
  315. int print_bool(FILE* f, u32 val) {
  316. return print_to_file(f, val ? "true" : "false");
  317. }
  318. int print_u32(FILE* f, u32 num) {
  319. return print_to_file(f, "%u", num);
  320. }
  321. int print_u64(FILE* f, u64 num) {
  322. return print_to_file(f, "%llu", num);
  323. }
  324. int print_s32(FILE* f, s32 num) {
  325. return print_to_file(f, "%d", num);
  326. }
  327. int print_s64(FILE* f, s64 num) {
  328. return print_to_file(f, "%lld", num);
  329. }
  330. int print_flt(FILE* f, double arg) {
  331. return print_to_file(f, "%f", arg);
  332. }
  333. int print_str(FILE* f, char* str) {
  334. return print_to_file(f, "%s", str);
  335. }
  336. int print_color_start(FILE* f, char* str) {
  337. color_stack.append(str);
  338. return print_to_file(f, "%s", str);
  339. }
  340. int print_color_end(FILE* f) {
  341. --color_stack.next_index;
  342. if (color_stack.next_index == 0) {
  343. return print_to_file(f, "%s", console_normal);
  344. } else {
  345. return print_to_file(f, "%s", color_stack[color_stack.next_index-1]);
  346. }
  347. }
  348. int print_ptr(FILE* f, void* ptr) {
  349. if (ptr)
  350. return print_to_file(f, "%#0*X", sizeof(void*)*2+2, ptr);
  351. return print_to_file(f, "nullptr");
  352. }
  353. auto print_Str(FILE* f, String* str) -> s32 {
  354. return print_to_file(f, "%.*s", str->length, str->data);
  355. }
  356. auto print_str_line(FILE* f, char* str) -> s32 {
  357. u32 length = 0;
  358. char* str_cpy = str;
  359. while (str[length] != '\0') {
  360. if (str[length] == '\n')
  361. break;
  362. length++;
  363. }
  364. return print_to_file(f, "%.*s", length, str);
  365. }
  366. namespace {
  367. struct Printer_Initter {
  368. Printer_Initter() {
  369. register_printer("u32", print_u32, Printer_Function_Type::_32b);
  370. register_printer("u64", print_u64, Printer_Function_Type::_64b);
  371. register_printer("bool", print_bool, Printer_Function_Type::_32b);
  372. register_printer("s64", print_s64, Printer_Function_Type::_64b);
  373. register_printer("s32", print_s32, Printer_Function_Type::_32b);
  374. register_printer("f32", print_flt, Printer_Function_Type::_flt);
  375. register_printer("f64", print_flt, Printer_Function_Type::_flt);
  376. register_printer("->char", print_str, Printer_Function_Type::_ptr);
  377. register_printer("->", print_ptr, Printer_Function_Type::_ptr);
  378. register_printer("color<", print_color_start, Printer_Function_Type::_ptr);
  379. register_printer(">color", print_color_end, Printer_Function_Type::_void);
  380. register_printer("->Str", print_Str, Printer_Function_Type::_ptr);
  381. register_printer("->char_line", print_str_line, Printer_Function_Type::_ptr);
  382. }
  383. } printer_initter;
  384. }