Read lua files more efficiently

... replacing horribly hacky loop.
This commit is contained in:
Pasi Kallinen
2019-11-29 21:49:03 +02:00
parent 2954608a39
commit f8a1059a87
+7 -8
View File
@@ -804,7 +804,7 @@ const char *fname;
dlb *fh; dlb *fh;
char *buf = (char *) 0; char *buf = (char *) 0;
long buflen; long buflen;
int c, llret; int cnt, llret;
long bufidx = 0; long bufidx = 0;
fh = dlb_fopen(fname, "r"); fh = dlb_fopen(fname, "r");
@@ -819,13 +819,12 @@ const char *fname;
buf = (char *) alloc(sizeof(char) * (buflen + 1)); buf = (char *) alloc(sizeof(char) * (buflen + 1));
dlb_fseek(fh, 0L, SEEK_SET); dlb_fseek(fh, 0L, SEEK_SET);
do { if ((cnt = dlb_fread(buf, 1, buflen, fh)) != buflen) {
c = dlb_fgetc(fh); impossible("nhl_loadlua: Error loading %s, got %i/%li bytes", fname, cnt, buflen);
if (c == EOF) ret = FALSE;
break; goto give_up;
buf[bufidx++] = (char) c; }
} while (bufidx < buflen); buf[buflen] = '\0';
buf[bufidx] = '\0';
(void) dlb_fclose(fh); (void) dlb_fclose(fh);
llret = luaL_loadstring(L, buf); llret = luaL_loadstring(L, buf);