Clean up a few functions in mact scriplib

git-svn-id: https://svn.eduke32.com/eduke32@7110 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-10-25 23:31:35 +00:00
parent 59196c42ea
commit 50ea556fc6

View file

@ -226,11 +226,16 @@ void SCRIPT_AddEntry(int32_t scripthandle, const char * sectionname, const char
int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length) int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
{ {
char *fence = data + length; if (!data || length < 0) return 1;
char *dp, *sp, ch=0, lastch=0;
char const *currentsection = ""; char const *currentsection = "";
char *currententry = NULL; char const *currententry = NULL;
char *currentvalue = NULL; char const *currentvalue = NULL;
char *fence = data + length;
char ch = 0;
char lastch = 0;
enum enum
{ {
ParsingIdle, ParsingIdle,
@ -240,6 +245,7 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
ParsingValueBegin, ParsingValueBegin,
ParsingValue ParsingValue
}; };
enum enum
{ {
ExpectingSection = 1, ExpectingSection = 1,
@ -248,18 +254,17 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
ExpectingValue = 8, ExpectingValue = 8,
ExpectingComment = 16 ExpectingComment = 16
}; };
int32_t state;
int32_t expect;
int32_t linenum=1;
int32_t rv = 0;
#define SETRV(v) if (v>rv||rv==0) rv=v
if (!data) return 1;
if (length < 0) return 1;
dp = sp = data; #define SETERR(v) do { if (v>errlevel||errlevel==0) errlevel=v; } while (0)
state = ParsingIdle;
expect = ExpectingSection | ExpectingEntry; char *sp = data;
char *dp = data;
int state = ParsingIdle;
int expect = ExpectingSection | ExpectingEntry;
int linenum = 1;
int errlevel = 0;
#define EATLINE(p) while (length > 0 && *p != '\n' && *p != '\r') { p++; length--; } #define EATLINE(p) while (length > 0 && *p != '\n' && *p != '\r') { p++; length--; }
#define LETTER() { lastch = ch; ch = *(sp++); length--; } #define LETTER() { lastch = ch; ch = *(sp++); length--; }
@ -287,7 +292,7 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
{ {
// Unexpected section start // Unexpected section start
printf("Unexpected start of section on line %d.\n", linenum); printf("Unexpected start of section on line %d.\n", linenum);
SETRV(-1); SETERR(-1);
EATLINE(sp); EATLINE(sp);
continue; continue;
} }
@ -303,7 +308,7 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
{ {
// Unexpected name start // Unexpected name start
printf("Unexpected entry LabelText on line %d.\n", linenum); printf("Unexpected entry LabelText on line %d.\n", linenum);
SETRV(-1); SETERR(-1);
EATLINE(sp); EATLINE(sp);
continue; continue;
} }
@ -318,7 +323,7 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
{ {
// Unexpected character // Unexpected character
printf("Illegal character (ASCII %d) on line %d.\n", ch, linenum); printf("Illegal character (ASCII %d) on line %d.\n", ch, linenum);
SETRV(-1); SETERR(-1);
EATLINE(sp); EATLINE(sp);
continue; continue;
} }
@ -335,7 +340,7 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
case '\n': case '\n':
case '\r': // Unexpected newline case '\r': // Unexpected newline
printf("Unexpected newline on line %d.\n", linenum); printf("Unexpected newline on line %d.\n", linenum);
SETRV(-1); SETERR(-1);
state = ParsingIdle; state = ParsingIdle;
linenum++; linenum++;
continue; continue;
@ -361,13 +366,13 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
// unexpected comment // unexpected comment
EATLINE(sp); EATLINE(sp);
printf("Unexpected comment on line %d.\n", linenum); printf("Unexpected comment on line %d.\n", linenum);
SETRV(-1); SETERR(-1);
fallthrough__; fallthrough__;
case '\n': case '\n':
case '\r': case '\r':
// Unexpected newline // Unexpected newline
printf("Unexpected newline on line %d.\n", linenum); printf("Unexpected newline on line %d.\n", linenum);
SETRV(-1); SETERR(-1);
expect = ExpectingSection | ExpectingEntry; expect = ExpectingSection | ExpectingEntry;
state = ParsingIdle; state = ParsingIdle;
linenum++; linenum++;
@ -418,7 +423,7 @@ int32_t SCRIPT_ParseBuffer(int32_t scripthandle, char *data, int32_t length)
if (sp > fence) printf("Stepped outside the fence!\n"); if (sp > fence) printf("Stepped outside the fence!\n");
return rv; return errlevel;
} }