Advertisement
Deerenaros

Print valid C-code with select octals by color.

Jun 20th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5.  
  6. enum ConsoleColor
  7. {
  8.     Black         = 0,
  9.     Blue          = 1,
  10.     Green         = 2,
  11.     Cyan          = 3,
  12.     Red           = 4,
  13.     Magenta       = 5,
  14.     Brown         = 6,
  15.     LightGray     = 7,
  16.     DarkGray      = 8,
  17.     LightBlue     = 9,
  18.     LightGreen    = 10,
  19.     LightCyan     = 11,
  20.     LightRed      = 12,
  21.     LightMagenta  = 13,
  22.     Yellow        = 14,
  23.     White         = 15
  24. };
  25.  
  26. bool compare(char c){
  27.     char s[] = " \t\n(){}+=-/*&%x;,";
  28.     for(int i = 0; s[i]; i++)
  29.         if(c == s[i]) return true;
  30.     return false || (c == EOF);
  31. }
  32.  
  33. int main(int argc, char **argv){
  34.     char ch;
  35.     bool free = true;
  36.     FILE *fIN;
  37.     //SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)(White));
  38.     if(argc < 2)
  39.         fIN = fopen("input.c", "r");
  40.     else
  41.         fIN = fopen(argv[1], "r");
  42.  
  43.     //some octals: 023 0df 0123 0x123 123 25
  44.     while((ch = getc(fIN)) != EOF){
  45.         if(!free){
  46.             if(compare(ch))
  47.                 free = true;
  48.             putchar(ch);
  49.         }else{
  50.             if(ch == '0'){
  51.                 if(!compare(ch = getc(fIN))){
  52.                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)(Brown));                   
  53.                     putchar('0'); putchar(ch);
  54.                     while(!compare(ch = getc(fIN)))
  55.                         putchar(ch);
  56.                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)(White));
  57.                     if(ch != EOF) putchar(ch);
  58.                 }
  59.             }else{
  60.                 free = false;
  61.                 putchar(ch);
  62.             }
  63.         }
  64.     }
  65.    
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement