Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. /*
  2. * Exercise 1-23. Write a program to remove all comments from a C program.
  3. * Don't forget to handle quoted strings and character constants properly. C
  4. * comments don't nest.
  5. */
  6.  
  7. #undef _POSIX_C_SOURCE
  8. #define _POSIX_C_SOURCE 200112L
  9.  
  10. #include <stdio.h>
  11. #include <stdint.h>
  12.  
  13. #define MAXSIZE 65535
  14.  
  15. void stripComments(char input[], char output[], uint16_t len);
  16. uint16_t getline(char string[], uint16_t lim);
  17.  
  18. int main(void)
  19. {
  20. char input[MAXSIZE];
  21. char output[MAXSIZE];
  22. uint16_t len;
  23.  
  24. while((len = getline(input, MAXSIZE)) > 0)
  25. {
  26. stripComments(input, output, len);
  27. printf("%s", output);
  28. }
  29. }
  30.  
  31. /*
  32. * Remove comments from text input.
  33. */
  34. void stripComments(char input[], char output[], uint16_t len)
  35. {
  36. uint16_t i;
  37. uint16_t j = 0;
  38. uint16_t c;
  39. static uint16_t prev = 0;
  40. static uint16_t doubleQuotes = 0;
  41. static uint16_t singleQuotes = 0;
  42. static uint16_t isWrite = 1;
  43.  
  44. for(i = 0; i < len; i++)
  45. {
  46. /*
  47. * Store both the current and the previous values of i to keep
  48. * check on the current input status.
  49. */
  50. c = input[i];
  51. if(i > 0)
  52. prev = input[i-1];
  53.  
  54. /*
  55. * Set marker if double quotes are detected.
  56. */
  57. if(c == '"' && !doubleQuotes && !singleQuotes)
  58. doubleQuotes = 1;
  59. else if(c == '"' && doubleQuotes)
  60. doubleQuotes = 0;
  61. /*
  62. * Set marker if single quotes are detected.
  63. */
  64. if(c == 39 && !doubleQuotes && !singleQuotes)
  65. singleQuotes = 1;
  66. else if(c == 39 && singleQuotes)
  67. singleQuotes = 0;
  68.  
  69. /*
  70. * If not already in either of the above quotes mode, stop
  71. * writing to the output array, remove the comment, set the
  72. * head 'j' back two spaces to conceal the comment characters.
  73. */
  74. if((prev == '/') && (c == '*') && (!doubleQuotes) && (!singleQuotes) && isWrite)
  75. {
  76. isWrite = 0;
  77. j = j-2;
  78. continue;
  79. }
  80.  
  81. /*
  82. * If not is either quote mode and currently not writing, in
  83. * isWrite mode, then start writing.
  84. */
  85. if((prev == '*') && (c == '/') && (!doubleQuotes) && (!singleQuotes) && !isWrite)
  86. {
  87. isWrite = 1;
  88. continue;
  89. }
  90.  
  91. if(isWrite)
  92. output[j++] = input[i];
  93. }
  94. }
  95.  
  96. uint16_t getline(char string[], uint16_t lim)
  97. {
  98. uint16_t i;
  99. int c;
  100.  
  101. for (i = 0; i < lim-1 && ((c = getchar()) != EOF); i++)
  102. string[i] = c;
  103.  
  104. if (c == '\n')
  105. string[i++] = c;
  106.  
  107. string[i] = '\0';
  108. return i;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement