" |
#include <ctype.h> #include <stdio.h> enum status { S_TITL = 1 << 0, S_BOLD = 1 << 1, S_ITAL = 1 << 2, S_UNDE = 1 << 3, S_STRI = 1 << 4, }; int dotag(int flag, const char *t, int s) { if (s & flag) printf("[/%s]", t); else printf("[%s]", t); return s ^= flag; } /* just put file through stdin. it's easier (for me :3) */ int main(void) { int c, tmp; int s = 0; /* status of text */ int newpara = 1; /* if we are at a new line */ /* "if you need more than 3 levels of indentation, you’re screwed * anyway, and should fix your program." - linux kernel style guide */ while ((c = getchar()) != EOF) { switch(c) { case '*': s = dotag(S_BOLD, "b", s); break; case '_': s = dotag(S_ITAL, "i", s); break; case '\t': if (newpara) { s = dotag(S_TITL, "t", s); while (isspace(tmp = getchar())); /* vim :center */ ungetc(tmp, stdin); /* put back non-ws */ } break; case '\n': if (s & S_TITL) s = dotag(S_TITL, "t", s); if ((tmp = getchar()) == '\n') { /* new paragraph */ puts("\n"); /* does two newlines */ newpara = 1; goto nosetpara; } else { ungetc(tmp, stdin); putchar(' '); } break; default: putchar(c); break; } newpara = 0; nosetpara: } return 0; } |
Viewed: | 7 times |
Added: | 1 year, 6 months ago |