scriptfile.[cpp|h] cleanups

git-svn-id: https://svn.eduke32.com/eduke32@6299 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2017-06-27 02:24:28 +00:00
parent f490e5b2cb
commit ee367baaea
2 changed files with 106 additions and 79 deletions

View file

@ -21,14 +21,14 @@ char *scriptfile_gettoken(scriptfile *sf);
int32_t scriptfile_getnumber(scriptfile *sf, int32_t *num); int32_t scriptfile_getnumber(scriptfile *sf, int32_t *num);
int32_t scriptfile_getdouble(scriptfile *sf, double *num); int32_t scriptfile_getdouble(scriptfile *sf, double *num);
int32_t scriptfile_getstring(scriptfile *sf, char **st); int32_t scriptfile_getstring(scriptfile *sf, char **st);
int32_t scriptfile_getsymbol(scriptfile *sf, int32_t *num); int scriptfile_getsymbol(scriptfile *sf, int32_t *num);
int32_t scriptfile_getlinum(const scriptfile *sf, const char *ptr); int32_t scriptfile_getlinum(const scriptfile *sf, const char *ptr);
int32_t scriptfile_getbraces(scriptfile *sf, char **braceend); int32_t scriptfile_getbraces(scriptfile *sf, char **braceend);
scriptfile *scriptfile_fromfile(const char *fn); scriptfile *scriptfile_fromfile(const char *fn);
scriptfile *scriptfile_fromstring(const char *string); scriptfile *scriptfile_fromstring(const char *string);
void scriptfile_close(scriptfile *sf); void scriptfile_close(scriptfile *sf);
int32_t scriptfile_eof(scriptfile *sf); int scriptfile_eof(scriptfile *sf);
int32_t scriptfile_getsymbolvalue(char const *name, int32_t *val); int32_t scriptfile_getsymbolvalue(char const *name, int32_t *val);
int32_t scriptfile_addsymbolvalue(char const *name, int32_t val); int32_t scriptfile_addsymbolvalue(char const *name, int32_t val);

View file

