code simplified and works correct

This commit is contained in:
Kolan Sh 2011-06-28 16:16:35 +04:00
parent 4364c55c16
commit 46be388497
1 changed files with 88 additions and 268 deletions

View File

@ -8,8 +8,8 @@
#include "c_const.h"
/* only for debug */
//~ #define __TEX_TABLE_DEBUG 0
//~ #include <stdio.h>
#define __TEX_TABLE_DEBUG 0
#include <stdio.h>
int parse_table(const char *table_source, size_t len, struct table_s *table, struct xerror_s *error)
@ -38,7 +38,6 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
IN_TAG_MULTICOLUMN,
IN_TAG_MULTIROW,
IN_TAG_TABULARNEWLINE,
IN_TAG_SLASH,
IN_FORMULA, // in $...$
IN_LONGTABLE, // in longtable
@ -62,6 +61,7 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
#ifdef __TEX_TABLE_DEBUG
printf("tex_level = %lu, c = %c\n", tex_level, c);
#endif
/* looking at where_stack and encountered character */
switch (where_stack[tex_level]) {
case IN_UNDEF:
@ -94,7 +94,7 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
if (isalnum(c)) {
tag = zalloc_append8_str(tag, c);
} else if (!tag && (c == '\\' || c == ',' || c == '%')) { // newline tag or \,
} else if (!tag && (c == '\\' || c == ',' || c == '%')) {
where_stack[tex_level--] = IN_UNDEF;
} else if (!tag) {
@ -103,110 +103,50 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
return error->code;
} else {
if (!strcmp(tag, TEX_TAG_BEGIN)) {
if (!strcmp(tag, TEX_TAG_BEGIN))
where_stack[tex_level] = IN_TAG_BEGIN;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_CLINE)) {
else if (!strcmp(tag, TEX_TAG_CLINE))
where_stack[tex_level] = IN_TAG_CLINE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_END)) {
else if (!strcmp(tag, TEX_TAG_END))
where_stack[tex_level] = IN_TAG_END;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_ENDFOOT)) {
else if (!strcmp(tag, TEX_TAG_ENDFOOT))
where_stack[tex_level] = IN_TAG_ENDFOOT;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_ENDHEAD)) {
else if (!strcmp(tag, TEX_TAG_ENDHEAD))
where_stack[tex_level] = IN_TAG_ENDHEAD;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_HLINE)) {
else if (!strcmp(tag, TEX_TAG_HLINE))
where_stack[tex_level] = IN_TAG_HLINE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_HSPACE)) {
else if (!strcmp(tag, TEX_TAG_HSPACE))
where_stack[tex_level] = IN_TAG_HSPACE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_MULTICOLUMN)) {
else if (!strcmp(tag, TEX_TAG_MULTICOLUMN))
where_stack[tex_level] = IN_TAG_MULTICOLUMN;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_MULTIROW)) {
else if (!strcmp(tag, TEX_TAG_MULTIROW))
where_stack[tex_level] = IN_TAG_MULTIROW;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_TABULARNEWLINE)) {
else if (!strcmp(tag, TEX_TAG_TABULARNEWLINE))
where_stack[tex_level] = IN_TAG_TABULARNEWLINE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_SLASH)) {
where_stack[tex_level] = IN_TAG_SLASH;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else {
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
else
where_stack[tex_level] = IN_TAG_UNKNOWN;
}
if (isspace(c)) { // \tag { for example
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG_UNKNOWN;
where_stack[++tex_level] = IN_SPACE;
} else if (c == '\\' || c == '}'
|| c == ']' || c == '{'
|| c == '[' || c == '%')
i--;
}
break;
case IN_SPACE:
if (isspace(c)) {
} else {
if (!isspace(c)) {
where_stack[tex_level--] = IN_UNDEF;
i--;
}
@ -239,111 +179,32 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
break;
case IN_TAG_UNKNOWN:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
case IN_TAG_BEGIN:
case IN_TAG_CLINE:
case IN_TAG_END:
case IN_TAG_ENDFOOT:
case IN_TAG_ENDHEAD:
case IN_TAG_HLINE:
case IN_TAG_HSPACE:
case IN_TAG_MULTICOLUMN:
case IN_TAG_MULTIROW:
case IN_TAG_TABULARNEWLINE:
switch(where_stack[tex_level]) {
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
case IN_TAG_UNKNOWN:
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_UNKNOWN)";
return error->code;
}
break;
case IN_TAG_BEGIN:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isalnum(c) || c == '#') {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_BEGIN)";
return error->code;
}
break;
case IN_TAG_CLINE:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_UNKNOWN)";
return error->code;
}
break;
case IN_TAG_END:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_UNKNOWN)";
return error->code;
}
break;
case IN_TAG_ENDFOOT:
@ -355,7 +216,6 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
break;
case IN_TAG_HLINE:
where_stack[tex_level--] = IN_UNDEF;
break;
@ -364,90 +224,50 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
break;
case IN_TAG_MULTICOLUMN:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isalnum(c) || c == '#' || c == '&' || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_MULTICOLUMN)";
return error->code;
}
break;
case IN_TAG_MULTIROW:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_MULTIROW)";
return error->code;
}
break;
case IN_TAG_TABULARNEWLINE:
if (c == '{' || c == '[') { // tag params
break;
default:
error->code = (int)i;
error->message = "unknown error (IN_TAG_))";
return error->code;
break;
}
if (c == '{' || c == '[')
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isalnum(c) || c == '#') {
} else if (c == '\\') {
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (isspace(c))
where_stack[++tex_level] = IN_SPACE;
else if (c == '%')
where_stack[++tex_level] = IN_COMMENT;
else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_TABULARNEWLINE)";
error->message = "unexpected symbol (IN_TAG_)";
return error->code;
}
break;
case IN_TAG_SLASH:
break;
case IN_FORMULA:
break;