Sys.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package cpp;
  2. class Sys {
  3. public static function args() : Array<String> untyped {
  4. return __global__.__get_args();
  5. }
  6. public static function getEnv( s : String ):String {
  7. var v = get_env(s);
  8. if( v == null )
  9. return null;
  10. return v;
  11. }
  12. public static function putEnv( s : String, v : String ) {
  13. put_env(s,v);
  14. }
  15. public static function sleep( seconds : Float ) {
  16. _sleep(seconds);
  17. }
  18. public static function setTimeLocale( loc : String ) : Bool {
  19. return set_time_locale(loc);
  20. }
  21. public static function getCwd() : String {
  22. return new String(get_cwd());
  23. }
  24. public static function setCwd( s : String ) {
  25. set_cwd(s);
  26. }
  27. public static function systemName() : String {
  28. return sys_string();
  29. }
  30. public static function escapeArgument( arg : String ) : String {
  31. var ok = true;
  32. for( i in 0...arg.length )
  33. switch( arg.charCodeAt(i) ) {
  34. case 32, 34: // [space] "
  35. ok = false;
  36. case 0, 13, 10: // [eof] [cr] [lf]
  37. arg = arg.substr(0,i);
  38. }
  39. if( ok )
  40. return arg;
  41. return '"'+arg.split('"').join('\\"')+'"';
  42. }
  43. public static function command( cmd : String, ?args : Array<String> ) : Int {
  44. if( args != null ) {
  45. cmd = escapeArgument(cmd);
  46. for( a in args )
  47. cmd += " "+escapeArgument(a);
  48. }
  49. return sys_command(cmd);
  50. }
  51. public static function exit( code : Int ) {
  52. sys_exit(code);
  53. }
  54. public static function time() : Float {
  55. return sys_time();
  56. }
  57. public static function cpuTime() : Float {
  58. return sys_cpu_time();
  59. }
  60. public static function executablePath() : String {
  61. return new String(sys_exe_path());
  62. }
  63. public static function environment() : Hash<String> {
  64. var vars:Array<String> = sys_env();
  65. var result = new Hash<String>();
  66. var i = 0;
  67. while(i<vars.length) {
  68. result.set( vars[i], vars[i+1] );
  69. i+=2;
  70. }
  71. return result;
  72. }
  73. private static var get_env = Lib.load("std","get_env",1);
  74. private static var put_env = Lib.load("std","put_env",2);
  75. private static var _sleep = Lib.load("std","sys_sleep",1);
  76. private static var set_time_locale = Lib.load("std","set_time_locale",1);
  77. private static var get_cwd = Lib.load("std","get_cwd",0);
  78. private static var set_cwd = Lib.load("std","set_cwd",1);
  79. private static var sys_string = Lib.load("std","sys_string",0);
  80. private static var sys_command = Lib.load("std","sys_command",1);
  81. private static var sys_exit = Lib.load("std","sys_exit",1);
  82. private static var sys_time = Lib.load("std","sys_time",0);
  83. private static var sys_cpu_time = Lib.load("std","sys_cpu_time",0);
  84. private static var sys_exe_path = Lib.load("std","sys_exe_path",0);
  85. private static var sys_env = Lib.load("std","sys_env",0);
  86. }