1/* see copyright notice in squirrel.h */
2#include <squirrel.h>
3#include <time.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <sqstdsystem.h>
7
8#ifdef SQUNICODE
9#include <wchar.h>
10#define scgetenv _wgetenv
11#define scsystem _wsystem
12#define scasctime _wasctime
13#define scremove _wremove
14#define screname _wrename
15#else
16#define scgetenv getenv
17#define scsystem system
18#define scasctime asctime
19#define scremove remove
20#define screname rename
21#endif
22
23static SQInteger _system_getenv(HSQUIRRELVM v)
24{
25 const SQChar *s;
26 if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
27 sq_pushstring(v,scgetenv(s),-1);
28 return 1;
29 }
30 return 0;
31}
32
33
34static SQInteger _system_system(HSQUIRRELVM v)
35{
36 const SQChar *s;
37 if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
38 sq_pushinteger(v,scsystem(s));
39 return 1;
40 }
41 return sq_throwerror(v,_SC("wrong param"));
42}
43
44
45static SQInteger _system_clock(HSQUIRRELVM v)
46{
47 sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
48 return 1;
49}
50
51static SQInteger _system_time(HSQUIRRELVM v)
52{
53 SQInteger t = (SQInteger)time(NULL);
54 sq_pushinteger(v,t);
55 return 1;
56}
57
58static SQInteger _system_remove(HSQUIRRELVM v)
59{
60 const SQChar *s;
61 sq_getstring(v,2,&s);
62 if(scremove(s)==-1)
63 return sq_throwerror(v,_SC("remove() failed"));
64 return 0;
65}
66
67static SQInteger _system_rename(HSQUIRRELVM v)
68{
69 const SQChar *oldn,*newn;
70 sq_getstring(v,2,&oldn);
71 sq_getstring(v,3,&newn);
72 if(screname(oldn,newn)==-1)
73 return sq_throwerror(v,_SC("rename() failed"));
74 return 0;
75}
76
77static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
78{
79 sq_pushstring(v,name,-1);
80 sq_pushinteger(v,val);
81 sq_rawset(v,-3);
82}
83
84static SQInteger _system_date(HSQUIRRELVM v)
85{
86 time_t t;
87 SQInteger it;
88 SQInteger format = 'l';
89 if(sq_gettop(v) > 1) {
90 sq_getinteger(v,2,&it);
91 t = it;
92 if(sq_gettop(v) > 2) {
93 sq_getinteger(v,3,(SQInteger*)&format);
94 }
95 }
96 else {
97 time(&t);
98 }
99 tm *date;
100 if(format == 'u')
101 date = gmtime(&t);
102 else
103 date = localtime(&t);
104 if(!date)
105 return sq_throwerror(v,_SC("crt api failure"));
106 sq_newtable(v);
107 _set_integer_slot(v, _SC("sec"), date->tm_sec);
108 _set_integer_slot(v, _SC("min"), date->tm_min);
109 _set_integer_slot(v, _SC("hour"), date->tm_hour);
110 _set_integer_slot(v, _SC("day"), date->tm_mday);
111 _set_integer_slot(v, _SC("month"), date->tm_mon);
112 _set_integer_slot(v, _SC("year"), date->tm_year+1900);
113 _set_integer_slot(v, _SC("wday"), date->tm_wday);
114 _set_integer_slot(v, _SC("yday"), date->tm_yday);
115 return 1;
116}
117
118
119
120#define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
121static const SQRegFunction systemlib_funcs[]={
122 _DECL_FUNC(getenv,2,_SC(".s")),
123 _DECL_FUNC(system,2,_SC(".s")),
124 _DECL_FUNC(clock,0,NULL),
125 _DECL_FUNC(time,1,NULL),
126 _DECL_FUNC(date,-1,_SC(".nn")),
127 _DECL_FUNC(remove,2,_SC(".s")),
128 _DECL_FUNC(rename,3,_SC(".ss")),
129 {NULL,(SQFUNCTION)0,0,NULL}
130};
131#undef _DECL_FUNC
132
133SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
134{
135 SQInteger i=0;
136 while(systemlib_funcs[i].name!=0)
137 {
138 sq_pushstring(v,systemlib_funcs[i].name,-1);
139 sq_newclosure(v,systemlib_funcs[i].f,0);
140 sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask);
141 sq_setnativeclosurename(v,-1,systemlib_funcs[i].name);
142 sq_newslot(v,-3,SQFalse);
143 i++;
144 }
145 return 1;
146}
147