/* Auteur: Ozwald */
/* Plateforme: Win32/Linux */
/* Compilateur/version:gcc/? */
#include <stdio.h>
#include <stdlib.h>
typedef struct state state;
struct state {
int nb_links; /*number of links starting from this state*/
state** followers; /*the 1 dimension array containing references
to the targets of the links from this state
(+1 which is used if no labelled link is activated */
char* key; /*the key char that activates a link*/
char should_we_write_this; /* 0 if the char we read in this state
are not to be shown on screen, 1 if they
are */
};
void create_state_machine(state* entry) {
state* start = malloc(sizeof(state));
state* a = malloc(sizeof(state));
state* sp2 = malloc(sizeof(state));
state* h = malloc(sizeof(state));
state* r_a = malloc(sizeof(state));
state* e = malloc(sizeof(state));
state* f = malloc(sizeof(state));
state* i = malloc(sizeof(state));
state* m = malloc(sizeof(state));
state* g = malloc(sizeof(state));
state* sp1 = malloc(sizeof(state));
state* s = malloc(sizeof(state));
state* r_i = malloc(sizeof(state));
state* c = malloc(sizeof(state));
state* equal = malloc(sizeof(state));
state* squote = malloc(sizeof(state)); state* dquote = malloc(sizeof(state));
state* waste = malloc(sizeof(state));
a->should_we_write_this=0;
a->nb_links=3;
a->followers = malloc(4*sizeof(state*));
a->key = malloc(3*sizeof(char));
a->followers[0]= sp2; a->key[0] = ' ';
a->followers[1]= sp2; a->key[1] = '\n';
a->followers[2]= sp2; a->key[2] = '\r';
a->followers[3]= waste;
sp2->should_we_write_this=0;
sp2->nb_links=2;
sp2->followers = malloc(3*sizeof(state*));
sp2->key = malloc(2*sizeof(char));
sp2->followers[0]= h; sp2->key[0] = 'H';
sp2->followers[1]= h; sp2->key[1] = 'h';
sp2->followers[2]= sp2;
h->should_we_write_this=0;
h->nb_links=2;
h->followers = malloc(3*sizeof(state*));
h->key = malloc(2*sizeof(char));
h->followers[0]= r_a; h->key[0] = 'R';
h->followers[1]= r_a; h->key[1] = 'r';
h->followers[2]= waste;
r_a->should_we_write_this=0;
r_a->nb_links=2;
r_a->followers = malloc(3*sizeof(state*));
r_a->key = malloc(2*sizeof(char));
r_a->followers[0]= e; r_a->key[0] = 'E';
r_a->followers[1]= e; r_a->key[1] = 'e';
r_a->followers[2]= waste;
e->should_we_write_this=0;
e->nb_links=2;
e->followers = malloc(3*sizeof(state*));
e->key = malloc(2*sizeof(char));
e->followers[0]= f; e->key[0] = 'F';
e->followers[1]= f; e->key[1] = 'f';
e->followers[2]= waste;
f->should_we_write_this=0;
f->nb_links=4;
f->followers = malloc(5*sizeof(state*));
f->key = malloc(4*sizeof(char));
f->followers[0]= equal; f->key[0] = '=';
f->followers[1]= f; f->key[1] = ' ';
f->followers[2]= f; f->key[2] = '\r';
f->followers[3]= f; f->key[3] = '\n';
f->followers[4]= waste;
/* ------------------ */
i->should_we_write_this=0;
i->nb_links=2;
i->followers = malloc(3*sizeof(state*));
i->key = malloc(2*sizeof(char));
i->followers[0]= m; i->key[0] = 'M';
i->followers[1]= m; i->key[1] = 'm';
i->followers[2]= waste;
m->should_we_write_this=0;
m->nb_links=2;
m->followers = malloc(3*sizeof(state*));
m->key = malloc(2*sizeof(char));
m->followers[0]= g; m->key[0] = 'G';
m->followers[1]= g; m->key[1] = 'g';
m->followers[2]= waste;
g->should_we_write_this=0;
g->nb_links=3;
g->followers = malloc(4*sizeof(state*));
g->key = malloc(3*sizeof(char));
g->followers[0]= sp1; g->key[0] = ' ';
g->followers[1]= sp1; g->key[1] = '\r';
g->followers[2]= sp1; g->key[2] = '\n';
g->followers[3]= waste;
sp1->should_we_write_this=0;
sp1->nb_links=2;
sp1->followers = malloc(3*sizeof(state*));
sp1->key = malloc(2*sizeof(char));
sp1->followers[0]= s; sp1->key[0] = 'S';
sp1->followers[1]= s; sp1->key[1] = 's';
sp1->followers[2]= sp1;
s->should_we_write_this=0;
s->nb_links=2;
s->followers = malloc(3*sizeof(state*));
s->key = malloc(2*sizeof(char));
s->followers[0]= r_i; s->key[0] = 'R';
s->followers[1]= r_i; s->key[1] = 'r';
s->followers[2]= waste;
r_i->should_we_write_this=0;
r_i->nb_links=2;
r_i->followers = malloc(3*sizeof(state*));
r_i->key = malloc(2*sizeof(char));
r_i->followers[0]= c; r_i->key[0] = 'C';
r_i->followers[1]= c; r_i->key[1] = 'c';
r_i->followers[2]= waste;
c->should_we_write_this=0;
c->nb_links=4;
c->followers = malloc(5*sizeof(state*));
c->key = malloc(4*sizeof(char));
c->followers[0]= equal; c->key[0] = '=';
c->followers[1]= c; c->key[1] = ' ';
c->followers[2]= c; c->key[2] = '\r';
c->followers[3]= c; c->key[3] = '\n';
c->followers[4]= waste;
/* ------------------ */
equal->should_we_write_this=0;
equal->nb_links=5;
equal->followers = malloc(6*sizeof(state*));
equal->key = malloc(5*sizeof(char));
equal->followers[0]= dquote; equal->key[0] = '"';
equal->followers[1]= squote; equal->key[1] = '\'';
equal->followers[2]= equal; equal->key[2] = ' ';
equal->followers[3]= equal; equal->key[3] = '\n';
equal->followers[4]= equal; equal->key[4] = '\r';
equal->followers[5]= waste;
dquote->should_we_write_this=1;
dquote->nb_links=1;
dquote->followers = malloc(2*sizeof(state*));
dquote->key = malloc(sizeof(char));
dquote->followers[0]= waste; dquote->key[0] = '"';
dquote->followers[1]= dquote;
squote->should_we_write_this=1;
squote->nb_links=1;
squote->followers = malloc(2*sizeof(state*));
squote->key = malloc(sizeof(char));
squote->followers[0]= waste; squote->key[0] = '\'';
squote->followers[1]= squote;
waste->should_we_write_this=0;
waste->nb_links=1;
waste->followers = malloc(2*sizeof(state*));
waste->key = malloc(sizeof(char));
waste->followers[0]= entry; waste->key[0] = '>';
waste->followers[1]= waste;
entry->should_we_write_this=0;
entry->nb_links=1;
entry->followers = malloc(2*sizeof(state*));
entry->key = malloc(sizeof(char));
entry->followers[0]= start; entry->key[0] = '<';
entry->followers[1]= entry;
start->should_we_write_this=0;
start->nb_links=7;
start->followers = malloc(8*sizeof(state*));
start->key = malloc(7*sizeof(char));
start->followers[0]= a; start->key[0] = 'A';
start->followers[1]= i; start->key[1] = 'I';
start->followers[2]= a; start->key[2] = 'a';
start->followers[3]= i; start->key[3] = 'i';
start->followers[4]= start; start->key[4] = ' ';
start->followers[5]= start; start->key[5] = '\n';
start->followers[6]= start; start->key[6] = '\r';
start->followers[7]= waste;
}
void destroy_state_machine(state* entry) {
state* start = entry->followers[0];
state* a = start->followers[0];
state* sp2 = a->followers[0];
state* h = sp2->followers[0];
state* r_a = h->followers[0];
state* e = r_a->followers[0];
state* f = e->followers[0];
state* i = start->followers[1];
state* m = i->followers[0];
state* g = m->followers[0];
state* sp1 = g->followers[0];
state* s = sp1->followers[0];
state* r_i = s->followers[0];
state* c = r_i->followers[0];
state* equal = c->followers[0];
state* squote = equal->followers[1];
state* dquote = equal->followers[0];
state* waste = dquote->followers[0];
free(a->followers); free(a->key); free(a);
free(sp2->followers); free(sp2->key); free(sp2);
free(h->followers); free(h->key); free(h);
free(r_a->followers); free(r_a->key); free(r_a);
free(e->followers); free(e->key); free(e);
free(f->followers); free(f->key); free(f);
free(i->followers); free(i->key); free(i);
free(m->followers); free(m->key); free(m);
free(g->followers); free(g->key); free(g);
free(sp1->followers); free(sp1->key); free(sp1);
free(s->followers); free(s->key); free(s);
free(r_i->followers); free(r_i->key); free(r_i);
free(c->followers); free(c->key); free(c);
free(equal->followers); free(equal->key); free(equal);
free(start->followers); free(start->key); free(start);
free(squote->followers); free(squote->key); free(squote);
free(dquote->followers); free(dquote->key); free(dquote);
free(waste->followers); free(waste->key); free(waste);
}
void parse() {
state entry;
create_state_machine(&entry);
state* current = &entry;
int position;
int i=0;
char buffer[1024];
int nb_char;
while( (nb_char=fread(buffer,1,1024,stdin)) != 0) {
for(position=0;position<nb_char;position++) {
i=0;
while(i<(current->nb_links)) {
if (buffer[position]==current->key[i]) {
if (current->should_we_write_this) printf("\n");
current = current->followers[i];
i=200;
} else {
if (current->should_we_write_this) printf("%c",buffer[position]);
if (i== (current->nb_links-1)) current = current->followers[current->nb_links];
}
i++;
}
}
}
destroy_state_machine(&entry);
}
int main(int argc,char** argv) {
parse();
return 0;
}