2006-04-23 06:44:19 +00:00
|
|
|
/*
|
|
|
|
* File Tokeniser/Parser/Whatever
|
|
|
|
* by Jonathon Fowler
|
|
|
|
* Remixed completely by Ken Silverman
|
|
|
|
* See the included license file "BUILDLIC.TXT" for license info.
|
|
|
|
*/
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
#include "compat.h"
|
2006-04-23 06:44:19 +00:00
|
|
|
#include "scriptfile.h"
|
|
|
|
#include "baselayer.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "cache1d.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define ISWS(x) ((x == ' ') || (x == '\t') || (x == '\r') || (x == '\n'))
|
2017-06-27 02:24:28 +00:00
|
|
|
static inline void skipoverws(scriptfile *sf) { if ((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++; }
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
char *scriptfile_gettoken(scriptfile *sf)
|
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
if (scriptfile_eof(sf)) return NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
char *start = sf->ltextptr = sf->textptr;
|
2006-04-24 19:04:22 +00:00
|
|
|
skipovertoken(sf);
|
|
|
|
return start;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_getstring(scriptfile *sf, char **retst)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
(*retst) = scriptfile_gettoken(sf);
|
|
|
|
if (*retst == NULL)
|
|
|
|
{
|
|
|
|
initprintf("Error on line %s:%d: unexpected eof\n",sf->filename,scriptfile_getlinum(sf,sf->textptr));
|
2016-06-21 00:34:41 +00:00
|
|
|
return -2;
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
2016-06-21 00:34:41 +00:00
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_getnumber(scriptfile *sf, int32_t *num)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
if (scriptfile_eof_error(sf)) return -1;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
while ((sf->textptr[0] == '0') && isdigit(sf->textptr[1]))
|
2006-04-24 19:04:22 +00:00
|
|
|
sf->textptr++; //hack to treat octal numbers like decimal
|
|
|
|
|
|
|
|
sf->ltextptr = sf->textptr;
|
|
|
|
(*num) = strtol((const char *)sf->textptr,&sf->textptr,0);
|
2007-12-12 17:42:14 +00:00
|
|
|
if (!ISWS(*sf->textptr) && *sf->textptr)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
char *p = sf->textptr;
|
|
|
|
skipovertoken(sf);
|
2009-01-09 09:29:17 +00:00
|
|
|
initprintf("Error on line %s:%d: expecting int32_t, got \"%s\"\n",sf->filename,scriptfile_getlinum(sf,sf->ltextptr),p);
|
2006-04-24 19:04:22 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double parsedouble(char *ptr, char **end)
|
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t beforedecimal = 1, negative = 0, dig;
|
|
|
|
int32_t exposgn = 0, expo = 0;
|
2006-04-24 19:04:22 +00:00
|
|
|
double num = 0.0, decpl = 0.1;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = ptr;
|
|
|
|
if (*p == '-') negative = 1, p++;
|
|
|
|
else if (*p == '+') p++;
|
2007-12-12 17:42:14 +00:00
|
|
|
for (;; p++)
|
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
if (isdigit(*p))
|
2007-12-12 17:42:14 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
dig = *p - '0';
|
|
|
|
if (beforedecimal) num = num * 10.0 + dig;
|
|
|
|
else if (exposgn) expo = expo*10 + dig;
|
2007-12-12 17:42:14 +00:00
|
|
|
else
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
num += (double)dig * decpl;
|
|
|
|
decpl /= 10.0;
|
|
|
|
}
|
2007-12-12 17:42:14 +00:00
|
|
|
}
|
|
|
|
else if (*p == '.')
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
if (beforedecimal) beforedecimal = 0;
|
|
|
|
else break;
|
2007-12-12 17:42:14 +00:00
|
|
|
}
|
|
|
|
else if ((*p == 'E') || (*p == 'e'))
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
exposgn = 1;
|
|
|
|
if (p[1] == '-') { exposgn = -1; p++; }
|
|
|
|
else if (p[1] == '+') p++;
|
2007-12-12 17:42:14 +00:00
|
|
|
}
|
|
|
|
else break;
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (end) *end = p;
|
|
|
|
if (exposgn) num *= pow(10.0,(double)(expo*exposgn));
|
|
|
|
return negative ? -num : num;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_getdouble(scriptfile *sf, double *num)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
if (scriptfile_eof_error(sf))
|
2006-04-24 19:04:22 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
sf->ltextptr = sf->textptr;
|
|
|
|
|
|
|
|
// On Linux, locale settings interfere with interpreting x.y format numbers
|
|
|
|
//(*num) = strtod((const char *)sf->textptr,&sf->textptr);
|
|
|
|
(*num) = parsedouble(sf->textptr, &sf->textptr);
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (!ISWS(*sf->textptr) && *sf->textptr)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
char *p = sf->textptr;
|
|
|
|
skipovertoken(sf);
|
|
|
|
initprintf("Error on line %s:%d: expecting float, got \"%s\"\n",sf->filename,scriptfile_getlinum(sf,sf->ltextptr),p);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
int scriptfile_getsymbol(scriptfile *sf, int32_t *num)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
char *t = scriptfile_gettoken(sf);
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!t) return -1;
|
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
char * e;
|
|
|
|
int32_t v = Bstrtol(t, &e, 10);
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (*e)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
// looks like a string, so find it in the symbol table
|
|
|
|
if (scriptfile_getsymbolvalue(t, num)) return 0;
|
2017-06-27 02:24:28 +00:00
|
|
|
initprintf("Error on line %s:%d: expecting symbol, got \"%s\"\n", sf->filename, scriptfile_getlinum(sf, sf->ltextptr), t);
|
|
|
|
return -2;
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*num = v;
|
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_getbraces(scriptfile *sf, char **braceend)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
if (scriptfile_eof_error(sf))
|
2006-04-24 19:04:22 +00:00
|
|
|
return -1;
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (sf->textptr[0] != '{')
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
initprintf("Error on line %s:%d: expecting '{'\n",sf->filename,scriptfile_getlinum(sf,sf->textptr));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-27 02:24:28 +00:00
|
|
|
|
|
|
|
char *bracestart = ++sf->textptr;
|
|
|
|
int bracecnt = 1;
|
|
|
|
|
|
|
|
do
|
2006-04-24 19:04:22 +00:00
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
if (sf->textptr >= sf->eof)
|
|
|
|
return 0;
|
|
|
|
else if (sf->textptr[0] == '{')
|
|
|
|
bracecnt++;
|
|
|
|
else if (sf->textptr[0] == '}')
|
|
|
|
if (!(--bracecnt))
|
|
|
|
break;
|
2006-04-24 19:04:22 +00:00
|
|
|
sf->textptr++;
|
2017-06-27 02:24:28 +00:00
|
|
|
} while (1);
|
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
(*braceend) = sf->textptr;
|
|
|
|
sf->textptr = bracestart;
|
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-10 23:45:34 +00:00
|
|
|
int32_t scriptfile_getlinum(const scriptfile *sf, const char *ptr)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t i, stp;
|
2008-06-10 19:21:16 +00:00
|
|
|
intptr_t ind;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2016-06-21 00:34:41 +00:00
|
|
|
//for(i=0;i<sf->linenum;i++) if (sf->lineoffs[i] >= ind) return i+1; //brute force algo
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2008-06-10 19:21:16 +00:00
|
|
|
ind = ((intptr_t)ptr) - ((intptr_t)sf->textbuf);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2016-01-12 10:31:12 +00:00
|
|
|
for (stp=1; stp+stp<sf->linenum; stp+=stp) { } //stp = highest power of 2 less than sf->linenum
|
2009-02-19 16:47:54 +00:00
|
|
|
for (i=0; stp; stp>>=1)
|
2016-10-23 19:47:23 +00:00
|
|
|
if ((i+stp-1 < sf->linenum) && (sf->lineoffs[i+stp-1] < ind)) i += stp;
|
2016-06-21 00:34:41 +00:00
|
|
|
return i+1; //i = index to highest lineoffs which is less than ind; convert to 1-based line numbers
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
void scriptfile_preparse(scriptfile *sf, char *tx, int32_t flen)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t i, cr, numcr, nflen, ws, cs, inquote;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
//Count number of lines
|
|
|
|
numcr = 1;
|
2009-02-19 16:47:54 +00:00
|
|
|
for (i=0; i<flen; i++)
|
2006-04-24 19:04:22 +00:00
|
|
|
{
|
|
|
|
//detect all 4 types of carriage return (\r, \n, \r\n, \n\r :)
|
2017-06-27 02:24:28 +00:00
|
|
|
cr = 0;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
numcr++;
|
|
|
|
continue;
|
|
|
|
}
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sf->linenum = numcr;
|
2014-05-30 00:02:19 +00:00
|
|
|
sf->lineoffs = (int32_t *)Xmalloc(sf->linenum*sizeof(int32_t));
|
2006-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
//Preprocess file for comments (// and /*...*/, and convert all whitespace to single spaces)
|
|
|
|
nflen = 0; ws = 0; cs = 0; numcr = 0; inquote = 0;
|
2009-02-19 16:47:54 +00:00
|
|
|
for (i=0; i<flen; i++)
|
2006-04-24 19:04:22 +00:00
|
|
|
{
|
|
|
|
//detect all 4 types of carriage return (\r, \n, \r\n, \n\r :)
|
2017-06-27 02:24:28 +00:00
|
|
|
cr = 0;
|
|
|
|
if (tx[i] == '\r')
|
|
|
|
{
|
|
|
|
i += (tx[i + 1] == '\n');
|
|
|
|
cr = 1;
|
|
|
|
}
|
|
|
|
else if (tx[i] == '\n')
|
|
|
|
{
|
|
|
|
i += (tx[i + 1] == '\r');
|
|
|
|
cr = 1;
|
|
|
|
}
|
2006-04-24 19:04:22 +00:00
|
|
|
if (cr)
|
|
|
|
{
|
|
|
|
//Remember line numbers by storing the byte index at the start of each line
|
|
|
|
//Line numbers can be retrieved by doing a binary search on the byte index :)
|
|
|
|
sf->lineoffs[numcr++] = nflen;
|
|
|
|
if (cs == 1) cs = 0;
|
|
|
|
ws = 1; continue; //strip CR/LF
|
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
if ((!inquote) && ((tx[i] == ' ') || (tx[i] == '\t')))
|
|
|
|
{
|
|
|
|
ws = 1;
|
|
|
|
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;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
if (ws)
|
|
|
|
tx[nflen++] = ws = 0;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
//quotes inside strings: \"
|
|
|
|
if ((tx[i] == '\\') && (tx[i+1] == '\"')) { i++; tx[nflen++] = '\"'; continue; }
|
|
|
|
if (tx[i] == '\"') { inquote ^= 1; continue; }
|
|
|
|
tx[nflen++] = tx[i];
|
|
|
|
}
|
|
|
|
tx[nflen++] = 0; sf->lineoffs[numcr] = nflen;
|
|
|
|
tx[nflen++] = 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
#if 0
|
2006-04-24 19:04:22 +00:00
|
|
|
//for debugging only:
|
|
|
|
printf("pre-parsed file:flen=%d,nflen=%d\n",flen,nflen);
|
2009-02-19 16:47:54 +00:00
|
|
|
for (i=0; i<nflen; i++) { if (tx[i] < 32) printf("_"); else printf("%c",tx[i]); }
|
2006-04-24 19:04:22 +00:00
|
|
|
printf("[eof]\nnumlines=%d\n",sf->linenum);
|
2009-02-19 16:47:54 +00:00
|
|
|
for (i=0; i<sf->linenum; i++) printf("line %d = byte %d\n",i,sf->lineoffs[i]);
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
sf->textbuf = sf->textptr = tx;
|
|
|
|
sf->textlength = nflen;
|
|
|
|
sf->eof = &sf->textbuf[nflen-1];
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2010-08-14 21:32:28 +00:00
|
|
|
scriptfile *scriptfile_fromfile(const char *fn)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
int32_t fp = kopen4load(fn, 0);
|
|
|
|
if (fp < 0) return nullptr;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
uint32_t flen = kfilelength(fp);
|
|
|
|
char * tx = (char *)Xmalloc(flen + 2);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
scriptfile *sf = (scriptfile *)Xmalloc(sizeof(scriptfile));
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
kread(fp, tx, flen);
|
|
|
|
kclose(fp);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
tx[flen] = tx[flen+1] = 0;
|
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
scriptfile_preparse(sf,tx,flen);
|
2014-05-30 00:02:19 +00:00
|
|
|
sf->filename = Xstrdup(fn);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
return sf;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2010-08-14 21:32:28 +00:00
|
|
|
scriptfile *scriptfile_fromstring(const char *string)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2017-06-27 02:24:28 +00:00
|
|
|
if (!string) return nullptr;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
uint32_t flen = Bstrlen(string);
|
|
|
|
char * tx = (char *)Xmalloc(flen + 2);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
scriptfile *sf = (scriptfile *)Xmalloc(sizeof(scriptfile));
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2009-06-09 06:19:58 +00:00
|
|
|
Bmemcpy(tx, string, flen);
|
2006-04-24 19:04:22 +00:00
|
|
|
tx[flen] = tx[flen+1] = 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
scriptfile_preparse(sf,tx,flen);
|
|
|
|
sf->filename = NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
return sf;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void scriptfile_close(scriptfile *sf)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!sf) return;
|
2015-09-23 17:55:31 +00:00
|
|
|
Bfree(sf->lineoffs);
|
|
|
|
Bfree(sf->textbuf);
|
|
|
|
Bfree(sf->filename);
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(sf);
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:28 +00:00
|
|
|
int scriptfile_eof(scriptfile *sf)
|
2006-10-15 18:59:45 +00:00
|
|
|
{
|
2006-11-13 23:12:47 +00:00
|
|
|
skipoverws(sf);
|
2017-06-27 02:24:28 +00:00
|
|
|
return !!(sf->textptr >= sf->eof);
|
2006-10-15 18:59:45 +00:00
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
#define SYMBTABSTARTSIZE 256
|
2009-01-09 09:29:17 +00:00
|
|
|
static int32_t symbtablength=0, symbtaballoclength=0;
|
2006-04-23 06:44:19 +00:00
|
|
|
static char *symbtab = NULL;
|
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
static char *getsymbtabspace(int32_t reqd)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
char *pos,*np;
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t i;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
if (symbtablength + reqd > symbtaballoclength)
|
|
|
|
{
|
2016-01-12 10:31:12 +00:00
|
|
|
for (i=max(symbtaballoclength,SYMBTABSTARTSIZE); symbtablength+reqd>i; i<<=1) { }
|
2014-05-30 00:02:19 +00:00
|
|
|
np = (char *)Xrealloc(symbtab, i);
|
2006-04-24 19:04:22 +00:00
|
|
|
symbtab = np; symbtaballoclength = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = &symbtab[symbtablength];
|
|
|
|
symbtablength += reqd;
|
|
|
|
return pos;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 05:06:10 +00:00
|
|
|
int32_t scriptfile_getsymbolvalue(char const *name, int32_t *val)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2016-08-27 01:40:11 +00:00
|
|
|
if (Bstrlen(name) > 2)
|
|
|
|
{
|
2016-10-23 19:47:29 +00:00
|
|
|
if (name[0] == '0' && tolower(name[1]) == 'x') // hex constants
|
2016-08-27 01:40:11 +00:00
|
|
|
{
|
2018-10-25 23:29:20 +00:00
|
|
|
uint64_t x;
|
2016-08-27 01:40:11 +00:00
|
|
|
sscanf(name + 2, "%" PRIx64 "", &x);
|
|
|
|
|
|
|
|
if (EDUKE32_PREDICT_FALSE(x > UINT32_MAX))
|
2017-06-05 10:05:12 +00:00
|
|
|
buildprint("warning: number 0x", hex(x), " truncated to 32 bits.\n");
|
2016-08-27 01:40:11 +00:00
|
|
|
|
|
|
|
*val = x;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2006-04-24 19:04:22 +00:00
|
|
|
char *scanner = symbtab;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!symbtab) return 0;
|
2007-12-12 17:42:14 +00:00
|
|
|
while (scanner - symbtab < symbtablength)
|
|
|
|
{
|
|
|
|
if (!Bstrcasecmp(name, scanner))
|
|
|
|
{
|
2015-05-03 07:03:48 +00:00
|
|
|
*val = B_UNBUF32(scanner + strlen(scanner) + 1);
|
2006-04-24 19:04:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
scanner += strlen(scanner) + 1 + sizeof(int32_t);
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 05:06:10 +00:00
|
|
|
int32_t scriptfile_addsymbolvalue(char const *name, int32_t val)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
char *sp;
|
|
|
|
// if (scriptfile_getsymbolvalue(name, &x)) return -1; // already exists
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (symbtab)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
char *scanner = symbtab;
|
2007-12-12 17:42:14 +00:00
|
|
|
while (scanner - symbtab < symbtablength)
|
|
|
|
{
|
|
|
|
if (!Bstrcasecmp(name, scanner))
|
|
|
|
{
|
2015-05-03 07:03:48 +00:00
|
|
|
B_BUF32(scanner + strlen(scanner) + 1, val);
|
2006-04-24 19:04:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
scanner += strlen(scanner) + 1 + sizeof(int32_t);
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
sp = getsymbtabspace(strlen(name) + 1 + sizeof(int32_t));
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!sp) return 0;
|
|
|
|
strcpy(sp, name);
|
|
|
|
sp += strlen(name)+1;
|
2015-05-03 07:03:48 +00:00
|
|
|
B_BUF32(sp, val);
|
2006-04-24 19:04:22 +00:00
|
|
|
return 1; // added
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void scriptfile_clearsymbols(void)
|
|
|
|
{
|
2015-09-23 17:55:31 +00:00
|
|
|
DO_FREE_AND_NULL(symbtab);
|
2006-04-24 19:04:22 +00:00
|
|
|
symbtablength = 0;
|
|
|
|
symbtaballoclength = 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|