MD_Printf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #if defined(_MSC_VER) || defined(WIN32)
  6. #include <winsock2.h>
  7. #include "MD_AnsiCons.h"
  8. #else
  9. #include <sys/socket.h>
  10. #endif
  11. #include "MagiDoor.h"
  12. void md_putchar(char c) {
  13. #if defined(_MSC_VER) || defined(WIN32)
  14. ansi_write(stdout, &c, 1);
  15. #endif
  16. if (mdcontrol.socket == -1) {
  17. write(STDOUT_FILENO, &c, 1);
  18. } else {
  19. send(mdcontrol.socket, &c, 1, 0);
  20. }
  21. }
  22. void md_set_cursor(int y, int x) {
  23. md_printf("\x1b[%d;%dH", y, x);
  24. }
  25. void md_clr_scr() {
  26. md_printf("\x1b[2J\x1b[1;1H");
  27. }
  28. void md_printcode(char *code) {
  29. char *part;
  30. int bright = 0;
  31. int colour = 0;
  32. int bgcolour = 0;
  33. char codebuffer[11];
  34. int i;
  35. part = strtok(code, " ");
  36. if (part != NULL) {
  37. if (strcmp(part, "bright") == 0) {
  38. bright = 1;
  39. } else if (strcmp(part, "black") == 0) {
  40. colour = 0;
  41. } else if (strcmp(part, "red") == 0) {
  42. colour = 1;
  43. } else if (strcmp(part, "green") == 0) {
  44. colour = 2;
  45. } else if (strcmp(part, "yellow") == 0) {
  46. colour = 3;
  47. } else if (strcmp(part, "blue") == 0) {
  48. colour = 4;
  49. } else if (strcmp(part, "magenta") == 0) {
  50. colour = 5;
  51. } else if (strcmp(part, "cyan") == 0) {
  52. colour = 6;
  53. } else if (strcmp(part, "white") == 0) {
  54. colour = 7;
  55. }
  56. part = strtok(NULL, " ");
  57. if (part != NULL) {
  58. if (bright == 0) {
  59. bgcolour = colour;
  60. colour = 0;
  61. }
  62. if (strcmp(part, "bright") == 0) {
  63. bright = 1;
  64. part = strtok(NULL, " ");
  65. }
  66. if (part != NULL) {
  67. if (strcmp(part, "black") == 0) {
  68. colour = 0;
  69. } else if (strcmp(part, "red") == 0) {
  70. colour = 1;
  71. } else if (strcmp(part, "green") == 0) {
  72. colour = 2;
  73. } else if (strcmp(part, "yellow") == 0) {
  74. colour = 3;
  75. } else if (strcmp(part, "blue") == 0) {
  76. colour = 4;
  77. } else if (strcmp(part, "magenta") == 0) {
  78. colour = 5;
  79. } else if (strcmp(part, "cyan") == 0) {
  80. colour = 6;
  81. } else if (strcmp(part, "white") == 0) {
  82. colour = 7;
  83. }
  84. }
  85. }
  86. }
  87. snprintf(codebuffer, 11, "\x1b[%d;4%d;3%dm", bright, bgcolour, colour);
  88. for (i=0;i<10;i++) {
  89. md_putchar(codebuffer[i]);
  90. }
  91. }
  92. void md_printf(const char *fmt, ...) {
  93. char buffer[4096];
  94. char codebuf[128];
  95. va_list ap;
  96. va_start(ap, fmt);
  97. vsnprintf(buffer, sizeof buffer, fmt, ap);
  98. va_end(ap);
  99. char *ptr;
  100. char *cbptr;
  101. ptr = buffer;
  102. while (*ptr != '\0') {
  103. if (*ptr == '`') {
  104. ptr++;
  105. if (*ptr == '`') {
  106. md_putchar('`');
  107. ptr++;
  108. continue;
  109. } else if (*ptr == '\0') {
  110. break;
  111. }
  112. cbptr = codebuf;
  113. while(*ptr != '`' && *ptr != '\0' && cbptr - codebuf < 127) {
  114. *cbptr = *ptr;
  115. ptr++;
  116. cbptr++;
  117. *cbptr = '\0';
  118. }
  119. md_printcode(codebuf);
  120. if (*ptr == '\0') {
  121. break;
  122. }
  123. ptr++;
  124. continue;
  125. }
  126. md_putchar(*ptr);
  127. ptr++;
  128. }
  129. }