Code::Blocks  SVN r11506
sqstdsystem.cpp
Go to the documentation of this file.
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 
23 static 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 
34 static SQInteger _system_system(HSQUIRRELVM v)
35 {
36  const SQChar *s;
37  if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
39  return 1;
40  }
41  return sq_throwerror(v,_SC("wrong param"));
42 }
43 
44 
45 static SQInteger _system_clock(HSQUIRRELVM v)
46 {
47  sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
48  return 1;
49 }
50 
51 static SQInteger _system_time(HSQUIRRELVM v)
52 {
53  time_t t;
54  time(&t);
55  sq_pushinteger(v,*((SQInteger *)&t));
56  return 1;
57 }
58 
59 static SQInteger _system_remove(HSQUIRRELVM v)
60 {
61  const SQChar *s;
62  sq_getstring(v,2,&s);
63  if(scremove(s)==-1)
64  return sq_throwerror(v,_SC("remove() failed"));
65  return 0;
66 }
67 
68 static SQInteger _system_rename(HSQUIRRELVM v)
69 {
70  const SQChar *oldn,*newn;
71  sq_getstring(v,2,&oldn);
72  sq_getstring(v,3,&newn);
73  if(screname(oldn,newn)==-1)
74  return sq_throwerror(v,_SC("rename() failed"));
75  return 0;
76 }
77 
78 static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
79 {
80  sq_pushstring(v,name,-1);
81  sq_pushinteger(v,val);
82  sq_rawset(v,-3);
83 }
84 
85 static SQInteger _system_date(HSQUIRRELVM v)
86 {
87  time_t t;
88  SQInteger it;
89  SQInteger format = 'l';
90  if(sq_gettop(v) > 1) {
91  sq_getinteger(v,2,&it);
92  t = it;
93  if(sq_gettop(v) > 2) {
94  sq_getinteger(v,3,(SQInteger*)&format);
95  }
96  }
97  else {
98  time(&t);
99  }
100  tm *date;
101  if(format == 'u')
102  date = gmtime(&t);
103  else
104  date = localtime(&t);
105  if(!date)
106  return sq_throwerror(v,_SC("crt api failure"));
107  sq_newtable(v);
108  _set_integer_slot(v, _SC("sec"), date->tm_sec);
109  _set_integer_slot(v, _SC("min"), date->tm_min);
110  _set_integer_slot(v, _SC("hour"), date->tm_hour);
111  _set_integer_slot(v, _SC("day"), date->tm_mday);
112  _set_integer_slot(v, _SC("month"), date->tm_mon);
113  _set_integer_slot(v, _SC("year"), date->tm_year+1900);
114  _set_integer_slot(v, _SC("wday"), date->tm_wday);
115  _set_integer_slot(v, _SC("yday"), date->tm_yday);
116  return 1;
117 }
118 
119 
120 
121 #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
122 static SQRegFunction systemlib_funcs[]={
123  _DECL_FUNC(getenv,2,_SC(".s")),
124  _DECL_FUNC(system,2,_SC(".s")),
125  _DECL_FUNC(clock,1,NULL),
126  _DECL_FUNC(time,1,NULL),
127  _DECL_FUNC(date,-1,_SC(".nn")),
128  _DECL_FUNC(remove,2,_SC(".s")),
129  _DECL_FUNC(rename,3,_SC(".ss")),
130  {0,0}
131 };
132 
133 
134 SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
135 {
136  SQInteger i=0;
137  while(systemlib_funcs[i].name!=0)
138  {
139  sq_pushstring(v,systemlib_funcs[i].name,-1);
140  sq_newclosure(v,systemlib_funcs[i].f,0);
141  sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask);
143  sq_createslot(v,-3);
144  i++;
145  }
146  return 1;
147 }
static SQInteger _system_rename(HSQUIRRELVM v)
Definition: sqstdsystem.cpp:68
static SQInteger _system_clock(HSQUIRRELVM v)
Definition: sqstdsystem.cpp:45
SQRESULT sq_throwerror(HSQUIRRELVM v, const SQChar *err)
Definition: sqapi.cpp:918
void sq_newtable(HSQUIRRELVM v)
Definition: sqapi.cpp:229
SQRESULT sq_rawset(HSQUIRRELVM v, SQInteger idx)
Definition: sqapi.cpp:742
#define scsystem
Definition: sqstdsystem.cpp:17
void sq_pushfloat(HSQUIRRELVM v, SQFloat n)
Definition: sqapi.cpp:212
SQRESULT sq_getinteger(HSQUIRRELVM v, SQInteger idx, SQInteger *i)
Definition: sqapi.cpp:509
SQInteger sq_gettop(HSQUIRRELVM v)
Definition: sqapi.cpp:664
void sq_pushinteger(HSQUIRRELVM v, SQInteger n)
Definition: sqapi.cpp:202
void sq_newclosure(HSQUIRRELVM v, SQFUNCTION func, SQUnsignedInteger nfreevars)
Definition: sqapi.cpp:338
SQRESULT sq_setnativeclosurename(HSQUIRRELVM v, SQInteger idx, const SQChar *name)
Definition: sqapi.cpp:362
static SQInteger _system_getenv(HSQUIRRELVM v)
Definition: sqstdsystem.cpp:23
static SQInteger _system_time(HSQUIRRELVM v)
Definition: sqstdsystem.cpp:51
#define _DECL_FUNC(name, nparams, pmask)
#define scgetenv
Definition: sqstdsystem.cpp:16
SQRESULT sq_setparamscheck(HSQUIRRELVM v, SQInteger nparamscheck, const SQChar *typemask)
Definition: sqapi.cpp:373
void sq_pushstring(HSQUIRRELVM v, const SQChar *s, SQInteger len)
Definition: sqapi.cpp:195
SQRESULT sq_getstring(HSQUIRRELVM v, SQInteger idx, const SQChar **c)
Definition: sqapi.cpp:539
static SQInteger _system_remove(HSQUIRRELVM v)
Definition: sqstdsystem.cpp:59
char SQChar
static SQInteger _system_date(HSQUIRRELVM v)
Definition: sqstdsystem.cpp:85
static SQRegFunction systemlib_funcs[]
static void _set_integer_slot(HSQUIRRELVM v, const SQChar *name, SQInteger val)
Definition: sqstdsystem.cpp:78
#define scremove
Definition: sqstdsystem.cpp:19
static SQInteger _system_system(HSQUIRRELVM v)
Definition: sqstdsystem.cpp:34
#define NULL
Definition: prefix.cpp:59
SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
#define screname
Definition: sqstdsystem.cpp:20