From fb4da7a214c1a251c9336e63034621e570aab23a Mon Sep 17 00:00:00 2001 From: Isak Date: Sun, 15 Apr 2018 00:44:30 +0200 Subject: [PATCH] Add files via upload --- highlight.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 highlight.cpp diff --git a/highlight.cpp b/highlight.cpp new file mode 100644 index 0000000..ae57236 --- /dev/null +++ b/highlight.cpp @@ -0,0 +1,61 @@ +/* Highlights reserved programming keywords*/ +#include +#include +#include +#include + +/* Color codes */ +#define COLOR_GREEN "\e[1;32m" +#define COLOR_RESET "\x1b[0m" +/* *********** */ + +bool is_keyword(std::string&); + +int main(int argc, char *argv[]) +{ + if (argc < 2) { + std::cout << "Usage: ./" << __FILE__ << " \n"; + exit(EXIT_FAILURE); + } + std::ifstream file(argv[1]); + if (!file.is_open()) { + std::cout << "Error opening file.\n"; + exit(EXIT_FAILURE); + } + + std::string word; char c; + while (file.get(c)) + { + if (isalpha(c)) + word += c; + else { + if (is_keyword(word)) + std::cout << COLOR_GREEN << word << COLOR_RESET; + else + std::cout << word; + std::cout << c; + word=""; + } + } + + return 0; +} + +bool is_keyword(std::string &word) +{ + static const char *keywords[] = + { + "auto","break","case","char", + "const","continue","default","do", + "double","else","enum","extern", + "float","for", "goto", "if", + "int","long","register","return", + "short","signed","sizeof","static", + "struct","switch","typedef","union", + "unsigned","void","volatile","while", + "bool" + }; + for (auto &w : keywords) + if (w==word) return true; + return false; +}