#include <stdio.h>
#include <strings.h>
#define INMATCH -10
#define BROKEN -20
static char dead[255] = {0};
typedef struct {
char **table;
int n; // number of entries
int s; // byte slices per entry
int m; // shift factor
} scheme;
int get_match(char **needles, int n, char c){
static int pos = 0;
static char alive[255] = {0};
int match = -1;
for (int i = 0; i < n; ++i){
if (pos == 0)
alive[i] = (needles[i][pos] == c);
else if (alive[i]){
alive[i] = (needles[i][pos] == c);
if (alive[i] && needles[i][pos+1] == '\0'){
match = i;
break;
}
}
}
if (memcmp(alive, dead, 255) == 0){
pos = 0;
return BROKEN;
}
else if (match != -1){
memset(alive, 0x00, 255);
pos = 0;
return match;
}
else {
++pos;
return INMATCH;
}
}
void parse_hidden(scheme *prog, int v, FILE *file){
#define GETINPUT(z) (z = fgetc(file))
char parts[1024*16];
int x = 0, c = 1, t = BROKEN, b = 0, h = 0;
t = get_match(prog[0].table, prog[0].n, GETINPUT(c));
while (c != EOF){
if (t == BROKEN){
if (b != 0 && h != 0){
fprintf(stdout, "\n+----------------- - - -\n| \n| ");
int n, o, m, s;
if ( h < (prog[1].n) ){
n = prog[1].n;
s = prog[1].s;
m = prog[1].m;
}
else {
n = prog[0].n;
s = prog[0].s;
m = prog[0].m;
}
for (int i = 0; i < b;){
x = 0;
for (o = s-1; o >= 0;)
x = (parts[i++] << (o--*m)) | x;
if ((x >= 32 && x <= 126) || x == '\n' || x == '\r'){
fputc(x, stdout);
if (x == '\n')
fprintf(stdout, "| ");
}
}
}
b = 0;
h = 0;
}
if (t == BROKEN && c != EOF)
do t = get_match(prog[0].table, prog[0].n, GETINPUT(c)); while (t == BROKEN && c != EOF);
while (t == INMATCH && c != EOF)
t = get_match(prog[0].table, prog[0].n, GETINPUT(c));
if (c == EOF) break;
if (t == BROKEN){
t = get_match(prog[0].table, prog[0].n, GETINPUT(c));
continue;
}
parts[b++] = t;
if (t > h) h = t;
t = get_match(prog[0].table, prog[0].n, GETINPUT(c));
}
}
int main(int argc, char **argv){
char *tableu4[] = {
"<span class=\"o\"></span>",\
"<i></i>",
"<b></b>",
"<u></u>"
};
scheme prog[2] = {
{
.table = tableu4,
.n = 4,
.s = 4,
.m = 2
},
{
.table = tableu4,
.n = 2,
.s = 8,
.m = 1
}
};
parse_hidden(prog, 2, stdin);
fprintf(stdout, "\n+----------------- - - -\n\n");
return 0;
}
[#]
#include <stdio.h>
#include <string.h>
#define INMATCH -10
#define BROKEN -20
typedef struct {
char **table;
int n; // number of entries
int s; // byte slices per entry
int m; // shift factor
} scheme;
int get_match(scheme *entry, char c){
static int pos = 0;
static char alive[255] = {0};
const static char dead[255] = {0};
char **needles = entry->table;
int n = entry->n;
int match = -1;
for (int i = 0; i < n; ++i){
if (pos == 0)
alive[i] = (needles[i][pos] == c);
else if (alive[i]){
alive[i] = (needles[i][pos] == c);
if (alive[i] && needles[i][pos+1] == '\0'){
match = i;
break;
}
}
}
if (memcmp(alive, dead, 255) == 0){
pos = 0;
return BROKEN;
}
else if (match != -1){
memset(alive, 0x00, 255);
pos = 0;
return match;
}
else {
++pos;
return INMATCH;
}
}
void parse_hidden(scheme *prog, int v, FILE *file, FILE *fout){
#define GETINPUT(z) (z = fgetc(file))
char parts[1024*16];
int c = 1, t = BROKEN, b = 0, h = 0;
do {
t = get_match(prog, GETINPUT(c));
if (t == BROKEN){
if (b != 0 && h != 0){
int n, o, m, s;
o = (h > 1) ? 0 : 1;
n = prog[o].n;
s = prog[o].s;
m = prog[o].m;
if (b >= s){
fprintf(fout, "\n+----------------- - - -\n| \n| ");
for (int i = 0, x = 0; i < b; x = 0){
for (o = s-1; o >= 0; x = (parts[i++] << (o--*m)) | x);
if ((x >= 32 && x <= 126) || x == '\n' || x == '\r')
if (fputc(x, fout) == '\n')
fprintf(fout, "| ");
}
}
}
b = 0;
h = 0;
do t = get_match(prog, GETINPUT(c)); while (t == BROKEN && c != EOF);
}
while (t == INMATCH)
t = get_match(prog, GETINPUT(c));
if (c == EOF) break;
if (t == BROKEN) continue;
parts[b++] = t;
if (t > h) h = t;
} while (c != EOF);
#undef GETINPUT
fprintf(fout, "\n+----------------- - - -\n\n");
}
int main(int argc, char **argv){
char *tableu4[] = {
"<span class=\"o\"></span>",
"<i></i>",
"<b></b>",
"<u></u>"
};
scheme prog[2] = {
{
.table = tableu4,
.n = 4,
.s = 4,
.m = 2
},
{
.table = tableu4,
.n = 2,
.s = 8,
.m = 1
}
};
parse_hidden(prog, 2, stdin, stdout);
return 0;
}
[/#]