dev/c/tex_parser/tex_table.c

103 lines
2.0 KiB
C

#include "tex_table.h"
#include <string.h>
#include <ctype.h>
#include "tex_table_tags.h"
#include "zalloc_ext.h"
#include "zalloc.h"
#define MAX_TEX_STACK_LEVEL 128
char * const PARSING_ERROR = "Error parsing";
int parse_table(const char *table_source, size_t len, struct table_s *table, struct xerror_s *error)
{
char *tag = NULL;
size_t i = 0;
if (!len)
len = strlen(table_source);
enum where_e
{
IN_UNDEF = 0, // undefined place
IN_COMMENT, // any comment
IN_TAG, // any tag
IN_CRLBR, // parameter in curly bracket
IN_SQRBR, // parameter in square bracket
IN_FORMULA, // in $...$
IN_LONGTABLE, // in tag
} where_stack[MAX_TEX_STACK_LEVEL] = {IN_UNDEF};
size_t tex_level = 0;
for (i = 0; i < len; i++) {
char c = table_source[i];
switch (where_stack[tex_level]) {
case IN_UNDEF:
if (c == '\\') {
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else {
error->code = -1;
error->message = PARSING_ERROR;
return -1;
}
break;
case IN_COMMENT:
if (c == '\r' || c == '\n') {
where_stack[tex_level--] = IN_UNDEF;
}
break;
case IN_TAG:
if (islower(c) || isdigit(c)) {
zalloc_append8_str(tag, c);
} else {
if (!strcmp(tag, TEX_TAG_BEGIN)) {
} else if (!strcmp(tag, TEX_TAG_CLINE)) {
} else if (!strcmp(tag, TEX_TAG_END)) {
} else if (!strcmp(tag, TEX_TAG_ENDFOOT)) {
} else if (!strcmp(tag, TEX_TAG_ENDHEAD)) {
} else if (!strcmp(tag, TEX_TAG_HLINE)) {
} else if (!strcmp(tag, TEX_TAG_HSPACE)) {
} else if (!strcmp(tag, TEX_TAG_MULTICOLUMN)) {
} else if (!strcmp(tag, TEX_TAG_MULTIROW)) {
} else if (!strcmp(tag, TEX_TAG_TABULARNEWLINE)) {
} else if (!strcmp(tag, TEX_TAG_SLASH)) {
}
}
break;
default:
error->code = -1;
error->message = PARSING_ERROR;
return -1;
break;
}
}
table->id = "Drossel1.Table1";
error->message = NULL;
return 0;
}