# sqstdsystem.cpp

    00001 /* see copyright notice in squirrel.h */
    00002 #include <squirrel.h>
    00003 #include <time.h>
    00004 #include <stdlib.h>
    00005 #include <stdio.h>
    00006 #include <sqstdsystem.h>
    00007
    00008 #ifdef SQUNICODE
    00009 #include <wchar.h>
    00010 #define scgetenv _wgetenv
    00011 #define scsystem _wsystem
    00012 #define scasctime _wasctime
    00013 #define scremove _wremove
    00014 #define screname _wrename
    00015 #else
    00016 #define scgetenv getenv
    00017 #define scsystem system
    00018 #define scasctime asctime
    00019 #define scremove remove
    00020 #define screname rename
    00021 #endif
    00022
    00023 static SQInteger _system_getenv(HSQUIRRELVM v)
    00024 {
    00025   const SQChar *s;
    00026   if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
    00027         sq_pushstring(v,scgetenv(s),-1);
    00028     return 1;
    00029   }
    00030   return 0;
    00031 }
    00032
    00033
    00034 static SQInteger _system_system(HSQUIRRELVM v)
    00035 {
    00036   const SQChar *s;
    00037   if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
    00038     sq_pushinteger(v,scsystem(s));
    00039     return 1;
    00040   }
    00041   return sq_throwerror(v,_SC("wrong param"));
    00042 }
    00043
    00044
    00045 static SQInteger _system_clock(HSQUIRRELVM v)
    00046 {
    00047   sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
    00048   return 1;
    00049 }
    00050
    00051 static SQInteger _system_time(HSQUIRRELVM v)
    00052 {
    00053   time_t t;
    00054   time(&t);
    00055   sq_pushinteger(v,*((SQInteger *)&t));
    00056   return 1;
    00057 }
    00058
    00059 static SQInteger _system_remove(HSQUIRRELVM v)
    00060 {
    00061   const SQChar *s;
    00062   sq_getstring(v,2,&s);
    00063   if(scremove(s)==-1)
    00064     return sq_throwerror(v,_SC("remove() failed"));
    00065   return 0;
    00066 }
    00067
    00068 static SQInteger _system_rename(HSQUIRRELVM v)
    00069 {
    00070   const SQChar *oldn,*newn;
    00071   sq_getstring(v,2,&oldn);
    00072   sq_getstring(v,3,&newn);
    00073   if(screname(oldn,newn)==-1)
    00074     return sq_throwerror(v,_SC("rename() failed"));
    00075   return 0;
    00076 }
    00077
    00078 static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
    00079 {
    00080   sq_pushstring(v,name,-1);
    00081   sq_pushinteger(v,val);
    00082   sq_rawset(v,-3);
    00083 }
    00084
    00085 static SQInteger _system_date(HSQUIRRELVM v)
    00086 {
    00087   time_t t;
    00088   SQInteger it;
    00089   SQInteger format = 'l';
    00090   if(sq_gettop(v) > 1) {
    00091     sq_getinteger(v,2,&it);
    00092     t = it;
    00093     if(sq_gettop(v) > 2) {
    00094       sq_getinteger(v,3,(SQInteger*)&format);
    00095     }
    00096   }
    00097   else {
    00098     time(&t);
    00099   }
    00100   tm *date;
    00101     if(format == 'u')
    00102     date = gmtime(&t);
    00103   else
    00104     date = localtime(&t);
    00105   if(!date)
    00106     return sq_throwerror(v,_SC("crt api failure"));
    00107   sq_newtable(v);
    00108   _set_integer_slot(v, _SC("sec"), date->tm_sec);
    00109     _set_integer_slot(v, _SC("min"), date->tm_min);
    00110     _set_integer_slot(v, _SC("hour"), date->tm_hour);
    00111     _set_integer_slot(v, _SC("day"), date->tm_mday);
    00112     _set_integer_slot(v, _SC("month"), date->tm_mon);
    00113     _set_integer_slot(v, _SC("year"), date->tm_year+1900);
    00114     _set_integer_slot(v, _SC("wday"), date->tm_wday);
    00115     _set_integer_slot(v, _SC("yday"), date->tm_yday);
    00116   return 1;
    00117 }
    00118
    00119
    00120
    00121 #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
    00122 static SQRegFunction systemlib_funcs[]={
    00123   _DECL_FUNC(getenv,2,_SC(".s")),
    00124   _DECL_FUNC(system,2,_SC(".s")),
    00125   _DECL_FUNC(clock,1,NULL),
    00126   _DECL_FUNC(time,1,NULL),
    00127   _DECL_FUNC(date,-1,_SC(".nn")),
    00128   _DECL_FUNC(remove,2,_SC(".s")),
    00129   _DECL_FUNC(rename,3,_SC(".ss")),
    00130   {0,0,0,0}
    00131 };
    00132
    00133
    00134 SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
    00135 {
    00136   SQInteger i=0;
    00137   while(systemlib_funcs[i].name!=0)
    00138   {
    00139     sq_pushstring(v,systemlib_funcs[i].name,-1);
    00140     sq_newclosure(v,systemlib_funcs[i].f,0);
    00141     sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask);
    00142     sq_setnativeclosurename(v,-1,systemlib_funcs[i].name);
    00143     sq_createslot(v,-3);
    00144     i++;
    00145   }
    00146   return 1;
    00147 }