sqstdsystem.cpp 3.6 KB

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