Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Created August 30, 2010 02:54
Show Gist options
  • Save ecounysis/556950 to your computer and use it in GitHub Desktop.
Save ecounysis/556950 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define BUFFER_SIZE 2
enum FLAGS {NONE, SLASH, COMMENT1, COMMENTM, QUOTE1, QUOTE2, COMMENTM_STAR};
char buffer[BUFFER_SIZE];
int buff_pointer = 0;
void flush(char buff[]);
int state = NONE;
char t;
/* this is a comment */
int main()
{
while ((t = getchar()) != EOF)
{
if (buff_pointer >= BUFFER_SIZE - 1)
flush(buffer);
if (state == NONE)
{
buffer[buff_pointer++] = t;
if (t=='/')
state = SLASH;
if (t=='"')
state = QUOTE2;
if (t=='\'')
state = QUOTE1;
}
else if (state == SLASH)
{
if (t=='*')
{
state = COMMENTM;
buff_pointer--;
}
else if (t=='/')
{
state = COMMENT1;
buff_pointer--;
}
else
{
state = NONE;
buffer[buff_pointer++] = t;
}
}
else if ((state == QUOTE1) && (t == '\''))
{
state = NONE;
buffer[buff_pointer++] = t;
}
else if ((state == QUOTE2) && (t == '"'))
{
state = NONE;
buffer[buff_pointer++] = t;
}
else if ((state == COMMENTM) && (t == '*'))
state = COMMENTM_STAR;
else if ((state == COMMENTM_STAR) && (t == '/'))
{
state = NONE;
buff_pointer--;
}
else if (state == COMMENTM_STAR)
{
state = COMMENTM;
}
else if ((state == COMMENT1) && (t == '\n'))
{
state = NONE;
buffer[buff_pointer++] = '\n';
}
else if (((state == QUOTE1) || (state == QUOTE2)) && (t == '\\'))
{
buffer[buff_pointer++] = t;
buffer[buff_pointer++] = getchar(); // just go ahead and get the next one, since it's escaped
}
else if ((state == QUOTE1) || (state == QUOTE2))
{
buffer[buff_pointer++] = t;
}
}
flush(buffer);
}
/* this is a multi
* lin
* comment
* */
// this is another comment
void flush(char buffer[])
{
if (!(state == SLASH) && !(state == COMMENTM_STAR))
{
int j;
for (j=0; j<buff_pointer; j++)
putchar(buffer[j]);
buff_pointer = 0;
}
}
/* this is a multi line
* comment */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment