User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,177 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,138 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Oct 23rd, 2007
Views: 2,249
hello frnds this is a snippet that removes comments of c, c++ , java files
cplusplus Syntax | 5 stars
  1. // author :Rohith Gowda
  2. // - remove C/C++ comments
  3. // - assume no nested block comments
  4.  
  5. // Modifications
  6.  
  7. #include<iostream>
  8. #include<fstream>
  9.  
  10. using namespace std;
  11.  
  12. void parse(ifstream &fin , ofstream &fout)
  13. {
  14. char curr, prev = '\0';
  15. bool comment = false;
  16. bool charLiteral = false;
  17. bool stringLiteral = false;
  18.  
  19. while (fin.get(curr)) {
  20. if (charLiteral) {
  21. fout << curr;
  22. if (curr == '\'' && prev != '\\') { // 'a' ends
  23. charLiteral = false;
  24. }
  25. prev = (prev == '\\' && curr == '\\') ? '\0' : curr;
  26. } else if (stringLiteral) {
  27. fout << curr;
  28. if (curr == '\"' && prev != '\\') { // "string" ends
  29. stringLiteral = false;
  30. }
  31. prev = (prev == '\\' && curr == '\\') ? '\0' : curr;
  32. } else if (comment) {
  33. if (curr == '/' && prev == '*') { /* comment ends */
  34. prev = '\0';
  35. comment = false;
  36. } else { /* comment text */
  37. prev = curr;
  38. }
  39. } else if (prev == '/') {
  40. if (curr == '/') { // end of line comment
  41. fout << '\n';
  42. prev = '\0';
  43. while (fin.get() != '\n');
  44. } else if (curr == '*') { /* comment starts */
  45. prev = '\0';
  46. comment = true;
  47. } else { // normal code
  48. fout << prev << curr;
  49. prev = curr;
  50. }
  51. } else {
  52. if (curr != '/') {
  53. fout << curr;
  54. }
  55. charLiteral = (prev != '\\' && curr == '\'');
  56. stringLiteral = (prev != '\\' && curr == '\"');
  57. prev = (prev == '\\' && curr == '\\') ? '\0' : curr;
  58. }
  59. }
  60. }
  61.  
  62. int main(int argc, char *argv[])
  63. {
  64. if (argc != 3) {
  65. cerr << "Usage:\t" << argv[0] << " <input_file> <output_file>\n";
  66. return 1;
  67. }
  68.  
  69. ifstream fin(argv[1]);
  70. ofstream fout(argv[2]);
  71.  
  72. if (!fin) {
  73. cerr << "Error:\t\"" << argv[1] << "\" - no such file\n";
  74. return 1;
  75. }
  76.  
  77. parse(fin, fout);
  78.  
  79. fin.close();
  80. fout.close();
  81.  
  82. return 0;
  83. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 9:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC