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"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
|
|
#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 void skipovertoken(scriptfile *sf) { while ((sf->textptr < sf->eof) && (sf->textptr[0])) sf->textptr++; }
|
|
|
|
|
|
|
|
char *scriptfile_gettoken(scriptfile *sf)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
char *start;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
skipoverws(sf);
|
|
|
|
if (sf->textptr >= sf->eof) return NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
start = sf->ltextptr = sf->textptr;
|
|
|
|
skipovertoken(sf);
|
|
|
|
return start;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
return(-2);
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((sf->textptr[0] == '0') && (sf->textptr[1] >= '0') && (sf->textptr[1] <= '9'))
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
if (*p >= '0' && *p <= '9')
|
|
|
|
{
|
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
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_getsymbol(scriptfile *sf, int32_t *num)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
char *t, *e;
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t v;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
t = scriptfile_gettoken(sf);
|
|
|
|
if (!t) return -1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
initprintf("Error on line %s:%d: expecting symbol, got \"%s\"\n",sf->filename,scriptfile_getlinum(sf,sf->ltextptr),t);
|
|
|
|
return -2; // not found
|
|
|
|
}
|
|
|
|
|
|
|
|
*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
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t bracecnt;
|
2006-04-24 19:04:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
bracestart = ++sf->textptr; bracecnt = 1;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (sf->textptr >= sf->eof) return(0);
|
|
|
|
if (sf->textptr[0] == '{') bracecnt++;
|
2007-12-12 17:42:14 +00:00
|
|
|
if (sf->textptr[0] == '}') { bracecnt--; if (!bracecnt) break; }
|
2006-04-24 19:04:22 +00:00
|
|
|
sf->textptr++;
|
|
|
|
}
|
|
|
|
(*braceend) = sf->textptr;
|
|
|
|
sf->textptr = bracestart;
|
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_getlinum(scriptfile *sf, 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
|
|
|
|
2006-04-24 19:04:22 +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
|
|
|
|
2009-02-19 16:47:54 +00:00
|
|
|
for (stp=1; stp+stp<sf->linenum; stp+=stp); //stp = highest power of 2 less than sf->linenum
|
|
|
|
for (i=0; stp; stp>>=1)
|
2006-04-24 19:04:22 +00:00
|
|
|
if ((i+stp < sf->linenum) && (sf->lineoffs[i+stp] < ind)) i += stp;
|
|
|
|
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 :)
|
2009-02-19 16:47:54 +00:00
|
|
|
cr=0; if (tx[i] == '\r') { i += (tx[i+1] == '\n'); cr = 1; }
|
2006-04-24 19:04:22 +00:00
|
|
|
else if (tx[i] == '\n') { i += (tx[i+1] == '\r'); cr = 1; }
|
|
|
|
if (cr) { numcr++; continue; }
|
|
|
|
}
|
|
|
|
|
|
|
|
sf->linenum = numcr;
|
2009-10-07 06:47:35 +00:00
|
|
|
sf->lineoffs = (int32_t *)Bmalloc(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 :)
|
2009-02-19 16:47:54 +00:00
|
|
|
cr=0; if (tx[i] == '\r') { i += (tx[i+1] == '\n'); cr = 1; }
|
2006-04-24 19:04:22 +00:00
|
|
|
else if (tx[i] == '\n') { i += (tx[i+1] == '\r'); cr = 1; }
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if ((!inquote) && ((tx[i] == ' ') || (tx[i] == '\t'))) { ws = 1; continue; } //strip Space/Tab
|
2006-04-24 19:04:22 +00:00
|
|
|
if ((tx[i] == '/') && (tx[i+1] == '/') && (!cs)) cs = 1;
|
2007-12-12 17:42:14 +00:00
|
|
|
if ((tx[i] == '/') && (tx[i+1] == '*') && (!cs)) { ws = 1; cs = 2; }
|
2006-04-24 19:04:22 +00:00
|
|
|
if ((tx[i] == '*') && (tx[i+1] == '/') && (cs == 2)) { cs = 0; i++; continue; }
|
|
|
|
if (cs) continue;
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (ws) { tx[nflen++] = 0; 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
|
|
|
flen = nflen;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
scriptfile *scriptfile_fromfile(char *fn)
|
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t fp;
|
2006-04-24 19:04:22 +00:00
|
|
|
scriptfile *sf;
|
|
|
|
char *tx;
|
2009-01-09 09:29:17 +00:00
|
|
|
uint32_t flen;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
fp = kopen4load(fn,0);
|
|
|
|
if (fp<0) return NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
flen = kfilelength(fp);
|
2009-10-07 06:47:35 +00:00
|
|
|
tx = (char *) Bmalloc(flen + 2);
|
2007-12-12 17:42:14 +00:00
|
|
|
if (!tx)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
kclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2009-10-07 06:47:35 +00:00
|
|
|
sf = (scriptfile*) Bmalloc(sizeof(scriptfile));
|
2007-12-12 17:42:14 +00:00
|
|
|
if (!sf)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
kclose(fp);
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(tx);
|
2006-04-24 19:04:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
kread(fp, tx, flen);
|
|
|
|
tx[flen] = tx[flen+1] = 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
kclose(fp);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
scriptfile_preparse(sf,tx,flen);
|
2009-10-07 06:47:35 +00:00
|
|
|
sf->filename = Bstrdup(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
|
|
|
}
|
|
|
|
|
|
|
|
scriptfile *scriptfile_fromstring(char *string)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
scriptfile *sf;
|
|
|
|
char *tx;
|
2009-01-09 09:29:17 +00:00
|
|
|
uint32_t flen;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!string) return NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
flen = strlen(string);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2009-10-07 06:47:35 +00:00
|
|
|
tx = (char *) Bmalloc(flen + 2);
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!tx) return NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2009-10-07 06:47:35 +00:00
|
|
|
sf = (scriptfile*) Bmalloc(sizeof(scriptfile));
|
2007-12-12 17:42:14 +00:00
|
|
|
if (!sf)
|
|
|
|
{
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(tx);
|
2006-04-24 19:04:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
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;
|
2009-10-07 06:47:35 +00:00
|
|
|
if (sf->lineoffs) Bfree(sf->lineoffs);
|
|
|
|
if (sf->textbuf) Bfree(sf->textbuf);
|
|
|
|
if (sf->filename) Bfree(sf->filename);
|
2006-04-24 19:04:22 +00:00
|
|
|
sf->textbuf = NULL;
|
|
|
|
sf->filename = NULL;
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(sf);
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_eof(scriptfile *sf)
|
2006-10-15 18:59:45 +00:00
|
|
|
{
|
2006-11-13 23:12:47 +00:00
|
|
|
skipoverws(sf);
|
|
|
|
if (sf->textptr >= sf->eof) return 1;
|
|
|
|
return 0;
|
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;
|
|
|
|
|
2009-01-09 09:29:17 +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)
|
|
|
|
{
|
2009-02-19 16:47:54 +00:00
|
|
|
for (i=max(symbtaballoclength,SYMBTABSTARTSIZE); symbtablength+reqd>i; i<<=1);
|
2009-10-07 06:47:35 +00:00
|
|
|
np = (char *)Brealloc(symbtab, i); if (!np) return NULL;
|
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
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_getsymbolvalue(char *name, int32_t *val)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
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))
|
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
*val = *(int32_t*)(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
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t scriptfile_addsymbolvalue(char *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))
|
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
*(int32_t*)(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;
|
2009-01-09 09:29:17 +00:00
|
|
|
*(int32_t*)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)
|
|
|
|
{
|
2009-10-07 06:47:35 +00:00
|
|
|
if (symbtab) Bfree(symbtab);
|
2006-04-24 19:04:22 +00:00
|
|
|
symbtab = NULL;
|
|
|
|
symbtablength = 0;
|
|
|
|
symbtaballoclength = 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|