[Scummvm-cvs-logs] SF.net SVN: scummvm:[33399] residual/trunk/engine/lua
aquadran at users.sourceforge.net
aquadran at users.sourceforge.net
Tue Jul 29 10:05:28 CEST 2008
Revision: 33399
http://scummvm.svn.sourceforge.net/scummvm/?rev=33399&view=rev
Author: aquadran
Date: 2008-07-29 08:05:28 +0000 (Tue, 29 Jul 2008)
Log Message:
-----------
formating code
Modified Paths:
--------------
residual/trunk/engine/lua/lfunc.cpp
residual/trunk/engine/lua/lfunc.h
residual/trunk/engine/lua/lgc.cpp
residual/trunk/engine/lua/lgc.h
residual/trunk/engine/lua/liolib.cpp
Modified: residual/trunk/engine/lua/lfunc.cpp
===================================================================
--- residual/trunk/engine/lua/lfunc.cpp 2008-07-29 06:04:16 UTC (rev 33398)
+++ residual/trunk/engine/lua/lfunc.cpp 2008-07-29 08:05:28 UTC (rev 33399)
@@ -6,69 +6,60 @@
-#include "lfunc.h"
-#include "lmem.h"
-#include "lstate.h"
+#include "engine/lua/lfunc.h"
+#include "engine/lua/lmem.h"
+#include "engine/lua/lstate.h"
#define gcsizeproto(p) 5 /* approximate "weight" for a prototype */
#define gcsizeclosure(c) 1 /* approximate "weight" for a closure */
-Closure *luaF_newclosure (int32 nelems)
-{
- Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
- luaO_insertlist(&(L->rootcl), (GCnode *)c);
- L->nblocks += gcsizeclosure(c);
- c->nelems = nelems;
- return c;
+Closure *luaF_newclosure(int32 nelems) {
+ Closure *c = (Closure *)luaM_malloc(sizeof(Closure) + nelems * sizeof(TObject));
+ luaO_insertlist(&(L->rootcl), (GCnode *)c);
+ L->nblocks += gcsizeclosure(c);
+ c->nelems = nelems;
+ return c;
}
-
-TProtoFunc *luaF_newproto (void)
-{
- TProtoFunc *f = luaM_new(TProtoFunc);
- f->code = NULL;
- f->lineDefined = 0;
- f->fileName = NULL;
- f->consts = NULL;
- f->nconsts = 0;
- f->locvars = NULL;
- luaO_insertlist(&(L->rootproto), (GCnode *)f);
- L->nblocks += gcsizeproto(f);
- return f;
+TProtoFunc *luaF_newproto() {
+ TProtoFunc *f = luaM_new(TProtoFunc);
+ f->code = NULL;
+ f->lineDefined = 0;
+ f->fileName = NULL;
+ f->consts = NULL;
+ f->nconsts = 0;
+ f->locvars = NULL;
+ luaO_insertlist(&(L->rootproto), (GCnode *)f);
+ L->nblocks += gcsizeproto(f);
+ return f;
}
-
-
-static void freefunc (TProtoFunc *f)
-{
- luaM_free(f->code);
- luaM_free(f->locvars);
- luaM_free(f->consts);
- luaM_free(f);
+static void freefunc(TProtoFunc *f) {
+ luaM_free(f->code);
+ luaM_free(f->locvars);
+ luaM_free(f->consts);
+ luaM_free(f);
}
-
-void luaF_freeproto (TProtoFunc *l)
-{
- while (l) {
- TProtoFunc *next = (TProtoFunc *)l->head.next;
- L->nblocks -= gcsizeproto(l);
- freefunc(l);
- l = next;
- }
+void luaF_freeproto(TProtoFunc *l) {
+ while (l) {
+ TProtoFunc *next = (TProtoFunc *)l->head.next;
+ L->nblocks -= gcsizeproto(l);
+ freefunc(l);
+ l = next;
+ }
}
-void luaF_freeclosure (Closure *l)
-{
- while (l) {
- Closure *next = (Closure *)l->head.next;
- L->nblocks -= gcsizeclosure(l);
- luaM_free(l);
- l = next;
- }
+void luaF_freeclosure(Closure *l) {
+ while (l) {
+ Closure *next = (Closure *)l->head.next;
+ L->nblocks -= gcsizeclosure(l);
+ luaM_free(l);
+ l = next;
+ }
}
@@ -76,22 +67,22 @@
** Look for n-th local variable at line "line" in function "func".
** Returns NULL if not found.
*/
-char *luaF_getlocalname (TProtoFunc *func, int32 local_number, int32 line)
-{
- int32 count = 0;
- char *varname = NULL;
- LocVar *lv = func->locvars;
- if (lv == NULL)
- return NULL;
- for (; lv->line != -1 && lv->line < line; lv++) {
- if (lv->varname) { /* register */
- if (++count == local_number)
- varname = lv->varname->str;
- }
- else /* unregister */
- if (--count < local_number)
- varname = NULL;
- }
- return varname;
+char *luaF_getlocalname (TProtoFunc *func, int32 local_number, int32 line) {
+ int32 count = 0;
+ char *varname = NULL;
+ LocVar *lv = func->locvars;
+ if (!lv)
+ return NULL;
+ for (; lv->line != -1 && lv->line < line; lv++) {
+ if (lv->varname) { // register
+ if (++count == local_number)
+ varname = lv->varname->str;
+ } else { // unregister
+ if (--count < local_number) {
+ varname = NULL;
+ }
+ }
+ }
+ return varname;
}
Modified: residual/trunk/engine/lua/lfunc.h
===================================================================
--- residual/trunk/engine/lua/lfunc.h 2008-07-29 06:04:16 UTC (rev 33398)
+++ residual/trunk/engine/lua/lfunc.h 2008-07-29 08:05:28 UTC (rev 33399)
@@ -8,15 +8,14 @@
#define lfunc_h
-#include "lobject.h"
+#include "engine/lua/lobject.h"
+TProtoFunc *luaF_newproto();
+Closure *luaF_newclosure(int32 nelems);
+void luaF_freeproto(TProtoFunc *l);
+void luaF_freeclosure(Closure *l);
-TProtoFunc *luaF_newproto (void);
-Closure *luaF_newclosure (int32 nelems);
-void luaF_freeproto (TProtoFunc *l);
-void luaF_freeclosure (Closure *l);
-
char *luaF_getlocalname (TProtoFunc *func, int32 local_number, int32 line);
Modified: residual/trunk/engine/lua/lgc.cpp
===================================================================
--- residual/trunk/engine/lua/lgc.cpp 2008-07-29 06:04:16 UTC (rev 33398)
+++ residual/trunk/engine/lua/lgc.cpp 2008-07-29 08:05:28 UTC (rev 33399)
@@ -5,284 +5,253 @@
*/
-#include "ldo.h"
-#include "lfunc.h"
-#include "lgc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-#include "lua.h"
+#include "engine/lua/ldo.h"
+#include "engine/lua/lfunc.h"
+#include "engine/lua/lgc.h"
+#include "engine/lua/lmem.h"
+#include "engine/lua/lobject.h"
+#include "engine/lua/lstate.h"
+#include "engine/lua/lstring.h"
+#include "engine/lua/ltable.h"
+#include "engine/lua/ltm.h"
+#include "engine/lua/lua.h"
-
-
static int32 markobject (TObject *o);
-
-
/*
** =======================================================
** REF mechanism
** =======================================================
*/
-
-int32 luaC_ref (TObject *o, int32 lock)
-{
- int32 ref;
- if (ttype(o) == LUA_T_NIL)
- ref = -1; /* special ref for nil */
- else {
- for (ref=0; ref<L->refSize; ref++)
- if (L->refArray[ref].status == FREE)
- goto found;
- /* no more empty spaces */ {
- int32 oldSize = L->refSize;
- L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref,
- refEM, MAX_INT);
- for (ref=oldSize; ref<L->refSize; ref++)
- L->refArray[ref].status = FREE;
- ref = oldSize;
- } found:
- L->refArray[ref].o = *o;
- L->refArray[ref].status = lock ? LOCK : HOLD;
- }
- return ref;
+int32 luaC_ref(TObject *o, int32 lock) {
+ int32 ref;
+ if (ttype(o) == LUA_T_NIL)
+ ref = -1; // special ref for nil
+ else {
+ for (ref = 0; ref < L->refSize; ref++) {
+ if (L->refArray[ref].status == FREE)
+ goto found;
+ // no more empty spaces */
+ {
+ int32 oldSize = L->refSize;
+ L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref, refEM, MAX_INT);
+ for (ref = oldSize; ref < L->refSize; ref++)
+ L->refArray[ref].status = FREE;
+ ref = oldSize;
+ }
+found:
+ L->refArray[ref].o = *o;
+ L->refArray[ref].status = lock ? LOCK : HOLD;
+ }
+ }
+ return ref;
}
-
-void lua_unref (int32 ref)
-{
- if (ref >= 0 && ref < L->refSize)
- L->refArray[ref].status = FREE;
+void lua_unref(int32 ref) {
+ if (ref >= 0 && ref < L->refSize)
+ L->refArray[ref].status = FREE;
}
-
-TObject* luaC_getref (int32 ref)
-{
- if (ref == -1)
- return &luaO_nilobject;
- if (ref >= 0 && ref < L->refSize &&
- (L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
- return &L->refArray[ref].o;
- else
- return NULL;
+TObject* luaC_getref(int32 ref) {
+ if (ref == -1)
+ return &luaO_nilobject;
+ if (ref >= 0 && ref < L->refSize && (L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
+ return &L->refArray[ref].o;
+ else
+ return NULL;
}
-
-static void travlock (void)
-{
- int32 i;
- for (i=0; i<L->refSize; i++)
- if (L->refArray[i].status == LOCK)
- markobject(&L->refArray[i].o);
+static void travlock() {
+ int32 i;
+ for (i =0; i < L->refSize; i++) {
+ if (L->refArray[i].status == LOCK) {
+ markobject(&L->refArray[i].o);
+ }
+ }
}
-
-static int32 ismarked (TObject *o)
-{
- /* valid only for locked objects */
- switch (o->ttype) {
- case LUA_T_STRING: case LUA_T_USERDATA:
- return o->value.ts->head.marked;
- case LUA_T_ARRAY:
- return o->value.a->head.marked;
- case LUA_T_CLOSURE:
- return o->value.cl->head.marked;
- case LUA_T_PROTO:
- return o->value.tf->head.marked;
+static int32 ismarked(TObject *o) {
+ // valid only for locked objects
+ switch (o->ttype) {
+ case LUA_T_STRING:
+ case LUA_T_USERDATA:
+ return o->value.ts->head.marked;
+ case LUA_T_ARRAY:
+ return o->value.a->head.marked;
+ case LUA_T_CLOSURE:
+ return o->value.cl->head.marked;
+ case LUA_T_PROTO:
+ return o->value.tf->head.marked;
#ifdef DEBUG
- case LUA_T_LINE: case LUA_T_CLMARK:
- case LUA_T_CMARK: case LUA_T_PMARK:
- LUA_INTERNALERROR("invalid type");
+ case LUA_T_LINE:
+ case LUA_T_CLMARK:
+ case LUA_T_CMARK:
+ case LUA_T_PMARK:
+ LUA_INTERNALERROR("invalid type");
#endif
- default: /* nil, number or cproto */
- return 1;
- }
+ default: // nil, number or cproto
+ return 1;
+ }
}
-
-static void invalidaterefs (void)
-{
- int32 i;
- for (i=0; i<L->refSize; i++)
- if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
- L->refArray[i].status = COLLECTED;
+static void invalidaterefs() {
+ int32 i;
+ for (i = 0; i < L->refSize; i++)
+ if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
+ L->refArray[i].status = COLLECTED;
}
-
-
-void luaC_hashcallIM (Hash *l)
-{
- TObject t;
- ttype(&t) = LUA_T_ARRAY;
- for (; l; l=(Hash *)l->head.next) {
- avalue(&t) = l;
- luaD_gcIM(&t);
- }
+void luaC_hashcallIM(Hash *l) {
+ TObject t;
+ ttype(&t) = LUA_T_ARRAY;
+ for (; l; l = (Hash *)l->head.next) {
+ avalue(&t) = l;
+ luaD_gcIM(&t);
+ }
}
-
-void luaC_strcallIM (TaggedString *l)
-{
- TObject o;
- ttype(&o) = LUA_T_USERDATA;
- for (; l; l=(TaggedString *)l->head.next)
- if (l->constindex == -1) { /* is userdata? */
- tsvalue(&o) = l;
- luaD_gcIM(&o);
- }
+void luaC_strcallIM(TaggedString *l) {
+ TObject o;
+ ttype(&o) = LUA_T_USERDATA;
+ for (; l; l=(TaggedString *)l->head.next) {
+ if (l->constindex == -1) { // is userdata?
+ tsvalue(&o) = l;
+ luaD_gcIM(&o);
+ }
+ }
}
-
-
-static GCnode *listcollect (GCnode *l)
-{
- GCnode *frees = NULL;
- while (l) {
- GCnode *next = l->next;
- l->marked = 0;
- while (next && !next->marked) {
- l->next = next->next;
- next->next = frees;
- frees = next;
- next = l->next;
- }
- l = next;
- }
- return frees;
+static GCnode *listcollect(GCnode *l) {
+ GCnode *frees = NULL;
+ while (l) {
+ GCnode *next = l->next;
+ l->marked = 0;
+ while (next && !next->marked) {
+ l->next = next->next;
+ next->next = frees;
+ frees = next;
+ next = l->next;
+ }
+ l = next;
+ }
+ return frees;
}
-
-static void strmark (TaggedString *s)
-{
- if (!s->head.marked)
- s->head.marked = 1;
+static void strmark(TaggedString *s) {
+ if (!s->head.marked)
+ s->head.marked = 1;
}
-
-static void protomark (TProtoFunc *f)
-{
- if (!f->head.marked) {
- LocVar *v = f->locvars;
- int32 i;
- f->head.marked = 1;
- if (f->fileName)
- strmark(f->fileName);
- for (i=0; i<f->nconsts; i++)
- markobject(&f->consts[i]);
- if (v) {
- for (; v->line != -1; v++)
- if (v->varname)
- strmark(v->varname);
- }
- }
+static void protomark(TProtoFunc *f) {
+ if (!f->head.marked) {
+ LocVar *v = f->locvars;
+ int32 i;
+ f->head.marked = 1;
+ if (f->fileName)
+ strmark(f->fileName);
+ for (i = 0; i < f->nconsts; i++)
+ markobject(&f->consts[i]);
+ if (v) {
+ for (; v->line != -1; v++) {
+ if (v->varname)
+ strmark(v->varname);
+ }
+ }
+ }
}
-
-static void closuremark (Closure *f)
-{
- if (!f->head.marked) {
- int32 i;
- f->head.marked = 1;
- for (i=f->nelems; i>=0; i--)
- markobject(&f->consts[i]);
- }
+static void closuremark(Closure *f) {
+ if (!f->head.marked) {
+ int32 i;
+ f->head.marked = 1;
+ for (i = f->nelems; i >= 0; i--)
+ markobject(&f->consts[i]);
+ }
}
-
-static void hashmark (Hash *h)
-{
- if (!h->head.marked) {
- int32 i;
- h->head.marked = 1;
- for (i=0; i<nhash(h); i++) {
- Node *n = node(h,i);
- if (ttype(ref(n)) != LUA_T_NIL) {
- markobject(&n->ref);
- markobject(&n->val);
- }
- }
- }
+static void hashmark(Hash *h) {
+ if (!h->head.marked) {
+ int32 i;
+ h->head.marked = 1;
+ for (i = 0; i < nhash(h); i++) {
+ Node *n = node(h, i);
+ if (ttype(ref(n)) != LUA_T_NIL) {
+ markobject(&n->ref);
+ markobject(&n->val);
+ }
+ }
+ }
}
-
-static void globalmark (void)
-{
- TaggedString *g;
- for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next){
- LUA_ASSERT(g->constindex >= 0, "userdata in global list");
- if (g->u.s.globalval.ttype != LUA_T_NIL) {
- markobject(&g->u.s.globalval);
- strmark(g); /* cannot collect non nil global variables */
- }
- }
+static void globalmark() {
+ TaggedString *g;
+ for (g = (TaggedString *)L->rootglobal.next; g; g = (TaggedString *)g->head.next){
+ LUA_ASSERT(g->constindex >= 0, "userdata in global list");
+ if (g->u.s.globalval.ttype != LUA_T_NIL) {
+ markobject(&g->u.s.globalval);
+ strmark(g); // cannot collect non nil global variables
+ }
+ }
}
-
-static int32 markobject (TObject *o)
-{
- switch (ttype(o)) {
- case LUA_T_USERDATA: case LUA_T_STRING:
- strmark(tsvalue(o));
- break;
- case LUA_T_ARRAY:
- hashmark(avalue(o));
- break;
- case LUA_T_CLOSURE: case LUA_T_CLMARK:
- closuremark(o->value.cl);
- break;
- case LUA_T_PROTO: case LUA_T_PMARK:
- protomark(o->value.tf);
- break;
- default: break; /* numbers, cprotos, etc */
- }
- return 0;
+static int32 markobject(TObject *o) {
+ switch (ttype(o)) {
+ case LUA_T_USERDATA:
+ case LUA_T_STRING:
+ strmark(tsvalue(o));
+ break;
+ case LUA_T_ARRAY:
+ hashmark(avalue(o));
+ break;
+ case LUA_T_CLOSURE:
+ case LUA_T_CLMARK:
+ closuremark(o->value.cl);
+ break;
+ case LUA_T_PROTO:
+ case LUA_T_PMARK:
+ protomark(o->value.tf);
+ break;
+ default:
+ break; // numbers, cprotos, etc
+ }
+ return 0;
}
-
-
-static void markall (void)
-{
- luaD_travstack(markobject); /* mark stack objects */
- globalmark(); /* mark global variable values and names */
- travlock(); /* mark locked objects */
- luaT_travtagmethods(markobject); /* mark fallbacks */
+static void markall() {
+ luaD_travstack(markobject); // mark stack objects
+ globalmark(); // mark global variable values and names
+ travlock(); // mark locked objects
+ luaT_travtagmethods(markobject); // mark fallbacks
}
-
-int32 lua_collectgarbage (int32 limit)
-{
- int32 recovered = L->nblocks; /* to subtract nblocks after gc */
- Hash *freetable;
- TaggedString *freestr;
- TProtoFunc *freefunc;
- Closure *freeclos;
- markall();
- invalidaterefs();
- freestr = luaS_collector();
- freetable = (Hash *)listcollect(&(L->roottable));
- freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
- freeclos = (Closure *)listcollect(&(L->rootcl));
- L->GCthreshold *= 4; /* to avoid GC during GC */
- luaC_hashcallIM(freetable); /* GC tag methods for tables */
- luaC_strcallIM(freestr); /* GC tag methods for userdata */
- luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
- luaH_free(freetable);
- luaS_free(freestr);
- luaF_freeproto(freefunc);
- luaF_freeclosure(freeclos);
- recovered = recovered-L->nblocks;
- L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
- return recovered;
+int32 lua_collectgarbage(int32 limit) {
+ int32 recovered = L->nblocks; // to subtract nblocks after gc
+ Hash *freetable;
+ TaggedString *freestr;
+ TProtoFunc *freefunc;
+ Closure *freeclos;
+ markall();
+ invalidaterefs();
+ freestr = luaS_collector();
+ freetable = (Hash *)listcollect(&(L->roottable));
+ freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
+ freeclos = (Closure *)listcollect(&(L->rootcl));
+ L->GCthreshold *= 4; // to avoid GC during GC
+ luaC_hashcallIM(freetable); // GC tag methods for tables
+ luaC_strcallIM(freestr); // GC tag methods for userdata
+ luaD_gcIM(&luaO_nilobject); // GC tag method for nil (signal end of GC)
+ luaH_free(freetable);
+ luaS_free(freestr);
+ luaF_freeproto(freefunc);
+ luaF_freeclosure(freeclos);
+ recovered = recovered-L->nblocks;
+ L->GCthreshold = (limit == 0) ? 2 * L->nblocks : L->nblocks + limit;
+ return recovered;
}
-
-void luaC_checkGC (void)
-{
- if (L->nblocks >= L->GCthreshold)
- lua_collectgarbage(0);
+void luaC_checkGC() {
+ if (L->nblocks >= L->GCthreshold)
+ lua_collectgarbage(0);
}
Modified: residual/trunk/engine/lua/lgc.h
===================================================================
--- residual/trunk/engine/lua/lgc.h 2008-07-29 06:04:16 UTC (rev 33398)
+++ residual/trunk/engine/lua/lgc.h 2008-07-29 08:05:28 UTC (rev 33399)
@@ -11,11 +11,11 @@
#include "lobject.h"
-void luaC_checkGC (void);
-TObject* luaC_getref (int32 ref);
-int32 luaC_ref (TObject *o, int32 lock);
-void luaC_hashcallIM (Hash *l);
-void luaC_strcallIM (TaggedString *l);
+void luaC_checkGC();
+TObject* luaC_getref(int32 ref);
+int32 luaC_ref(TObject *o, int32 lock);
+void luaC_hashcallIM(Hash *l);
+void luaC_strcallIM(TaggedString *l);
#endif
Modified: residual/trunk/engine/lua/liolib.cpp
===================================================================
--- residual/trunk/engine/lua/liolib.cpp 2008-07-29 06:04:16 UTC (rev 33398)
+++ residual/trunk/engine/lua/liolib.cpp 2008-07-29 08:05:28 UTC (rev 33399)
@@ -5,10 +5,10 @@
*/
-#include "lauxlib.h"
-#include "lua.h"
-#include "luadebug.h"
-#include "lualib.h"
+#include "engine/lua/lauxlib.h"
+#include "engine/lua/lua.h"
+#include "engine/lua/luadebug.h"
+#include "engine/lua/lualib.h"
#include "engine/resource.h"
#include "engine/backend/driver.h"
@@ -18,342 +18,305 @@
#define CLOSEDTAG 2
#define IOTAG 1
-#define FIRSTARG 3 /* 1st and 2nd are upvalues */
+#define FIRSTARG 3 // 1st and 2nd are upvalues
#define FINPUT "_INPUT"
#define FOUTPUT "_OUTPUT"
+static int32 gettag(int32 i) {
+ return (int32)lua_getnumber(lua_getparam(i));
+}
-#define popen(x,y) NULL /* that is, popen always fails */
-#define pclose(x) (-1)
-
-static int32 gettag (int32 i)
-{
- return (int32)lua_getnumber(lua_getparam(i));
+static void pushresult(int32 i) {
+ if (i)
+ lua_pushuserdata(NULL);
+ else {
+ lua_pushnil();
+ lua_pushstring(strerror(errno));
+ }
}
-static void pushresult (int32 i)
-{
- if (i)
- lua_pushuserdata(NULL);
- else {
- lua_pushnil();
- lua_pushstring(strerror(errno));
- }
+static int32 ishandler(lua_Object f) {
+ if (lua_isuserdata(f)) {
+ if (lua_tag(f) == gettag(CLOSEDTAG))
+ lua_error("cannot access a closed file");
+ return lua_tag(f) == gettag(IOTAG);
+ }
+ else return 0;
}
-
-static int32 ishandler (lua_Object f)
-{
- if (lua_isuserdata(f)) {
- if (lua_tag(f) == gettag(CLOSEDTAG))
- lua_error("cannot access a closed file");
- return lua_tag(f) == gettag(IOTAG);
- }
- else return 0;
+static FILE *getfile(const char *name) {
+ lua_Object f = lua_getglobal(name);
+ if (!ishandler(f))
+ luaL_verror("global variable `%.50s' is not a file handle", name);
+ return (FILE *)lua_getuserdata(f);
}
-static FILE *getfile (const char *name)
-{
- lua_Object f = lua_getglobal(name);
- if (!ishandler(f))
- luaL_verror("global variable `%.50s' is not a file handle", name);
- return (FILE *)lua_getuserdata(f);
+static FILE *getfileparam(const char *name, int32 *arg) {
+ lua_Object f = lua_getparam(*arg);
+ if (ishandler(f)) {
+ (*arg)++;
+ return (FILE *)lua_getuserdata(f);
+ } else
+ return getfile(name);
}
-
-static FILE *getfileparam (const char *name, int32 *arg)
-{
- lua_Object f = lua_getparam(*arg);
- if (ishandler(f)) {
- (*arg)++;
- return (FILE *)lua_getuserdata(f);
- }
- else
- return getfile(name);
+static void closefile (const char *name) {
+ FILE *f = getfile(name);
+ if (f == stdin || f == stdout)
+ return;
+ fclose(f);
+ lua_pushobject(lua_getglobal(name));
+ lua_settag(gettag(CLOSEDTAG));
}
-static void closefile (const char *name)
-{
- FILE *f = getfile(name);
- if (f == stdin || f == stdout) return;
- if (pclose(f) == -1)
- fclose(f);
- lua_pushobject(lua_getglobal(name));
- lua_settag(gettag(CLOSEDTAG));
+static void setfile(FILE *f, const char *name, int32 tag) {
+ lua_pushusertag(f, tag);
+ lua_setglobal(name);
}
-
-static void setfile (FILE *f, const char *name, int32 tag)
-{
- lua_pushusertag(f, tag);
- lua_setglobal(name);
+static void setreturn(FILE *f, const char *name) {
+ int32 tag = gettag(IOTAG);
+ setfile(f, name, tag);
+ lua_pushusertag(f, tag);
}
-
-static void setreturn (FILE *f, const char *name)
-{
- int32 tag = gettag(IOTAG);
- setfile(f, name, tag);
- lua_pushusertag(f, tag);
+static void io_readfrom() {
+ FILE *current;
+ lua_Object f = lua_getparam(FIRSTARG);
+ if (f == LUA_NOOBJECT) {
+ closefile(FINPUT);
+ current = stdin;
+ } else if (lua_tag(f) == gettag(IOTAG)) {
+ current = (FILE *)lua_getuserdata(f);
+ } else {
+ const char *s = luaL_check_string(FIRSTARG);
+ current = fopen(s, "r");
+ // if (current == NULL)
+ // current = g_resourceloader->openNewStream(s);
+ }
+ if (!current) {
+ pushresult(0);
+ return;
+ }
+ setreturn(current, FINPUT);
}
-
-static void io_readfrom (void)
-{
- FILE *current;
- lua_Object f = lua_getparam(FIRSTARG);
- if (f == LUA_NOOBJECT) {
- closefile(FINPUT);
- current = stdin;
- }
- else if (lua_tag(f) == gettag(IOTAG))
- current = (FILE *)lua_getuserdata(f);
- else {
- const char *s = luaL_check_string(FIRSTARG);
- if (*s == '|')
- current = popen(s+1, "r");
+static void io_writeto() {
+ FILE *current;
+ lua_Object f = lua_getparam(FIRSTARG);
+ if (f == LUA_NOOBJECT) {
+ closefile(FOUTPUT);
+ current = stdout;
+ } else if (lua_tag(f) == gettag(IOTAG))
+ current = (FILE *)lua_getuserdata(f);
else {
- current = fopen(s, "r");
-// if (current == NULL)
-// current = g_resourceloader->openNewStream(s);
+ const char *s = luaL_check_string(FIRSTARG);
+ current = fopen(s, "w");
+ if (!current) {
+ pushresult(0);
+ return;
+ }
}
- if (current == NULL) {
- pushresult(0);
- return;
- }
- }
- setreturn(current, FINPUT);
+ setreturn(current, FOUTPUT);
}
-
-static void io_writeto (void)
-{
- FILE *current;
- lua_Object f = lua_getparam(FIRSTARG);
- if (f == LUA_NOOBJECT) {
- closefile(FOUTPUT);
- current = stdout;
- }
- else if (lua_tag(f) == gettag(IOTAG))
- current = (FILE *)lua_getuserdata(f);
- else {
- const char *s = luaL_check_string(FIRSTARG);
- current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
- if (current == NULL) {
- pushresult(0);
- return;
- }
- }
- setreturn(current, FOUTPUT);
+static void io_appendto() {
+ const char *s = luaL_check_string(FIRSTARG);
+ FILE *fp = fopen (s, "a");
+ if (fp)
+ setreturn(fp, FOUTPUT);
+ else
+ pushresult(0);
}
+#define NEED_OTHER (EOF - 1) // just some flag different from EOF
-static void io_appendto (void)
-{
- const char *s = luaL_check_string(FIRSTARG);
- FILE *fp = fopen (s, "a");
- if (fp != NULL)
- setreturn(fp, FOUTPUT);
- else
- pushresult(0);
+static void read_until(FILE *f, int32 lim) {
+ int32 l = 0;
+ int32 c;
+ for (c = getc(f); c != EOF && c != lim; c = getc(f)) {
+ luaL_addchar(c);
+ l++;
+ }
+ if (l > 0 || c == lim) // read anything?
+ lua_pushlstring(luaL_buffer(), l);
}
-
-#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
-
-
-static void read_until (FILE *f, int32 lim) {
- int32 l = 0;
- int32 c;
- for (c = getc(f); c != EOF && c != lim; c = getc(f)) {
- luaL_addchar(c);
- l++;
- }
- if (l > 0 || c == lim) /* read anything? */
- lua_pushlstring(luaL_buffer(), l);
-}
-
static void io_read (void) {
- int32 arg = FIRSTARG;
- FILE *f = getfileparam(FINPUT, &arg);
- const char *p = luaL_opt_string(arg, NULL);
- luaL_resetbuffer();
- if (p == NULL) /* default: read a line */
- read_until(f, '\n');
- else if (p[0] == '.' && p[1] == '*' && p[2] == 0) /* p = ".*" */
- read_until(f, EOF);
- else {
- int32 l = 0; /* number of chars read in buffer */
- int32 inskip = 0; /* to control {skips} */
- int32 c = NEED_OTHER;
- while (*p) {
- switch (*p) {
- case '{':
- inskip++;
- p++;
- continue;
- case '}':
- if (inskip == 0)
- lua_error("unbalanced braces in read pattern");
- inskip--;
- p++;
- continue;
- default: {
- const char *ep; /* get what is next */
- int32 m; /* match result */
- if (c == NEED_OTHER) c = getc(f);
- if (c == EOF) {
- luaI_singlematch(0, p, &ep); /* to set "ep" */
- m = 0;
- }
- else {
- m = luaI_singlematch(c, p, &ep);
- if (m) {
- if (inskip == 0) {
- luaL_addchar(c);
- l++;
- }
- c = NEED_OTHER;
- }
- }
- switch (*ep) {
- case '*': /* repetition */
- if (!m) p = ep+1; /* else stay in (repeat) the same item */
- continue;
- case '?': /* optional */
- p = ep+1; /* continues reading the pattern */
- continue;
- default:
- if (m) p = ep; /* continues reading the pattern */
- else
- goto break_while; /* pattern fails */
- }
- }
- }
- } break_while:
- if (c >= 0) /* not EOF nor NEED_OTHER? */
- ungetc(c, f);
- if (l > 0 || *p == 0) /* read something or did not fail? */
- lua_pushlstring(luaL_buffer(), l);
- }
+ int32 arg = FIRSTARG;
+ FILE *f = getfileparam(FINPUT, &arg);
+ const char *p = luaL_opt_string(arg, NULL);
+ luaL_resetbuffer();
+ if (p == NULL) // default: read a line
+ read_until(f, '\n');
+ else if (p[0] == '.' && p[1] == '*' && p[2] == 0) // p = ".*"
+ read_until(f, EOF);
+ else {
+ int32 l = 0; // number of chars read in buffer
+ int32 inskip = 0; // to control {skips}
+ int32 c = NEED_OTHER;
+ while (*p) {
+ switch (*p) {
+ case '{':
+ inskip++;
+ p++;
+ continue;
+ case '}':
+ if (inskip == 0)
+ lua_error("unbalanced braces in read pattern");
+ inskip--;
+ p++;
+ continue;
+ default:
+ {
+ const char *ep; // get what is next
+ int32 m; // match result
+ if (c == NEED_OTHER)
+ c = getc(f);
+ if (c == EOF) {
+ luaI_singlematch(0, p, &ep); // to set "ep"
+ m = 0;
+ } else {
+ m = luaI_singlematch(c, p, &ep);
+ if (m) {
+ if (inskip == 0) {
+ luaL_addchar(c);
+ l++;
+ }
+ c = NEED_OTHER;
+ }
+ }
+ switch (*ep) {
+ case '*': // repetition
+ if (!m)
+ p = ep + 1; // else stay in (repeat) the same item
+ continue;
+ case '?': // optional
+ p = ep + 1; // continues reading the pattern
+ continue;
+ default:
+ if (m)
+ p = ep; // continues reading the pattern
+ else
+ goto break_while; // pattern fails
+ }
+ }
+ }
+ }
+break_while:
+ if (c >= 0) // not EOF nor NEED_OTHER?
+ ungetc(c, f);
+ if (l > 0 || *p == 0) // read something or did not fail?
+ lua_pushlstring(luaL_buffer(), l);
+ }
}
-
-static void io_write (void)
-{
- int32 arg = FIRSTARG;
- FILE *f = getfileparam(FOUTPUT, &arg);
- int32 status = 1;
- const char *s;
- int32 l;
- while ((s = luaL_opt_lstr(arg++, NULL, &l)) != NULL)
- status = status && (fwrite(s, 1, l, f) == (size_t)l);
- pushresult(status);
+static void io_write() {
+ int32 arg = FIRSTARG;
+ FILE *f = getfileparam(FOUTPUT, &arg);
+ int32 status = 1;
+ const char *s;
+ int32 l;
+ while ((s = luaL_opt_lstr(arg++, NULL, &l)))
+ status = status && (fwrite(s, 1, l, f) == (size_t)l);
+ pushresult(status);
}
+static void io_date() {
+ tm t;
+ char b[BUFSIZ];
-static void io_date () {
- tm t;
- char b[BUFSIZ];
-
- g_driver->getTimeAndDate(t);
- sprintf(b, "%02d.%02d.%d %02d:%02d.%02d", t.tm_mday, t.tm_mon + 1, 1900 + t.tm_year, t.tm_hour, t.tm_min, t.tm_sec);
- lua_pushstring(b);
+ g_driver->getTimeAndDate(t);
+ sprintf(b, "%02d.%02d.%d %02d:%02d.%02d", t.tm_mday, t.tm_mon + 1, 1900 + t.tm_year, t.tm_hour, t.tm_min, t.tm_sec);
+ lua_pushstring(b);
}
-
-static void io_exit () {
- lua_Object o = lua_getparam(1);
- exit((int)lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
+static void io_exit() {
+ lua_Object o = lua_getparam(1);
+ exit((int)lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
}
+static void lua_printstack(FILE *f) {
+ int32 level = 1; // skip level 0 (it's this function)
+ lua_Object func;
+ while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
+ const char *name;
+ int32 currentline;
+ const char *filename;
+ int32 linedefined;
+ lua_funcinfo(func, &filename, &linedefined);
+ fprintf(f, (level == 2) ? "Active Stack:\n\t" : "\t");
+ switch (*lua_getobjname(func, &name)) {
+ case 'g':
+ fprintf(f, "function %s", name);
+ break;
+ case 't':
+ fprintf(f, "`%s' tag method", name);
+ break;
+ default:
+ {
+ if (linedefined == 0)
+ fprintf(f, "main of %s", filename);
+ else if (linedefined < 0)
+ fprintf(f, "%s", filename);
+ else
+ fprintf(f, "function (%s:%d)", filename, (int)linedefined);
+ filename = NULL;
+ }
+ }
-static void lua_printstack (FILE *f)
-{
- int32 level = 1; /* skip level 0 (it's this function) */
- lua_Object func;
- while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
- const char *name;
- int32 currentline;
- const char *filename;
- int32 linedefined;
- lua_funcinfo(func, &filename, &linedefined);
- fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
- switch (*lua_getobjname(func, &name)) {
- case 'g':
- fprintf(f, "function %s", name);
- break;
- case 't':
- fprintf(f, "`%s' tag method", name);
- break;
- default: {
- if (linedefined == 0)
- fprintf(f, "main of %s", filename);
- else if (linedefined < 0)
- fprintf(f, "%s", filename);
- else
- fprintf(f, "function (%s:%d)", filename, (int)linedefined);
- filename = NULL;
- }
- }
- if ((currentline = lua_currentline(func)) > 0)
- fprintf(f, " at line %d", (int)currentline);
- if (filename)
- fprintf(f, " [in file %s]", filename);
- fprintf(f, "\n");
- }
+ if ((currentline = lua_currentline(func)) > 0)
+ fprintf(f, " at line %d", (int)currentline);
+ if (filename)
+ fprintf(f, " [in file %s]", filename);
+ fprintf(f, "\n");
+ }
}
-
-static void errorfb (void)
-{
- fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
- lua_printstack(stderr);
+static void errorfb() {
+ fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
+ lua_printstack(stderr);
}
-
-
static struct luaL_reg iolib[] = {
-{"date", io_date},
-{"exit", io_exit},
-{"print_stack", errorfb}
+ { "date", io_date },
+ { "exit", io_exit },
+ { "print_stack", errorfb }
};
static struct luaL_reg iolibtag[] = {
-{"readfrom", io_readfrom},
-{"writeto", io_writeto},
-{"appendto", io_appendto},
-{"read", io_read},
-{"write", io_write}
+ { "readfrom", io_readfrom },
+ { "writeto", io_writeto },
+ { "appendto", io_appendto },
+ { "read", io_read },
+ { "write", io_write }
};
-static void openwithtags (void)
-{
- int32 iotag = lua_newtag();
- int32 closedtag = lua_newtag();
- uint32 i;
- for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
- /* put both tags as upvalues for these functions */
- lua_pushnumber(iotag);
- lua_pushnumber(closedtag);
- lua_pushcclosure(iolibtag[i].func, 2);
- lua_setglobal(iolibtag[i].name);
- }
- setfile(stdin, FINPUT, iotag);
- setfile(stdout, FOUTPUT, iotag);
- setfile(stdin, "_STDIN", iotag);
- setfile(stdout, "_STDOUT", iotag);
- setfile(stderr, "_STDERR", iotag);
+static void openwithtags() {
+ int32 iotag = lua_newtag();
+ int32 closedtag = lua_newtag();
+ uint32 i;
+ for (i = 0; i < sizeof(iolibtag) / sizeof(iolibtag[0]); i++) {
+ // put both tags as upvalues for these functions
+ lua_pushnumber(iotag);
+ lua_pushnumber(closedtag);
+ lua_pushcclosure(iolibtag[i].func, 2);
+ lua_setglobal(iolibtag[i].name);
+ }
+ setfile(stdin, FINPUT, iotag);
+ setfile(stdout, FOUTPUT, iotag);
+ setfile(stdin, "_STDIN", iotag);
+ setfile(stdout, "_STDOUT", iotag);
+ setfile(stderr, "_STDERR", iotag);
}
-void lua_iolibopen (void)
-{
- luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
- luaL_addlibtolist(iolibtag, (sizeof(iolib)/sizeof(iolib[0])));
- openwithtags();
- lua_pushcfunction(errorfb);
- lua_seterrormethod();
+void lua_iolibopen() {
+ luaL_openlib(iolib, (sizeof(iolib) / sizeof(iolib[0])));
+ luaL_addlibtolist(iolibtag, (sizeof(iolib) / sizeof(iolib[0])));
+ openwithtags();
+ lua_pushcfunction(errorfb);
+ lua_seterrormethod();
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Scummvm-git-logs
mailing list