file_print.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. // void fputc(char s, FILE* f);
  20. // CONSTANT TRUE 1
  21. #define TRUE 1
  22. // CONSTANT FALSE 0
  23. #define FALSE 0
  24. int in_set(int c, char* s);
  25. void file_print(char* s, FILE* f);
  26. char char_lookup(int c)
  27. {
  28. if(c == '\a') return 'a';
  29. else if(c == '\b') return 'b';
  30. else if(c == '\t') return 't';
  31. else if(c == '\v') return 'v';
  32. else if(c == '\n') return 'n';
  33. else if(c == '\f') return 'f';
  34. else if(c == '\r') return 'r';
  35. else if(c == '\033') return 'e';
  36. else if(c == '\\') return '\\';
  37. else if(c == '"') return '"';
  38. return c;
  39. }
  40. int char2hex(int c);
  41. int hexify(int c, int high)
  42. {
  43. int i = char2hex(c);
  44. if(0 > i)
  45. {
  46. file_print("Tried to print non-hex number\n", stderr);
  47. exit(EXIT_FAILURE);
  48. }
  49. if(high)
  50. {
  51. i = i << 4;
  52. }
  53. return i;
  54. }
  55. void file_print(char* s, FILE* f)
  56. {
  57. while(0 != s[0])
  58. {
  59. fputc(s[0], f);
  60. s = s + 1;
  61. }
  62. }
  63. int escape_lookup(char* c)
  64. {
  65. if('\\' != c[0]) return c[0];
  66. if(c[1] == 'x')
  67. {
  68. int t1 = hexify(c[2], TRUE);
  69. int t2 = hexify(c[3], FALSE);
  70. return t1 + t2;
  71. }
  72. else if(c[1] == '0') return 0;
  73. else if(c[1] == 'a') return 7;
  74. else if(c[1] == 'b') return 8;
  75. else if(c[1] == 't') return 9;
  76. else if(c[1] == 'n') return 10;
  77. else if(c[1] == 'v') return 11;
  78. else if(c[1] == 'f') return 12;
  79. else if(c[1] == 'r') return 13;
  80. else if(c[1] == 'e') return 27;
  81. else if(c[1] == '"') return 34;
  82. else if(c[1] == '\'') return 39;
  83. else if(c[1] == '\\') return 92;
  84. file_print("Unknown escape received: ", stderr);
  85. file_print(c, stderr);
  86. file_print(" Unable to process\n", stderr);
  87. exit(EXIT_FAILURE);
  88. }
  89. void raw_print(char* s, FILE* f)
  90. {
  91. char c;
  92. while(0 != s[0])
  93. {
  94. c = s[0];
  95. if(in_set(c, "\a\b\t\b\v\f\n\r\033\"\\"))
  96. {
  97. fputc('\\', f);
  98. c = char_lookup(c);
  99. }
  100. fputc(c, f);
  101. s = s + 1;
  102. }
  103. }
  104. void ugly_print(char* s, FILE* f, int length)
  105. {
  106. int c;
  107. int tmp;
  108. char* table = "0123456789ABCDEF";
  109. while(length > 0)
  110. {
  111. c = s[0];
  112. if((c < 32) || (c > 126))
  113. {
  114. fputc('\\', f);
  115. fputc('x', f);
  116. tmp = (c >> 4) & 0xF;
  117. fputc(table[tmp], f);
  118. tmp = c & 0xF;
  119. fputc(table[tmp], f);
  120. }
  121. else
  122. {
  123. fputc(c, f);
  124. }
  125. length = length - 1;
  126. s = s + 1;
  127. }
  128. }