@ -13,21 +13,30 @@
#define ISWS(x) ((x == ' ') || (x == '\t') || (x == '\r') || (x == '\n')) #define ISWS(x) ((x == ' ') || (x == '\t') || (x == '\r') || (x == '\n'))
static void skipoverws(scriptfile *sf) { if ((sf->textptr < sf->eof) && (!sf->textptr[0])) sf->textptr++; } static inline void skipoverws(scriptfile *sf) { if ((sf->textptr < sf->eof) && (!sf->textptr[0])) sf->textptr++; }
static void skipovertoken(scriptfile *sf) { while ((sf->textptr < sf->eof) && (sf->textptr[0])) sf->textptr++; } static inline void skipovertoken(scriptfile *sf) { while ((sf->textptr < sf->eof) && (sf->textptr[0])) sf->textptr++; }
char *scriptfile_gettoken(scriptfile *sf) char *scriptfile_gettoken(scriptfile *sf)
{ {
char *start; if (scriptfile_eof(sf)) return NULL;
skipoverws(sf); char *start = sf->ltextptr = sf->textptr;
if (sf->textptr >= sf->eof) return NULL;
start = sf->ltextptr = sf->textptr;
skipovertoken(sf); skipovertoken(sf);
return start; return start;
} }
static int scriptfile_eof_error(scriptfile *sf)
{
if (scriptfile_eof(sf))
{
initprintf("Error on line %s:%d: unexpected eof\n", sf->filename, scriptfile_getlinum(sf, sf->textptr));
return -1;
}
return 0;
}
int32_t scriptfile_getstring(scriptfile *sf, char **retst) int32_t scriptfile_getstring(scriptfile *sf, char **retst)
{ {
(*retst) = scriptfile_gettoken(sf); (*retst) = scriptfile_gettoken(sf);
@ -41,14 +50,9 @@ int32_t scriptfile_getstring(scriptfile *sf, char **retst)
int32_t scriptfile_getnumber(scriptfile *sf, int32_t *num) int32_t scriptfile_getnumber(scriptfile *sf, int32_t *num)
{ {
skipoverws(sf); if (scriptfile_eof_error(sf)) return -1;
if (sf->textptr >= sf->eof)
{
initprintf("Error on line %s:%d: unexpected eof\n",sf->filename,scriptfile_getlinum(sf,sf->textptr));
return -1;
}
while ((sf->textptr[0] == '0') && (sf->textptr[1] >= '0') && (sf->textptr[1] <= '9')) while ((sf->textptr[0] == '0') && isdigit(sf->textptr[1]))
sf->textptr++; //hack to treat octal numbers like decimal sf->textptr++; //hack to treat octal numbers like decimal
sf->ltextptr = sf->textptr; sf->ltextptr = sf->textptr;
@ -75,7 +79,7 @@ static double parsedouble(char *ptr, char **end)
else if (*p == '+') p++; else if (*p == '+') p++;
for (;; p++) for (;; p++)
{ {
if (*p >= '0' && *p <= '9') if (isdigit(*p))
{ {
dig = *p - '0'; dig = *p - '0';
if (beforedecimal) num = num * 10.0 + dig; if (beforedecimal) num = num * 10.0 + dig;
@ -107,12 +111,8 @@ static double parsedouble(char *ptr, char **end)
int32_t scriptfile_getdouble(scriptfile *sf, double *num) int32_t scriptfile_getdouble(scriptfile *sf, double *num)
{ {
skipoverws(sf); if (scriptfile_eof_error(sf))
if (sf->textptr >= sf->eof)
{
initprintf("Error on line %s:%d: unexpected eof\n",sf->filename,scriptfile_getlinum(sf,sf->textptr));
return -1; return -1;
}
sf->ltextptr = sf->textptr; sf->ltextptr = sf->textptr;
@ -130,21 +130,20 @@ int32_t scriptfile_getdouble(scriptfile *sf, double *num)
return 0; return 0;
} }
int32_t scriptfile_getsymbol(scriptfile *sf, int32_t *num) int scriptfile_getsymbol(scriptfile *sf, int32_t *num)
{ {
char *t, *e; char *t = scriptfile_gettoken(sf);
int32_t v;
t = scriptfile_gettoken(sf);
if (!t) return -1; if (!t) return -1;
v = Bstrtol(t, &e, 10); char * e;
int32_t v = Bstrtol(t, &e, 10);
if (*e) if (*e)
{ {
// looks like a string, so find it in the symbol table // looks like a string, so find it in the symbol table
if (scriptfile_getsymbolvalue(t, num)) return 0; if (scriptfile_getsymbolvalue(t, num)) return 0;
initprintf("Error on line %s:%d: expecting symbol, got \"%s\"\n",sf->filename,scriptfile_getlinum(sf,sf->ltextptr),t); initprintf("Error on line %s:%d: expecting symbol, got \"%s\"\n", sf->filename, scriptfile_getlinum(sf, sf->ltextptr), t);
return -2; // not found return -2;
} }
*num = v; *num = v;
@ -153,29 +152,30 @@ int32_t scriptfile_getsymbol(scriptfile *sf, int32_t *num)
int32_t scriptfile_getbraces(scriptfile *sf, char **braceend) int32_t scriptfile_getbraces(scriptfile *sf, char **braceend)
{ {
int32_t bracecnt; if (scriptfile_eof_error(sf))
char *bracestart;
skipoverws(sf);
if (sf->textptr >= sf->eof)
{
initprintf("Error on line %s:%d: unexpected eof\n",sf->filename,scriptfile_getlinum(sf,sf->textptr));
return -1; return -1;
}
if (sf->textptr[0] != '{') if (sf->textptr[0] != '{')
{ {
initprintf("Error on line %s:%d: expecting '{'\n",sf->filename,scriptfile_getlinum(sf,sf->textptr)); initprintf("Error on line %s:%d: expecting '{'\n",sf->filename,scriptfile_getlinum(sf,sf->textptr));
return -1; return -1;
} }
bracestart = ++sf->textptr; bracecnt = 1;
while (1) char *bracestart = ++sf->textptr;
int bracecnt = 1;
do
{ {
if (sf->textptr >= sf->eof) return 0; if (sf->textptr >= sf->eof)
if (sf->textptr[0] == '{') bracecnt++; return 0;
if (sf->textptr[0] == '}') { bracecnt--; if (!bracecnt) break; } else if (sf->textptr[0] == '{')
bracecnt++;
else if (sf->textptr[0] == '}')
if (!(--bracecnt))
break;
sf->textptr++; sf->textptr++;
} } while (1);
(*braceend) = sf->textptr; (*braceend) = sf->textptr;
sf->textptr = bracestart; sf->textptr = bracestart;
return 0; return 0;
@ -206,9 +206,22 @@ void scriptfile_preparse(scriptfile *sf, char *tx, int32_t flen)
for (i=0; i<flen; i++) for (i=0; i<flen; i++)
{ {
//detect all 4 types of carriage return (\r, \n, \r\n, \n\r :) //detect all 4 types of carriage return (\r, \n, \r\n, \n\r :)
cr=0; if (tx[i] == '\r') { i += (tx[i+1] == '\n'); cr = 1; } cr = 0;
else if (tx[i] == '\n') { i += (tx[i+1] == '\r'); cr = 1; } if (tx[i] == '\r')
if (cr) { numcr++; continue; } {
i += (tx[i + 1] == '\n');
cr = 1;
}
else if (tx[i] == '\n')
{
i += (tx[i + 1] == '\r');
cr = 1;
}
if (cr)
{
numcr++;
continue;
}
} }
sf->linenum = numcr; sf->linenum = numcr;
@ -219,8 +232,17 @@ void scriptfile_preparse(scriptfile *sf, char *tx, int32_t flen)
for (i=0; i<flen; i++) for (i=0; i<flen; i++)
{ {
//detect all 4 types of carriage return (\r, \n, \r\n, \n\r :) //detect all 4 types of carriage return (\r, \n, \r\n, \n\r :)
cr=0; if (tx[i] == '\r') { i += (tx[i+1] == '\n'); cr = 1; } cr = 0;
else if (tx[i] == '\n') { i += (tx[i+1] == '\r'); cr = 1; } if (tx[i] == '\r')
{
i += (tx[i + 1] == '\n');
cr = 1;
}
else if (tx[i] == '\n')
{
i += (tx[i + 1] == '\r');
cr = 1;
}
if (cr) if (cr)
{ {
//Remember line numbers by storing the byte index at the start of each line //Remember line numbers by storing the byte index at the start of each line
@ -230,13 +252,29 @@ void scriptfile_preparse(scriptfile *sf, char *tx, int32_t flen)
ws = 1; continue; //strip CR/LF ws = 1; continue; //strip CR/LF
} }
if ((!inquote) && ((tx[i] == ' ') || (tx[i] == '\t'))) { ws = 1; continue; } //strip Space/Tab if ((!inquote) && ((tx[i] == ' ') || (tx[i] == '\t')))
if ((tx[i] == '/') && (tx[i+1] == '/') && (!cs)) cs = 1; {
if ((tx[i] == '/') && (tx[i+1] == '*') && (!cs)) { ws = 1; cs = 2; } ws = 1;
if ((tx[i] == '*') && (tx[i+1] == '/') && (cs == 2)) { cs = 0; i++; continue; } continue;
if (cs) continue; } // strip Space/Tab
if ((tx[i] == '/') && (tx[i + 1] == '/') && (!cs))
cs = 1;
if ((tx[i] == '/') && (tx[i + 1] == '*') && (!cs))
{
ws = 1;
cs = 2;
}
if ((tx[i] == '*') && (tx[i + 1] == '/') && (cs == 2))
{
cs = 0;
i++;
continue;
}
if (cs)
continue;
if (ws) { tx[nflen++] = 0; ws = 0; } if (ws)
tx[nflen++] = ws = 0;
//quotes inside strings: \" //quotes inside strings: \"
if ((tx[i] == '\\') && (tx[i+1] == '\"')) { i++; tx[nflen++] = '\"'; continue; } if ((tx[i] == '\\') && (tx[i+1] == '\"')) { i++; tx[nflen++] = '\"'; continue; }
@ -262,24 +300,19 @@ void scriptfile_preparse(scriptfile *sf, char *tx, int32_t flen)
scriptfile *scriptfile_fromfile(const char *fn) scriptfile *scriptfile_fromfile(const char *fn)
{ {
int32_t fp; int32_t fp = kopen4load(fn, 0);
scriptfile *sf; if (fp < 0) return nullptr;
char *tx;
uint32_t flen;
fp = kopen4load(fn,0); uint32_t flen = kfilelength(fp);
if (fp<0) return NULL; char * tx = (char *)Xmalloc(flen + 2);
flen = kfilelength(fp); scriptfile *sf = (scriptfile *)Xmalloc(sizeof(scriptfile));
tx = (char *)Xmalloc(flen + 2);
sf = (scriptfile *)Xmalloc(sizeof(scriptfile));
kread(fp, tx, flen); kread(fp, tx, flen);
tx[flen] = tx[flen+1] = 0;
kclose(fp); kclose(fp);
tx[flen] = tx[flen+1] = 0;
scriptfile_preparse(sf,tx,flen); scriptfile_preparse(sf,tx,flen);
sf->filename = Xstrdup(fn); sf->filename = Xstrdup(fn);
@ -288,17 +321,12 @@ scriptfile *scriptfile_fromfile(const char *fn)
scriptfile *scriptfile_fromstring(const char *string) scriptfile *scriptfile_fromstring(const char *string)
{ {
scriptfile *sf; if (!string) return nullptr;
char *tx;
uint32_t flen;
if (!string) return NULL; uint32_t flen = Bstrlen(string);
char * tx = (char *)Xmalloc(flen + 2);
flen = strlen(string); scriptfile *sf = (scriptfile *)Xmalloc(sizeof(scriptfile));
tx = (char *)Xmalloc(flen + 2);
sf = (scriptfile *)Xmalloc(sizeof(scriptfile));
Bmemcpy(tx, string, flen); Bmemcpy(tx, string, flen);
tx[flen] = tx[flen+1] = 0; tx[flen] = tx[flen+1] = 0;
@ -318,11 +346,10 @@ void scriptfile_close(scriptfile *sf)
Bfree(sf); Bfree(sf);
} }
int32_t scriptfile_eof(scriptfile *sf) int scriptfile_eof(scriptfile *sf)
{ {
skipoverws(sf); skipoverws(sf);
if (sf->textptr >= sf->eof) return 1; return !!(sf->textptr >= sf->eof);
return 0;
} }
#define SYMBTABSTARTSIZE 256 #define SYMBTABSTARTSIZE 256