Created
April 28, 2018 07:07
-
-
Save takehiko/b069ee7f076a8a70aade126a61f486a1 to your computer and use it in GitHub Desktop.
colorshow.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* colorchow.c : ANSIエスケープシーケンスを利用して, | |
大文字を赤色,小文字を青色で表示する. | |
* 作成者: 村川猛彦 ([email protected]) | |
* コンパイル方法: cc colorshow.c -o colorshow | |
* 実行方法: cat report1-cipher.txt | sed -e 'y/ABC/xyz/' | ./colorshow | |
*/ | |
#include <stdio.h> | |
#include <ctype.h> | |
#define MODE_UPPER 'u' /* 大文字を読み込んだ */ | |
#define MODE_LOWER 'l' /* 小文字を読み込んだ */ | |
#define MODE_OTHER 'o' /* その他の文字を読み込んだ */ | |
/* ANSIエスケープシーケンスを利用して,以降の文字の色を変更する */ | |
#define set_color_upper() fputs("\033[01;31m", stdout) /* 大文字:赤 */ | |
#define set_color_lower() fputs("\033[01;34m", stdout) /* 小文字:青 */ | |
#define set_color_other() fputs("\033[0m", stdout) /* その他:元の色 */ | |
void set_color(int mode) | |
{ | |
switch (mode) { | |
case MODE_UPPER: | |
set_color_upper(); | |
break; | |
case MODE_LOWER: | |
set_color_lower(); | |
break; | |
default: | |
set_color_other(); | |
} | |
} | |
int main(void) | |
{ | |
int c; | |
int mode = MODE_OTHER; | |
while ((c = getchar()) != EOF) { | |
int current_mode; | |
if (isupper(c)) { | |
current_mode = MODE_UPPER; | |
} else if (islower(c)) { | |
current_mode = MODE_LOWER; | |
} else { | |
current_mode = MODE_OTHER; | |
} | |
if (mode != current_mode) { | |
set_color(current_mode); | |
mode = current_mode; | |
} | |
putchar(c); | |
} | |
if (mode != MODE_OTHER) { | |
set_color(MODE_OTHER); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment