Sys.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C)2005-2015 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 __global__.__hxcpp_print(v);
  25. }
  26. public static function println( v : Dynamic ) : Void {
  27. print(v);
  28. print("\n");
  29. }
  30. public static function stdin() : haxe.io.Input {
  31. return untyped new sys.io.FileInput(file_stdin());
  32. }
  33. public static function stdout() : haxe.io.Output {
  34. return untyped new sys.io.FileOutput(file_stdout());
  35. }
  36. public static function stderr() : haxe.io.Output {
  37. return untyped new sys.io.FileOutput(file_stderr());
  38. }
  39. public static function getChar( echo : Bool ) : Int {
  40. return getch(echo);
  41. }
  42. public static function args() : Array<String> untyped {
  43. return __global__.__get_args();
  44. }
  45. public static function getEnv( s : String ):String {
  46. var v = get_env(s);
  47. if( v == null )
  48. return null;
  49. return v;
  50. }
  51. public static function putEnv( s : String, v : String ) : Void {
  52. put_env(s,v);
  53. }
  54. public static function sleep( seconds : Float ) : Void {
  55. _sleep(seconds);
  56. }
  57. public static function setTimeLocale( loc : String ) : Bool {
  58. return set_time_locale(loc);
  59. }
  60. public static function getCwd() : String {
  61. return new String(get_cwd());
  62. }
  63. public static function setCwd( s : String ) : Void {
  64. set_cwd(s);
  65. }
  66. public static function systemName() : String {
  67. return sys_string();
  68. }
  69. static function escapeArgument( arg : String ) : String {
  70. var ok = true;
  71. for( i in 0...arg.length )
  72. switch( arg.charCodeAt(i) ) {
  73. case ' '.code, '\t'.code, '"'.code, '&'.code, '|'.code, '<'.code, '>'.code, '#'.code , ';'.code, '*'.code, '?'.code, '('.code, ')'.code, '{'.code, '}'.code, '$'.code:
  74. ok = false;
  75. case 0, 13, 10: // [eof] [cr] [lf]
  76. arg = arg.substr(0,i);
  77. }
  78. if( ok )
  79. return arg;
  80. return '"'+arg.split('\\').join("\\\\").split('"').join('\\"')+'"';
  81. }
  82. public static function command( cmd : String, ?args : Array<String> ) : Int {
  83. if( args != null ) {
  84. cmd = escapeArgument(cmd);
  85. for( a in args )
  86. cmd += " "+escapeArgument(a);
  87. }
  88. if (systemName() == "Windows") cmd = '"$cmd"';
  89. return sys_command(cmd);
  90. }
  91. public static function exit( code : Int ) : Void {
  92. untyped __global__.__hxcpp_exit(code);
  93. }
  94. public static function time() : Float {
  95. return sys_time();
  96. }
  97. public static function cpuTime() : Float {
  98. return sys_cpu_time();
  99. }
  100. public static function executablePath() : String {
  101. return new String(sys_exe_path());
  102. }
  103. public static function environment() : Map<String,String> {
  104. var vars:Array<String> = sys_env();
  105. var result = new haxe.ds.StringMap<String>();
  106. var i = 0;
  107. while(i<vars.length) {
  108. result.set( vars[i], vars[i+1] );
  109. i+=2;
  110. }
  111. return result;
  112. }
  113. private static var get_env = cpp.Lib.load("std","get_env",1);
  114. private static var put_env = cpp.Lib.load("std","put_env",2);
  115. private static var _sleep = cpp.Lib.load("std","sys_sleep",1);
  116. private static var set_time_locale = cpp.Lib.load("std","set_time_locale",1);
  117. private static var get_cwd = cpp.Lib.load("std","get_cwd",0);
  118. private static var set_cwd = cpp.Lib.load("std","set_cwd",1);
  119. private static var sys_string = cpp.Lib.load("std","sys_string",0);
  120. private static var sys_command = cpp.Lib.load("std","sys_command",1);
  121. private static var sys_time = cpp.Lib.load("std","sys_time",0);
  122. private static var sys_cpu_time = cpp.Lib.load("std","sys_cpu_time",0);
  123. private static var sys_exe_path = cpp.Lib.load("std","sys_exe_path",0);
  124. private static var sys_env = cpp.Lib.load("std","sys_env",0);
  125. private static var file_stdin = cpp.Lib.load("std","file_stdin",0);
  126. private static var file_stdout = cpp.Lib.load("std","file_stdout",0);
  127. private static var file_stderr = cpp.Lib.load("std","file_stderr",0);
  128. private static var getch = cpp.Lib.load("std","sys_getch",1);
  129. }