Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- enum file_extension
- {
- C,
- PY,
- JS,
- UNDEFINED
- };
- bool string_ends_with(const std::string& str, const std::string& ending)
- {
- return !str.compare(str.size() - ending.size(), str.size(), ending) && str.length() >= ending.length();
- }
- file_extension get_file_extension(const std::string& filename)
- {
- if (string_ends_with(filename, ".py"))
- return PY;
- if (string_ends_with(filename, ".js"))
- return JS;
- if (string_ends_with(filename, ".c"))
- return C;
- return UNDEFINED;
- }
- void search_comments(const std::string& filename)
- {
- std::vector<std::string> comment_tokens;
- switch (get_file_extension(filename))
- {
- case PY:
- comment_tokens = {"#", "\"\"\""};
- break;
- case C:
- case JS:
- comment_tokens = {"//", "/*"};
- break;
- default:
- std::cout << "Incorrect file extension. Allowed file extensions are: .c, .js, .py" << std::endl;
- return;
- }
- std::ifstream fin(filename);
- if (!fin.is_open())
- {
- std::cout << "Unable to open file, check if it exists" << std::endl;
- return;
- }
- fin.close();
- std::string line;
- while (std::getline(fin, line))
- {
- for (const auto& token: comment_tokens)
- {
- if (line.find(token) != std::string::npos)
- {
- std::cout << "File contains comments" << std::endl;
- return;
- }
- }
- }
- std::cout << "File does not contain comments" << std::endl;
- }
- int main(int argc, const char* argv[])
- {
- if (argc < 1)
- {
- std::cout << "Specify the file to check comments" << std::endl;
- return 0;
- }
- std::cout << argv[0] << std::endl;
- search_comments(argv[0]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment