file_print.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #define TRUE 1
  21. #define FALSE 0
  22. int in_set(int c, char* s);
  23. void file_print(char* s, FILE* f);
  24. char char_lookup(int c)
  25. {
  26. if(c == '\a') return 'a';
  27. else if(c == '\b') return 'b';
  28. else if(c == '\t') return 't';
  29. else if(c == '\v') return 'v';
  30. else if(c == '\n') return 'n';
  31. else if(c == '\f') return 'f';
  32. else if(c == '\r') return 'r';
  33. else if(c == '\033') return 'e';
  34. else if(c == '\\') return '\\';
  35. else if(c == '"') return '"';
  36. return c;
  37. }
  38. int char2hex(int c);
  39. int hexify(int c, int high)
  40. {
  41. int i = char2hex(c);
  42. if(0 > i)
  43. {
  44. file_print("Tried to print non-hex number\n", stderr);
  45. exit(EXIT_FAILURE);
  46. }
  47. if(high)
  48. {
  49. i = i << 4;
  50. }
  51. return i;
  52. }
  53. void file_print(char* s, FILE* f)
  54. {
  55. while(0 != s[0])
  56. {
  57. fputc(s[0], f);
  58. s = s + 1;
  59. }
  60. }
  61. int escape_lookup(char* c)
  62. {
  63. if('\\' != c[0]) return c[0];
  64. if(c[1] == 'x')
  65. {
  66. int t1 = hexify(c[2], TRUE);
  67. int t2 = hexify(c[3], FALSE);
  68. return t1 + t2;
  69. }
  70. else if(c[1] == '0') return 0;
  71. else if(c[1] == 'a') return 7;
  72. else if(c[1] == 'b') return 8;
  73. else if(c[1] == 't') return 9;
  74. else if(c[1] == 'n') return 10;
  75. else if(c[1] == 'v') return 11;
  76. else if(c[1] == 'f') return 12;
  77. else if(c[1] == 'r') return 13;
  78. else if(c[1] == 'e') return 27;
  79. else if(c[1] == '"') return 34;
  80. else if(c[1] == '\'') return 39;
  81. else if(c[1] == '\\') return 92;
  82. file_print("Unknown escape received: ", stderr);
  83. file_print(c, stderr);
  84. file_print(" Unable to process\n", stderr);
  85. exit(EXIT_FAILURE);
  86. }
  87. void raw_print(char* s, FILE* f)
  88. {
  89. char c;
  90. while(0 != s[0])
  91. {
  92. c = s[0];
  93. if(in_set(c, "\a\b\t\b\v\f\n\r\033\"\\"))
  94. {
  95. fputc('\\', f);
  96. c = char_lookup(c);
  97. }
  98. fputc(c, f);
  99. s = s + 1;
  100. }
  101. }
  102. void ugly_print(char* s, FILE* f, int length)
  103. {
  104. int c;
  105. int tmp;
  106. char* table = "0123456789ABCDEF";
  107. while(length > 0)
  108. {
  109. c = s[0];
  110. if((c < 32) || (c > 126))
  111. {
  112. fputc('\\', f);
  113. fputc('x', f);
  114. tmp = (c >> 4) & 0xF;
  115. fputc(table[tmp], f);
  116. tmp = c & 0xF;
  117. fputc(table[tmp], f);
  118. }
  119. else
  120. {
  121. fputc(c, f);
  122. }
  123. length = length - 1;
  124. s = s + 1;
  125. }
  126. }