My piece of code adding HTML tags to input of user. FOR example
4tab6tab7
gives me
`<tr><td>4<td><td>5<td><td>7<td></tr>`
But sometimes I can have headings which means that I have to add (in our example) `<tr><th>4<th><th>5<th><th>7<th></tr>` (td is changed to th).
I dont know how to make optional argument "-h num" (I run like "table -h3") which states how many lines at the beginning are "heading" lines; if this option is not present, it defaults to zero. I know that I have to use getopt(). But I dont know how to do it...Help me please....Thanks
Code:
#include <stdio.h>
enum ctypes {
CHAR, BLANK, LINE, NTYPES
};
int type(int c)
{
return c == '\n' ? LINE : c == '\t';
}
int main(void)
{
static const struct t {
const char *fmt;
int state;
} trans[][NTYPES] = {
{ { "<tr><td>%c", 1 }, { "", 0 }, { "", 0 } },
{ { "%c", 1 }, { "</td>", 2 }, { "</td></tr>\n", 0 } },
{ { "<td>%c", 1 }, { "", 2 }, { "</tr>\n", 0 } }
};
const struct t *tp;
int c, state;
// puts("<table>");
for (state = 0; (c = getchar()) != EOF; state = tp->state) {
tp = trans[state] + type(c);
printf(tp->fmt, c);
}
// puts("</table>");}