Sys.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. @:coreApi class Sys {
  23. public static function print( v : Dynamic ) : Void {
  24. untyped __dollar__print(v);
  25. }
  26. public static function println( v : Dynamic ) : Void {
  27. untyped __dollar__print(v,"\n");
  28. }
  29. public static function getChar( echo : Bool ) : Int {
  30. return getch(echo);
  31. }
  32. public static function stdin() : haxe.io.Input {
  33. return untyped new sys.io.FileInput(file_stdin());
  34. }
  35. public static function stdout() : haxe.io.Output {
  36. return untyped new sys.io.FileOutput(file_stdout());
  37. }
  38. public static function stderr() : haxe.io.Output {
  39. return untyped new sys.io.FileOutput(file_stderr());
  40. }
  41. public static function args() : Array<String> untyped {
  42. var a = __dollar__loader.args;
  43. if( __dollar__typeof(a) != __dollar__tarray )
  44. return [];
  45. var r = new Array();
  46. var i = 0;
  47. var l = __dollar__asize(a);
  48. while( i < l ) {
  49. if( __dollar__typeof(a[i]) == __dollar__tstring )
  50. r.push(new String(a[i]));
  51. i += 1;
  52. }
  53. return r;
  54. }
  55. public static function getEnv( s : String ) : String {
  56. var v = get_env(untyped s.__s);
  57. if( v == null )
  58. return null;
  59. return new String(v);
  60. }
  61. public static function putEnv( s : String, v : String ) : Void {
  62. untyped put_env(s.__s,if( v == null ) null else v.__s);
  63. }
  64. public static function sleep( seconds : Float ) : Void {
  65. _sleep(seconds);
  66. }
  67. public static function setTimeLocale( loc : String ) : Bool {
  68. return set_time_locale(untyped loc.__s);
  69. }
  70. public static function getCwd() : String {
  71. return new String(get_cwd());
  72. }
  73. public static function setCwd( s : String ) : Void {
  74. set_cwd(untyped s.__s);
  75. }
  76. public static function systemName() : String {
  77. return new String(sys_string());
  78. }
  79. static function escapeArgument( arg : String, windows : Bool ) : String {
  80. var ok = true;
  81. for( i in 0...arg.length )
  82. switch( arg.charCodeAt(i) ) {
  83. case ' '.code, '\t'.code, '"'.code, '&'.code, '|'.code, '<'.code, '>'.code, '#'.code , ';'.code, '*'.code, '?'.code, '('.code, ')'.code, '{'.code, '}'.code, '$'.code:
  84. ok = false;
  85. case 0, 13, 10: // [eof] [cr] [lf]
  86. arg = arg.substr(0,i);
  87. break;
  88. }
  89. if( ok )
  90. return arg;
  91. return windows ? '"'+arg.split('"').join('""').split("%").join('"%"')+'"' : "'"+arg.split("'").join("'\\''")+"'";
  92. }
  93. public static function command( cmd : String, ?args : Array<String> ) : Int {
  94. var win = systemName() == "Windows";
  95. cmd = escapeArgument(cmd, win);
  96. if( args != null ) {
  97. for( a in args )
  98. cmd += " "+escapeArgument(a, win);
  99. }
  100. if (win) cmd = '"$cmd"';
  101. return sys_command(untyped cmd.__s);
  102. }
  103. public static function exit( code : Int ) : Void {
  104. sys_exit(code);
  105. }
  106. public static function time() : Float {
  107. return sys_time();
  108. }
  109. public static function cpuTime() : Float {
  110. return sys_cpu_time();
  111. }
  112. public static function executablePath() : String {
  113. return new String(sys_exe_path());
  114. }
  115. public static function environment() : Map<String,String> {
  116. var l : Array<Dynamic> = sys_env();
  117. var h = new haxe.ds.StringMap();
  118. while( l != null ) {
  119. h.set(new String(l[0]),new String(l[1]));
  120. l = l[2];
  121. }
  122. return h;
  123. }
  124. private static var get_env = neko.Lib.load("std","get_env",1);
  125. private static var put_env = neko.Lib.load("std","put_env",2);
  126. private static var _sleep = neko.Lib.load("std","sys_sleep",1);
  127. private static var set_time_locale = neko.Lib.load("std","set_time_locale",1);
  128. private static var get_cwd = neko.Lib.load("std","get_cwd",0);
  129. private static var set_cwd = neko.Lib.load("std","set_cwd",1);
  130. private static var sys_string = neko.Lib.load("std","sys_string",0);
  131. private static var sys_command = neko.Lib.load("std","sys_command",1);
  132. private static var sys_exit = neko.Lib.load("std","sys_exit",1);
  133. private static var sys_time = neko.Lib.load("std","sys_time",0);
  134. private static var sys_cpu_time = neko.Lib.load("std","sys_cpu_time",0);
  135. private static var sys_exe_path = neko.Lib.load("std","sys_exe_path",0);
  136. private static var sys_env = neko.Lib.load("std","sys_env",0);
  137. private static var file_stdin = neko.Lib.load("std","file_stdin",0);
  138. private static var file_stdout = neko.Lib.load("std","file_stdout",0);
  139. private static var file_stderr = neko.Lib.load("std","file_stderr",0);
  140. private static var getch = neko.Lib.load("std","sys_getch",1);
  141. }